oxvg_optimiser 0.0.5

The OXVG optimiser is library for optimising SVG documents.
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use std::sync::LazyLock;

use oxvg_ast::{
    has_attribute, is_attribute,
    node::{self, Ref},
    style::{ComputedStyles, Mode},
    visitor::{Context, ContextFlags, PrepareOutcome},
};

use oxvg_ast::{element::Element, visitor::Visitor};
use oxvg_collections::{
    attribute::{AttributeGroup, AttributeInfo},
    content_type::ContentTypeId,
    element::ElementId,
    is_prefix,
    name::Prefix,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "wasm")]
use tsify::Tsify;

use crate::error::JobsError;

#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "napi", napi(object))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[allow(clippy::struct_excessive_bools)]
/// Removes elements and attributes that are not expected in an SVG document. Removes
/// attributes that are not expected on a given element. Removes attributes that are
/// the default for a given element. Removes elements that are not expected as a child
/// for a given element.
///
/// # Differences to SVGO
///
/// SVGO will avoid removing `<tspan>` from `<a>`, whereas we will remove it.
///
/// # Correctness
///
/// This job should never visually change the document.
///
/// # Errors
///
/// Never.
///
/// If this job produces an error or panic, please raise an [issue](https://github.com/noahbald/oxvg/issues)
pub struct RemoveUnknownsAndDefaults {
    #[cfg_attr(feature = "serde", serde(default = "default_unknown_content"))]
    /// Whether to remove elements that are unknown or unknown for it's parent element.
    pub unknown_content: bool,
    #[cfg_attr(feature = "serde", serde(default = "default_unknown_attrs"))]
    /// Whether to remove attributes that are unknown or unknown for it's element.
    pub unknown_attrs: bool,
    #[cfg_attr(feature = "serde", serde(default = "default_default_attrs"))]
    /// Whether to remove attributes that are equivalent to the default for it's element.
    pub default_attrs: bool,
    #[cfg_attr(
        feature = "serde",
        serde(default = "default_default_markup_declarations")
    )]
    /// Whether to remove xml declarations equivalent to the default.
    pub default_markup_declarations: bool,
    #[cfg_attr(feature = "serde", serde(default = "default_useless_overrides"))]
    /// Whether to remove attributes equivalent to it's inherited value.
    pub useless_overrides: bool,
    #[cfg_attr(feature = "serde", serde(default = "default_keep_data_attrs"))]
    /// Whether to keep attributes prefixed with `data-`
    pub keep_data_attrs: bool,
    #[cfg_attr(feature = "serde", serde(default = "default_keep_aria_attrs"))]
    /// Whether to keep attributes prefixed with `aria-`
    pub keep_aria_attrs: bool,
    #[cfg_attr(feature = "serde", serde(default = "default_keep_role_attr"))]
    /// Whether to keep the `role` attribute
    pub keep_role_attr: bool,
}

impl Default for RemoveUnknownsAndDefaults {
    fn default() -> Self {
        RemoveUnknownsAndDefaults {
            unknown_content: default_unknown_content(),
            unknown_attrs: default_unknown_attrs(),
            default_attrs: default_default_attrs(),
            default_markup_declarations: default_default_markup_declarations(),
            useless_overrides: default_useless_overrides(),
            keep_data_attrs: default_keep_data_attrs(),
            keep_aria_attrs: default_keep_aria_attrs(),
            keep_role_attr: default_keep_role_attr(),
        }
    }
}

impl<'input, 'arena> Visitor<'input, 'arena> for RemoveUnknownsAndDefaults {
    type Error = JobsError<'input>;

    fn prepare(
        &self,
        document: &Element<'input, 'arena>,
        context: &mut Context<'input, 'arena, '_>,
    ) -> Result<PrepareOutcome, Self::Error> {
        context.query_has_stylesheet(document);
        Ok(PrepareOutcome::none)
    }

    fn processing_instruction(
        &self,
        processing_instruction: Ref<'input, 'arena>,
        context: &Context<'input, 'arena, '_>,
    ) -> Result<(), Self::Error> {
        if !self.default_markup_declarations {
            return Ok(());
        }

        let (target, data) = processing_instruction.processing_instruction().unwrap();
        let Some(data) = data else {
            return Ok(());
        };
        let Some(parent) = processing_instruction.parent_node() else {
            return Ok(());
        };
        let data = PI_STANDALONE.replace(data.as_str(), "").to_string().into();
        let new_pi = context.root.as_document().create_processing_instruction(
            target.clone(),
            data,
            &context.info.allocator,
        );
        log::debug!("replacing processing instruction");
        parent.replace_child(new_pi, &processing_instruction);
        Ok(())
    }

