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

//! Removes elements by ID or class attribute
//!
//! This plugin removes arbitrary elements that match specified ID or class attributes.
//! Elements can be removed based on their id attribute or class attribute values.
//!
//! Reference: SVGO's removeElementsByAttr plugin

use std::collections::HashSet;

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

use crate::Plugin;

/// Configuration for the removeElementsByAttr plugin
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[derive(Default)]
pub struct RemoveElementsByAttrConfig {
    /// IDs of elements to remove
    #[serde(default)]
    pub id: Vec<String>,
    /// Class names of elements to remove
    #[serde(default)]
    pub class: Vec<String>,
}

/// Removes elements by ID or class attribute
pub struct RemoveElementsByAttrPlugin {
    config: RemoveElementsByAttrConfig,
}

impl RemoveElementsByAttrPlugin {
    pub fn new() -> Self {
        Self {
            config: RemoveElementsByAttrConfig::default(),
        }
    }

    pub fn with_config(config: RemoveElementsByAttrConfig) -> Self {
        Self { config }
    }

    fn parse_config(params: &Value) -> Result<RemoveElementsByAttrConfig> {
        if params.is_null() {
            Ok(RemoveElementsByAttrConfig::default())
        } else if let Value::Object(obj) = params {
            let mut config = RemoveElementsByAttrConfig::default();

            // Parse IDs
            if let Some(id_value) = obj.get("id") {
                match id_value {
                    Value::String(id) => config.id.push(id.clone()),
                    Value::Array(ids) => {
                        for id in ids {
                            if let Value::String(id_str) = id {
                                config.id.push(id_str.clone());
                            }
                        }
                    }
                    _ => {}
                }
            }

            // Parse classes
            if let Some(class_value) = obj.get("class") {
                match class_value {
                    Value::String(class) => config.class.push(class.clone()),
                    Value::Array(classes) => {
                        for class in classes {
                            if let Value::String(class_str) = class {
                                config.class.push(class_str.clone());
                            }
                        }
                    }
                    _ => {}
                }
            }

            Ok(config)
        } else {
            serde_json::from_value(params.clone())
                .map_err(|e| anyhow::anyhow!("Invalid plugin configuration: {}", e))
        }
    }

    fn should_remove_element(&self, element: &Element) -> bool {
        // Check if element ID matches any configured IDs
        if !self.config.id.is_empty() {
            if let Some(id) = element.attr("id") {
                if self.config.id.contains(&id.to_string()) {
                    return true;
                }
            }
        }

        // Check if element class contains any of the configured classes
        if !self.config.class.is_empty() {
            if let Some(class_attr) = element.attr("class") {
                let class_list: HashSet<&str> = class_attr.split_whitespace().collect();
                for config_class in &self.config.class {
                    if class_list.contains(config_class.as_str()) {
                        return true;
                    }
                }
            }
        }

        false
    }

    fn process_element(&self, element: &mut Element) {
        // Process children, removing elements that match the criteria
        let mut i = 0;
        while i < element.children.len() {
            let should_remove = match &element.children[i] {
                Node::Element(child_elem) => self.should_remove_element(child_elem),
                _ => false,
            };

            if should_remove {
                element.children.remove(i);
            } else {
                // Recursively process child elements
                if let Node::Element(child_elem) = &mut element.children[i] {
                    self.process_element(child_elem);
                }
                i += 1;
            }
        }
    }
}

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

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

    fn description(&self) -> &'static str {
        "removes arbitrary elements by ID or className (disabled by default)"
    }

    fn validate_params(&self, params: &Value) -> Result<()> {
        Self::parse_config(params)?;
        Ok(())
    }

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

    fn apply(&self, document: &mut Document) -> Result<()> {
        // Only proceed if we have something to remove
        if self.config.id.is_empty() && self.config.class.is_empty() {
            return Ok(());
        }

        self.process_element(&mut document.root);
        Ok(())
    }
}

