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
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// this_file: crates/vexy-vsvg-plugin-sdk/src/plugins/remove_attrs.rs

//! Remove specified attributes based on patterns
//!
//! This plugin removes attributes from elements based on flexible pattern matching.
//! Patterns can specify element names, attribute names, and attribute values using regex.
//!
//! Reference: SVGO's removeAttrs plugin

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

/// Configuration for the removeAttrs plugin
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct RemoveAttrsConfig {
    /// Attribute patterns to remove (element:attribute:value format)
    pub attrs: Vec<String>,

    /// Element separator for patterns (default ":")
    #[serde(default = "default_elem_separator")]
    pub elem_separator: String,

    /// Whether to preserve currentColor values in fill/stroke attributes
    #[serde(default)]
    pub preserve_current_color: bool,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct RemoveAttrsConfigInput {
    attrs: AttrPatterns,
    #[serde(default)]
    elem_separator: Option<String>,
    #[serde(default)]
    preserve_current_color: Option<bool>,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum AttrPatterns {
    One(String),
    Many(Vec<String>),
}

impl AttrPatterns {
    fn into_vec(self) -> Vec<String> {
        match self {
            Self::One(value) => vec![value],
            Self::Many(values) => values,
        }
    }
}

fn default_elem_separator() -> String {
    ":".to_string()
}

impl Default for RemoveAttrsConfig {
    fn default() -> Self {
        Self {
            attrs: Vec::new(),
            elem_separator: default_elem_separator(),
            preserve_current_color: false,
        }
    }
}

/// Compiled pattern for attribute removal
#[derive(Debug)]
struct CompiledPattern {
    element_regex: Regex,
    attribute_regex: Regex,
    value_regex: Regex,
}

impl CompiledPattern {
    /// Compile a pattern string into regex components
    fn compile(pattern: &str, separator: &str) -> Result<Self> {
        let mut parts: Vec<String> = pattern.split(separator).map(|s| s.to_string()).collect();

        // Expand pattern based on number of parts
        match parts.len() {
            1 => {
                // Just attribute name - apply to all elements with any value
                parts.insert(0, ".*".to_string());
                parts.push(".*".to_string());
            }
            2 => {
                // Element and attribute - apply with any value
                parts.push(".*".to_string());
            }
            3 => {
                // Full pattern - use as is
            }
            _ => {
                return Err(anyhow::anyhow!(
                    "Invalid pattern format: {}. Pattern should have at most 3 parts separated by '{}'",
                    pattern, separator
                ));
            }
        }

        // Convert single * to .*
        for part in &mut parts {
            if part == "*" {
                *part = ".*".to_string();
            }
        }

        // Compile regexes
        let element_regex = Regex::new(&format!("^{}$", parts[0]))
            .map_err(|e| anyhow::anyhow!("Invalid element regex '{}': {}", parts[0], e))?;
        let attribute_regex = Regex::new(&format!("^{}$", parts[1]))
            .map_err(|e| anyhow::anyhow!("Invalid attribute regex '{}': {}", parts[1], e))?;
        let value_regex = Regex::new(&format!("^{}$", parts[2]))
            .map_err(|e| anyhow::anyhow!("Invalid value regex '{}': {}", parts[2], e))?;

        Ok(Self {
            element_regex,
            attribute_regex,
            value_regex,
        })
    }

    /// Check if this pattern matches the given element, attribute, and value
    fn matches(&self, element_name: &str, attr_name: &str, attr_value: &str) -> bool {
        self.element_regex.is_match(element_name)
            && self.attribute_regex.is_match(attr_name)
            && self.value_regex.is_match(attr_value)
    }
}

/// Main plugin struct for removing attributes
pub struct RemoveAttrsPlugin {
    config: RemoveAttrsConfig,
}

impl RemoveAttrsPlugin {
    pub fn new() -> Self {
        Self {
            config: RemoveAttrsConfig::default(),
        }
    }

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

    fn parse_config(params: &Value) -> Result<RemoveAttrsConfig> {
        if params.is_null() {
            return Err(anyhow::anyhow!(
                "removeAttrs plugin requires 'attrs' parameter"
            ));
        }

        // Handle both object format and simple string/array format
        let config = if params.is_object() {
            let parsed: RemoveAttrsConfigInput = serde_json::from_value(params.clone())
                .map_err(|e| anyhow::anyhow!("Invalid plugin configuration: {}", e))?;
            RemoveAttrsConfig {
                attrs: parsed.attrs.into_vec(),
                elem_separator: parsed.elem_separator.unwrap_or_else(default_elem_separator),
                preserve_current_color: parsed.preserve_current_color.unwrap_or(false),
            }
        } else if let Some(attrs_str) = params.as_str() {
            RemoveAttrsConfig {
                attrs: vec![attrs_str.to_string()],
                ..Default::default()
            }
        } else if let Some(attrs_array) = params.as_array() {
            let mut attrs = Vec::new();
            for attr in attrs_array {
                if let Some(s) = attr.as_str() {
                    attrs.push(s.to_string());
                } else {
                    return Err(anyhow::anyhow!("attrs array must contain only strings"));
                }
            }
            RemoveAttrsConfig {
                attrs,
                ..Default::default()
            }
        } else {
            return Err(anyhow::anyhow!(
                "removeAttrs plugin parameters must be an object, string, or array of strings"
            ));
        };

        // Validate that attrs is not empty
        if config.attrs.is_empty() {
            return Err(anyhow::anyhow!(
                "removeAttrs plugin requires non-empty 'attrs' parameter"
            ));
        }

        Ok(config)
    }

    fn process_element(&self, element: &mut Element, patterns: &[CompiledPattern]) {
        // Process children first
        let mut i = 0;
        while i < element.children.len() {
            if let Node::Element(child) = &mut element.children[i] {
                self.process_element(child, patterns);
            }
            i += 1;
        }

        // Remove matching attributes
        let mut attrs_to_remove = Vec::new();

        for (attr_name, attr_value) in &element.attributes {
            for pattern in patterns {
                if pattern.matches(&element.name, attr_name, attr_value) {
                    // Check for currentColor preservation
                    if self.config.preserve_current_color {
                        let is_current_color = attr_value.to_lowercase() == "currentcolor";
                        let is_fill_or_stroke = attr_name == "fill" || attr_name == "stroke";

                        if is_fill_or_stroke && is_current_color {
                            continue; // Skip removal
                        }
                    }

                    attrs_to_remove.push(attr_name.clone());
                    break; // No need to check other patterns for this attribute
                }
            }
        }

        // Remove the attributes
        for attr_name in attrs_to_remove {
            element.remove_attr(&attr_name);
        }
    }
}

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

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

    fn description(&self) -> &'static str {
        "removes specified attributes"
    }

    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<()> {
        // Parse the configuration fresh for this apply call
        let config = if self.config.attrs.is_empty() {
            return Err(anyhow::anyhow!(
                "removeAttrs plugin requires 'attrs' parameter"
            ));
        } else {
            self.config.clone()
        };

        // Compile all patterns
        let mut compiled_patterns = Vec::new();
        for pattern in &config.attrs {
            compiled_patterns.push(CompiledPattern::compile(pattern, &config.elem_separator)?);
        }

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

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_plugin_info() {
        let plugin = RemoveAttrsPlugin::new();
        assert_eq!(plugin.name(), "removeAttrs");
        assert_eq!(plugin.description(), "removes specified attributes");
    }

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

        // Test null params - should fail
        assert!(plugin.validate_params(&Value::Null).is_err());

        // Test empty object - should fail
        assert!(plugin.validate_params(&json!({})).is_err());

        // Test valid string param
        assert!(plugin.validate_params(&json!("fill")).is_ok());

        // Test valid array param
        assert!(plugin.validate_params(&json!(["fill", "stroke"])).is_ok());

        // Test valid object param
        assert!(plugin
            .validate_params(&json!({
                "attrs": ["fill"],
                "preserveCurrentColor": true
            }))
            .is_ok());

        // Test invalid param type in array
        assert!(plugin.validate_params(&json!(["fill", 123])).is_err());

        // Test unknown field
        assert!(plugin
            .validate_params(&json!({
                "attrs": ["fill"],
                "unknownField": true
            }))
            .is_err());
    }

    #[test]
    fn test_simple_attribute_removal() {
        let mut document = Document::new();
        document.root.name = "svg".into();

        let mut rect = Element::new("rect");
        rect.set_attr("fill", "red");
        rect.set_attr("stroke", "blue");
        rect.set_attr("width", "100");

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

        let config = RemoveAttrsConfig {
            attrs: vec!["fill".to_string()],
            ..Default::default()
        };
        let plugin = RemoveAttrsPlugin::with_config(config);
        plugin.apply(&mut document).unwrap();

        if let Node::Element(rect) = &document.root.children[0] {
            assert!(!rect.has_attr("fill"));
            assert!(rect.has_attr("stroke"));
            assert!(rect.has_attr("width"));
        }
    }

    #[test]
    fn test_multiple_attribute_removal() {
        let mut document = Document::new();
        document.root.name = "svg".into();

        let mut rect = Element::new("rect");
        rect.set_attr("fill", "red");
        rect.set_attr("stroke", "blue");
        rect.set_attr("width", "100");

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

        let config = RemoveAttrsConfig {
            attrs: vec!["fill".to_string(), "stroke".to_string()],
            ..Default::default()
        };
        let plugin = RemoveAttrsPlugin::with_config(config);
        plugin.apply(&mut document).unwrap();

        if let Node::Element(rect) = &document.root.children[0] {
            assert!(!rect.has_attr("fill"));
            assert!(!rect.has_attr("stroke"));
            assert!(rect.has_attr("width"));
        }
    }

    #[test]
    fn test_regex_pattern_removal() {
        let mut document = Document::new();
        document.root.name = "svg".into();

        let mut rect = Element::new("rect");
        rect.set_attr("fill", "red");
        rect.set_attr("stroke", "blue");
        rect.set_attr("stroke-width", "2");
        rect.set_attr("stroke-opacity", "0.5");

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

        let config = RemoveAttrsConfig {
            attrs: vec!["stroke.*".to_string()],
            ..Default::default()
        };
        let plugin = RemoveAttrsPlugin::with_config(config);
        plugin.apply(&mut document).unwrap();

        if let Node::Element(rect) = &document.root.children[0] {
            assert!(rect.has_attr("fill"));
            assert!(!rect.has_attr("stroke"));
            assert!(!rect.has_attr("stroke-width"));
            assert!(!rect.has_attr("stroke-opacity"));
        }
    }

    #[test]
    fn test_element_specific_removal() {
        let mut document = Document::new();
        document.root.name = "svg".into();

        let mut circle = Element::new("circle");
        circle.set_attr("fill", "red");

        let mut rect = Element::new("rect");
        rect.set_attr("fill", "blue");

        document.root.children.push(Node::Element(circle));
        document.root.children.push(Node::Element(rect));

        let config = RemoveAttrsConfig {
            attrs: vec!["circle:fill".to_string()],
            ..Default::default()
        };
        let plugin = RemoveAttrsPlugin::with_config(config);
        plugin.apply(&mut document).unwrap();

        // Circle should have fill removed, rect should keep it
        if let Node::Element(circle_elem) = &document.root.children[0] {
            assert!(!circle_elem.has_attr("fill"));
        }
        if let Node::Element(rect_elem) = &document.root.children[1] {
            assert!(rect_elem.has_attr("fill"));
        }
    }

    #[test]
    fn test_value_specific_removal() {
        let mut document = Document::new();
        document.root.name = "svg".into();

        let mut rect = Element::new("rect");
        rect.set_attr("fill", "red");
        rect.set_attr("stroke", "blue");

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

        let config = RemoveAttrsConfig {
            attrs: vec!["*:fill:red".to_string()],
            ..Default::default()
        };
        let plugin = RemoveAttrsPlugin::with_config(config);
        plugin.apply(&mut document).unwrap();

        if let Node::Element(rect) = &document.root.children[0] {
            assert!(!rect.has_attr("fill"));
            assert!(rect.has_attr("stroke"));
        }
    }

    #[test]
    fn test_preserve_current_color() {
        let mut document = Document::new();
        document.root.name = "svg".into();

        let mut rect = Element::new("rect");
        rect.set_attr("fill", "currentColor");
        rect.set_attr("stroke", "red");

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

        let config = RemoveAttrsConfig {
            attrs: vec!["(fill|stroke)".to_string()],
            preserve_current_color: true,
            ..Default::default()
        };
        let plugin = RemoveAttrsPlugin::with_config(config);
        plugin.apply(&mut document).unwrap();

        if let Node::Element(rect) = &document.root.children[0] {
            assert!(rect.has_attr("fill")); // currentColor preserved
            assert!(!rect.has_attr("stroke")); // red removed
        }
    }

    #[test]
    fn test_preserve_current_color_case_insensitive() {
        let mut document = Document::new();
        document.root.name = "svg".into();

        let mut rect = Element::new("rect");
        rect.set_attr("fill", "currentcolor");
        rect.set_attr("stroke", "CURRENTCOLOR");

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

        let config = RemoveAttrsConfig {
            attrs: vec!["(fill|stroke)".to_string()],
            preserve_current_color: true,
            ..Default::default()
        };
        let plugin = RemoveAttrsPlugin::with_config(config);
        plugin.apply(&mut document).unwrap();

        if let Node::Element(rect) = &document.root.children[0] {
            assert!(rect.has_attr("fill")); // currentcolor preserved
            assert!(rect.has_attr("stroke")); // CURRENTCOLOR preserved
        }
    }

    #[test]
    fn test_custom_separator() {
        let mut document = Document::new();
        document.root.name = "svg".into();

        let mut rect = Element::new("rect");
        rect.set_attr("fill", "red");
        rect.set_attr("stroke", "blue");

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

        let config = RemoveAttrsConfig {
            attrs: vec!["rect|fill".to_string()],
            elem_separator: "|".to_string(),
            ..Default::default()
        };
        let plugin = RemoveAttrsPlugin::with_config(config);
        plugin.apply(&mut document).unwrap();

        if let Node::Element(rect) = &document.root.children[0] {
            assert!(!rect.has_attr("fill"));
            assert!(rect.has_attr("stroke"));
        }
    }

    #[test]
    fn test_nested_elements() {
        let mut document = Document::new();
        document.root.name = "svg".into();

        let mut group = Element::new("g");
        group.set_attr("fill", "red");

        let mut rect = Element::new("rect");
        rect.set_attr("fill", "blue");
        rect.set_attr("stroke", "green");

        group.children.push(Node::Element(rect));
        document.root.children.push(Node::Element(group));

        let config = RemoveAttrsConfig {
            attrs: vec!["fill".to_string()],
            ..Default::default()
        };
        let plugin = RemoveAttrsPlugin::with_config(config);
        plugin.apply(&mut document).unwrap();

        if let Node::Element(group_elem) = &document.root.children[0] {
            assert!(!group_elem.has_attr("fill"));

            if let Node::Element(rect_elem) = &group_elem.children[0] {
                assert!(!rect_elem.has_attr("fill"));
                assert!(rect_elem.has_attr("stroke"));
            }
        }
    }
}