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