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

//! Remove editors namespace data plugin implementation
//!
//! This plugin removes namespace declarations, attributes, and elements from various
//! SVG editors like Inkscape, Illustrator, Sketch, etc. These editor-specific data
//! are not needed for SVG rendering and can significantly reduce file size.
//!
//! SVGO parameters supported:
//! - `additionalNamespaces` (default: []) - Additional namespace URIs to remove

use once_cell::sync::Lazy;
use std::collections::HashSet;

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use vexy_vsvg::ast::{Document, Element, Node};
use vexy_vsvg::error::VexyError;
use vexy_vsvg::visitor::Visitor;

use crate::Plugin;

/// Default editor namespaces to remove
static EDITOR_NAMESPACES: Lazy<HashSet<&'static str>> = Lazy::new(|| {
    HashSet::from([
        "http://creativecommons.org/ns#",
        "http://inkscape.sourceforge.net/DTD/sodipodi-0.dtd",
        "http://krita.org/namespaces/svg/krita",
        "http://ns.adobe.com/AdobeIllustrator/10.0/",
        "http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/",
        "http://ns.adobe.com/Extensibility/1.0/",
        "http://ns.adobe.com/Flows/1.0/",
        "http://ns.adobe.com/GenericCustomNamespace/1.0/",
        "http://ns.adobe.com/Graphs/1.0/",
        "http://ns.adobe.com/ImageReplacement/1.0/",
        "http://ns.adobe.com/SaveForWeb/1.0/",
        "http://ns.adobe.com/Variables/1.0/",
        "http://ns.adobe.com/XPath/1.0/",
        "http://purl.org/dc/elements/1.1/",
        "http://schemas.microsoft.com/visio/2003/SVGExtensions/",
        "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
        "http://taptrix.com/vectorillustrator/svg_extensions",
        "http://www.bohemiancoding.com/sketch/ns",
        "http://www.figma.com/figma/ns",
        "http://www.inkscape.org/namespaces/inkscape",
        "http://www.serif.com/",
        "http://www.vector.evaxdesign.sk",
        "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "https://boxy-svg.com",
    ])
});

/// Configuration parameters for remove editors ns data plugin
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Default)]
pub struct RemoveEditorsNSDataConfig {
    /// Additional namespace URIs to remove
    #[serde(default)]
    pub additional_namespaces: Vec<String>,
}

/// Plugin that removes editor namespace data
pub struct RemoveEditorsNSDataPlugin {
    #[allow(dead_code)]
    config: RemoveEditorsNSDataConfig,
}

impl RemoveEditorsNSDataPlugin {
    /// Create a new RemoveEditorsNSDataPlugin
    pub fn new() -> Self {
        Self {
            #[allow(dead_code)]
            config: RemoveEditorsNSDataConfig::default(),
        }
    }

    /// Create a new RemoveEditorsNSDataPlugin with config
    pub fn with_config(config: RemoveEditorsNSDataConfig) -> Self {
        Self { config }
    }

    /// Parse configuration from JSON
    fn _parse_config(params: &Value) -> Result<RemoveEditorsNSDataConfig> {
        if let Some(_obj) = params.as_object() {
            serde_json::from_value(params.clone())
                .map_err(|e| anyhow!("Invalid configuration: {}", e))
        } else {
            Ok(RemoveEditorsNSDataConfig::default())
        }
    }
}

impl Default for RemoveEditorsNSDataPlugin {
    fn default() -> Self {
        Self::new()
    }
}

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

    fn description(&self) -> &'static str {
        "Remove editors namespaces, elements and attributes"
    }

    fn validate_params(&self, params: &Value) -> Result<()> {
        if let Some(obj) = params.as_object() {
            // Validate parameters
            for (key, value) in obj {
                match key.as_str() {
                    "additionalNamespaces" => {
                        if !value.is_array() {
                            return Err(anyhow!("{} must be an array", key));
                        }
                        if let Some(arr) = value.as_array() {
                            for item in arr {
                                if !item.is_string() {
                                    return Err(anyhow!(
                                        "additionalNamespaces must contain only strings"
                                    ));
                                }
                            }
                        }
                    }
                    _ => return Err(anyhow!("Unknown parameter: {}", key)),
                }
            }
        }
        Ok(())
    }

    fn configure(&mut self, params: &Value) -> Result<()> {
        self.config = Self::_parse_config(params)?;
        Ok(())
    }

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

/// State for tracking discovered namespace prefixes
#[derive(Debug)]
struct NamespaceState {
    /// Namespace URIs to remove
    namespaces_to_remove: HashSet<String>,
    /// Discovered prefixes that map to editor namespaces
    prefixes_to_remove: HashSet<String>,
}