    fn element(
        &self,
        element: &Element<'input, 'arena>,
        context: &mut Context<'input, 'arena, '_>,
    ) -> Result<(), Self::Error> {
        if context.flags.contains(ContextFlags::within_foreign_object) {
            return Ok(());
        }

        let name = element.qual_name();
        if !name.prefix().is_empty() {
            return Ok(());
        }

        self.remove_unknown_content(element);
        let inherited = ComputedStyles::default()
            .with_inherited(element, &context.query_has_stylesheet_result)
            .map_err(JobsError::ComputedStylesError)?;
        self.remove_unknown_and_default_attrs(element, &inherited);

        Ok(())
    }
}

impl RemoveUnknownsAndDefaults {
    fn remove_unknown_content(&self, element: &Element) {
        if !self.unknown_content {
            return;
        }

        let name = element.qual_name().unaliased();
        if matches!(name, ElementId::Unknown(_)) {
            log::debug!("removing unknown element type");
            element.remove();
        }

        let Some(parent) = Element::parent_element(element) else {
            return;
        };
        if parent.node_type() == node::Type::Document {
            return;
        }

        let parent_name = parent.qual_name();
        if !parent_name.is_permitted_child(name) {
            log::debug!("removing unknown element of parent");
            element.remove();
        }
    }

