Skip to content

Commit 09c750d

Browse files
authored
Merge pull request #9 from TomCaserta/issue-8
#8 Check if parent node is a document or document fragment
2 parents 05f5205 + 8325a9a commit 09c750d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/querySelectorDeep.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function findMatchingElement(splitSelector, possibleElementsIndex, root) {
7777
let position = possibleElementsIndex;
7878
let parent = element;
7979
let foundElement = false;
80-
while (parent) {
80+
while (parent && !isDocumentNode(parent)) {
8181
const foundMatch = parent.matches(splitSelector[position]);
8282
if (foundMatch && position === 0) {
8383
foundElement = true;
@@ -111,6 +111,14 @@ function splitByCharacterUnlessQuoted(selector, character) {
111111
}, { a: [''] }).a;
112112
}
113113

114+
/**
115+
* Checks if the node is a document node or not.
116+
* @param {Node} node
117+
* @returns {node is Document | DocumentFragment}
118+
*/
119+
function isDocumentNode(node) {
120+
return node.nodeType === Node.DOCUMENT_FRAGMENT_NODE || node.nodeType === Node.DOCUMENT_NODE;
121+
}
114122

115123
function findParentOrHost(element, root) {
116124
const parentNode = element.parentNode;

test/basic.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ describe("Basic Suite", function() {
9595
expect(testSubComponent.textContent).toEqual('Child 2')
9696
});
9797

98+
it('returns null when reaching the document node and no matching parent was found', function() {
99+
const rootComponent = createTestComponent(parent, {
100+
childClassName: 'child',
101+
})
102+
const testSubComponent = querySelectorDeep('.parent .child', rootComponent);
103+
expect(testSubComponent).toBeNull();
104+
});
98105
});
99106

100107

0 commit comments

Comments
 (0)