Skip to content

Commit a3da8b3

Browse files
authored
refactor: use set in _collections (#1899)
1 parent a7859eb commit a3da8b3

24 files changed

+810
-758
lines changed

lib/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ const parseSvg = (data, from) => {
213213
sax.ontext = (text) => {
214214
if (current.type === 'element') {
215215
// prevent trimming of meaningful whitespace inside textual tags
216-
if (textElems.includes(current.name)) {
216+
if (textElems.has(current.name)) {
217217
/**
218218
* @type {XastText}
219219
*/

lib/stringifier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ const stringifyElement = (node, config, state) => {
235235
tagCloseStart = defaults.tagCloseStart;
236236
tagCloseEnd = defaults.tagCloseEnd;
237237
openIndent = '';
238-
} else if (textElems.includes(node.name)) {
238+
} else if (textElems.has(node.name)) {
239239
tagOpenEnd = defaults.tagOpenEnd;
240240
tagCloseStart = defaults.tagCloseStart;
241241
closeIndent = '';

lib/style.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ const parseStyleDeclarations = (css) => {
130130
};
131131

132132
/**
133-
* @type {(stylesheet: Stylesheet, node: XastElement) => ComputedStyles}
133+
* @param {Stylesheet} stylesheet
134+
* @param {XastElement} node
135+
* @returns {ComputedStyles}
134136
*/
135137
const computeOwnStyle = (stylesheet, node) => {
136138
/** @type {ComputedStyles} */
@@ -139,7 +141,7 @@ const computeOwnStyle = (stylesheet, node) => {
139141

140142
// collect attributes
141143
for (const [name, value] of Object.entries(node.attributes)) {
142-
if (attrsGroups.presentation.includes(name)) {
144+
if (attrsGroups.presentation.has(name)) {
143145
computedStyle[name] = { type: 'static', inherited: false, value };
144146
importantStyles.set(name, false);
145147
}
@@ -217,29 +219,31 @@ exports.compareSpecificity = compareSpecificity;
217219
* @type {(root: XastRoot) => Stylesheet}
218220
*/
219221
const collectStylesheet = (root) => {
220-
/** @type {Array<StylesheetRule>} */
222+
/** @type {StylesheetRule[]} */
221223
const rules = [];
222224
/** @type {Map<XastElement, XastParent>} */
223225
const parents = new Map();
226+
224227
visit(root, {
225228
element: {
226229
enter: (node, parentNode) => {
227-
// store parents
228230
parents.set(node, parentNode);
229-
// find and parse all styles
230-
if (node.name === 'style') {
231+
232+
if (node.name !== 'style') {
233+
return;
234+
}
235+
236+
if (
237+
node.attributes.type == null ||
238+
node.attributes.type === '' ||
239+
node.attributes.type === 'text/css'
240+
) {
231241
const dynamic =
232242
node.attributes.media != null && node.attributes.media !== 'all';
233-
if (
234-
node.attributes.type == null ||
235-
node.attributes.type === '' ||
236-
node.attributes.type === 'text/css'
237-
) {
238-
const children = node.children;
239-
for (const child of children) {
240-
if (child.type === 'text' || child.type === 'cdata') {
241-
rules.push(...parseStylesheet(child.value, dynamic));
242-
}
243+
244+
for (const child of node.children) {
245+
if (child.type === 'text' || child.type === 'cdata') {
246+
rules.push(...parseStylesheet(child.value, dynamic));
243247
}
244248
}
245249
}
@@ -253,21 +257,21 @@ const collectStylesheet = (root) => {
253257
exports.collectStylesheet = collectStylesheet;
254258

255259
/**
256-
* @type {(stylesheet: Stylesheet, node: XastElement) => ComputedStyles}
260+
* @param {Stylesheet} stylesheet
261+
* @param {XastElement} node
262+
* @returns {ComputedStyles}
257263
*/
258264
const computeStyle = (stylesheet, node) => {
259265
const { parents } = stylesheet;
260-
// collect inherited styles
261266
const computedStyles = computeOwnStyle(stylesheet, node);
262267
let parent = parents.get(node);
263268
while (parent != null && parent.type !== 'root') {
264269
const inheritedStyles = computeOwnStyle(stylesheet, parent);
265270
for (const [name, computed] of Object.entries(inheritedStyles)) {
266271
if (
267272
computedStyles[name] == null &&
268-
// ignore not inheritable styles
269-
inheritableAttrs.includes(name) === true &&
270-
presentationNonInheritableGroupAttrs.includes(name) === false
273+
inheritableAttrs.has(name) &&
274+
!presentationNonInheritableGroupAttrs.has(name)
271275
) {
272276
computedStyles[name] = { ...computed, inherited: true };
273277
}

lib/svgo/tools.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ exports.includesUrlReference = includesUrlReference;
204204
const findReferences = (attribute, value) => {
205205
const results = [];
206206

207-
if (referencesProps.includes(attribute)) {
207+
if (referencesProps.has(attribute)) {
208208
const matches = value.matchAll(regReferencesUrl);
209209
for (const match of matches) {
210210
results.push(match[2]);

0 commit comments

Comments
 (0)