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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// this_file: crates/vexy-vsvg-plugin-sdk/src/plugins/remove_xlink.rs

//! Plugin to remove xlink namespace and replace attributes with SVG 2 equivalents
//!
//! This plugin modernizes SVG by converting deprecated XLink attributes to their SVG 2
//! equivalents. XLink was deprecated in SVG 2 in favor of simpler attribute names.
//!
//! ## What It Converts
//!
//! - `xlink:href` → `href` (most common)
//! - `xlink:show="new"` → `target="_blank"`
//! - `xlink:show="replace"` → `target="_self"`
//! - `xlink:title` → `<title>` child element
//! - Removes unused xlink attributes
//! - Removes `xmlns:xlink` namespace declaration if no longer needed
//!
//! ## Legacy Elements
//!
//! By default, preserves XLink on legacy elements (`cursor`, `filter`, `font-face-uri`,
//! `glyphRef`, `tref`) that don't support SVG 2 `href`. Set `includeLegacy: true` to
//! convert these too (may break in older SVG renderers).
//!
//! ## Why Use This
//!
//! - **Modern SVG**: SVG 2 is the current standard
//! - **Simplicity**: `href` is simpler than `xlink:href`
//! - **Compatibility**: Modern browsers support both, but SVG 2 is preferred
//! - **File size**: Shorter attribute names + removed namespace
//!
//! ## Configuration
//!
//! ```json
//! {
//!   "includeLegacy": false  // Default: false. Preserve xlink on legacy elements.
//! }
//! ```
//!
//! ## Example
//!
//! Before:
//! ```xml
//! <svg xmlns:xlink="http://www.w3.org/1999/xlink">
//!   <use xlink:href="#icon"/>
//!   <a xlink:show="new" xlink:title="External Link">
//!     <text>Click</text>
//!   </a>
//! </svg>
//! ```
//!
//! After:
//! ```xml
//! <svg>
//!   <use href="#icon"/>
//!   <a target="_blank">
//!     <title>External Link</title>
//!     <text>Click</text>
//!   </a>
//! </svg>
//! ```
//!
//! ## SVGO Compatibility
//!
//! Ports SVGO's `removeXlink` plugin. Matches conversion rules and legacy handling.
//!
//! Reference: https://github.com/svg/svgo/blob/main/plugins/removeXlink.js

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

/// XLink namespace URI
const XLINK_NAMESPACE: &str = "http://www.w3.org/1999/xlink";

/// Elements that use xlink:href but were deprecated in SVG 2
const LEGACY_ELEMENTS: &[&str] = &["cursor", "filter", "font-face-uri", "glyphRef", "tref"];

/// Configuration for the removeXlink plugin
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[derive(Default)]
pub struct RemoveXlinkConfig {
    /// Include legacy elements that don't support SVG 2 href
    #[serde(default)]
    pub include_legacy: bool,
}

/// Plugin to remove xlink namespace and convert to SVG 2 equivalents
pub struct RemoveXlinkPlugin {
    config: RemoveXlinkConfig,
}

impl RemoveXlinkPlugin {
    pub fn new() -> Self {
        Self {
            config: RemoveXlinkConfig::default(),
        }
    }

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

    fn parse_config(params: &Value) -> Result<RemoveXlinkConfig> {
        if params.is_null() {
            Ok(RemoveXlinkConfig::default())
        } else {
            serde_json::from_value(params.clone())
                .map_err(|e| anyhow::anyhow!("Invalid plugin configuration: {}", e))
        }
    }

