Skip to content

Commit

Permalink
refactor: use set in _collections
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Dec 24, 2023
1 parent a7859eb commit 3863185
Show file tree
Hide file tree
Showing 24 changed files with 810 additions and 758 deletions.
2 changes: 1 addition & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ const parseSvg = (data, from) => {
sax.ontext = (text) => {
if (current.type === 'element') {
// prevent trimming of meaningful whitespace inside textual tags
if (textElems.includes(current.name)) {
if (textElems.has(current.name)) {
/**
* @type {XastText}
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ const stringifyElement = (node, config, state) => {
tagCloseStart = defaults.tagCloseStart;
tagCloseEnd = defaults.tagCloseEnd;
openIndent = '';
} else if (textElems.includes(node.name)) {
} else if (textElems.has(node.name)) {
tagOpenEnd = defaults.tagOpenEnd;
tagCloseStart = defaults.tagCloseStart;
closeIndent = '';
Expand Down
46 changes: 25 additions & 21 deletions lib/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ const parseStyleDeclarations = (css) => {
};

/**
* @type {(stylesheet: Stylesheet, node: XastElement) => ComputedStyles}
* @param {Stylesheet} stylesheet
* @param {XastElement} node
* @returns {ComputedStyles}
*/
const computeOwnStyle = (stylesheet, node) => {
/** @type {ComputedStyles} */
Expand All @@ -139,7 +141,7 @@ const computeOwnStyle = (stylesheet, node) => {

// collect attributes
for (const [name, value] of Object.entries(node.attributes)) {
if (attrsGroups.presentation.includes(name)) {
if (attrsGroups.presentation.has(name)) {
computedStyle[name] = { type: 'static', inherited: false, value };
importantStyles.set(name, false);
}
Expand Down Expand Up @@ -217,29 +219,31 @@ exports.compareSpecificity = compareSpecificity;
* @type {(root: XastRoot) => Stylesheet}
*/
const collectStylesheet = (root) => {
/** @type {Array<StylesheetRule>} */
/** @type {StylesheetRule[]} */
const rules = [];
/** @type {Map<XastElement, XastParent>} */
const parents = new Map();

visit(root, {
element: {
enter: (node, parentNode) => {
// store parents
parents.set(node, parentNode);
// find and parse all styles
if (node.name === 'style') {

if (node.name !== 'style') {
return;
}

if (
node.attributes.type == null ||
node.attributes.type === '' ||
node.attributes.type === 'text/css'
) {
const dynamic =
node.attributes.media != null && node.attributes.media !== 'all';
if (
node.attributes.type == null ||
node.attributes.type === '' ||
node.attributes.type === 'text/css'
) {
const children = node.children;
for (const child of children) {
if (child.type === 'text' || child.type === 'cdata') {
rules.push(...parseStylesheet(child.value, dynamic));
}

for (const child of node.children) {
if (child.type === 'text' || child.type === 'cdata') {
rules.push(...parseStylesheet(child.value, dynamic));
}
}
}
Expand All @@ -253,21 +257,21 @@ const collectStylesheet = (root) => {
exports.collectStylesheet = collectStylesheet;

/**
* @type {(stylesheet: Stylesheet, node: XastElement) => ComputedStyles}
* @param {Stylesheet} stylesheet
* @param {XastElement} node
* @returns {ComputedStyles}
*/
const computeStyle = (stylesheet, node) => {
const { parents } = stylesheet;
// collect inherited styles
const computedStyles = computeOwnStyle(stylesheet, node);
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 &&
// ignore not inheritable styles
inheritableAttrs.includes(name) === true &&
presentationNonInheritableGroupAttrs.includes(name) === false
inheritableAttrs.has(name) &&
!presentationNonInheritableGroupAttrs.has(name)
) {
computedStyles[name] = { ...computed, inherited: true };
}
Expand Down
2 changes: 1 addition & 1 deletion lib/svgo/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ exports.includesUrlReference = includesUrlReference;
const findReferences = (attribute, value) => {
const results = [];

if (referencesProps.includes(attribute)) {
if (referencesProps.has(attribute)) {
const matches = value.matchAll(regReferencesUrl);
for (const match of matches) {
results.push(match[2]);
Expand Down
Loading

0 comments on commit 3863185

Please sign in to comment.