Skip to content

Commit d5bc9f8

Browse files
committed
Add support for content of <template> elements
Previously, the content of `<template>` elements was not exposed in any way. As their content should not be part of the actual document, but instead their own fragment, a new `content` property on elements is introduced to house a `root` node representing the template’s content.
1 parent d0927c1 commit d5bc9f8

File tree

4 files changed

+603
-13
lines changed

4 files changed

+603
-13
lines changed

index.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,25 @@ function nodes(children, config) {
8181
/* Transform a document.
8282
* Stores `ast.quirksMode` in `node.data.quirksMode`. */
8383
function root(ast, children, config) {
84-
var node = {
85-
type: 'root',
86-
children: children,
87-
data: {
88-
quirksMode: ast.mode === 'quirks' || ast.mode === 'limited-quirks'
84+
var quirks = ast.mode === 'quirks' || ast.mode === 'limited-quirks';
85+
var node = {type: 'root', children: children};
86+
var position;
87+
88+
node.data = {quirksMode: quirks};
89+
90+
if (ast.__location) {
91+
if (config.toPosition) {
92+
config.location = true;
93+
position = ast.__location;
8994
}
90-
};
95+
} else if (config.file && config.location) {
96+
position = {startOffset: 0, endOffset: String(config.file).length};
97+
}
98+
99+
position = position && location(position, ast, node, config);
91100

92-
if (config.file && config.location) {
93-
node.position = location({
94-
startOffset: 0,
95-
endOffset: String(config.file).length
96-
}, ast, node, config);
101+
if (position) {
102+
node.position = position;
97103
}
98104

99105
return node;
@@ -120,19 +126,36 @@ function comment(ast) {
120126
}
121127

122128
/* Transform an element. */
123-
function element(ast, children) {
129+
function element(ast, children, config) {
124130
var props = {};
125131
var values = ast.attrs;
126132
var length = values.length;
127133
var index = -1;
128134
var attr;
135+
var node;
136+
var fragment;
129137

130138
while (++index < length) {
131139
attr = values[index];
132140
props[(attr.prefix ? attr.prefix + ':' : '') + attr.name] = attr.value;
133141
}
134142

135-
return h(ast.tagName, props, children);
143+
node = h(ast.tagName, props, children);
144+
145+
if (ast.nodeName === 'template' && 'content' in ast) {
146+
fragment = ast.content;
147+
148+
if (ast.__location) {
149+
fragment.__location = {
150+
startOffset: ast.__location.startTag.endOffset,
151+
endOffset: ast.__location.endTag.startOffset
152+
};
153+
}
154+
155+
node.content = transform(ast.content, config);
156+
}
157+
158+
return node;
136159
}
137160

138161
/* Create clean positional information. */

test/fixtures/template/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!doctype html>
2+
<title>Templates and their content</title>
3+
<body>
4+
<template id="text">!</template>
5+
6+
<TEMPLATE id="html"><strong>importance</strong> and <em>emphasis</em>.</TEMPLATE>

0 commit comments

Comments
 (0)