use super::{DocumentTree, ElementId, TreeScopeId};
use crate::dom::{DomError, NodeData, SVG_NAMESPACE, XLINK_NAMESPACE, shadow::ShadowRoot};
use ego_tree::NodeId;
use std::collections::HashMap;
const MAX_INSTANCE_NODES: usize = 1_000_000;
const INSTANCE_NODES: &str = "SVG use-element instance nodes";
const USE_ELEMENT: &str = "use";
const HREF_ATTRIBUTE: &str = "href";
const ID_ATTRIBUTE: &str = "id";
const FRAGMENT: char = '#';
type Instance = (TreeScopeId, HashMap<NodeId, NodeId>);
struct Pending {
host: NodeId,
original: NodeId,
enclosing: Vec<NodeId>,
}
impl DocumentTree {
pub(super) fn attach_instance(
&mut self,
original: NodeId,
host: NodeId,
source: Option<NodeId>,
node_limit: usize,
) -> Result<Option<Instance>, DomError> {
if self.shadow.roots.contains_key(&host) {
return Ok(None);
}
let originals: Vec<NodeId> = self
.html
.tree
.get(original)
.expect("original node")
.descendants()
.map(|node| node.id())
.collect();
if originals.len() > node_limit.saturating_sub(self.instance_nodes) {
return Err(DomError::TreeLimit {
resource: INSTANCE_NODES,
limit: node_limit,
});
}
let depth = self
.element(ElementId {
document: self.identity,
node: host,
})
.and_then(super::Element::shadow_host)
.map_or(1, |parent| self.shadow.roots[&parent.raw.id()].depth + 1);
self.sibling_positions.get_mut().clear();
let fragment = self.html.tree.orphan(NodeData::Fragment).id();
let mut clones = HashMap::with_capacity(originals.len());
for &node in &originals {
let parent = if node == original {
fragment
} else {
clones[&self.html.tree.get(node).unwrap().parent().unwrap().id()]
};
let data = self.html.tree.get(node).unwrap().value().clone();
let cloned = self.html.tree.get_mut(parent).unwrap().append(data).id();
self.shadow.scopes.insert(cloned, fragment);
clones.insert(node, cloned);
}
self.shadow
.roots
.insert(host, ShadowRoot { fragment, depth });
self.shadow.hosts.insert(fragment, host);
if let Some(source) = source {
self.shadow.instance_sources.insert(fragment, source);
}
self.shadow.order_contexts([fragment]);
self.cache_initial_states(clones.values().copied());
self.instance_nodes += originals.len();
Ok(Some((
TreeScopeId {
document: self.identity,
root: fragment,
},
clones,
)))
}
pub(crate) fn attach_use_instances(&mut self) -> Result<(), DomError> {
let mut identified: HashMap<(NodeId, &str), NodeId> = HashMap::new();
let mut pending = Vec::new();
for element in self.shadow_including_elements() {
if let Some(id) = element.attr(ID_ATTRIBUTE) {
identified
.entry((element.tree_scope().root, id))
.or_insert(element.raw.id());
}
if element.namespace() == SVG_NAMESPACE && element.name() == USE_ELEMENT {
pending.push(Pending {
host: element.raw.id(),
original: element.raw.id(),
enclosing: Vec::new(),
});
}
}
let references: HashMap<NodeId, (NodeId, NodeId)> = pending
.iter()
.filter_map(|instance| {
let element = self.element(ElementId {
document: self.identity,
node: instance.original,
})?;
let scope = element.tree_scope().root;
let reference = element
.attr_in_namespace(None, HREF_ATTRIBUTE)
.or_else(|| element.attr_in_namespace(Some(XLINK_NAMESPACE), HREF_ATTRIBUTE))?
.strip_prefix(FRAGMENT)?;
let target = *identified.get(&(scope, reference))?;
Some((instance.original, (target, scope)))
})
.collect();
pending.reverse();
while let Some(instance) = pending.pop() {
let Some(&(target, scope)) = references.get(&instance.original) else {
continue;
};
let circular = std::iter::once(&instance.original)
.chain(&instance.enclosing)
.any(|&original| {
self.html
.tree
.get(original)
.unwrap()
.ancestors()
.any(|ancestor| ancestor.id() == target)
|| original == target
});
if circular {
continue;
}
let Some((_, clones)) =
self.attach_instance(target, instance.host, Some(scope), MAX_INSTANCE_NODES)?
else {
continue;
};
let mut enclosing = instance.enclosing;
enclosing.push(instance.original);
let mut nested: Vec<_> = clones
.into_iter()
.filter(|(original, _)| references.contains_key(original))
.collect();
nested.sort_unstable();
pending.extend(nested.into_iter().rev().map(|(original, host)| Pending {
host,
original,
enclosing: enclosing.clone(),
}));
}
Ok(())
}
}