Skip to content

Commit

Permalink
refactor: do not stringify x-data
Browse files Browse the repository at this point in the history
x-data needs a JavaScript expression and not a JSON string, therefore
JSON stringifying it will not work
  • Loading branch information
thetutlage committed Aug 8, 2023
1 parent 1c78d1e commit bd7387f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
12 changes: 7 additions & 5 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Edge } from '../index.js'
import { join } from 'node:path'
import { createServer } from 'node:http'
import { getDirname } from '@poppinss/utils'
import { migrate } from '../src/migrate/plugin.js'

const edge = Edge.create()
edge.use(migrate)
edge.mount(join(getDirname(import.meta.url), 'views'))

class Base {
Expand Down Expand Up @@ -37,11 +39,11 @@ user.parent = user

createServer(async (_req, res) => {
res.writeHead(200, { 'content-type': 'text/html' })
res.end(
await edge.render('welcome', {
user: user,
})
)
const html = await edge.render('welcome', {
user: user,
})
console.log(html)
res.end(html)
}).listen(3000, () => {
console.log('Listening on 127.0.0.1:3000')
})
17 changes: 16 additions & 1 deletion example/views/components/modal.edge
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
{{ inspect(state) }}

<div class="modal">
<div {{
({
class: 'modal',
x: {
data: `{
title: '${title}',
trigger: {
['@click']() {
alert(this.title)
}
}
}`
}
})
}}
>
<h1 class="title">{{ title }}</h1>
<div>
{{{ await $slots.body() }}}
Expand Down
2 changes: 1 addition & 1 deletion example/views/partials/button.edge
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<button>delete</button>
<button x-bind="trigger">delete</button>
1 change: 1 addition & 0 deletions example/views/welcome.edge
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title></title>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
</head>
<body>
{{ inspect(state.user) }}
Expand Down
7 changes: 1 addition & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* file that was distributed with this source code.
*/

// @ts-expect-error untyped module
import JSStringify from 'js-stringify'
import classNames from 'classnames'
import { EdgeError } from 'edge-error'
import type { TagToken } from 'edge-lexer/types'
Expand Down Expand Up @@ -341,14 +339,11 @@ export function stringifyAttributes(props: any, namespace?: string): string {
* Encoding rules for certain properties.
*
* - Class values can be objects with conditionals
* - x-data as an object will be converted to a JSON value
* - Arrays will be concatenated into a string list and html escaped
* - Non-booleanish and numeric properties will be html escaped
* - Arrays will be concatenated into a string list and html escaped
*/
if (key === 'class') {
value = `"${classNames(value)}"`
} else if (key === 'x-data') {
value = typeof value === 'string' ? `"${value}"` : JSStringify(value)
} else if (Array.isArray(value)) {
value = `"${value.join(propInfo.commaSeparated ? ',' : ' ')}"`
} else if (!propInfo.booleanish && !propInfo.number) {
Expand Down
4 changes: 2 additions & 2 deletions tests/template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ test.group('Template | toAttributes', () => {
<div {{
({
x: {
data: { open: false },
data: '{ open: false }',
init: "console.log('Here I am')",
show: 'open',
}
Expand All @@ -392,7 +392,7 @@ test.group('Template | toAttributes', () => {
assert.equal(
html,
dedent`
<div x-data={"open":false} x-init="console.log('Here I am')" x-show="open">
<div x-data="{ open: false }" x-init="console.log('Here I am')" x-show="open">
</div>
`
)
Expand Down

0 comments on commit bd7387f

Please sign in to comment.