    fn process_element(&self, element: &mut Element, context: &mut XlinkContext) {
        // Collect xlink namespace prefixes
        let mut current_xlink_prefixes = Vec::new();

        for (key, value) in &element.attributes {
            if key.starts_with("xmlns:") && value == XLINK_NAMESPACE {
                let prefix = key.strip_prefix("xmlns:").unwrap();
                current_xlink_prefixes.push(prefix.to_string());
                context.xlink_prefixes.push(prefix.to_string());
            }
        }

        // Handle xlink:href conversion
        let is_legacy = LEGACY_ELEMENTS.contains(&element.name.as_ref());

        if is_legacy && !self.config.include_legacy {
            // Mark prefixes as used in legacy elements - check for any xlink attributes
            let has_xlink_attrs = element.attributes.keys().any(|key| {
                context
                    .xlink_prefixes
                    .iter()
                    .any(|prefix| key.starts_with(&format!("{}:", prefix)))
            });

            if has_xlink_attrs {
                for prefix in &context.xlink_prefixes {
                    if !context.used_in_legacy.contains(prefix) {
                        context.used_in_legacy.push(prefix.clone());
                    }
                }
            }
        } else {
            // Convert xlink:href to href if no href exists
            self.convert_href_attributes(element, &context.xlink_prefixes);
        }

        // Only convert other xlink attributes if not a legacy element or include_legacy is true
        if !is_legacy || self.config.include_legacy {
            // Handle xlink:show conversion to target
            self.convert_show_attributes(element, &context.xlink_prefixes);

            // Handle xlink:title conversion to <title> element
            self.convert_title_attributes(element, &context.xlink_prefixes);

            // Remove unused xlink attributes
            self.remove_unused_xlink_attributes(
                element,
                &context.xlink_prefixes,
                &context.used_in_legacy,
            );
        }

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

        // Remove xlink namespace declarations if not used in legacy elements
        for prefix in &current_xlink_prefixes {
            if !context.used_in_legacy.contains(prefix) {
                let xmlns_key = format!("xmlns:{}", prefix);
                element.remove_attr(&xmlns_key);
            }
        }

        // Remove processed prefixes from context
        for prefix in &current_xlink_prefixes {
            context.xlink_prefixes.retain(|p| p != prefix);
        }
    }

    fn convert_href_attributes(&self, element: &mut Element, xlink_prefixes: &[String]) {
        // Find xlink:href attributes
        let href_attrs: Vec<String> = element
            .attributes
            .keys()
            .filter(|key| {
                xlink_prefixes
                    .iter()
                    .any(|prefix| *key == &format!("{}:href", prefix))
            })
            .map(|k| k.to_string())
            .collect();

        for href_attr in href_attrs {
            if let Some(href_value) = element.attr(&href_attr).map(|s| s.to_string()) {
                // Only convert if no href attribute exists
                if !element.has_attr("href") {
                    element.set_attr("href", &href_value);
                }
                element.remove_attr(&href_attr);
            }
        }
    }

    fn convert_show_attributes(&self, element: &mut Element, xlink_prefixes: &[String]) {
        // Find xlink:show attributes
        let show_attrs: Vec<String> = element
            .attributes
            .keys()
            .filter(|key| {
                xlink_prefixes
                    .iter()
                    .any(|prefix| *key == &format!("{}:show", prefix))
            })
            .map(|k| k.to_string())
            .collect();

        for show_attr in show_attrs {
            if let Some(show_value) = element.attr(&show_attr) {
                // Convert to target attribute if no target exists
                if !element.has_attr("target") {
                    let target_value = match show_value {
                        "new" => "_blank",
                        "replace" => "_self",
                        _ => {
                            // Remove unknown values
                            element.remove_attr(&show_attr);
                            continue;
                        }
                    };
                    element.set_attr("target", target_value);
                }
                element.remove_attr(&show_attr);
            }
        }
    }

    fn convert_title_attributes(&self, element: &mut Element, xlink_prefixes: &[String]) {
        // Find xlink:title attributes
        let title_attrs: Vec<String> = element
            .attributes
            .keys()
            .filter(|key| {
                xlink_prefixes
                    .iter()
                    .any(|prefix| *key == &format!("{}:title", prefix))
            })
            .map(|k| k.to_string())
            .collect();

        for title_attr in title_attrs {
            if let Some(title_value) = element.attr(&title_attr) {
                // Check if element already has a title child
                let has_title_child = element
                    .children
                    .iter()
                    .any(|child| matches!(child, Node::Element(elem) if elem.name == "title"));

                if !has_title_child {
                    // Create title element
                    let title_element = Element {
                        name: "title".to_string().into(),
                        attributes: indexmap::IndexMap::new(),
                        namespaces: indexmap::IndexMap::new(),
                        children: vec![Node::Text(title_value.to_string().into())],
                    };
                    element.children.insert(0, Node::Element(title_element));
                }
                element.remove_attr(&title_attr);
            }
        }
    }

