Skip to content

Commit 197247c

Browse files
committed
Fix support for hName on non-element
Previously, an mdast text could not result in an element through a `hName`. As it’s rather clear that: `{type: 'text', data: {hName: 'span'}}` should turn into a span element node, this was fixed.
1 parent deab265 commit 197247c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lib/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ function factory(tree, options) {
5151
if (left && 'data' in left) {
5252
data = left.data
5353

54-
if (right.type === 'element' && data.hName) {
54+
if (data.hName) {
55+
if (right.type !== 'element') {
56+
right = {
57+
type: 'element',
58+
tagName: '',
59+
properties: {},
60+
children: []
61+
}
62+
}
63+
5564
right.tagName = data.hName
5665
}
5766

test/text.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,51 @@ test('Nodes', function (t) {
1313
'should trim spaces and tabs around eols'
1414
)
1515

16+
t.deepEqual(
17+
to(u('text', {data: {hName: 'span'}}, 'charlie')),
18+
u('element', {tagName: 'span', properties: {}}, []),
19+
'should transform text nodes w/ `hName` to an `element`'
20+
)
21+
22+
t.deepEqual(
23+
to(u('text', {data: {hProperties: {className: 'delta'}}}, 'echo')),
24+
{type: 'text', value: 'echo'},
25+
'should not transform text nodes w/ `hProperties` w/o `hName` to an `element`'
26+
)
27+
28+
t.deepEqual(
29+
to(
30+
u(
31+
'text',
32+
{data: {hName: 'span', hProperties: {className: ['foxtrot']}}},
33+
'golf'
34+
)
35+
),
36+
u('element', {tagName: 'span', properties: {className: ['foxtrot']}}, []),
37+
'should transform text nodes w/ `hProperties` and `hName` to an `element`'
38+
)
39+
40+
t.deepEqual(
41+
to(
42+
u('text', {data: {hChildren: [{type: 'text', value: 'hotel'}]}}, 'india')
43+
),
44+
{type: 'text', value: 'india'},
45+
'should not transform text nodes w/ `hChildren` w/o `hName` to an `element`'
46+
)
47+
48+
t.deepEqual(
49+
to(
50+
u(
51+
'text',
52+
{data: {hName: 'span', hChildren: [{type: 'text', value: 'juliett'}]}},
53+
'kilo'
54+
)
55+
),
56+
u('element', {tagName: 'span', properties: {}}, [
57+
{type: 'text', value: 'juliett'}
58+
]),
59+
'should transform text nodes w/ `hChildren` and `hName` to an `element`'
60+
)
61+
1662
t.end()
1763
})

0 commit comments

Comments
 (0)