|
39 | 39 | root = doc.documentElement, |
40 | 40 | slice = Array.prototype.slice, |
41 | 41 |
|
| 42 | + // Factory fallback for documents without a window. |
| 43 | + ELEMENT_PROTO = global.Element && global.Element.prototype, |
| 44 | + |
42 | 45 | HSP = '\\x20\\t', |
43 | 46 | VSP = '\\r\\n\\f', |
44 | 47 | WSP = '[' + HSP + VSP + ']', |
|
88 | 91 | locationpc: '(any\\-link|link|visited|target|defined)\\b', |
89 | 92 | useraction: '(hover|active|focus\\-within|focus\\-visible|focus)\\b', |
90 | 93 | structural: '(scope|root|empty|(?:(?:first|last|only)(?:-child|\\-of\\-type)))\\b', |
91 | | - inputstate: '(enabled|disabled|read\\-only|read\\-write|placeholder\\-shown|default)\\b', |
| 94 | + inputstate: '(enabled|disabled|read\\-only|read\\-write|placeholder\\-shown|default|autofill|-webkit\\-autofill)\\b', |
92 | 95 | inputvalue: '(checked|indeterminate|required|optional|valid|invalid|in\\-range|out\\-of\\-range)\\b', |
93 | 96 | // pseudo-classes not requiring parameters and describing functional state |
94 | 97 | rsrc_state: '(playing|paused|seeking|buffering|stalled|muted|volume\\-locked)\\b', |
95 | 98 | disp_state: '(open|closed|modal|fullscreen|picture\\-in\\-picture|popover\\-open|popover)\\b', |
96 | 99 | time_state: '(current|past|future)\\b', |
97 | | - // pseudo-classes for parsing only selectors |
98 | | - pseudo_nop: '(autofill|-webkit\\-autofill)\\b', |
99 | 100 | // pseudo-elements starting with single colon (:) |
100 | 101 | pseudo_sng: '(after|before|first\\-letter|first\\-line)\\b', |
101 | 102 | // pseudo-elements starting with double colon (::) |
|
115 | 116 | time_state: RegExp('^:(?:' + GROUPS.time_state + ')(.*)', 'i'), |
116 | 117 | locationpc: RegExp('^:(?:' + GROUPS.locationpc + ')(.*)', 'i'), |
117 | 118 | logicalsel: RegExp('^:(?:' + GROUPS.logicalsel + ')(.*)', 'i'), |
118 | | - pseudo_nop: RegExp('^:(?:' + GROUPS.pseudo_nop + ')(.*)', 'i'), |
119 | 119 | pseudo_sng: RegExp('^:(?:' + GROUPS.pseudo_sng + ')(.*)', 'i'), |
120 | 120 | pseudo_dbl: RegExp('^:(?:' + GROUPS.pseudo_dbl + ')(.*)', 'i'), |
121 | 121 | // combinator symbols |
|
469 | 469 | // argument left unclosed is closed by EOF, as the CSS Syntax parser does |
470 | 470 | // with any open construct. Returns a match-like array so that callers can |
471 | 471 | // pop() the remainder the same way they do with a RegExp match. |
| 472 | + splitList = |
| 473 | + function(text) { |
| 474 | + var chr, depth = 0, escaped, i = 0, l = text.length, |
| 475 | + quote = '', start = 0, list = [ ]; |
| 476 | + |
| 477 | + for (; l > i; ++i) { |
| 478 | + chr = text.charAt(i); |
| 479 | + if (escaped) { escaped = false; continue; } |
| 480 | + if (chr == '\\') { escaped = true; } |
| 481 | + else if (quote) { if (chr == quote) { quote = ''; } } |
| 482 | + else if (chr == '\x22' || chr == '\x27') { quote = chr; } |
| 483 | + else if (chr == '\x28' || chr == '\x5b') { ++depth; } |
| 484 | + else if (chr == '\x29' || chr == '\x5d') { --depth; } |
| 485 | + else if (chr == ',' && depth === 0) { |
| 486 | + list[list.length] = text.slice(start, i).replace(REX.TrimSpaces, ''); |
| 487 | + start = i + 1; |
| 488 | + } |
| 489 | + } |
| 490 | + list[list.length] = text.slice(start).replace(REX.TrimSpaces, ''); |
| 491 | + return list; |
| 492 | + }, |
| 493 | + |
472 | 494 | matchLogical = |
473 | 495 | function(selector) { |
474 | 496 | var chr, close, escaped, depth = 1, i, l, quote = '', |
|
778 | 800 | return false; |
779 | 801 | }, |
780 | 802 |
|
| 803 | + // Called during document setup only when legacy mode needs an alias. |
| 804 | + legacyMatcher = |
| 805 | + function(proto) { |
| 806 | + return proto && (proto.webkitMatchesSelector || |
| 807 | + proto.mozMatchesSelector || proto.msMatchesSelector); |
| 808 | + }, |
| 809 | + |
781 | 810 | // use the native selector state when it is available; when NWSAPI has |
782 | 811 | // installed itself, _matches retains the native implementation |
783 | 812 | matchesNative = |
784 | 813 | function(node, selector) { |
785 | | - // A host such as jsdom can delegate matches() back to this engine. |
786 | | - if (matchingNative) return false; |
787 | | - var matcher = _matches || node.matches || node.webkitMatchesSelector || |
788 | | - node.mozMatchesSelector || node.msMatchesSelector; |
789 | | - if (!matcher) return false; |
| 814 | + var view, proto, matcher, ownerDoc = node.ownerDocument || doc; |
| 815 | + // Record delegation before doing any lookup. Nested calls must not |
| 816 | + // replace the document record belonging to the outer matcher. |
| 817 | + if (matchingNative) { matchingNative.delegates = true; return false; } |
| 818 | + if (ownerDoc !== matcherDoc) { |
| 819 | + if (matcherCache === null) { matcherCache = createWeakMap(); } |
| 820 | + matcherDoc = ownerDoc; |
| 821 | + matcherRecord = matcherCache && matcherCache.get(ownerDoc); |
| 822 | + if (!matcherRecord) { |
| 823 | + matcherRecord = { |
| 824 | + fallback: null, |
| 825 | + matcher: undefined, |
| 826 | + delegates: false |
| 827 | + }; |
| 828 | + if (matcherCache) { matcherCache.set(ownerDoc, matcherRecord); } |
| 829 | + } |
| 830 | + } |
| 831 | + // Host methods can change after setup, including element overrides. |
| 832 | + // Retain delegation only while the selected function stays the same. |
| 833 | + matcher = _matches || |
| 834 | + ((ownerDoc.defaultView || Object.prototype.hasOwnProperty.call(node, 'matches')) && node.matches) || |
| 835 | + (ELEMENT_PROTO && ELEMENT_PROTO.matches); |
| 836 | + if (!matcher && Config.LEGACY) { |
| 837 | + if (matcherRecord.fallback === null) { |
| 838 | + view = ownerDoc.defaultView; |
| 839 | + proto = view && view.Element && view.Element.prototype; |
| 840 | + matcherRecord.fallback = legacyMatcher(proto) || |
| 841 | + (proto !== ELEMENT_PROTO ? legacyMatcher(ELEMENT_PROTO) : undefined); |
| 842 | + } |
| 843 | + matcher = matcherRecord.fallback; |
| 844 | + } |
| 845 | + if (matcher !== matcherRecord.matcher) { |
| 846 | + matcherRecord.matcher = matcher; |
| 847 | + matcherRecord.delegates = false; |
| 848 | + } |
| 849 | + if (!matcher || matcherRecord.delegates) { return false; } |
790 | 850 | try { |
791 | | - matchingNative = true; |
| 851 | + matchingNative = matcherRecord; |
792 | 852 | return matcher.call(node, selector); |
793 | 853 | } catch (e) { |
794 | 854 | return false; |
795 | 855 | } finally { |
796 | | - matchingNative = false; |
| 856 | + matchingNative = null; |
797 | 857 | } |
798 | 858 | }, |
799 | 859 |
|
800 | | - matchingNative = false, |
| 860 | + // The active record is marked directly on re-entry, even if the host throws. |
| 861 | + matchingNative = null, |
| 862 | + |
| 863 | + // Consecutive queries avoid a WeakMap lookup. Retain other documents weakly |
| 864 | + // so switching realms does not repeat delegation detection. Allocate after |
| 865 | + // legacy configuration, on first use; undefined selects the bounded fallback. |
| 866 | + matcherDoc = null, |
| 867 | + matcherRecord = null, |
| 868 | + matcherCache = null, |
801 | 869 |
|
802 | 870 | // :open and :closed have a portable DOM state for details and dialog. |
803 | 871 | // Native matching extends support to host-language states such as pickers. |
|
868 | 936 | if (typeof option == 'string') { return !!Config[option]; } |
869 | 937 | if (typeof option != 'object') { return Config; } |
870 | 938 | for (var i in option) { |
| 939 | + // Compiled logical selectors capture the forgiving mode. |
| 940 | + if (i == 'FORGIVING' && Config[i] !== !!option[i]) { clear = true; } |
| 941 | + if (i == 'LEGACY' && Config[i] !== !!option[i]) { |
| 942 | + matcherDoc = matcherCache = null; |
| 943 | + } |
871 | 944 | Config[i] = !!option[i]; |
872 | 945 | } |
873 | 946 | // clear lambda cache |
|
977 | 1050 | '(?:[.#]?' + identifier + ')|' + |
978 | 1051 | '(?:' + attributes + ')' + |
979 | 1052 | ')+|' + |
980 | | - '(?:' + WSP + '?[>+~][^>+~]' + WSP + '?)|' + |
| 1053 | + // the combinator is only recognized, not consumed: taking the |
| 1054 | + // character after it swallows the '[' of a following attribute |
| 1055 | + // selector, which then cannot be parsed |
| 1056 | + '(?:' + WSP + '?[>+~](?=[^>+~])' + WSP + '?)|' + |
981 | 1057 | '(?:' + WSP + '?,' + WSP + '?)|' + |
982 | 1058 | '(?:' + WSP + '?)|' + |
983 | 1059 | '(?:\\x29|$)' + |
|
1363 | 1439 | case 'is': |
1364 | 1440 | case 'where': |
1365 | 1441 | if (Config.FORGIVING) { |
1366 | | - source = |
1367 | | - 'try{' + |
1368 | | - 'if(s.match("' + expr + '",e)){' + source + '}' + |
1369 | | - '}catch(E){}'; |
| 1442 | + source = 'if(s.matchForgiving(' + |
| 1443 | + JSON.stringify(splitList(match[2])) + ',e)){' + source + '}'; |
1370 | 1444 | } else { |
1371 | 1445 | source = 'if(s.match("' + expr + '",e)){' + source + '}'; |
1372 | 1446 | } |
|
1541 | 1615 | break; |
1542 | 1616 | case 'autofill': |
1543 | 1617 | case '-webkit-autofill': |
1544 | | - source = 'if(e.matches&&e.matches(":-webkit-autofill,:autofill")){' + source + '}'; |
| 1618 | + source = 'if(s.matchesNative(e,":autofill")||s.matchesNative(e,":-webkit-autofill")){' + source + '}'; |
1545 | 1619 | break; |
1546 | 1620 | case 'placeholder-shown': |
1547 | 1621 | source = |
|
1700 | 1774 | } |
1701 | 1775 | } |
1702 | 1776 |
|
1703 | | - // placeholder for parse only no-op selectors |
1704 | | - else if ((match = selector.match(Patterns.pseudo_nop))) { |
1705 | | - break; |
1706 | | - } |
1707 | | - |
1708 | 1777 | // allow pseudo-elements starting with single colon (:) |
1709 | 1778 | // :after, :before, :first-letter, :first-line |
1710 | 1779 | // assert: e.type is in double-colon format, like ::after |
|
1908 | 1977 |
|
1909 | 1978 | // parse, validate and split possible compound selectors |
1910 | 1979 | if ((selectors = parsed.match(reValidator)) && selectors.join('') == parsed) { |
1911 | | - selectors = parsed.match(REX.SplitGroup); |
| 1980 | + selectors = splitList(parsed); |
1912 | 1981 | if (parsed[parsed.length - 1] == ',') { |
1913 | 1982 | emit(qsInvalid); |
1914 | 1983 | return Config.VERBOSITY ? undefined : (type ? none : false); |
|
1917 | 1986 | if (Config.FORGIVING) { |
1918 | 1987 | // forgiving pseudos allow to continue even after parse errors |
1919 | 1988 | if (!(parsed.includes(':is(') || parsed.includes(':where('))) { |
1920 | | - emit('\'' + selectors + '\'' + qsInvalid); |
| 1989 | + // 'selectors' holds the fragments the validator did match, |
| 1990 | + // which read as a mangled selector once joined by String() |
| 1991 | + emit('\'' + parsed + '\'' + qsInvalid); |
1921 | 1992 | return Config.VERBOSITY ? undefined : (type ? none : false); |
1922 | 1993 | } |
1923 | 1994 | // The validator cannot read this selector, but it holds a |
|
1929 | 2000 | // the fragments compiled each of them as a selector of its own, |
1930 | 2001 | // which made 'div:not(:is(svg|div))' match every element in the |
1931 | 2002 | // document rather than the divs. |
1932 | | - selectors = parsed.match(REX.SplitGroup) || [ parsed ]; |
| 2003 | + selectors = splitList(parsed); |
1933 | 2004 | } |
1934 | 2005 | } |
1935 | 2006 |
|
|
1938 | 2009 |
|
1939 | 2010 | // equivalent of w3c 'matches' method |
1940 | 2011 | match = |
1941 | | - function _matches(selectors, element, callback) { |
| 2012 | + function _matches(selectors, element, callback?: (element: Element) => unknown) { |
1942 | 2013 |
|
1943 | 2014 | var resolver; |
1944 | 2015 |
|
|
1952 | 2023 | return match_assert(resolver.factory, element, callback); |
1953 | 2024 | }, |
1954 | 2025 |
|
| 2026 | + // Invalid items do not discard the remaining forgiving selectors. |
| 2027 | + matchForgiving = |
| 2028 | + function(list, element) { |
| 2029 | + for (var i = 0, l = list.length; l > i; ++i) { |
| 2030 | + try { |
| 2031 | + if (match(list[i], element)) { return true; } |
| 2032 | + } catch (e) { } |
| 2033 | + } |
| 2034 | + return false; |
| 2035 | + }, |
| 2036 | + |
1955 | 2037 | // true if element matches the selector |
1956 | 2038 | has = |
1957 | 2039 | function(selector, context, callback) { |
|
2214 | 2296 | Element.prototype.querySelector = |
2215 | 2297 | HTMLElement.prototype.querySelector = _querySelector; |
2216 | 2298 | Element.prototype.querySelectorAll = |
2217 | | - HTMLElement.prototype.querySelectorAll = _querySelector; |
| 2299 | + HTMLElement.prototype.querySelectorAll = _querySelectorAll; |
2218 | 2300 | } |
2219 | 2301 | if (_querySelectorAllDoc) { |
2220 | 2302 | Document.prototype.querySelector = |
|
2243 | 2325 | HOVER?: EventTarget; |
2244 | 2326 | doc: Document; from: Node; root: Element; |
2245 | 2327 | byTag: typeof byTag; has: typeof has; first: typeof first; |
2246 | | - match: typeof match; select: typeof select; ancestor: typeof ancestor; |
| 2328 | + match: typeof match; matchForgiving: typeof matchForgiving; |
| 2329 | + select: typeof select; ancestor: typeof ancestor; |
2247 | 2330 | nthOfType: typeof nthOfType; nthElement: typeof nthElement; |
| 2331 | + matchesNative: typeof matchesNative; |
2248 | 2332 | isOpen: typeof isOpen; isClosed: typeof isClosed; isModal: typeof isModal; |
2249 | 2333 | isFullscreen: typeof isFullscreen; isPictureInPicture: typeof isPictureInPicture; |
2250 | 2334 | isPopoverOpen: typeof isPopoverOpen; isFocusable: typeof isFocusable; |
|
2261 | 2345 | has: has, |
2262 | 2346 | first: first, |
2263 | 2347 | match: match, |
| 2348 | + matchForgiving: matchForgiving, |
2264 | 2349 | select: select, |
2265 | 2350 |
|
2266 | 2351 | ancestor: ancestor, |
2267 | 2352 |
|
2268 | 2353 | nthOfType: nthOfType, |
2269 | 2354 | nthElement: nthElement, |
2270 | 2355 |
|
| 2356 | + matchesNative: matchesNative, |
2271 | 2357 | isOpen: isOpen, |
2272 | 2358 | isClosed: isClosed, |
2273 | 2359 | isModal: isModal, |
|
0 commit comments