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

//! Remove empty text plugin implementation
//!
//! This plugin removes empty text elements from SVG documents.
//! It handles:
//! - Empty `<text>` elements
//! - Empty `<tspan>` elements  
//! - `<tref>` elements with empty xlink:href attributes
//!
//! Reference: https://www.w3.org/TR/SVG11/text.html

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

/// Configuration parameters for remove empty text plugin
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct RemoveEmptyTextConfig {
    /// Remove empty text elements (default: true)
    #[serde(default = "default_true")]
    pub text: bool,
    /// Remove empty tspan elements (default: true)
    #[serde(default = "default_true")]
    pub tspan: bool,
    /// Remove tref elements with empty xlink:href (default: true)
    #[serde(default = "default_true")]
    pub tref: bool,
}

fn default_true() -> bool {
    true
}

impl Default for RemoveEmptyTextConfig {
    fn default() -> Self {
        Self {
            text: true,
            tspan: true,
            tref: true,
        }
    }
}

/// Plugin that removes empty text elements
pub struct RemoveEmptyTextPlugin {
    config: RemoveEmptyTextConfig,
}

impl RemoveEmptyTextPlugin {
    /// Create a new RemoveEmptyTextPlugin
    pub fn new() -> Self {
        Self {
            config: RemoveEmptyTextConfig::default(),
        }
    }

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

    /// Parse configuration from JSON
    fn parse_config(params: &Value) -> Result<RemoveEmptyTextConfig> {
        if params.is_null() || (params.is_object() && params.as_object().unwrap().is_empty()) {
            Ok(RemoveEmptyTextConfig::default())
        } else if params.is_object() {
            serde_json::from_value(params.clone())
                .map_err(|e| anyhow::anyhow!("Invalid configuration: {}", e))
        } else {
            Ok(RemoveEmptyTextConfig::default())
        }
    }

    /// Recursively remove empty text elements from an element and its children
    fn remove_empty_text_recursive(&self, element: &mut Element) {
        // Remove empty text elements from children
        element.children.retain(|child| {
            if let Node::Element(elem) = child {
                // Remove empty text element
                if self.config.text && elem.name == "text" && elem.children.is_empty() {
                    return false;
                }

                // Remove empty tspan element
                if self.config.tspan && elem.name == "tspan" && elem.children.is_empty() {
                    return false;
                }

                // Remove tref with empty xlink:href attribute
                if self.config.tref && elem.name == "tref" {
                    if let Some(href) = elem.attributes.get("xlink:href") {
                        if href.is_empty() {
                            return false;
                        }
                    } else {
                        // No xlink:href attribute at all
                        return false;
                    }
                }
            }
            true // Keep all other nodes
        });

        // Process child elements recursively
        for child in &mut element.children {
            if let Node::Element(elem) = child {
                self.remove_empty_text_recursive(elem);
            }
        }
    }
}

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

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

    fn description(&self) -> &'static str {
        "removes empty <text> elements"
    }

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

    fn apply(&self, document: &mut Document) -> Result<()> {
        // Remove empty text elements from the document
        self.remove_empty_text_recursive(&mut document.root);
        Ok(())
    }
}

