vexy-vsvg-plugin-sdk 2.4.2

Plugin SDK for vexy-vsvg
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
// this_file: crates/vexy-vsvg-plugin-sdk/src/plugins/remove_useless_defs.rs

//! Remove useless definitions plugin implementation
//!
//! This plugin removes unused definitions from SVG documents, specifically targeting
//! <defs> elements and non-rendering elements that don't have IDs. It follows the
//! same logic as SVGO's removeUselessDefs plugin.

use crate::Plugin;
use anyhow::Result;
use vexy_vsvg::ast::{Document, Element, Node};
use vexy_vsvg::error::VexyError;
use vexy_vsvg::visitor::Visitor;

/// Plugin that removes useless definitions from SVG documents
#[derive(Default)]
pub struct RemoveUselessDefsPlugin;

impl RemoveUselessDefsPlugin {
    /// Create a new RemoveUselessDefsPlugin
    pub fn new() -> Self {
        Self
    }

    /// List of non-rendering elements that can be filtered
    fn non_rendering_elements() -> &'static [&'static str] {
        &[
            "clipPath",
            "filter",
            "linearGradient",
            "marker",
            "mask",
            "pattern",
            "radialGradient",
            "solidColor",
            "symbol",
        ]
    }

    /// Check if an element is a non-rendering element
    fn is_non_rendering_element(name: &str) -> bool {
        Self::non_rendering_elements().contains(&name)
    }

    /// Check if a node should be preserved
    /// Elements are preserved if they have an id attribute or are style elements
    fn should_preserve_node(node: &Node) -> bool {
        match node {
            Node::Element(element) => {
                // Always preserve style elements
                if element.name.as_ref() == "style" {
                    return true;
                }

                // Preserve elements with IDs
                element.attributes.contains_key("id")
            }
            _ => true, // Preserve non-element nodes (text, comments, etc.)
        }
    }

    /// Check if an element should be processed for filtering
    fn should_process_element(element: &Element) -> bool {
        element.name.as_ref() == "defs"
            || (Self::is_non_rendering_element(&element.name)
                && !element.attributes.contains_key("id"))
    }

    /// Collect useful nodes from children, flattening structure where possible
    fn collect_useful_nodes<'a>(children: &[Node<'a>]) -> Vec<Node<'a>> {
        let mut useful_nodes = Vec::new();

        for child in children {
            match child {
                Node::Element(element) => {
                    if Self::should_preserve_node(child) {
                        // Element should be preserved - add it directly
                        useful_nodes.push(child.clone());
                    } else if Self::should_process_element(element) {
                        // Element can be processed - flatten its children
                        let flattened = Self::collect_useful_nodes(&element.children);
                        useful_nodes.extend(flattened);
                    } else {
                        // Element doesn't have ID - check if it has useful children to flatten
                        let flattened = Self::collect_useful_nodes(&element.children);
                        useful_nodes.extend(flattened);
                    }
                }
                _ => {
                    // Preserve non-element nodes (text, comments, etc.)
                    useful_nodes.push(child.clone());
                }
            }
        }

        useful_nodes
    }
}

impl Plugin for RemoveUselessDefsPlugin {
    fn name(&self) -> &'static str {
        "removeUselessDefs"
    }

    fn description(&self) -> &'static str {
        "Remove useless definitions from SVG document"
    }

    fn validate_params(&self, params: &serde_json::Value) -> anyhow::Result<()> {
        // This plugin doesn't accept any parameters in the original SVGO implementation
        if !params.is_null() && !params.as_object().is_some_and(|obj| obj.is_empty()) {
            return Err(anyhow::anyhow!(
                "removeUselessDefs plugin does not accept any parameters"
            ));
        }
        Ok(())
    }

    fn apply(&self, document: &mut Document) -> anyhow::Result<()> {
        let mut visitor = UselessDefsRemovalVisitor::new();
        vexy_vsvg::visitor::walk_document(&mut visitor, document)?;
        Ok(())
    }
}

/// Visitor implementation that removes useless definitions
struct UselessDefsRemovalVisitor;

impl UselessDefsRemovalVisitor {
    fn new() -> Self {
        Self
    }
}

