Skip to content

Commit

Permalink
Avoid node.parentNode in style manager (#1576)
Browse files Browse the repository at this point in the history
node.parentNode will be removed in v3 along with JSAPI class wrapper on
each node.

Style manager uses it to find inherited styles. To workaround this
I collected all parents along with all styles. This constraints style
manager to work only with initial ast which should not be a problem as
each plagin execution is isolated.
  • Loading branch information
TrySound committed Sep 21, 2021
1 parent e3c6873 commit 79ceb09
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 168 deletions.
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

0 comments on commit 79ceb09

Please sign in to comment.