From 6027080aeee9cdd1c04e1f3771992df57fd21316 Mon Sep 17 00:00:00 2001 From: Diego Perini Date: Fri, 17 Jan 2025 08:45:00 +0100 Subject: [PATCH] redefined previous :scope transformations, added instead a real pseudo-class (except in .closest() method) --- src/nwsapi.js | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/src/nwsapi.js b/src/nwsapi.js index f4d77d2..8bbb488 100644 --- a/src/nwsapi.js +++ b/src/nwsapi.js @@ -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 @@ -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;' : '') + '}'; @@ -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; @@ -1529,10 +1518,6 @@ selectors = '' + selectors; } - if ((/:scope/i).test(selectors)) { - selectors = makeref(selectors, element); - } - // normalize input string parsed = selectors. replace(/\x00|\\$/g, '\ufffd'). @@ -1635,10 +1620,6 @@ selectors = '' + selectors; } - if ((/:scope/i).test(selectors)) { - selectors = makeref(selectors, context); - } - // normalize input string parsed = selectors. replace(/\x00|\\$/g, '\ufffd').