Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop template contents interatively. #416

Merged
merged 1 commit into from
Jun 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions rcdom/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub enum NodeData {
/// For HTML \<template\> elements, the [template contents].
///
/// [template contents]: https://html.spec.whatwg.org/multipage/#template-contents
template_contents: Option<Handle>,
template_contents: RefCell<Option<Handle>>,

/// Whether the node is a [HTML integration point].
///
Expand Down Expand Up @@ -131,6 +131,11 @@ impl Drop for Node {
while let Some(node) = nodes.pop() {
let children = mem::replace(&mut *node.children.borrow_mut(), vec![]);
nodes.extend(children.into_iter());
if let NodeData::Element { ref template_contents, .. } = node.data {
if let Some(template_contents) = template_contents.borrow_mut().take() {
nodes.push(template_contents);
}
}
}
}
}
Expand Down Expand Up @@ -226,11 +231,11 @@ impl TreeSink for RcDom {

fn get_template_contents(&mut self, target: &Handle) -> Handle {
if let NodeData::Element {
template_contents: Some(ref contents),
ref template_contents,
..
} = target.data
{
contents.clone()
template_contents.borrow().as_ref().expect("not a template element!").clone()
} else {
panic!("not a template element!")
}
Expand Down Expand Up @@ -260,11 +265,11 @@ impl TreeSink for RcDom {
Node::new(NodeData::Element {
name: name,
attrs: RefCell::new(attrs),
template_contents: if flags.template {
template_contents: RefCell::new(if flags.template {
Some(Node::new(NodeData::Document))
} else {
None
},
}),
mathml_annotation_xml_integration_point: flags.mathml_annotation_xml_integration_point,
})
}
Expand Down
11 changes: 11 additions & 0 deletions rcdom/tests/html-driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ fn from_utf8() {
"<html><head><title>Test</title></head><body></body></html>"
);
}

#[test]
fn many_templates() {
let mut body = String::new();
for _ in 1..10000 {
body.push_str("<template>");
}
let _ = driver::parse_document(RcDom::default(), Default::default())
.from_utf8()
.one(body.as_bytes());
}
14 changes: 8 additions & 6 deletions rcdom/tests/html-tree-builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,17 @@ fn serialize(buf: &mut String, indent: usize, handle: Handle) {
}

if let NodeData::Element {
template_contents: Some(ref content),
ref template_contents,
..
} = node.data
{
buf.push_str("|");
buf.push_str(&repeat(" ").take(indent + 2).collect::<String>());
buf.push_str("content\n");
for child in content.children.borrow().iter() {
serialize(buf, indent + 4, child.clone());
if let Some(ref content) = &*template_contents.borrow() {
buf.push_str("|");
buf.push_str(&repeat(" ").take(indent + 2).collect::<String>());
buf.push_str("content\n");
for child in content.children.borrow().iter() {
serialize(buf, indent + 4, child.clone());
}
}
}
}
Expand Down