Skip to content

Commit

Permalink
redefined previous :scope transformations, added instead a real pseud…
Browse files Browse the repository at this point in the history
…o-class (except in .closest() method)
  • Loading branch information
dperini committed Jan 17, 2025
1 parent 8d74a96 commit 6027080
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions src/nwsapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
// pseudo-classes not requiring parameters
locationpc: '(any\\-link|link|visited|target)\\b',
useraction: '(hover|active|focus\\-within|focus\\-visible|focus)\\b',
structural: '(root|empty|(?:(?:first|last|only)(?:-child|\\-of\\-type)))\\b',
structural: '(scope|root|empty|(?:(?:first|last|only)(?:-child|\\-of\\-type)))\\b',
inputstate: '(enabled|disabled|read\\-only|read\\-write|placeholder\\-shown|default)\\b',
inputvalue: '(checked|indeterminate|required|optional|valid|invalid|in\\-range|out\\-of\\-range)\\b',
// pseudo-classes not requiring parameters and describing functional state
Expand Down Expand Up @@ -1002,6 +1002,10 @@
if ((match = selector.match(Patterns.structural))) {
match[1] = match[1].toLowerCase();
switch (match[1]) {
case 'scope':
// use the root (documentElement) when comparing against a document
source = 'if(e===(s.from.nodeType===9?s.root:s.from)){' + source + '}';
break;
case 'root':
// there can only be one :root element, so exit the loop once found
source = 'if((e===s.root)){' + source + (mode ? 'break main;' : '') + '}';
Expand Down Expand Up @@ -1460,28 +1464,13 @@
return source;
},

// replace ':scope' pseudo-class with element references
makeref =
function(selectors, element) {
// DOCUMENT_NODE (9)
if (element.nodeType === 9) {
element = element.documentElement;
}

return selectors.replace(/:scope/ig,
element.localName +
(element.id ? '#' + element.id : '') +
(element.className ? '.' + element.classList[0] : ''));
},

// equivalent of w3c 'closest' method
ancestor =
function _closest(selectors, element, callback) {

if ((/:scope/i).test(selectors)) {
selectors = makeref(selectors, element);
}

// replace DOCUMENT with first element (root)
if (element.nodeType === 9) element = root;
// replace :scope with element tag references
selectors = selectors.replace(/:scope/ig, element.localName);
while (element) {
if (match(selectors, element, callback)) break;
element = element.parentElement;
Expand Down Expand Up @@ -1529,10 +1518,6 @@
selectors = '' + selectors;
}

if ((/:scope/i).test(selectors)) {
selectors = makeref(selectors, element);
}

// normalize input string
parsed = selectors.
replace(/\x00|\\$/g, '\ufffd').
Expand Down Expand Up @@ -1635,10 +1620,6 @@
selectors = '' + selectors;
}

if ((/:scope/i).test(selectors)) {
selectors = makeref(selectors, context);
}

// normalize input string
parsed = selectors.
replace(/\x00|\\$/g, '\ufffd').
Expand Down

0 comments on commit 6027080

Please sign in to comment.