impl Visitor<'_> for UselessDefsRemovalVisitor {
    fn visit_element_enter(&mut self, element: &mut Element<'_>) -> Result<(), VexyError> {
        // Process elements that should be filtered
        if RemoveUselessDefsPlugin::should_process_element(element) {
            // Collect useful nodes from children, applying flattening
            element.children = RemoveUselessDefsPlugin::collect_useful_nodes(&element.children);
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::borrow::Cow;
    use vexy_vsvg::ast::{Document, Element, Node};

    fn create_element(name: &'static str) -> Element<'static> {
        let mut element = Element::new(name);
        element.name = Cow::Borrowed(name);
        element
    }

    fn create_element_with_id(name: &'static str, id: &str) -> Element<'static> {
        let mut element = create_element(name);
        element
            .attributes
            .insert(Cow::Borrowed("id"), Cow::Owned(id.to_string()));
        element
    }

    #[test]
    fn test_plugin_creation() {
        let plugin = RemoveUselessDefsPlugin::new();
        assert_eq!(plugin.name(), "removeUselessDefs");
    }

    #[test]
    fn test_parameter_validation() {
        let plugin = RemoveUselessDefsPlugin::new();

        // Valid: null parameters
        assert!(plugin.validate_params(&json!(null)).is_ok());

        // Valid: empty object
        assert!(plugin.validate_params(&json!({})).is_ok());

        // Invalid: non-empty parameters
        assert!(plugin.validate_params(&json!({"someParam": true})).is_err());
    }

    #[test]
    fn test_non_rendering_elements() {
        assert!(RemoveUselessDefsPlugin::is_non_rendering_element(
            "clipPath"
        ));
        assert!(RemoveUselessDefsPlugin::is_non_rendering_element("filter"));
        assert!(RemoveUselessDefsPlugin::is_non_rendering_element(
            "linearGradient"
        ));
        assert!(RemoveUselessDefsPlugin::is_non_rendering_element("marker"));
        assert!(RemoveUselessDefsPlugin::is_non_rendering_element("mask"));
        assert!(RemoveUselessDefsPlugin::is_non_rendering_element("pattern"));
        assert!(RemoveUselessDefsPlugin::is_non_rendering_element(
            "radialGradient"
        ));
        assert!(RemoveUselessDefsPlugin::is_non_rendering_element(
            "solidColor"
        ));
        assert!(RemoveUselessDefsPlugin::is_non_rendering_element("symbol"));

        // Not non-rendering elements
        assert!(!RemoveUselessDefsPlugin::is_non_rendering_element("rect"));
        assert!(!RemoveUselessDefsPlugin::is_non_rendering_element("path"));
        assert!(!RemoveUselessDefsPlugin::is_non_rendering_element("defs"));
    }

    #[test]
    fn test_should_preserve_node() {
        // Elements with IDs should be preserved
        let element_with_id = create_element_with_id("path", "mypath");
        assert!(RemoveUselessDefsPlugin::should_preserve_node(
            &Node::Element(element_with_id)
        ));

        // Style elements should always be preserved
        let style_element = create_element("style");
        assert!(RemoveUselessDefsPlugin::should_preserve_node(
            &Node::Element(style_element)
        ));

        // Elements without IDs should not be preserved (unless they're style)
        let element_without_id = create_element("path");
        assert!(!RemoveUselessDefsPlugin::should_preserve_node(
            &Node::Element(element_without_id)
        ));

        // Non-element nodes should be preserved
        assert!(RemoveUselessDefsPlugin::should_preserve_node(
            &Node::Comment("comment".to_string().into())
        ));
    }

    #[test]
    fn test_should_process_element() {
        // defs elements should be processed
        let defs_element = create_element("defs");
        assert!(RemoveUselessDefsPlugin::should_process_element(
            &defs_element
        ));

        // Non-rendering elements without IDs should be processed
        let clippath_no_id = create_element("clipPath");
        assert!(RemoveUselessDefsPlugin::should_process_element(
            &clippath_no_id
        ));

        // Non-rendering elements with IDs should not be processed
        let clippath_with_id = create_element_with_id("clipPath", "clip1");
        assert!(!RemoveUselessDefsPlugin::should_process_element(
            &clippath_with_id
        ));

        // Regular elements should not be processed
        let rect_element = create_element("rect");
        assert!(!RemoveUselessDefsPlugin::should_process_element(
            &rect_element
        ));
    }

    #[test]
    fn test_collect_useful_nodes_basic() {
        // Create test children: one with ID, one without
        let path_with_id = create_element_with_id("path", "path1");
        let path_without_id = create_element("path");

        let children = vec![
            Node::Element(path_with_id.clone()),
            Node::Element(path_without_id),
        ];

        let useful = RemoveUselessDefsPlugin::collect_useful_nodes(&children);

        // Only the element with ID should be preserved
        assert_eq!(useful.len(), 1);
        if let Node::Element(element) = &useful[0] {
            assert!(element.attributes.contains_key("id"));
        }
    }

    #[test]
    fn test_collect_useful_nodes_style_preservation() {
        let style_element = create_element("style");
        let path_without_id = create_element("path");

        let children = vec![
            Node::Element(style_element.clone()),
            Node::Element(path_without_id),
        ];

        let useful = RemoveUselessDefsPlugin::collect_useful_nodes(&children);

        // Style element should be preserved even without ID
        assert_eq!(useful.len(), 1);
        if let Node::Element(element) = &useful[0] {
            assert_eq!(element.name.as_ref(), "style");
        }
    }

    #[test]
    fn test_collect_useful_nodes_flattening() {
        // Test flattening within a defs context via the full plugin
        let plugin = RemoveUselessDefsPlugin::new();
        let mut doc = Document::new();

        // Create defs with nested structure: defs > g > path (with ID)
        let path_with_id = create_element_with_id("path", "path1");
        let mut group_no_id = create_element("g");
        group_no_id
            .children
            .push(Node::Element(path_with_id.clone()));

        let mut defs = create_element("defs");
        defs.children.push(Node::Element(group_no_id));

        doc.root.children.push(Node::Element(defs));

        // Apply the plugin
        plugin.apply(&mut doc).unwrap();

        // Check that flattening occurred within defs
        if let Some(Node::Element(defs_element)) = doc.root.children.first() {
            assert_eq!(defs_element.name.as_ref(), "defs");
            // Group should be removed, path should be flattened up
            assert_eq!(defs_element.children.len(), 1);

            if let Node::Element(child) = &defs_element.children[0] {
                assert_eq!(child.name.as_ref(), "path");
                assert!(child.attributes.contains_key("id"));
            }
        }
    }

    #[test]
    fn test_plugin_apply() {
        let plugin = RemoveUselessDefsPlugin::new();
        let mut doc = Document::new();

        // Create defs with mixed content
        let mut defs = create_element("defs");
        defs.children.push(Node::Element(create_element("path"))); // Should be removed
        defs.children
            .push(Node::Element(create_element_with_id("path", "keep"))); // Should be kept
        defs.children.push(Node::Element(create_element("style"))); // Should be kept

        doc.root.children.push(Node::Element(defs));

        // Apply the plugin
        let result = plugin.apply(&mut doc);
        assert!(result.is_ok());

        // Check that defs was processed
        if let Some(Node::Element(defs_element)) = doc.root.children.first() {
            assert_eq!(defs_element.name.as_ref(), "defs");
            // Should have 2 children now (path with ID and style)
            assert_eq!(defs_element.children.len(), 2);

            // Verify preserved elements
            let has_path_with_id = defs_element.children.iter().any(|child| {
                if let Node::Element(elem) = child {
                    elem.name.as_ref() == "path" && elem.attributes.contains_key("id")
                } else {
                    false
                }
            });

            let has_style = defs_element.children.iter().any(|child| {
                if let Node::Element(elem) = child {
                    elem.name.as_ref() == "style"
                } else {
                    false
                }
            });

            assert!(has_path_with_id);
            assert!(has_style);
        } else {
            panic!("Expected defs element not found");
        }
    }

    #[test]
    fn test_non_rendering_element_without_id() {
        let plugin = RemoveUselessDefsPlugin::new();
        let mut doc = Document::new();

        // Create clipPath without ID containing useful content
        let mut clippath = create_element("clipPath");
        clippath
            .children
            .push(Node::Element(create_element("path"))); // Should be removed
        clippath
            .children
            .push(Node::Element(create_element_with_id("circle", "keep"))); // Should be kept

        doc.root.children.push(Node::Element(clippath));

        plugin.apply(&mut doc).unwrap();

        // clipPath without ID should be processed, keeping only useful children
        if let Some(Node::Element(clippath_element)) = doc.root.children.first() {
            assert_eq!(clippath_element.name.as_ref(), "clipPath");
            assert_eq!(clippath_element.children.len(), 1);

            if let Node::Element(child) = &clippath_element.children[0] {
                assert_eq!(child.name.as_ref(), "circle");
                assert!(child.attributes.contains_key("id"));
            }
        }
    }
}