Skip to content

Commit 3e300ea

Browse files
committed
Fix to keep content around for hName on text
1 parent 6093b77 commit 3e300ea

File tree

4 files changed

+78
-29
lines changed

4 files changed

+78
-29
lines changed

lib/state.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -269,25 +269,18 @@ function applyData(from, to) {
269269
}
270270
// Transforming the node resulted in a non-element, which happens for
271271
// raw, text, and root nodes (unless custom handlers are passed).
272-
// The intent is likely to keep the content around (otherwise: pass
273-
// `hChildren`).
272+
// The intent of `hName` is to create an element, but likely also to keep
273+
// the content around (otherwise: pass `hChildren`).
274274
else {
275-
result = {type: 'element', tagName: hName, properties: {}, children: []}
276-
277-
// To do: next major: take the children from the `root`, or inject the
278-
// raw/text/comment or so into the element?
279-
// if ('children' in from) {
280-
// // @ts-expect-error: assume `children` are allowed in elements.
281-
// result.children = from.children
282-
// } else {
283-
// // @ts-expect-error: assume `node` is allowed in elements.
284-
// result.children.push(from)
285-
// }
275+
/** @type {Array<HastElementContent>} */
276+
// @ts-expect-error: assume no doctypes in `root`.
277+
const children = 'children' in result ? result.children : [result]
278+
result = {type: 'element', tagName: hName, properties: {}, children}
286279
}
287280
}
288281

289282
if (result.type === 'element' && hProperties) {
290-
result.properties = {...result.properties, ...hProperties}
283+
Object.assign(result.properties, structuredClone(hProperties))
291284
}
292285

293286
if (

test/core.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,25 @@ test('toHast', async function (t) {
309309
})
310310

311311
await t.test('should support `passThrough`', async function () {
312-
assert.deepEqual(
313-
toHast(customMdast, {passThrough: ['alpha', 'bravo']}),
314-
h('p', [
312+
assert.deepEqual(toHast(customMdast, {passThrough: ['alpha', 'bravo']}), {
313+
type: 'element',
314+
tagName: 'p',
315+
properties: {},
316+
children: [
315317
{type: 'alpha', value: 'alpha'},
316-
// @ts-expect-error: to do: remove when `hastscript` is released.
317-
{type: 'bravo', children: [h('img', {src: 'bravo'})]},
318-
'charlie'
319-
])
320-
)
318+
{
319+
type: 'bravo',
320+
children: [
321+
{
322+
type: 'element',
323+
tagName: 'img',
324+
properties: {src: 'bravo'},
325+
children: []
326+
}
327+
]
328+
},
329+
{type: 'text', value: 'charlie'}
330+
]
331+
})
321332
})
322333
})

test/root.js

+49
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,53 @@ test('root', async function (t) {
77
await t.test('should map `root`s', async function () {
88
assert.deepEqual(toHast({type: 'root', children: []}), h(null))
99
})
10+
11+
await t.test('should transform root nodes w/ `hName`', async function () {
12+
assert.deepEqual(
13+
toHast({
14+
type: 'root',
15+
children: [{type: 'text', value: 'alpha'}],
16+
data: {hName: 'article'}
17+
}),
18+
h('article', 'alpha')
19+
)
20+
})
21+
22+
await t.test(
23+
'should transform root nodes w/ `hName`, `hProperties`',
24+
async function () {
25+
assert.deepEqual(
26+
toHast({
27+
type: 'root',
28+
children: [{type: 'text', value: 'alpha'}],
29+
data: {hName: 'article', hProperties: {className: ['bravo']}}
30+
}),
31+
h('article.bravo', 'alpha')
32+
)
33+
}
34+
)
35+
36+
await t.test(
37+
'should transform root nodes w/ `hName`, `hChildren`',
38+
async function () {
39+
assert.deepEqual(
40+
toHast({
41+
type: 'root',
42+
children: [{type: 'text', value: 'alpha'}],
43+
data: {
44+
hName: 'article',
45+
hChildren: [
46+
{
47+
type: 'element',
48+
tagName: 'section',
49+
properties: {},
50+
children: [{type: 'text', value: 'bravo'}]
51+
}
52+
]
53+
}
54+
}),
55+
h('article', h('section', 'bravo'))
56+
)
57+
}
58+
)
1059
})

test/text.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,20 @@ test('text', async function (t) {
2323
async function () {
2424
assert.deepEqual(
2525
toHast({type: 'text', value: 'delta', data: {hName: 'span'}}),
26-
// To do: keep `value`?
27-
h('span')
26+
h('span', 'delta')
2827
)
2928
}
3029
)
3130

3231
await t.test(
33-
'should not transform text nodes w/ `hProperties` w/o `hName` to an `element`',
32+
'should ignore `hProperties` on text nodes w/o `hName`',
3433
async function () {
3534
assert.deepEqual(
3635
toHast({
3736
type: 'text',
3837
value: 'echo',
3938
data: {hProperties: {className: ['foxtrot']}}
4039
}),
41-
// To do: `div` or `span`?
4240
{type: 'text', value: 'echo'}
4341
)
4442
}
@@ -53,8 +51,7 @@ test('text', async function (t) {
5351
value: 'golf',
5452
data: {hName: 'span', hProperties: {className: ['hotel']}}
5553
}),
56-
// To do: keep `value`?
57-
h('span.hotel')
54+
h('span.hotel', 'golf')
5855
)
5956
}
6057
)
@@ -68,7 +65,6 @@ test('text', async function (t) {
6865
value: 'india',
6966
data: {hChildren: [{type: 'text', value: 'juliett'}]}
7067
}),
71-
// To do: `div` or `span`?
7268
{type: 'text', value: 'india'}
7369
)
7470
}

0 commit comments

Comments
 (0)