svgm-core 0.3.8

SVG optimization engine — fast, safe, fixed-point convergence SVG optimizer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::collections::HashMap;

use super::{Pass, PassResult};
use crate::ast::{Document, NodeId, NodeKind};

pub struct RemoveUnknownsAndDefaults;

/// SVG attributes that can be safely removed when they match their default values.
/// Conservative list — only includes values that are unambiguously default per SVG spec.
const DEFAULT_ATTRS: &[(&str, &str)] = &[
    // Presentation defaults
    ("fill", "black"),
    ("fill", "#000"),
    ("fill", "#000000"),
    ("fill-opacity", "1"),
    ("fill-rule", "nonzero"),
    ("stroke", "none"),
    ("stroke-opacity", "1"),
    ("stroke-width", "1"),
    ("stroke-linecap", "butt"),
    ("stroke-linejoin", "miter"),
    ("stroke-miterlimit", "4"),
    ("stroke-dasharray", "none"),
    ("stroke-dashoffset", "0"),
    ("opacity", "1"),
    ("visibility", "visible"),
    ("display", "inline"),
    ("overflow", "visible"),
    ("clip-rule", "nonzero"),
    ("color-interpolation", "sRGB"),
    ("color-interpolation-filters", "linearRGB"),
    ("direction", "ltr"),
    ("font-style", "normal"),
    ("font-variant", "normal"),
    ("font-weight", "normal"),
    ("font-stretch", "normal"),
    ("text-anchor", "start"),
    ("text-decoration", "none"),
    ("dominant-baseline", "auto"),
    ("alignment-baseline", "auto"),
    ("baseline-shift", "0"),
    ("writing-mode", "lr-tb"),
    ("letter-spacing", "normal"),
    ("word-spacing", "normal"),
    ("filter", "none"),
    ("flood-opacity", "1"),
    ("lighting-color", "white"),
    ("lighting-color", "#fff"),
    ("lighting-color", "#ffffff"),
    ("pointer-events", "visiblePainted"),
    ("image-rendering", "auto"),
    ("shape-rendering", "auto"),
    ("text-rendering", "auto"),
    ("color-profile", "auto"),
    ("cursor", "auto"),
    ("enable-background", "accumulate"),
    ("stop-color", "black"),
    ("stop-color", "#000"),
    ("stop-color", "#000000"),
    ("stop-opacity", "1"),
    // SVG filter defaults
    ("mode", "normal"),
    ("color-interpolation-filters", "linearRGB"),
    ("flood-color", "black"),
    ("flood-color", "#000"),
    ("flood-color", "#000000"),
    // Gradient defaults
    ("spreadMethod", "pad"),
    ("gradientUnits", "objectBoundingBox"),
    // Misc defaults
    ("clip-path", "none"),
    ("mask", "none"),
    ("unicode-bidi", "normal"),
    ("baseline-shift", "baseline"),
    ("white-space", "normal"),
    ("text-overflow", "clip"),
];

/// Elements where `fill="black"` is NOT the default and should not be removed.
/// For these, the default fill behavior differs or fill is inherited.
const SKIP_FILL_REMOVAL: &[&str] = &[
    // On the root <svg>, fill is inherited to all children, so removing it changes behavior.
    "svg",
];

/// Inheritable presentation attributes (matches SVGO's inheritableAttrs minus
/// presentationNonInheritableGroupAttrs). These inherit from parent to child
/// in the SVG cascade.
const INHERITABLE_ATTRS: &[&str] = &[
    "clip-rule",
    "color",
    "color-interpolation",
    "color-interpolation-filters",
    "color-profile",
    "cursor",
    "direction",
    "dominant-baseline",
    "fill",
    "fill-opacity",
    "fill-rule",
    "font",
    "font-family",
    "font-size",
    "font-size-adjust",
    "font-stretch",
    "font-style",
    "font-variant",
    "font-weight",
    "image-rendering",
    "letter-spacing",
    "marker",
    "marker-end",
    "marker-mid",
    "marker-start",
    "paint-order",
    "pointer-events",
    "shape-rendering",
    "stroke",
    "stroke-dasharray",
    "stroke-dashoffset",
    "stroke-linecap",
    "stroke-linejoin",
    "stroke-miterlimit",
    "stroke-opacity",
    "stroke-width",
    "text-anchor",
    "text-rendering",
    "visibility",
    "word-spacing",
    "writing-mode",
];

/// Compute the inherited value for each inheritable attribute by walking the
/// ancestor chain. Returns a map of attr_name → nearest ancestor's value.
/// Matches SVGO's computeStyle ancestor-walking logic.
fn compute_inherited_values(doc: &Document, id: NodeId) -> HashMap<String, String> {
    let mut values = HashMap::new();
    let mut current = doc.node(id).parent;
    while let Some(parent_id) = current {
        if let NodeKind::Element(ref elem) = doc.node(parent_id).kind {
            for attr in &elem.attributes {
                if attr.prefix.is_none()
                    && INHERITABLE_ATTRS.contains(&attr.name.as_str())
                    && !values.contains_key(&attr.name)
                {
                    // Nearest ancestor wins — don't overwrite
                    values.insert(attr.name.clone(), attr.value.clone());
                }
            }
        }
        current = doc.node(parent_id).parent;
    }
    values
}

