Skip to content

Commit 17ae11c

Browse files
committed
Add support for <template>s
1 parent 750e1d6 commit 17ae11c

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

index.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var own = {}.hasOwnProperty;
1212
var one = zwitch('type');
1313
var all = mapz(one, {key: 'children', indices: false});
1414

15+
var customProps = ['__location', 'childNodes', 'content', 'parentNode', 'namespaceURI'];
16+
1517
one.handlers.root = root;
1618
one.handlers.element = element;
1719
one.handlers.text = text;
@@ -63,6 +65,7 @@ function element(node) {
6365

6466
return toH(function (name, attrs) {
6567
var values = [];
68+
var content;
6669
var key;
6770

6871
for (key in attrs) {
@@ -79,12 +82,18 @@ function element(node) {
7982
}
8083
}
8184

85+
if (name === 'template') {
86+
content = transform(shallow.content);
87+
delete content.mode;
88+
content.nodeName = '#document-fragment';
89+
}
90+
8291
return wrap(node, {
8392
nodeName: node.tagName,
8493
tagName: node.tagName,
8594
attrs: values,
8695
childNodes: node.children ? all(node) : []
87-
});
96+
}, content);
8897
}, shallow);
8998
}
9099

@@ -112,7 +121,7 @@ function comment(node) {
112121
}
113122

114123
/* Patch position. */
115-
function wrap(node, ast) {
124+
function wrap(node, ast, content) {
116125
if (node.position && node.position.start && node.position.end) {
117126
ast.__location = {
118127
line: node.position.start.line,
@@ -122,6 +131,10 @@ function wrap(node, ast) {
122131
};
123132
}
124133

134+
if (content) {
135+
ast.content = content;
136+
}
137+
125138
return ast;
126139
}
127140

@@ -137,7 +150,7 @@ function patch(node, parent, ns) {
137150
var key;
138151

139152
for (key in node) {
140-
if (key !== '__location' && key !== 'childNodes') {
153+
if (customProps.indexOf(key) === -1) {
141154
replacement[key] = node[key];
142155
}
143156
}
@@ -160,6 +173,10 @@ function patch(node, parent, ns) {
160173
}
161174
}
162175

176+
if (name === 'template') {
177+
replacement.content = patch(node.content, null, ns);
178+
}
179+
163180
if (parent) {
164181
replacement.parentNode = parent;
165182
}

test/element.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,59 @@ test('element', function (t) {
9191
'should transform foreign elements'
9292
);
9393

94+
node = parse5.parseFragment('<template id="a">Alpha</template>');
95+
96+
node = node.childNodes[0];
97+
delete node.parentNode;
98+
99+
t.deepEqual(
100+
inspect(toParse5({
101+
type: 'element',
102+
tagName: 'template',
103+
properties: {id: 'a'},
104+
children: [],
105+
content: {
106+
type: 'root',
107+
children: [{type: 'text', value: 'Alpha'}]
108+
}
109+
}), {depth: null}),
110+
inspect(node, {depth: null}),
111+
'should transform templates with text'
112+
);
113+
114+
node = parse5.parseFragment('<template id="b"><b>bold</b> and <i>italic</i></template>');
115+
116+
node = node.childNodes[0];
117+
delete node.parentNode;
118+
119+
t.deepEqual(
120+
inspect(toParse5({
121+
type: 'element',
122+
tagName: 'template',
123+
properties: {id: 'b'},
124+
children: [],
125+
content: {
126+
type: 'root',
127+
children: [
128+
{
129+
type: 'element',
130+
tagName: 'b',
131+
properties: {},
132+
children: [{type: 'text', value: 'bold'}]
133+
},
134+
{type: 'text', value: ' and '},
135+
{
136+
type: 'element',
137+
tagName: 'i',
138+
properties: {},
139+
children: [{type: 'text', value: 'italic'}]
140+
}
141+
]
142+
}
143+
}), {depth: null}),
144+
inspect(node, {depth: null}),
145+
'should transform templates with elements'
146+
);
147+
94148
t.end();
95149
});

0 commit comments

Comments
 (0)