Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parse5-utils): add support for templates in findNode #2751

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sour-roses-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@web/parse5-utils': patch
---

Adds support to findNode to find nodes within <template> elements
34 changes: 21 additions & 13 deletions packages/parse5-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function findNode(nodes, test) {
if (test(node)) {
return node;
}
const children = adapter.getChildNodes(/** @type {ParentNode} */ (node));
const children = getNodeChildren(node);
if (Array.isArray(children)) {
n.unshift(...children);
}
Expand Down Expand Up @@ -216,25 +216,33 @@ function findNodes(nodes, test) {
found.push(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));
}

const children = getNodeChildren(node);
if (Array.isArray(children)) {
n.unshift(...children);
}
}
return found;
}

/**
* Get all children of a node or all children of a `<template>` element's content
* @param {Node} node
* @returns {Node[]}
*/
function getNodeChildren(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));
}
return children;
}

/**
* Looks for a child element which passes the given test
* @param {Node[] | Node} nodes
Expand Down
18 changes: 18 additions & 0 deletions packages/parse5-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ describe('parse5-utils', () => {
}
expect(found).to.exist;
});

it('returns elements within template element', () => {
const doc = parse(`
<html>
<body>
<template>
<div id="foo">Hello world</div>
</template>
</body>
</html>
`);

const found = utils.findElement(doc, el => utils.getAttribute(el, 'id') === 'foo');
if (!found) {
throw new Error('No element found.');
}
expect(found).to.exist;
});
});

describe('findElements()', () => {
Expand Down
Loading