impl NamespaceState {
    fn new(additional_namespaces: &[String]) -> Self {
        let mut namespaces_to_remove = HashSet::new();

        // Add default editor namespaces
        for ns in EDITOR_NAMESPACES.iter() {
            namespaces_to_remove.insert((*ns).to_string());
        }

        // Add additional namespaces
        for ns in additional_namespaces {
            namespaces_to_remove.insert(ns.clone());
        }

        Self {
            namespaces_to_remove,
            prefixes_to_remove: HashSet::new(),
        }
    }
}

/// Visitor implementation that removes editor namespace data
struct EditorsNSDataRemovalVisitor {
    #[allow(dead_code)]
    config: RemoveEditorsNSDataConfig,
    state: NamespaceState,
}

impl EditorsNSDataRemovalVisitor {
    fn new(config: RemoveEditorsNSDataConfig) -> Self {
        let state = NamespaceState::new(&config.additional_namespaces);
        Self { config, state }
    }

    /// Process namespace declarations on SVG element
    fn process_namespace_declarations(&mut self, element: &mut Element) {
        if element.name == "svg" {
            let mut attrs_to_remove = Vec::new();
            let mut namespaces_to_remove = Vec::new();

            // Find xmlns declarations that match editor namespaces
            for (name, value) in &element.attributes {
                if let Some(prefix) = name.strip_prefix("xmlns:") {
                    if self.state.namespaces_to_remove.contains(value.as_ref()) {
                        // Extract the prefix
                        self.state.prefixes_to_remove.insert(prefix.to_string());
                        attrs_to_remove.push(name.clone());
                    }
                } else if name == "xmlns"
                    && self.state.namespaces_to_remove.contains(value.as_ref())
                {
                    // Default namespace is an editor namespace
                    attrs_to_remove.push(name.clone());
                }
            }

            // Handle namespace declarations stored in Element::namespaces.
            for (prefix, uri) in &element.namespaces {
                if self.state.namespaces_to_remove.contains(uri.as_ref()) {
                    self.state.prefixes_to_remove.insert(prefix.to_string());
                    namespaces_to_remove.push(prefix.clone());
                }
            }

            // Remove the xmlns declarations
            for attr in attrs_to_remove {
                element.attributes.shift_remove(&attr);
            }
            for prefix in namespaces_to_remove {
                element.namespaces.shift_remove(&prefix);
            }
        }
    }

    /// Remove attributes with editor namespace prefixes
    fn remove_prefixed_attributes(&self, element: &mut Element) {
        let mut attrs_to_remove = Vec::new();

        for name in element.attributes.keys() {
            if let Some(colon_pos) = name.find(':') {
                let prefix = &name[..colon_pos];
                if self.state.prefixes_to_remove.contains(prefix) {
                    attrs_to_remove.push(name.clone());
                }
            }
        }

        // Remove the editor attributes
        for attr in attrs_to_remove {
            element.attributes.shift_remove(&attr);
        }
    }

    /// Check if an element should be removed based on its namespace prefix
    fn should_remove_element(&self, element: &Element) -> bool {
        if let Some(colon_pos) = element.name.find(':') {
            let prefix = &element.name[..colon_pos];
            return self.state.prefixes_to_remove.contains(prefix);
        }
        false
    }
}

impl Visitor<'_> for EditorsNSDataRemovalVisitor {
    fn visit_element_enter(&mut self, element: &mut Element<'_>) -> Result<(), VexyError> {
        // Process namespace declarations on SVG root
        self.process_namespace_declarations(element);

        // Remove prefixed attributes
        self.remove_prefixed_attributes(element);

        Ok(())
    }

    fn visit_element_exit(&mut self, element: &mut Element<'_>) -> Result<(), VexyError> {
        // Remove child elements with editor namespace prefixes
        element.children.retain(|child| {
            if let Node::Element(child_element) = child {
                !self.should_remove_element(child_element)
            } else {
                true // Keep non-element nodes
            }
        });

        Ok(())
    }
}

#[cfg(test)]
mod unit_tests {
    use std::borrow::Cow;

    use serde_json::json;
    use vexy_vsvg::ast::{Document, Element, Node};

    use super::*;

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

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

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

        // Valid parameters
        assert!(plugin.validate_params(&json!({})).is_ok());
        assert!(plugin
            .validate_params(&json!({
                "additionalNamespaces": ["http://example.com/ns"]
            }))
            .is_ok());

