Skip to content

Commit 199451a

Browse files
authored
cleanupAttributes: handle identity transforms (#182)
1 parent e045b46 commit 199451a

15 files changed

+816
-510
lines changed

lib/css/styleData.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,12 @@ export class StyleData {
136136

137137
// Override with style element rules.
138138
const importantProperties = new Set();
139-
for (const rule of this.getSortedRules()) {
140-
if (rule.matches(element)) {
141-
const isDynamic = rule.isInMediaQuery() || rule.hasPseudos();
142-
for (const [name, value] of rule.getDeclarationEntries()) {
143-
if (isDynamic) {
144-
computedStyles.set(name, null);
145-
} else {
146-
const hasVars = value.toString().includes('var(');
147-
if (hasVars) {
148-
computedStyles.set(name, null);
149-
} else if (geometryProperties[name]) {
150-
// Support for geometry properties is inconsistent. Avoid changing these.
151-
computedStyles.set(name, null);
152-
} else {
153-
computedStyles.set(name, value);
154-
if (value.isImportant()) {
155-
importantProperties.add(name);
156-
}
157-
}
158-
}
159-
}
160-
}
161-
}
139+
this.computeStyleElementProps(element, computedStyles, importantProperties);
162140

163141
// Override with inline styles.
164142
if (!declarations) {
165-
const styleAttValue = StyleAttValue.getAttValue(element);
143+
/** @type {StyleAttValue|undefined} */
144+
const styleAttValue = element.svgAtts.get('style');
166145
if (styleAttValue) {
167146
declarations = new SvgAttMap(styleAttValue.entries());
168147
}
@@ -347,6 +326,45 @@ export class StyleData {
347326
return computedStyles;
348327
}
349328

329+
/**
330+
* @param {import('../types.js').XastElement} element
331+
* @param {import('../types.js').ComputedPropertyMap} [computedStyles]
332+
* @param {Set<string>} [importantProperties]
333+
* @returns {import('../types.js').ComputedPropertyMap}
334+
*/
335+
computeStyleElementProps(
336+
element,
337+
computedStyles = new Map(),
338+
importantProperties = new Set(),
339+
) {
340+
// Override with style element rules.
341+
for (const rule of this.getSortedRules()) {
342+
if (rule.matches(element)) {
343+
const isDynamic = rule.isInMediaQuery() || rule.hasPseudos();
344+
for (const [name, value] of rule.getDeclarationEntries()) {
345+
if (isDynamic) {
346+
computedStyles.set(name, null);
347+
} else {
348+
const hasVars = value.toString().includes('var(');
349+
if (hasVars) {
350+
computedStyles.set(name, null);
351+
} else if (geometryProperties[name]) {
352+
// Support for geometry properties is inconsistent. Avoid changing these.
353+
computedStyles.set(name, null);
354+
} else {
355+
computedStyles.set(name, value);
356+
if (value.isImportant()) {
357+
importantProperties.add(name);
358+
}
359+
}
360+
}
361+
}
362+
}
363+
}
364+
365+
return computedStyles;
366+
}
367+
350368
/** @param {Set<import('./css.js').CSSRule>} rules */
351369
deleteRules(rules) {
352370
if (rules.size === 0) {

lib/tools-ast.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ export function addReferencedIdsInStyleAttribute2(refs, element, properties) {
6565
}
6666
}
6767

68+
/**
69+
* @param {import('./types.js').XastElement} element
70+
* @param {string} propName
71+
*/
72+
export function deleteAttAndProp(element, propName) {
73+
element.svgAtts.delete(propName);
74+
/** @type {import('../types/types.js').StyleAttValue|undefined} */
75+
const styleAtt = element.svgAtts.get('style');
76+
if (styleAtt !== undefined) {
77+
styleAtt.delete(propName);
78+
if (styleAtt.count() === 0) {
79+
element.svgAtts.delete('style');
80+
}
81+
}
82+
}
83+
6884
/**
6985
* @param {import('./types.js').XastElement} element
7086
* @param {...string} attNames

lib/types.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ export type ComputedPropertyMap = Map<string, AttValue | null>;
142142

143143
export class StyleData {
144144
addStyleSection(css: string): void;
145-
computeOwnProps(node: XastElement): ComputedPropertyMap;
145+
computeOwnProps(
146+
element: XastElement,
147+
declarations?: SvgAttValues,
148+
): ComputedPropertyMap;
146149
/** @deprecated */
147150
computeOwnStyle(node: XastElement): Map<string, string | null>;
148151
computeParentProps(parentList: Readonly<ParentList>): ComputedPropertyMap;
@@ -161,6 +164,7 @@ export class StyleData {
161164
parentList: Readonly<ParentList>,
162165
declarations?: SvgAttValues,
163166
): ComputedStyleMap;
167+
computeStyleElementProps(element: XastElement): ComputedPropertyMap;
164168
deleteRules(rules: Set<CSSRule>): void;
165169
getFeatures(): Set<CSSFeatures>;
166170
getIdsReferencedByProperties(): string[];

lib/types/svgTransforms.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ export function svgStringifyTransform(transforms) {
148148
break;
149149
}
150150
}
151+
152+
if (transforms.length === 0) {
153+
return 'scale(1)';
154+
}
151155
let str = '';
152156
for (let index = 0; index < transforms.length; index++) {
153157
const fn = transforms[index];
@@ -202,6 +206,10 @@ export function svgStringifyTransformAsProperty(transforms) {
202206
throw new Error();
203207
}
204208
}
209+
210+
if (transforms.length === 0) {
211+
return 'scale(1)';
212+
}
205213
return transforms.reduce(
206214
(str, transform) => (str += stringify(transform)),
207215
'',

0 commit comments

Comments
 (0)