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

Avoid node.parentNode in style manager #1576

Merged
merged 1 commit into from
Sep 21, 2021
Merged
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
39 changes: 22 additions & 17 deletions lib/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* @typedef {import('css-tree').Rule} CsstreeRule
* @typedef {import('./types').Specificity} Specificity
* @typedef {import('./types').Stylesheet} Stylesheet
* @typedef {import('./types').StylesheetRule} StylesheetRule
* @typedef {import('./types').StylesheetDeclaration} StylesheetDeclaration
* @typedef {import('./types').ComputedStyles} ComputedStyles
Expand Down Expand Up @@ -128,7 +129,7 @@ const parseStyleDeclarations = (css) => {
};

/**
* @type {(stylesheet: Array<StylesheetRule>, node: XastElement) => ComputedStyles}
* @type {(stylesheet: Stylesheet, node: XastElement) => ComputedStyles}
*/
const computeOwnStyle = (stylesheet, node) => {
/**
Expand All @@ -146,7 +147,7 @@ const computeOwnStyle = (stylesheet, node) => {
}

// collect matching rules
for (const { selectors, declarations, dynamic } of stylesheet) {
for (const { selectors, declarations, dynamic } of stylesheet.rules) {
if (matches(node, selectors)) {
for (const { name, value, important } of declarations) {
const computed = computedStyle[name];
Expand Down Expand Up @@ -211,17 +212,23 @@ const compareSpecificity = (a, b) => {
};

/**
* @type {(root: XastRoot) => Array<StylesheetRule>}
* @type {(root: XastRoot) => Stylesheet}
*/
const collectStylesheet = (root) => {
/**
* @type {Array<StylesheetRule>}
*/
const stylesheet = [];
// find and parse all styles
const rules = [];
/**
* @type {Map<XastElement, XastParent>}
*/
const parents = new Map();
visit(root, {
element: {
enter: (node) => {
enter: (node, parentNode) => {
// store parents
parents.set(node, parentNode);
// find and parse all styles
if (node.name === 'style') {
const dynamic =
node.attributes.media != null && node.attributes.media !== 'all';
Expand All @@ -233,7 +240,7 @@ const collectStylesheet = (root) => {
const children = node.children;
for (const child of children) {
if (child.type === 'text' || child.type === 'cdata') {
stylesheet.push(...parseStylesheet(child.value, dynamic));
rules.push(...parseStylesheet(child.value, dynamic));
}
}
}
Expand All @@ -242,24 +249,23 @@ const collectStylesheet = (root) => {
},
});
// sort by selectors specificity
stable.inplace(stylesheet, (a, b) =>
stable.inplace(rules, (a, b) =>
compareSpecificity(a.specificity, b.specificity)
);
return stylesheet;
return { rules, parents };
};
exports.collectStylesheet = collectStylesheet;

/**
* @type {(stylesheet: Array<StylesheetRule>, node: XastElement) => ComputedStyles}
* @type {(stylesheet: Stylesheet, node: XastElement) => ComputedStyles}
*/
const computeStyle = (stylesheet, node) => {
const { parents } = stylesheet;
// collect inherited styles
const computedStyles = computeOwnStyle(stylesheet, node);
let parent = node;
// @ts-ignore parentNode is forbidden in public usage
while (parent.parentNode && parent.parentNode.type !== 'root') {
// @ts-ignore parentNode is forbidden in public usage
const inheritedStyles = computeOwnStyle(stylesheet, parent.parentNode);
let parent = parents.get(node);
while (parent != null && parent.type !== 'root') {
const inheritedStyles = computeOwnStyle(stylesheet, parent);
for (const [name, computed] of Object.entries(inheritedStyles)) {
if (
computedStyles[name] == null &&
Expand All @@ -270,8 +276,7 @@ const computeStyle = (stylesheet, node) => {
computedStyles[name] = { ...computed, inherited: true };
}
}
// @ts-ignore parentNode is forbidden in public usage
parent = parent.parentNode;
parent = parents.get(parent);
}
return computedStyles;
};
Expand Down
Loading