#[cfg(test)]
mod unit_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 count_elements_by_name(element: &Element, name: &str) -> usize {
        let mut count = 0;
        for child in &element.children {
            if let Node::Element(elem) = child {
                if elem.name == name {
                    count += 1;
                }
                count += count_elements_by_name(elem, name);
            }
        }
        count
    }

    #[test]
    fn test_plugin_creation() {
        let plugin = RemoveEmptyTextPlugin::new();
        assert_eq!(plugin.name(), "removeEmptyText");
        assert_eq!(plugin.description(), "removes empty <text> elements");
    }

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

        // Valid parameters
        assert!(plugin.validate_params(&json!({})).is_ok());
        assert!(plugin.validate_params(&json!({"text": true})).is_ok());
        assert!(plugin
            .validate_params(&json!({"text": false, "tspan": true}))
            .is_ok());

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

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

        // Add empty text element
        let empty_text = create_element("text");
        doc.root.children.push(Node::Element(empty_text));

        // Add text element with content
        let mut text_with_content = create_element("text");
        text_with_content
            .children
            .push(Node::Text("Hello".to_string().into()));
        doc.root.children.push(Node::Element(text_with_content));

        // Add a regular element
        let rect = create_element("rect");
        doc.root.children.push(Node::Element(rect));

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

        // Should have removed the empty text element
        assert_eq!(count_elements_by_name(&doc.root, "text"), 1);
        assert_eq!(doc.root.children.len(), 2);
    }

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

        // Add empty tspan element
        let empty_tspan = create_element("tspan");
        doc.root.children.push(Node::Element(empty_tspan));

        // Add tspan element with content
        let mut tspan_with_content = create_element("tspan");
        tspan_with_content
            .children
            .push(Node::Text("World".to_string().into()));
        doc.root.children.push(Node::Element(tspan_with_content));

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

        // Should have removed the empty tspan element
        assert_eq!(count_elements_by_name(&doc.root, "tspan"), 1);
        assert_eq!(doc.root.children.len(), 1);
    }

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

        // Add tref with empty xlink:href
        let mut tref_empty = create_element("tref");
        tref_empty.set_attr("xlink:href", "");
        doc.root.children.push(Node::Element(tref_empty));

        // Add tref with no xlink:href
        let tref_no_href = create_element("tref");
        doc.root.children.push(Node::Element(tref_no_href));

        // Add tref with valid xlink:href
        let mut tref_valid = create_element("tref");
        tref_valid.set_attr("xlink:href", "#validref");
        doc.root.children.push(Node::Element(tref_valid));

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

        // Should have removed the tref elements with empty/missing href
        assert_eq!(count_elements_by_name(&doc.root, "tref"), 1);
        assert_eq!(doc.root.children.len(), 1);
    }

    #[test]
    fn test_config_text_disabled() {
        let config = RemoveEmptyTextConfig {
            text: false,
            tspan: true,
            tref: true,
        };
        let plugin = RemoveEmptyTextPlugin::with_config(config);
        let mut doc = Document::new();

        // Add empty text element
        let empty_text = create_element("text");
        doc.root.children.push(Node::Element(empty_text));

        // Add empty tspan element
        let empty_tspan = create_element("tspan");
        doc.root.children.push(Node::Element(empty_tspan));

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

        // Should have kept the text element but removed tspan
        assert_eq!(count_elements_by_name(&doc.root, "text"), 1);
        assert_eq!(count_elements_by_name(&doc.root, "tspan"), 0);
    }

    #[test]
    fn test_config_tspan_disabled() {
        let config = RemoveEmptyTextConfig {
            text: true,
            tspan: false,
            tref: true,
        };
        let plugin = RemoveEmptyTextPlugin::with_config(config);
        let mut doc = Document::new();

        // Add empty text element
        let empty_text = create_element("text");
        doc.root.children.push(Node::Element(empty_text));

        // Add empty tspan element
        let empty_tspan = create_element("tspan");
        doc.root.children.push(Node::Element(empty_tspan));

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

        // Should have removed the text element but kept tspan
        assert_eq!(count_elements_by_name(&doc.root, "text"), 0);
        assert_eq!(count_elements_by_name(&doc.root, "tspan"), 1);
    }

    #[test]
    fn test_config_tref_disabled() {
        let config = RemoveEmptyTextConfig {
            text: true,
            tspan: true,
            tref: false,
        };
        let plugin = RemoveEmptyTextPlugin::with_config(config);
        let mut doc = Document::new();

        // Add tref with empty xlink:href
        let mut tref_empty = create_element("tref");
        tref_empty.set_attr("xlink:href", "");
        doc.root.children.push(Node::Element(tref_empty));

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

        // Should have kept the tref element
        assert_eq!(count_elements_by_name(&doc.root, "tref"), 1);
    }

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

        // Create a group with nested empty text elements
        let mut group = create_element("g");
        let empty_text = create_element("text");
        group.children.push(Node::Element(empty_text));

        // Add regular element
        let rect = create_element("rect");
        group.children.push(Node::Element(rect));

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

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

        // Should have removed the nested empty text element
        assert_eq!(count_elements_by_name(&doc.root, "text"), 0);
        assert_eq!(doc.root.children.len(), 1);

        // The group should still exist with only the rect
        if let Node::Element(group_elem) = &doc.root.children[0] {
            assert_eq!(group_elem.name, "g");
            assert_eq!(group_elem.children.len(), 1);
        }
    }

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

        // Add only non-empty text elements
        let mut text_with_content = create_element("text");
        text_with_content
            .children
            .push(Node::Text("Hello".to_string().into()));
        doc.root.children.push(Node::Element(text_with_content));

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

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

        // Should have no changes
        assert_eq!(count_elements_by_name(&doc.root, "text"), 1);
        assert_eq!(doc.root.children.len(), 2);
    }

    #[test]
    fn test_config_parsing() {
        let config = RemoveEmptyTextPlugin::parse_config(&json!({
            "text": false,
            "tspan": true,
            "tref": false
        }))
        .unwrap();

        assert!(!config.text);
        assert!(config.tspan);
        assert!(!config.tref);
    }
}

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