use svgdom::{
Attribute,
};
use super::prelude::*;
pub fn resolve_inherit(doc: &Document) {
let mut ids = Vec::new();
for (_, mut node) in doc.root().descendants().svg() {
ids.clear();
for (aid, attr) in node.attributes().iter().svg() {
if let AValue::Inherit = attr.value {
ids.push(aid);
}
}
for id in &ids {
resolve_impl(&mut node, *id);
}
}
}
fn resolve_impl(node: &mut Node, attr: AId) {
if attr.is_inheritable() {
if let Some(n) = node.ancestors().skip(1).find(|n| n.has_attribute(attr)) {
let av = n.attributes().get_value(attr).cloned();
if let Some(av) = av {
node.set_attribute((attr, av.clone()));
return;
}
}
} else {
if let Some(parent) = node.parent() {
let av = parent.attributes().get_value(attr).cloned();
if let Some(av) = av {
node.set_attribute((attr, av.clone()));
return;
}
}
}
match Attribute::new_default(attr) {
Some(a) => node.set_attribute((attr, a.value)),
None => {
warn!("Failed to resolve attribute: {}. Removing it.",
node.attributes().get(attr).unwrap());
node.remove_attribute(attr);
}
}
}