Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions cjs/shared/matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
34 changes: 25 additions & 9 deletions esm/shared/matches.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
36 changes: 36 additions & 0 deletions test/html/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div><p class="a"></p></div>');
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('<div><p></p><span></span></div>');
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('<div><p></p><span class="yes"></span></div>');
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('<div><p class="a"/></div>', '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(`<!DOCTYPE html><html><head></head><body></body></html>`, 'text/html').defaultView.window.document;
Expand Down
13 changes: 13 additions & 0 deletions test/html/matches-mutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const assert = require('../assert.js').for('Retained :has() reflected attributes');
const {parseHTML} = global[Symbol.for('linkedom')];

const {document} = parseHTML('<div><p></p><span></span></div>');
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');