|
| 1 | +use std::{cell::RefCell, collections::BTreeSet}; |
| 2 | + |
| 3 | +use oxvg_ast::{ |
| 4 | + atom::Atom, |
| 5 | + attribute::{Attr, Attributes}, |
| 6 | + element::Element, |
| 7 | + visitor::{Context, Visitor}, |
| 8 | +}; |
| 9 | +use oxvg_collections::collections::EDITOR_NAMESPACES; |
| 10 | +use serde::Deserialize; |
| 11 | + |
| 12 | +#[derive(Deserialize, Clone, Default)] |
| 13 | +#[serde(rename_all = "camelCase")] |
| 14 | +pub struct RemoveEditorsNSData { |
| 15 | + additional_namespaces: Option<BTreeSet<String>>, |
| 16 | + #[serde(skip_deserializing)] |
| 17 | + prefixes: RefCell<BTreeSet<String>>, |
| 18 | +} |
| 19 | + |
| 20 | +impl<E: Element> Visitor<E> for RemoveEditorsNSData { |
| 21 | + type Error = String; |
| 22 | + |
| 23 | + fn document( |
| 24 | + &mut self, |
| 25 | + document: &mut E, |
| 26 | + _context: &Context<'_, '_, E>, |
| 27 | + ) -> Result<(), Self::Error> { |
| 28 | + document.for_each_element_child(|ref e| { |
| 29 | + self.collect_svg_namespace(e); |
| 30 | + self.remove_editor_attributes(e); |
| 31 | + self.remove_editor_element(e); |
| 32 | + }); |
| 33 | + Ok(()) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl RemoveEditorsNSData { |
| 38 | + fn collect_svg_namespace<E: Element>(&self, element: &E) { |
| 39 | + if element.local_name().as_ref() != "svg" { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + let mut prefixes = self.prefixes.borrow_mut(); |
| 44 | + for xmlns in element |
| 45 | + .attributes() |
| 46 | + .into_iter() |
| 47 | + .filter(|a| a.prefix().as_ref().is_some_and(|p| p.as_ref() == "xmlns")) |
| 48 | + { |
| 49 | + let value = xmlns.value(); |
| 50 | + let value = value.as_str(); |
| 51 | + if !EDITOR_NAMESPACES.contains(value) |
| 52 | + && !self |
| 53 | + .additional_namespaces |
| 54 | + .as_ref() |
| 55 | + .is_some_and(|n| n.contains(value)) |
| 56 | + { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + let name = xmlns.local_name(); |
| 61 | + log::debug!("Adding {name} to prefixes"); |
| 62 | + prefixes.insert(name.to_string()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + fn remove_editor_attributes<E: Element>(&self, element: &E) { |
| 67 | + let prefixes = self.prefixes.borrow(); |
| 68 | + element.attributes().retain(|attr| { |
| 69 | + let Some(prefix) = attr.prefix() else { |
| 70 | + return true; |
| 71 | + }; |
| 72 | + |
| 73 | + !prefixes.contains(prefix.as_ref()) |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + fn remove_editor_element<E: Element>(&self, element: &E) { |
| 78 | + let Some(prefix) = element.prefix() else { |
| 79 | + return; |
| 80 | + }; |
| 81 | + |
| 82 | + if self.prefixes.borrow().contains(prefix.as_ref()) { |
| 83 | + log::debug!("Removing element with prefix: {prefix}"); |
| 84 | + element.remove(); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[test] |
| 90 | +fn remove_editors_ns_data() -> anyhow::Result<()> { |
| 91 | + use crate::test_config; |
| 92 | + |
| 93 | + insta::assert_snapshot!(test_config( |
| 94 | + r#"{ "removeEditorsNsData": {} }"#, |
| 95 | + Some( |
| 96 | + r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"> |
| 97 | + <sodipodi:namedview> |
| 98 | + ... |
| 99 | + </sodipodi:namedview> |
| 100 | +
|
| 101 | + <path d="..." sodipodi:nodetypes="cccc"/> |
| 102 | +</svg>"# |
| 103 | + ), |
| 104 | + )?); |
| 105 | + |
| 106 | + insta::assert_snapshot!(test_config( |
| 107 | + r#"{ "removeEditorsNsData": {} }"#, |
| 108 | + Some( |
| 109 | + r#"<svg xmlns="http://www.w3.org/2000/svg" xmlns:sodipodi="http://inkscape.sourceforge.net/DTD/sodipodi-0.dtd"> |
| 110 | + <sodipodi:namedview> |
| 111 | + ... |
| 112 | + </sodipodi:namedview> |
| 113 | +
|
| 114 | + <path d="..." sodipodi:nodetypes="cccc"/> |
| 115 | +</svg>"# |
| 116 | + ), |
| 117 | + )?); |
| 118 | + |
| 119 | + Ok(()) |
| 120 | +} |
0 commit comments