Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(css selector): temporarily remove light dom shortcut #2008

Merged
merged 1 commit into from
Apr 28, 2020
Merged
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
38 changes: 21 additions & 17 deletions src/injected/cssSelectorEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,28 @@ export function createCSSEngine(shadow: boolean): SelectorEngine {
},

query(root: SelectorRoot, selector: string): Element | undefined {
const simple = root.querySelector(selector);
if (simple)
return simple;
if (!shadow)
return;
// TODO: uncomment for performance.
// const simple = root.querySelector(selector);
// if (simple)
// return simple;
// if (!shadow)
// return;
const parts = split(selector);
if (!parts.length)
return;
parts.reverse();
return queryShadowInternal(root, root, parts);
return queryShadowInternal(root, root, parts, shadow);
},

queryAll(root: SelectorRoot, selector: string): Element[] {
if (!shadow)
return Array.from(root.querySelectorAll(selector));
// TODO: uncomment for performance.
// if (!shadow)
// return Array.from(root.querySelectorAll(selector));
const result: Element[] = [];
const parts = split(selector);
if (parts.length) {
parts.reverse();
queryShadowAllInternal(root, root, parts, result);
queryShadowAllInternal(root, root, parts, shadow, result);
}
return result;
}
Expand All @@ -109,43 +111,45 @@ export function createCSSEngine(shadow: boolean): SelectorEngine {
return engine;
}

function queryShadowInternal(boundary: SelectorRoot, root: SelectorRoot, parts: string[]): Element | undefined {
function queryShadowInternal(boundary: SelectorRoot, root: SelectorRoot, parts: string[], shadow: boolean): Element | undefined {
const matching = root.querySelectorAll(parts[0]);
for (let i = 0; i < matching.length; i++) {
const element = matching[i];
if (parts.length === 1 || matches(element, parts, boundary))
return element;
}
if (!shadow)
return;
if ((root as Element).shadowRoot) {
const child = queryShadowInternal(boundary, (root as Element).shadowRoot!, parts);
const child = queryShadowInternal(boundary, (root as Element).shadowRoot!, parts, shadow);
if (child)
return child;
}
const elements = root.querySelectorAll('*');
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
if (element.shadowRoot) {
const child = queryShadowInternal(boundary, element.shadowRoot, parts);
const child = queryShadowInternal(boundary, element.shadowRoot, parts, shadow);
if (child)
return child;
}
}
}

function queryShadowAllInternal(boundary: SelectorRoot, root: SelectorRoot, parts: string[], result: Element[]) {
function queryShadowAllInternal(boundary: SelectorRoot, root: SelectorRoot, parts: string[], shadow: boolean, result: Element[]) {
const matching = root.querySelectorAll(parts[0]);
for (let i = 0; i < matching.length; i++) {
const element = matching[i];
if (parts.length === 1 || matches(element, parts, boundary))
result.push(element);
}
if ((root as Element).shadowRoot)
queryShadowAllInternal(boundary, (root as Element).shadowRoot!, parts, result);
if (shadow && (root as Element).shadowRoot)
queryShadowAllInternal(boundary, (root as Element).shadowRoot!, parts, shadow, result);
const elements = root.querySelectorAll('*');
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
if (element.shadowRoot)
queryShadowAllInternal(boundary, element.shadowRoot, parts, result);
if (shadow && element.shadowRoot)
queryShadowAllInternal(boundary, element.shadowRoot, parts, shadow, result);
}
}

Expand Down