Skip to content

Commit

Permalink
Auto merge of #416 - servo:rcdom-template, r=Manishearth
Browse files Browse the repository at this point in the history
Drop template contents interatively.

This is an extension of the work from #384.
  • Loading branch information
bors-servo committed Jun 6, 2020
2 parents 3e9fe22 + f54a307 commit ecdfe69
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
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

0 comments on commit ecdfe69

Please sign in to comment.