Skip to content

Commit

Permalink
Merge pull request #9 from TomCaserta/issue-8
Browse files Browse the repository at this point in the history
#8 Check if parent node is a document or document fragment
  • Loading branch information
Georgegriff authored Aug 30, 2019
2 parents 05f5205 + 8325a9a commit 09c750d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/querySelectorDeep.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function findMatchingElement(splitSelector, possibleElementsIndex, root) {
let position = possibleElementsIndex;
let parent = element;
let foundElement = false;
while (parent) {
while (parent && !isDocumentNode(parent)) {
const foundMatch = parent.matches(splitSelector[position]);
if (foundMatch && position === 0) {
foundElement = true;
Expand Down Expand Up @@ -111,6 +111,14 @@ function splitByCharacterUnlessQuoted(selector, character) {
}, { a: [''] }).a;
}

/**
* Checks if the node is a document node or not.
* @param {Node} node
* @returns {node is Document | DocumentFragment}
*/
function isDocumentNode(node) {
return node.nodeType === Node.DOCUMENT_FRAGMENT_NODE || node.nodeType === Node.DOCUMENT_NODE;
}

function findParentOrHost(element, root) {
const parentNode = element.parentNode;
Expand Down
7 changes: 7 additions & 0 deletions test/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ describe("Basic Suite", function() {
expect(testSubComponent.textContent).toEqual('Child 2')
});

it('returns null when reaching the document node and no matching parent was found', function() {
const rootComponent = createTestComponent(parent, {
childClassName: 'child',
})
const testSubComponent = querySelectorDeep('.parent .child', rootComponent);
expect(testSubComponent).toBeNull();
});
});


Expand Down

0 comments on commit 09c750d

Please sign in to comment.