impl Pass for RemoveUnknownsAndDefaults {
    fn name(&self) -> &'static str {
        "removeUnknownsAndDefaults"
    }

    fn run(&self, doc: &mut Document) -> PassResult {
        let mut changed = false;
        let ids = doc.traverse();

        for id in ids {
            // Phase 1: compute inherited attribute values (immutable borrow)
            let inherited = compute_inherited_values(doc, id);

            // Phase 2: mutate attributes (mutable borrow)
            let node = doc.node_mut(id);
            if let NodeKind::Element(ref mut elem) = node.kind {
                let elem_name = elem.name.clone();
                let has_id = elem
                    .attributes
                    .iter()
                    .any(|a| a.prefix.is_none() && a.name == "id");
                let before = elem.attributes.len();

                elem.attributes.retain(|attr| {
                    // Only check unprefixed attributes
                    if attr.prefix.is_some() {
                        return true;
                    }

                    // Remove obsolete version attribute from <svg> (SVG2)
                    if attr.name == "version" && elem_name == "svg" {
                        return false;
                    }

                    // Remove x="0" and y="0" from <svg> (spec defaults)
                    if (attr.name == "x" || attr.name == "y")
                        && attr.value == "0"
                        && elem_name == "svg"
                    {
                        return false;
                    }

                    // Check fill removal exceptions
                    if attr.name == "fill" && SKIP_FILL_REMOVAL.contains(&elem_name.as_str()) {
                        return true;
                    }

                    // SVGO parity: skip default removal and useless override removal
                    // for elements with id (CSS/JS may target them)
                    if has_id {
                        return true;
                    }

                    let is_default = DEFAULT_ATTRS
                        .iter()
                        .any(|&(name, val)| attr.name == name && attr.value == val);
                    let is_inheritable = INHERITABLE_ATTRS.contains(&attr.name.as_str());

                    // Check A (SVGO defaultAttrs): remove known defaults
                    if is_default {
                        if is_inheritable {
                            // Only remove if NO ancestor has this property at all.
                            // If any ancestor sets it, keep — removing would change
                            // the inherited value. (Falls through to Check B.)
                            if !inherited.contains_key(&attr.name) {
                                return false;
                            }
                        } else {
                            // Non-inheritable default → always safe to remove
                            return false;
                        }
                    }

                    // Check B (SVGO uselessOverrides): remove attrs that duplicate
                    // the inherited value (redundant override)
                    if is_inheritable && inherited.get(&attr.name).is_some_and(|v| *v == attr.value)
                    {
                        return false;
                    }

                    true
                });

                if elem.attributes.len() != before {
                    changed = true;
                }
            }
        }

        if changed {
            PassResult::Changed
        } else {
            PassResult::Unchanged
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::parse;
    use crate::serializer::serialize;

    #[test]
    fn removes_default_fill_black() {
        let input =
            r#"<svg xmlns="http://www.w3.org/2000/svg"><rect fill="black" width="10"/></svg>"#;
        let mut doc = parse(input).unwrap();
        assert_eq!(RemoveUnknownsAndDefaults.run(&mut doc), PassResult::Changed);
        let output = serialize(&doc);
        assert!(
            !output.contains("fill"),
            "default fill=black should be removed: {output}"
        );
    }

    #[test]
    fn removes_default_opacity() {
        let input = r#"<svg xmlns="http://www.w3.org/2000/svg"><rect opacity="1" fill-opacity="1" stroke-opacity="1"/></svg>"#;
        let mut doc = parse(input).unwrap();
        assert_eq!(RemoveUnknownsAndDefaults.run(&mut doc), PassResult::Changed);
        let output = serialize(&doc);
        assert!(
            !output.contains("opacity"),
            "default opacities should be removed: {output}"
        );
    }

    #[test]
    fn removes_default_stroke_none() {
        let input = r#"<svg xmlns="http://www.w3.org/2000/svg"><rect stroke="none"/></svg>"#;
        let mut doc = parse(input).unwrap();
        assert_eq!(RemoveUnknownsAndDefaults.run(&mut doc), PassResult::Changed);
        let output = serialize(&doc);
        assert!(
            !output.contains("stroke"),
            "default stroke=none should be removed: {output}"
        );
    }

    #[test]
    fn keeps_non_default_fill() {
        let input = r#"<svg xmlns="http://www.w3.org/2000/svg"><rect fill="red"/></svg>"#;
        let mut doc = parse(input).unwrap();
        assert_eq!(
            RemoveUnknownsAndDefaults.run(&mut doc),
            PassResult::Unchanged
        );
    }

    #[test]
    fn keeps_fill_on_svg_element() {
        let input = r#"<svg xmlns="http://www.w3.org/2000/svg" fill="black"><rect/></svg>"#;
        let mut doc = parse(input).unwrap();
        // fill="black" on <svg> should be kept — it's inherited by children
        assert_eq!(
            RemoveUnknownsAndDefaults.run(&mut doc),
            PassResult::Unchanged
        );
    }

    #[test]
    fn removes_version_from_svg() {
        let input = r#"<svg xmlns="http://www.w3.org/2000/svg" version="1.1"><rect/></svg>"#;
        let mut doc = parse(input).unwrap();
        assert_eq!(RemoveUnknownsAndDefaults.run(&mut doc), PassResult::Changed);
        let output = serialize(&doc);
        assert!(!output.contains("version"));
    }

    #[test]
    fn removes_hex_default_fill() {
        let input =
            "<svg xmlns=\"http://www.w3.org/2000/svg\"><path fill=\"#000000\" d=\"M0 0\"/></svg>";
        let mut doc = parse(input).unwrap();
        assert_eq!(RemoveUnknownsAndDefaults.run(&mut doc), PassResult::Changed);
        let output = serialize(&doc);
        assert!(
            !output.contains("fill"),
            "fill=#000000 should be removed: {output}"
        );
    }

    #[test]
    fn keeps_fill_black_when_parent_has_fill_none() {
        let input = "<svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"none\"><path fill=\"black\" d=\"M0 0\"/></svg>";
        let mut doc = parse(input).unwrap();
        RemoveUnknownsAndDefaults.run(&mut doc);
        let output = serialize(&doc);
        assert!(
            output.contains("fill=\"black\""),
            "fill=black must be kept when parent has fill=none: {output}"
        );
    }

    #[test]
    fn keeps_fill_black_with_grandparent_override() {
        let input = "<svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"red\"><g><path fill=\"black\" d=\"M0 0\"/></g></svg>";
        let mut doc = parse(input).unwrap();
        RemoveUnknownsAndDefaults.run(&mut doc);
        let output = serialize(&doc);
        assert!(
            output.contains("fill=\"black\""),
            "fill=black must be kept when grandparent has fill=red: {output}"
        );
    }

    #[test]
    fn keeps_stroke_none_when_parent_has_stroke() {
        let input = "<svg xmlns=\"http://www.w3.org/2000/svg\" stroke=\"red\"><path stroke=\"none\" d=\"M0 0\"/></svg>";
        let mut doc = parse(input).unwrap();
        RemoveUnknownsAndDefaults.run(&mut doc);
        let output = serialize(&doc);
        assert!(
            output.contains("stroke=\"none\""),
            "stroke=none must be kept when parent has stroke=red: {output}"
        );
    }

    #[test]
    fn removes_useless_override_same_value() {
        // Parent has fill="red", child has fill="red" → useless override, remove
        let input = "<svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"red\"><path fill=\"red\" d=\"M0 0\"/></svg>";
        let mut doc = parse(input).unwrap();
        RemoveUnknownsAndDefaults.run(&mut doc);
        let output = serialize(&doc);
        // Child's fill="red" should be removed (matches inherited value)
        assert_eq!(
            output.matches("fill=\"red\"").count(),
            1,
            "child fill=red should be removed as useless override: {output}"
        );
    }

    #[test]
    fn keeps_default_when_parent_has_same_attr_different_value() {
        // Parent has fill="black" → computedParentStyle['fill'] is non-null →
        // Check A skips. Check B: "black" != "#000" → keeps. Matches SVGO.
        let input = "<svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"black\"><path fill=\"#000\" d=\"M0 0\"/></svg>";
        let mut doc = parse(input).unwrap();
        RemoveUnknownsAndDefaults.run(&mut doc);
        let output = serialize(&doc);
        // SVGO keeps this because the string values differ, even though semantically both are black.
        // (convert_colors normalizes these before this pass runs in practice.)
        assert!(
            output.contains("fill=\"#000\""),
            "fill=#000 should be kept when parent has fill=black (different string): {output}"
        );
    }

    #[test]
    fn removes_useless_override_exact_match() {
        // Parent has fill="black", child has fill="black" → exact match → remove
        let input = "<svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"black\"><path fill=\"black\" d=\"M0 0\"/></svg>";
        let mut doc = parse(input).unwrap();
        RemoveUnknownsAndDefaults.run(&mut doc);
        let output = serialize(&doc);
        // svg keeps fill="black" (SKIP_FILL_REMOVAL), path's is removed (useless override)
        assert_eq!(
            output.matches("fill=\"black\"").count(),
            1,
            "child fill=black should be removed as useless override: {output}"
        );
    }

    #[test]
    fn keeps_defaults_on_element_with_id() {
        // SVGO skips default removal for elements with id
        let input = "<svg xmlns=\"http://www.w3.org/2000/svg\"><path id=\"a\" fill=\"black\" d=\"M0 0\"/></svg>";
        let mut doc = parse(input).unwrap();
        RemoveUnknownsAndDefaults.run(&mut doc);
        let output = serialize(&doc);
        assert!(
            output.contains("fill=\"black\""),
            "fill=black should be kept on element with id: {output}"
        );
    }
}