Skip to content

Commit

Permalink
Merge pull request #2398 from WillsonSmith/parse5-utils-support-templ…
Browse files Browse the repository at this point in the history
…ates

fix(parse5-utils): add support for templates
  • Loading branch information
koddsson authored Aug 4, 2023
2 parents f40a5b2 + d5da0e4 commit 7bd56e1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/soft-meals-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@web/parse5-utils': patch
---

Adds support for traversing <template> elements
14 changes: 13 additions & 1 deletion packages/parse5-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,19 @@ function findNodes(nodes, test) {
if (test(node)) {
found.push(node);
}
const children = adapter.getChildNodes(/** @type {ParentNode} */ (node));

/** @type {Node[]} */
let children = [];

if (adapter.isElementNode(node) && adapter.getTagName(node) === 'template') {
const content = adapter.getTemplateContent(node);
if (content) {
children = adapter.getChildNodes(content);
}
} else {
children = adapter.getChildNodes(/** @type {ParentNode} */ (node));
}

if (Array.isArray(children)) {
n.unshift(...children);
}
Expand Down
15 changes: 15 additions & 0 deletions packages/parse5-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,21 @@ describe('parse5-utils', () => {
const found = utils.findElements(doc, el => utils.hasAttribute(el, 'non-existing'));
expect(found.length).to.equal(0);
});

it('returns child elements within template elements', () => {
const doc = parse(`
<html>
<body>
<template>
<img src="foo.png" />
</template>
</body>
</html>
`);

const found = utils.findElements(doc, el => utils.hasAttribute(el, 'src'));
expect(found.length).to.equal(1);
});
});

describe('prependToDocument', () => {
Expand Down

0 comments on commit 7bd56e1

Please sign in to comment.