#[cfg(test)]
mod tests {

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

    use super::*;

    fn create_test_document() -> Document<'static> {
        Document {
            root: Element {
                name: "svg".into(),
                attributes: IndexMap::new(),
                namespaces: IndexMap::new(),
                children: vec![],
            },
            prologue: vec![],
            epilogue: vec![],
            metadata: DocumentMetadata {
                path: None,
                encoding: None,
                version: None,
                ..Default::default()
            },
            memory_budget: None,
        }
    }

    #[test]
    fn test_plugin_info() {
        let plugin = RemoveElementsByAttrPlugin::new();
        assert_eq!(plugin.name(), "removeElementsByAttr");
        assert_eq!(
            plugin.description(),
            "removes arbitrary elements by ID or className (disabled by default)"
        );
    }

    #[test]
    fn test_param_validation() {
        let plugin = RemoveElementsByAttrPlugin::new();

        // Test null params
        assert!(plugin.validate_params(&Value::Null).is_ok());

        // Test valid params
        assert!(plugin
            .validate_params(&json!({
                "id": "test"
            }))
            .is_ok());

        assert!(plugin
            .validate_params(&json!({
                "class": "test-class"
            }))
            .is_ok());

        assert!(plugin
            .validate_params(&json!({
                "id": ["id1", "id2"],
                "class": ["class1", "class2"]
            }))
            .is_ok());
    }

    #[test]
    fn test_parse_config_single_id() {
        let config_json = json!({
            "id": "elementToRemove"
        });

        let config = RemoveElementsByAttrPlugin::parse_config(&config_json).unwrap();
        assert_eq!(config.id, vec!["elementToRemove"]);
        assert!(config.class.is_empty());
    }

    #[test]
    fn test_parse_config_multiple_ids() {
        let config_json = json!({
            "id": ["elementToRemove1", "elementToRemove2"]
        });

        let config = RemoveElementsByAttrPlugin::parse_config(&config_json).unwrap();
        assert_eq!(config.id, vec!["elementToRemove1", "elementToRemove2"]);
        assert!(config.class.is_empty());
    }

    #[test]
    fn test_parse_config_single_class() {
        let config_json = json!({
            "class": "classToRemove"
        });

        let config = RemoveElementsByAttrPlugin::parse_config(&config_json).unwrap();
        assert!(config.id.is_empty());
        assert_eq!(config.class, vec!["classToRemove"]);
    }

    #[test]
    fn test_parse_config_multiple_classes() {
        let config_json = json!({
            "class": ["classToRemove1", "classToRemove2"]
        });

        let config = RemoveElementsByAttrPlugin::parse_config(&config_json).unwrap();
        assert!(config.id.is_empty());
        assert_eq!(config.class, vec!["classToRemove1", "classToRemove2"]);
    }

    #[test]
    fn test_parse_config_mixed() {
        let config_json = json!({
            "id": "elementToRemove",
            "class": ["classToRemove1", "classToRemove2"]
        });

        let config = RemoveElementsByAttrPlugin::parse_config(&config_json).unwrap();
        assert_eq!(config.id, vec!["elementToRemove"]);
        assert_eq!(config.class, vec!["classToRemove1", "classToRemove2"]);
    }

    #[test]
    fn test_should_remove_element_by_id() {
        let config = RemoveElementsByAttrConfig {
            id: vec!["removeMe".to_string()],
            class: vec![],
        };
        let plugin = RemoveElementsByAttrPlugin::with_config(config);

        let mut element = Element {
            name: "rect".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element.set_attr("id", "removeMe");

        assert!(plugin.should_remove_element(&element));

        // Test element that shouldn't be removed
        let mut element2 = Element {
            name: "rect".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element2.set_attr("id", "keepMe");

        assert!(!plugin.should_remove_element(&element2));
    }

    #[test]
    fn test_should_remove_element_by_class() {
        let config = RemoveElementsByAttrConfig {
            id: vec![],
            class: vec!["removeMe".to_string()],
        };
        let plugin = RemoveElementsByAttrPlugin::with_config(config);

        // Test element with matching class
        let mut element = Element {
            name: "rect".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element.set_attr("class", "someClass removeMe anotherClass");

        assert!(plugin.should_remove_element(&element));

        // Test element that shouldn't be removed
        let mut element2 = Element {
            name: "rect".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element2.set_attr("class", "someClass keepMe anotherClass");

        assert!(!plugin.should_remove_element(&element2));
    }

    #[test]
    fn test_apply_removes_by_id() {
        let config = RemoveElementsByAttrConfig {
            id: vec!["elementToRemove".to_string()],
            class: vec![],
        };
        let plugin = RemoveElementsByAttrPlugin::with_config(config);
        let mut doc = create_test_document();

        // Add element to remove
        let mut element_to_remove = Element {
            name: "rect".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element_to_remove.set_attr("id", "elementToRemove");
        doc.root.children.push(Node::Element(element_to_remove));

        // Add element to keep
        let mut element_to_keep = Element {
            name: "circle".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element_to_keep.set_attr("id", "elementToKeep");
        doc.root.children.push(Node::Element(element_to_keep));

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

        // Should have only one element remaining
        assert_eq!(doc.root.children.len(), 1);
        if let Node::Element(element) = &doc.root.children[0] {
            assert_eq!(element.name, "circle");
            assert_eq!(element.attr("id"), Some("elementToKeep"));
        } else {
            panic!("Expected element");
        }
    }

    #[test]
    fn test_apply_removes_by_class() {
        let config = RemoveElementsByAttrConfig {
            id: vec![],
            class: vec!["removeMe".to_string()],
        };
        let plugin = RemoveElementsByAttrPlugin::with_config(config);
        let mut doc = create_test_document();

        // Add element to remove
        let mut element_to_remove = Element {
            name: "rect".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element_to_remove.set_attr("class", "some-class removeMe another-class");
        doc.root.children.push(Node::Element(element_to_remove));

        // Add element to keep
        let mut element_to_keep = Element {
            name: "circle".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element_to_keep.set_attr("class", "some-class keep-me another-class");
        doc.root.children.push(Node::Element(element_to_keep));

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

        // Should have only one element remaining
        assert_eq!(doc.root.children.len(), 1);
        if let Node::Element(element) = &doc.root.children[0] {
            assert_eq!(element.name, "circle");
            assert_eq!(
                element.attr("class"),
                Some("some-class keep-me another-class")
            );
        } else {
            panic!("Expected element");
        }
    }

    #[test]
    fn test_apply_no_config_does_nothing() {
        let plugin = RemoveElementsByAttrPlugin::new();
        let mut doc = create_test_document();

        // Add some elements
        let mut element = Element {
            name: "rect".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element.set_attr("id", "someId");
        doc.root.children.push(Node::Element(element));

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

        // Should still have the element
        assert_eq!(doc.root.children.len(), 1);
    }

    #[test]
    fn test_apply_recursive() {
        let config = RemoveElementsByAttrConfig {
            id: vec!["removeMe".to_string()],
            class: vec![],
        };
        let plugin = RemoveElementsByAttrPlugin::with_config(config);
        let mut doc = create_test_document();

        // Create nested structure
        let mut nested_element = Element {
            name: "rect".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        nested_element.set_attr("id", "removeMe");

        let mut group = Element {
            name: "g".into(),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![Node::Element(nested_element)],
        };
        group.set_attr("id", "group");

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

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

        // Group should remain but nested element should be removed
        assert_eq!(doc.root.children.len(), 1);
        if let Node::Element(group) = &doc.root.children[0] {
            assert_eq!(group.name, "g");
            assert_eq!(group.children.len(), 0); // Nested element removed
        } else {
            panic!("Expected group element");
        }
    }
}