diff --git a/cjs/shared/matches.js b/cjs/shared/matches.js index e4deed74..f8cb90c6 100644 --- a/cjs/shared/matches.js +++ b/cjs/shared/matches.js @@ -108,14 +108,30 @@ const prepareMatch = (element, selectors) => CSSselect.compile( ); exports.prepareMatch = prepareMatch; -const matches = (element, selectors) => CSSselect.is( - element, - selectors, - { - strict: true, - context: selectors.includes(':scope') ? element : void 0, - xmlMode: !ignoreCase(element), - adapter +// matches() is called once per element and selector pair, so the selector +// is compiled once and kept, instead of parsed again on every call. +// DOM results must stay fresh: css-select may otherwise memoize ancestors. +// :scope depends on the element, so those are compiled each time. +const compiled = new Map; +const MAX_COMPILED = 1024; + +/** @returns {boolean} */ +const matches = (element, selectors) => { + const xmlMode = !ignoreCase(element); + if (selectors.includes(':scope')) + return CSSselect.is( + element, + selectors, + {strict: true, context: element, xmlMode, adapter} + ); + const key = (xmlMode ? 'x' : 'h') + selectors; + let test = compiled.get(key); + if (!test) { + if (compiled.size === MAX_COMPILED) + compiled.delete(compiled.keys().next().value); + test = CSSselect.compile(selectors, {strict: true, xmlMode, adapter, cacheResults: false}); + compiled.set(key, test); } -); + return test(element); +}; exports.matches = matches; diff --git a/esm/shared/matches.js b/esm/shared/matches.js index dbc3339a..76b83857 100644 --- a/esm/shared/matches.js +++ b/esm/shared/matches.js @@ -106,13 +106,29 @@ export const prepareMatch = (element, selectors) => CSSselect.compile( } ); -export const matches = (element, selectors) => CSSselect.is( - element, - selectors, - { - strict: true, - context: selectors.includes(':scope') ? element : void 0, - xmlMode: !ignoreCase(element), - adapter +// matches() is called once per element and selector pair, so the selector +// is compiled once and kept, instead of parsed again on every call. +// DOM results must stay fresh: css-select may otherwise memoize ancestors. +// :scope depends on the element, so those are compiled each time. +const compiled = new Map; +const MAX_COMPILED = 1024; + +/** @returns {boolean} */ +export const matches = (element, selectors) => { + const xmlMode = !ignoreCase(element); + if (selectors.includes(':scope')) + return CSSselect.is( + element, + selectors, + {strict: true, context: element, xmlMode, adapter} + ); + const key = (xmlMode ? 'x' : 'h') + selectors; + let test = compiled.get(key); + if (!test) { + if (compiled.size === MAX_COMPILED) + compiled.delete(compiled.keys().next().value); + test = CSSselect.compile(selectors, {strict: true, xmlMode, adapter, cacheResults: false}); + compiled.set(key, test); } -); + return test(element); +}; diff --git a/test/html/document.js b/test/html/document.js index b5e15062..12116bf9 100644 --- a/test/html/document.js +++ b/test/html/document.js @@ -92,6 +92,42 @@ emptyClass.getElementById('b').className = 'x'; emptyClass.getElementById('b').classList.remove('x'); assert(emptyClass.querySelectorAll('[class=""]').map(({id}) => id).join(','), 'b,c', '[class=""] follows class mutations'); +const {document: compiled} = parseHTML('

'); +const compiledP = compiled.querySelector('p'); +assert(compiledP.matches('div .a'), true, 'matches() compiles a selector'); +compiledP.className = 'b'; +assert(compiledP.matches('div .a'), false, 'a compiled selector follows mutations'); +// A retained selector must not retain css-select's ancestor result caches. +// Keep the exact same selector through attribute and tree mutations, and +// reuse it on a second document whose first result is positive. +const hasSelector = 'div:has(.yes) p'; +const {document: hasDocument} = parseHTML('

'); +const hasP = hasDocument.querySelector('p'); +const hasSpan = hasDocument.querySelector('span'); +const hasResults = [hasP.matches(hasSelector)]; +hasSpan.className = 'yes'; +hasResults.push(hasP.matches(hasSelector)); +hasSpan.removeAttribute('class'); +hasResults.push(hasP.matches(hasSelector)); +hasSpan.className = 'yes'; +hasSpan.remove(); +hasResults.push(hasP.matches(hasSelector)); +hasP.parentNode.appendChild(hasSpan); +hasResults.push(hasP.matches(hasSelector)); +const {document: initiallyHas} = parseHTML('

'); +const initiallyHasP = initiallyHas.querySelector('p'); +hasResults.push(initiallyHasP.matches(hasSelector)); +initiallyHas.querySelector('span').remove(); +hasResults.push(initiallyHasP.matches(hasSelector)); +assert(hasResults.join(','), 'false,true,false,false,true,true,false', 'compiled :has() selectors follow positive and negative DOM mutations'); + +const compiledXML = (new DOMParser).parseFromString('

', 'text/xml'); +assert(compiledXML.querySelector('p').matches('DIV P'), false, 'compiled selectors are kept per document kind'); +assert(compiledP.matches('DIV P'), true, 'compiled selectors are kept per document kind'); +for (let i = 0; i < 1100; i++) + compiledP.matches(`.b, .c${i}`); +assert(compiledP.matches('div .b'), true, 'the oldest compiled selectors are dropped'); + assert((new DOMParser).parseFromString('...', 'text/html').firstElementChild.localName, 'html'); const issue187 = (new DOMParser).parseFromString(``, 'text/html').defaultView.window.document; diff --git a/test/html/matches-mutations.js b/test/html/matches-mutations.js new file mode 100644 index 00000000..b7851408 --- /dev/null +++ b/test/html/matches-mutations.js @@ -0,0 +1,13 @@ +const assert = require('../assert.js').for('Retained :has() reflected attributes'); +const {parseHTML} = global[Symbol.for('linkedom')]; + +const {document} = parseHTML('

'); +const p = document.querySelector('p'); +const span = document.querySelector('span'); +const selector = 'div:has([slot="yes"]) p'; +const results = [span.slot, p.matches(selector)]; +span.slot = 'yes'; +results.push(span.slot, p.matches(selector)); +span.slot = ''; +results.push(span.slot, p.matches(selector)); +assert(JSON.stringify(results), '["",false,"yes",true,"",false]', 'retained :has() follows reflected attribute changes');