Skip to content

Commit

Permalink
Fix to keep content around for hName on text
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 17, 2023
1 parent 6093b77 commit 3e300ea
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 29 deletions.
21 changes: 7 additions & 14 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,25 +269,18 @@ function applyData(from, to) {
}
// Transforming the node resulted in a non-element, which happens for
// raw, text, and root nodes (unless custom handlers are passed).
// The intent is likely to keep the content around (otherwise: pass
// `hChildren`).
// The intent of `hName` is to create an element, but likely also to keep
// the content around (otherwise: pass `hChildren`).
else {
result = {type: 'element', tagName: hName, properties: {}, children: []}

// To do: next major: take the children from the `root`, or inject the
// raw/text/comment or so into the element?
// if ('children' in from) {
// // @ts-expect-error: assume `children` are allowed in elements.
// result.children = from.children
// } else {
// // @ts-expect-error: assume `node` is allowed in elements.
// result.children.push(from)
// }
/** @type {Array<HastElementContent>} */
// @ts-expect-error: assume no doctypes in `root`.
const children = 'children' in result ? result.children : [result]
result = {type: 'element', tagName: hName, properties: {}, children}
}
}

if (result.type === 'element' && hProperties) {
result.properties = {...result.properties, ...hProperties}
Object.assign(result.properties, structuredClone(hProperties))
}

if (
Expand Down
27 changes: 19 additions & 8 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,25 @@ test('toHast', async function (t) {
})

await t.test('should support `passThrough`', async function () {
assert.deepEqual(
toHast(customMdast, {passThrough: ['alpha', 'bravo']}),
h('p', [
assert.deepEqual(toHast(customMdast, {passThrough: ['alpha', 'bravo']}), {
type: 'element',
tagName: 'p',
properties: {},
children: [
{type: 'alpha', value: 'alpha'},
// @ts-expect-error: to do: remove when `hastscript` is released.
{type: 'bravo', children: [h('img', {src: 'bravo'})]},
'charlie'
])
)
{
type: 'bravo',
children: [
{
type: 'element',
tagName: 'img',
properties: {src: 'bravo'},
children: []
}
]
},
{type: 'text', value: 'charlie'}
]
})
})
})
49 changes: 49 additions & 0 deletions test/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,53 @@ test('root', async function (t) {
await t.test('should map `root`s', async function () {
assert.deepEqual(toHast({type: 'root', children: []}), h(null))
})

await t.test('should transform root nodes w/ `hName`', async function () {
assert.deepEqual(
toHast({
type: 'root',
children: [{type: 'text', value: 'alpha'}],
data: {hName: 'article'}
}),
h('article', 'alpha')
)
})

await t.test(
'should transform root nodes w/ `hName`, `hProperties`',
async function () {
assert.deepEqual(
toHast({
type: 'root',
children: [{type: 'text', value: 'alpha'}],
data: {hName: 'article', hProperties: {className: ['bravo']}}
}),
h('article.bravo', 'alpha')
)
}
)

await t.test(
'should transform root nodes w/ `hName`, `hChildren`',
async function () {
assert.deepEqual(
toHast({
type: 'root',
children: [{type: 'text', value: 'alpha'}],
data: {
hName: 'article',
hChildren: [
{
type: 'element',
tagName: 'section',
properties: {},
children: [{type: 'text', value: 'bravo'}]
}
]
}
}),
h('article', h('section', 'bravo'))
)
}
)
})
10 changes: 3 additions & 7 deletions test/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@ test('text', async function (t) {
async function () {
assert.deepEqual(
toHast({type: 'text', value: 'delta', data: {hName: 'span'}}),
// To do: keep `value`?
h('span')
h('span', 'delta')
)
}
)

await t.test(
'should not transform text nodes w/ `hProperties` w/o `hName` to an `element`',
'should ignore `hProperties` on text nodes w/o `hName`',
async function () {
assert.deepEqual(
toHast({
type: 'text',
value: 'echo',
data: {hProperties: {className: ['foxtrot']}}
}),
// To do: `div` or `span`?
{type: 'text', value: 'echo'}
)
}
Expand All @@ -53,8 +51,7 @@ test('text', async function (t) {
value: 'golf',
data: {hName: 'span', hProperties: {className: ['hotel']}}
}),
// To do: keep `value`?
h('span.hotel')
h('span.hotel', 'golf')
)
}
)
Expand All @@ -68,7 +65,6 @@ test('text', async function (t) {
value: 'india',
data: {hChildren: [{type: 'text', value: 'juliett'}]}
}),
// To do: `div` or `span`?
{type: 'text', value: 'india'}
)
}
Expand Down

0 comments on commit 3e300ea

Please sign in to comment.