        // Invalid parameters
        assert!(plugin
            .validate_params(&json!({"additionalNamespaces": "invalid"}))
            .is_err());
        assert!(plugin
            .validate_params(&json!({"additionalNamespaces": [123]}))
            .is_err());
        assert!(plugin
            .validate_params(&json!({"unknownParam": true}))
            .is_err());
    }

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

        // Set up SVG with Inkscape namespace
        doc.root.set_attr(
            "xmlns:inkscape",
            "http://www.inkscape.org/namespaces/inkscape",
        );

        // Add element with Inkscape attributes
        let mut rect = create_element("rect");
        rect.set_attr("inkscape:label", "Layer 1");
        rect.set_attr("width", "100");

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

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

        // Namespace declaration should be removed
        assert!(!doc.root.attributes.contains_key("xmlns:inkscape"));

        // Inkscape attribute should be removed
        if let Some(Node::Element(rect)) = doc.root.children.first() {
            assert!(!rect.attributes.contains_key("inkscape:label"));
            assert_eq!(rect.attr("width"), Some("100"));
        }
    }

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

        // Set up SVG with Illustrator namespace
        doc.root
            .set_attr("xmlns:i", "http://ns.adobe.com/AdobeIllustrator/10.0/");

        // Add Illustrator-specific element
        let mut ai_element = create_element("i:pgf");
        ai_element.set_attr("id", "adobe_illustrator");

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

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

        // Namespace declaration should be removed
        assert!(!doc.root.attributes.contains_key("xmlns:i"));

        // Illustrator element should be removed
        assert!(doc.root.children.is_empty());
    }

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

        // Set up SVG with multiple editor namespaces
        doc.root.set_attr(
            "xmlns:inkscape",
            "http://www.inkscape.org/namespaces/inkscape",
        );
        doc.root.set_attr(
            "xmlns:sodipodi",
            "http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd",
        );
        doc.root
            .set_attr("xmlns:sketch", "http://www.bohemiancoding.com/sketch/ns");

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

        // All editor namespace declarations should be removed
        assert!(!doc.root.attributes.contains_key("xmlns:inkscape"));
        assert!(!doc.root.attributes.contains_key("xmlns:sodipodi"));
        assert!(!doc.root.attributes.contains_key("xmlns:sketch"));
    }

    #[test]
    fn test_additional_namespaces() {
        let config = RemoveEditorsNSDataConfig {
            additional_namespaces: vec!["http://custom.editor/ns".to_string()],
        };
        let plugin = RemoveEditorsNSDataPlugin::with_config(config);
        let mut doc = Document::new();

        // Set up SVG with custom namespace
        doc.root.set_attr("xmlns:custom", "http://custom.editor/ns");

        // Add element with custom namespace
        let mut elem = create_element("custom:data");
        elem.set_attr("value", "test");

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

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

        // Custom namespace should be removed
        assert!(!doc.root.attributes.contains_key("xmlns:custom"));

        // Custom element should be removed
        assert!(doc.root.children.is_empty());
    }

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

        // Set up SVG with standard namespaces
        doc.root.set_attr("xmlns", "http://www.w3.org/2000/svg");
        doc.root
            .set_attr("xmlns:xlink", "http://www.w3.org/1999/xlink");

        // Add Inkscape namespace to be removed
        doc.root.set_attr(
            "xmlns:inkscape",
            "http://www.inkscape.org/namespaces/inkscape",
        );

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

        // Standard namespaces should be preserved
        assert_eq!(doc.root.attr("xmlns"), Some("http://www.w3.org/2000/svg"));
        assert_eq!(
            doc.root.attr("xmlns:xlink"),
            Some("http://www.w3.org/1999/xlink")
        );

        // Editor namespace should be removed
        assert!(!doc.root.attributes.contains_key("xmlns:inkscape"));
    }

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

        // Set up SVG with Inkscape namespace
        doc.root.set_attr(
            "xmlns:inkscape",
            "http://www.inkscape.org/namespaces/inkscape",
        );

        // Create nested structure with mixed elements
        let mut g = create_element("g");

        let rect = create_element("rect");
        g.children.push(Node::Element(rect));

        let inkscape_elem = create_element("inkscape:perspective");
        g.children.push(Node::Element(inkscape_elem));

        let circle = create_element("circle");
        g.children.push(Node::Element(circle));

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

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

        // Check that only non-editor elements remain
        if let Some(Node::Element(g)) = doc.root.children.first() {
            assert_eq!(g.children.len(), 2);

            if let Some(Node::Element(elem1)) = g.children.first() {
                assert_eq!(elem1.name, "rect");
            }
            if let Some(Node::Element(elem2)) = g.children.get(1) {
                assert_eq!(elem2.name, "circle");
            }
        }
    }

    #[test]
    fn test_config_parsing() {
        let config = RemoveEditorsNSDataPlugin::_parse_config(&json!({
            "additionalNamespaces": ["http://example.com/ns1", "http://example.com/ns2"]
        }))
        .unwrap();

        assert_eq!(config.additional_namespaces.len(), 2);
        assert_eq!(config.additional_namespaces[0], "http://example.com/ns1");
        assert_eq!(config.additional_namespaces[1], "http://example.com/ns2");
    }
}

// Use parameterized testing framework for SVGO fixture tests
#[cfg(test)]
#[cfg(test)]
vexy_vsvg_test_utils::plugin_fixture_tests!(RemoveEditorsNSDataPlugin, "removeEditorsNSData");