    fn remove_unknown_and_default_attrs<'input>(
        &self,
        element: &Element<'input, '_>,
        inherited_styles: &ComputedStyles<'input>,
    ) {
        let element_name = element.qual_name();
        let has_id = has_attribute!(element, Id);

        element.attributes().retain(|attr| {
            let name = attr.name().unaliased();
            let local_name = name.local_name();
            let prefix = attr.prefix();
            let inheritable = matches!(name.r#type(), ContentTypeId::Inheritable(_));
            if is_prefix!(prefix, XML | XLink | XMLNS) || matches!(prefix, Prefix::Unknown { .. }) {
                log::debug!("ignoring prefix: {prefix:?}");
                return true;
            } else if self.keep_data_attrs && local_name.starts_with("data-") {
                log::debug!("keeping data attribute");
                return true;
            } else if local_name.as_str().starts_with("aria-") {
                log::debug!("keeping aria attribute: {}", self.keep_aria_attrs);
                return self.keep_aria_attrs;
            } else if is_attribute!(name, Role) {
                log::debug!("keeping role attribute: {}", self.keep_role_attr);
                return self.keep_role_attr;
            }

            if self.unknown_attrs
                && !is_attribute!(name, XMLNS)
                && !element_name.is_permitted_attribute(name)
            {
                log::debug!("removing unknown attr");
                return false;
            }

            let inherited_value = if name.prefix().is_empty() {
                if inheritable {
                    inherited_styles.get(name.unaliased())
                } else {
                    None
                }
            } else {
                None
            };
            if self.default_attrs
                && !has_id
                && inherited_value.is_none()
                && name.default().is_some_and(|a| a == *attr)
            {
                log::debug!(r#"removing "{name}" attr with default value"#);
                return false;
            }

            if self.useless_overrides
                && !has_id
                && name
                    .attribute_group()
                    .contains(AttributeGroup::Presentation)
                && !name
                    .info()
                    .contains(AttributeInfo::PresentationNonInheritableGroupAttrs)
                && inherited_value.is_some_and(|(inherited, mode)| {
                    if matches!(mode, Mode::Dynamic) {
                        log::debug!("not removing attr with inherited dynamic value");
                        return false;
                    }
                    inherited.value() == attr.value()
                })
            {
                log::debug!("removing useless override");
                return false;
            }
            true
        });
    }
}

const fn default_unknown_content() -> bool {
    true
}
const fn default_unknown_attrs() -> bool {
    true
}
const fn default_default_attrs() -> bool {
    true
}
const fn default_useless_overrides() -> bool {
    true
}
const fn default_default_markup_declarations() -> bool {
    true
}
const fn default_keep_data_attrs() -> bool {
    true
}
const fn default_keep_aria_attrs() -> bool {
    true
}
const fn default_keep_role_attr() -> bool {
    false
}

static PI_STANDALONE: LazyLock<regex::Regex> =
    LazyLock::new(|| regex::Regex::new(r#"\s*standalone\s*=\s*["']no["']"#).unwrap());

#[test]
#[allow(clippy::too_many_lines)]
fn remove_unknowns_and_defaults() -> anyhow::Result<()> {
    use crate::test_config;

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r##"<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:test="http://" attr="val" x="0" y="10" test:attr="val" xml:space="preserve">
    <!-- preserve xmlns and unknown prefixes -->
    <!-- preserves id'd attributes -->
    <rect fill="#000" d="M0 0"/>
    <rect fill="#000" d="M0 0" id="black-rect"/>
</svg>"##
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://">
    <!-- unknown elements are removed -->
    <test>
        test
    </test>
    <test:test>
        test
    </test:test>
    <g>
        test
    </g>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r##"<svg xmlns="http://www.w3.org/2000/svg">
    <!-- default values are preserved when inheritable -->
    <g fill="red">
        <path fill="#000" d="M118.8 186.9l79.2"/>
    </g>
</svg>"##
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r#"<svg xmlns="http://www.w3.org/2000/svg">
    <!-- remove attributes equal to inherited value -->
    <g fill="black">
        <g fill="red">
            <path fill="red" d="M118.8 186.9l79.2"/>
        </g>
    </g>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r#"<svg xmlns="http://www.w3.org/2000/svg">
    <!-- remove attributes equal to inherited value, excluding those with id -->
    <g fill="red">
        <g fill="red">
            <g fill="green">
                <g fill="green">
                    <path fill="red" d="M18.8 86.9l39.2"/>
                </g>
            </g>
            <path fill="red" d="M118.8 186.9l79.2"/>
            <path id="red" fill="red" d="M118.8 186.9l79.2"/>
        </g>
    </g>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r##"<svg xmlns="http://www.w3.org/2000/svg">
    <!-- allow data attributes -->
    <g fill="red" data-foo="bar">
        <path fill="#000" d="M118.8 186.9l79.2" data-bind="smth"/>
    </g>
</svg>"##
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:test="http://">
    <!-- skip `foreignObject` and it's children -->
    <foreignObject>
        <div class="test">
            fallback test
        </div>
    </foreignObject>

    <test>
        test
    </test>
    <test:test>
        test
    </test:test>
    <g>
        test
    </g>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r#"<svg xmlns="http://www.w3.org/2000/svg" x="0" y="0">
    <!-- remove defaults of non-inheritable values -->
    <svg x="10" y="10">
        <svg x="0" y="0">
            <path/>
        </svg>
        <svg x="0" y="10">
            <path/>
        </svg>
        <svg x="50" y="0">
            <path/>
        </svg>
    </svg>
    <svg x="100" y="100">
        <path/>
    </svg>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r#"<svg xmlns="http://www.w3.org/2000/svg">
    <!-- remove unknown elements -->
    <metadata>
        <sfw>
            <slices></slices>
            <sliceSourceBounds height="67.3" width="85.9" y="-40.8" x="-42.5" bottomLeftOrigin="true"></sliceSourceBounds>
        </sfw>
        <ellipse/>
    </metadata>
    <ellipse>
        <font-face/>
    </ellipse>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r#"<svg xmlns="http://www.w3.org/2000/svg">
    <!-- retain matching non-inheritable attributes -->
    <g transform="translate(792)">
        <g transform="translate(792)">
            <path d="M118.8 186.9l79.2"/>
        </g>
    </g>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r##"<svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="title">
    <!-- retain aria attributes -->
    <title id="title">
        Title
    </title>
    <g aria-label="foo">
        test
    </g>
    <path id="t" d="M10 10h10L10 20"/>
    <use href="#t"/>
</svg>"##
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": { "keepAriaAttrs": false } }"#,
        Some(
            r#"<svg xmlns="http://www.w3.org/2000/svg" aria-labelledby="title">
    <!-- remove aria attrs -->
    <title id="title">
        Title
    </title>
    <g aria-label="foo">
        test
    </g>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r#"<svg xmlns="http://www.w3.org/2000/svg" role="img">
    <!-- remove default role -->
    <g/>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": { "keepRoleAttr": true } }"#,
        Some(
            r#"<svg xmlns="http://www.w3.org/2000/svg" role="img">
    <!-- retain default role -->
    <g/>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r##"<svg width="480" height="360" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <!-- handle xlink and xmlns -->
    <text x="50" y="50">
        A <a xlink:href="#"><tspan>link around tspan</tspan></a> for testing
    </text>
</svg>"##
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r#"<svg width="64" height="18" xmlns="http://www.w3.org/2000/svg">
    <!-- removes `standalone="no" from xml declaration -->
    <text x="4" y="18">uwu</text>
</svg>"#
        ),
    )?);

    insta::assert_snapshot!(test_config(
        r#"{ "removeUnknownsAndDefaults": {} }"#,
        Some(
            r##"<svg width="50" height="50" xmlns="http://www.w3.org/2000/svg">
    <!-- do not remove default when inherited value differs -->
    <g fill="#fff">
      <g>
        <rect x="0" y="0" width="50" height="50" fill="#000" />
      </g>
    </g>
</svg>"##
        ),
    )?);

    Ok(())
}