Skip to content

Commit 8acb095

Browse files
committed
Add support for creating <template> elements
If an element with `'template'` as its tag is created, instead of setting the given `childNodes` as `children`, they’re set on `.content` as a `root` fragment.
1 parent d685d62 commit 8acb095

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ function h(selector, properties, children) {
3535

3636
addChild(node.children, children);
3737

38+
if (node.tagName === 'template') {
39+
node.content = {type: 'root', children: node.children};
40+
node.children = [];
41+
}
42+
3843
return node;
3944
}
4045

test.js

+69
Original file line numberDiff line numberDiff line change
@@ -588,5 +588,74 @@ test('hastscript', function (t) {
588588
st.end();
589589
});
590590

591+
t.test('<template>', function (st) {
592+
st.deepEqual(
593+
h('template'),
594+
{
595+
type: 'element',
596+
tagName: 'template',
597+
properties: {},
598+
children: [],
599+
content: {
600+
type: 'root',
601+
children: []
602+
}
603+
},
604+
'empty template'
605+
);
606+
607+
st.deepEqual(
608+
h('template', 'Alpha'),
609+
{
610+
type: 'element',
611+
tagName: 'template',
612+
properties: {},
613+
children: [],
614+
content: {
615+
type: 'root',
616+
children: [{type: 'text', value: 'Alpha'}]
617+
}
618+
},
619+
'template with text'
620+
);
621+
622+
st.deepEqual(
623+
h('template', [
624+
h('b', 'Bold'),
625+
' and ',
626+
h('i', 'italic'),
627+
'.'
628+
]),
629+
{
630+
type: 'element',
631+
tagName: 'template',
632+
properties: {},
633+
children: [],
634+
content: {
635+
type: 'root',
636+
children: [
637+
{
638+
type: 'element',
639+
tagName: 'b',
640+
properties: {},
641+
children: [{type: 'text', value: 'Bold'}]
642+
},
643+
{type: 'text', value: ' and '},
644+
{
645+
type: 'element',
646+
tagName: 'i',
647+
properties: {},
648+
children: [{type: 'text', value: 'italic'}]
649+
},
650+
{type: 'text', value: '.'}
651+
]
652+
}
653+
},
654+
'template with elements'
655+
);
656+
657+
st.end();
658+
});
659+
591660
t.end();
592661
});

0 commit comments

Comments
 (0)