    fn remove_unused_xlink_attributes(
        &self,
        element: &mut Element,
        xlink_prefixes: &[String],
        used_in_legacy: &[String],
    ) {
        // Remove any remaining xlink attributes that weren't converted
        let attrs_to_remove: Vec<String> = element
            .attributes
            .keys()
            .filter(|key| {
                if let Some(colon_pos) = key.find(':') {
                    let prefix = &key[..colon_pos];
                    xlink_prefixes.contains(&prefix.to_string())
                        && !used_in_legacy.contains(&prefix.to_string())
                } else {
                    false
                }
            })
            .map(|k| k.to_string())
            .collect();

        for attr in attrs_to_remove {
            element.remove_attr(&attr);
        }
    }
}

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

struct XlinkContext {
    xlink_prefixes: Vec<String>,
    used_in_legacy: Vec<String>,
}

impl XlinkContext {
    fn new() -> Self {
        Self {
            xlink_prefixes: Vec::new(),
            used_in_legacy: Vec::new(),
        }
    }
}

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

    fn description(&self) -> &'static str {
        "remove xlink namespace and replaces attributes with the SVG 2 equivalent where applicable"
    }

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

    fn apply(&self, document: &mut Document) -> Result<()> {
        let mut context = XlinkContext::new();
        self.process_element(&mut document.root, &mut context);
        Ok(())
    }
}

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

    fn create_test_document() -> Document<'static> {
        Document {
            root: Element {
                name: Cow::Borrowed("svg"),
                attributes: IndexMap::new(),
                namespaces: IndexMap::new(),
                children: vec![],
            },
            ..Default::default()
        }
    }

    #[test]
    fn test_plugin_info() {
        let plugin = RemoveXlinkPlugin::new();
        assert_eq!(plugin.name(), "removeXlink");
        assert_eq!(
            plugin.description(),
            "remove xlink namespace and replaces attributes with the SVG 2 equivalent where applicable"
        );
    }

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

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

        // Test valid params
        assert!(plugin
            .validate_params(&serde_json::json!({
                "includeLegacy": true
            }))
            .is_ok());

        // Test invalid params
        assert!(plugin
            .validate_params(&serde_json::json!({
                "invalidParam": true
            }))
            .is_err());
    }

    #[test]
    fn test_convert_xlink_href_to_href() {
        let mut document = create_test_document();

        // Add xlink namespace
        document.root.set_attr("xmlns:xlink", XLINK_NAMESPACE);

        // Add element with xlink:href
        let mut use_element = Element {
            name: Cow::Borrowed("use"),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        use_element.set_attr("xlink:href", "#symbol1");

        document.root.children = vec![Node::Element(use_element)];

        let plugin = RemoveXlinkPlugin::new();
        let result = plugin.apply(&mut document);
        assert!(result.is_ok());

        // xlink:href should be converted to href
        if let Node::Element(ref use_elem) = document.root.children[0] {
            assert!(!use_elem.has_attr("xlink:href"));
            assert_eq!(use_elem.attr("href"), Some("#symbol1"));
        } else {
            panic!("Expected use element");
        }

        // xlink namespace should be removed
        assert!(!document.root.has_attr("xmlns:xlink"));
    }

    #[test]
    fn test_preserve_existing_href() {
        let mut document = create_test_document();

        document.root.set_attr("xmlns:xlink", XLINK_NAMESPACE);

        let mut element = Element {
            name: Cow::Borrowed("a"),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element.set_attr("href", "#existing");
        element.set_attr("xlink:href", "#xlink");

        document.root.children = vec![Node::Element(element)];

        let plugin = RemoveXlinkPlugin::new();
        let result = plugin.apply(&mut document);
        assert!(result.is_ok());

        // Should preserve existing href and remove xlink:href
        if let Node::Element(ref elem) = document.root.children[0] {
            assert!(!elem.has_attr("xlink:href"));
            assert_eq!(elem.attr("href"), Some("#existing"));
        } else {
            panic!("Expected element");
        }
    }

    #[test]
    fn test_convert_xlink_show_to_target() {
        let mut document = create_test_document();

        document.root.set_attr("xmlns:xlink", XLINK_NAMESPACE);

        let mut element = Element {
            name: Cow::Borrowed("a"),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element.set_attr("xlink:show", "new");

        document.root.children = vec![Node::Element(element)];

        let plugin = RemoveXlinkPlugin::new();
        let result = plugin.apply(&mut document);
        assert!(result.is_ok());

        // xlink:show="new" should become target="_blank"
        if let Node::Element(ref elem) = document.root.children[0] {
            assert!(!elem.has_attr("xlink:show"));
            assert_eq!(elem.attr("target"), Some("_blank"));
        } else {
            panic!("Expected element");
        }
    }

    #[test]
    fn test_convert_xlink_title_to_title_element() {
        let mut document = create_test_document();

        document.root.set_attr("xmlns:xlink", XLINK_NAMESPACE);

        let mut element = Element {
            name: Cow::Borrowed("rect"),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        element.set_attr("xlink:title", "Element title");

        document.root.children = vec![Node::Element(element)];

        let plugin = RemoveXlinkPlugin::new();
        let result = plugin.apply(&mut document);
        assert!(result.is_ok());

        // xlink:title should be converted to title element
        if let Node::Element(ref elem) = document.root.children[0] {
            assert!(!elem.has_attr("xlink:title"));
            assert_eq!(elem.children.len(), 1);

            if let Node::Element(ref title) = elem.children[0] {
                assert_eq!(title.name, "title");
                assert_eq!(title.children.len(), 1);
                if let Node::Text(ref text) = title.children[0] {
                    assert_eq!(text.as_ref(), "Element title");
                } else {
                    panic!("Expected text in title");
                }
            } else {
                panic!("Expected title element");
            }
        } else {
            panic!("Expected element");
        }
    }

    #[test]
    fn test_preserve_legacy_elements() {
        let mut document = create_test_document();

        document.root.set_attr("xmlns:xlink", XLINK_NAMESPACE);

        let mut filter_element = Element {
            name: Cow::Borrowed("filter"),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        filter_element.set_attr("xlink:href", "#filter1");

        document.root.children = vec![Node::Element(filter_element)];

        let plugin = RemoveXlinkPlugin::new();
        let result = plugin.apply(&mut document);
        assert!(result.is_ok());

        // Legacy elements should preserve xlink attributes by default
        if let Node::Element(ref filter) = document.root.children[0] {
            assert!(filter.has_attr("xlink:href"));
            assert!(!filter.has_attr("href"));
        } else {
            panic!("Expected filter element");
        }

        // xlink namespace should be preserved for legacy usage
        assert!(document.root.has_attr("xmlns:xlink"));
    }

    #[test]
    fn test_include_legacy_option() {
        let mut document = create_test_document();

        document.root.set_attr("xmlns:xlink", XLINK_NAMESPACE);

        let mut filter_element = Element {
            name: Cow::Borrowed("filter"),
            attributes: IndexMap::new(),
            namespaces: IndexMap::new(),
            children: vec![],
        };
        filter_element.set_attr("xlink:href", "#filter1");

        document.root.children = vec![Node::Element(filter_element)];

        let params = serde_json::json!({"includeLegacy": true});
        let plugin = RemoveXlinkPlugin::new();
        plugin.validate_params(&params).unwrap();
        let plugin = RemoveXlinkPlugin::with_config(RemoveXlinkConfig {
            include_legacy: true,
        });
        let result = plugin.apply(&mut document);
        assert!(result.is_ok());

        // With includeLegacy=true, should convert even legacy elements
        if let Node::Element(ref filter) = document.root.children[0] {
            assert!(!filter.has_attr("xlink:href"));
            assert_eq!(filter.attr("href"), Some("#filter1"));
        } else {
            panic!("Expected filter element");
        }
    }
}