mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
Convert node serialization to a purely iterative algorithm.
We maintain a stack of open element nodes and non-node elements and use it to determine when to close them.
This commit is contained in:
parent
2bb4f65100
commit
033b31979b
3 changed files with 161 additions and 65 deletions
|
@ -14,7 +14,7 @@ use dom::documenttype::DocumentType;
|
||||||
use dom::element::Element;
|
use dom::element::Element;
|
||||||
use dom::htmlscriptelement::HTMLScriptElement;
|
use dom::htmlscriptelement::HTMLScriptElement;
|
||||||
use dom::htmltemplateelement::HTMLTemplateElement;
|
use dom::htmltemplateelement::HTMLTemplateElement;
|
||||||
use dom::node::Node;
|
use dom::node::{Node, TreeIterator};
|
||||||
use dom::processinginstruction::ProcessingInstruction;
|
use dom::processinginstruction::ProcessingInstruction;
|
||||||
use dom::servoparser::Sink;
|
use dom::servoparser::Sink;
|
||||||
use html5ever::QualName;
|
use html5ever::QualName;
|
||||||
|
@ -115,17 +115,10 @@ unsafe impl JSTraceable for HtmlTokenizer<TreeBuilder<JS<Node>, Sink>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Serialize for &'a Node {
|
fn start_element<S: Serializer>(node: &Element, serializer: &mut S) -> io::Result<()> {
|
||||||
fn serialize<S: Serializer>(&self, serializer: &mut S,
|
let name = QualName::new(None, node.namespace().clone(),
|
||||||
traversal_scope: TraversalScope) -> io::Result<()> {
|
node.local_name().clone());
|
||||||
let node = *self;
|
let attrs = node.attrs().iter().map(|attr| {
|
||||||
match (traversal_scope, node.type_id()) {
|
|
||||||
(_, NodeTypeId::Element(..)) => {
|
|
||||||
let elem = node.downcast::<Element>().unwrap();
|
|
||||||
let name = QualName::new(None, elem.namespace().clone(),
|
|
||||||
elem.local_name().clone());
|
|
||||||
if traversal_scope == IncludeNode {
|
|
||||||
let attrs = elem.attrs().iter().map(|attr| {
|
|
||||||
let qname = QualName::new(None, attr.namespace().clone(),
|
let qname = QualName::new(None, attr.namespace().clone(),
|
||||||
attr.local_name().clone());
|
attr.local_name().clone());
|
||||||
let value = attr.value().clone();
|
let value = attr.value().clone();
|
||||||
|
@ -135,59 +128,124 @@ impl<'a> Serialize for &'a Node {
|
||||||
let ar: AttrRef = (&qname, &**value);
|
let ar: AttrRef = (&qname, &**value);
|
||||||
ar
|
ar
|
||||||
});
|
});
|
||||||
serializer.start_elem(name.clone(), attr_refs)?;
|
serializer.start_elem(name, attr_refs)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
let children = if let Some(tpl) = node.downcast::<HTMLTemplateElement>() {
|
fn end_element<S: Serializer>(node: &Element, serializer: &mut S) -> io::Result<()> {
|
||||||
// https://github.com/w3c/DOM-Parsing/issues/1
|
let name = QualName::new(None, node.namespace().clone(),
|
||||||
tpl.Content().upcast::<Node>().children()
|
node.local_name().clone());
|
||||||
} else {
|
serializer.end_elem(name)
|
||||||
node.children()
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum SerializationCommand {
|
||||||
|
OpenElement(Root<Element>),
|
||||||
|
CloseElement(Root<Element>),
|
||||||
|
SerializeNonelement(Root<Node>),
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SerializationIterator {
|
||||||
|
stack: Vec<SerializationCommand>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rev_children_iter(n: &Node) -> impl Iterator<Item=Root<Node>>{
|
||||||
|
match n.downcast::<HTMLTemplateElement>() {
|
||||||
|
Some(t) => t.Content().upcast::<Node>().rev_children(),
|
||||||
|
None => n.rev_children(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SerializationIterator {
|
||||||
|
fn new(node: &Node, skip_first: bool) -> SerializationIterator {
|
||||||
|
let mut ret = SerializationIterator {
|
||||||
|
stack: vec![],
|
||||||
};
|
};
|
||||||
|
if skip_first {
|
||||||
for handle in children {
|
for c in rev_children_iter(node) {
|
||||||
(&*handle).serialize(serializer, IncludeNode)?;
|
ret.push_node(&*c);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ret.push_node(node);
|
||||||
|
}
|
||||||
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
if traversal_scope == IncludeNode {
|
fn push_node(&mut self, n: &Node) {
|
||||||
serializer.end_elem(name.clone())?;
|
match n.downcast::<Element>() {
|
||||||
|
Some(e) => self.stack.push(SerializationCommand::OpenElement(Root::from_ref(e))),
|
||||||
|
None => self.stack.push(SerializationCommand::SerializeNonelement(Root::from_ref(n))),
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
|
|
||||||
(ChildrenOnly, NodeTypeId::Document(_)) => {
|
|
||||||
for handle in node.children() {
|
|
||||||
(&*handle).serialize(serializer, IncludeNode)?;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
}
|
||||||
|
|
||||||
|
impl Iterator for SerializationIterator {
|
||||||
|
type Item = SerializationCommand;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<SerializationCommand> {
|
||||||
|
let res = self.stack.pop();
|
||||||
|
|
||||||
|
if let Some(SerializationCommand::OpenElement(ref e)) = res {
|
||||||
|
self.stack.push(SerializationCommand::CloseElement(e.clone()));
|
||||||
|
for c in rev_children_iter(&*e.upcast::<Node>()) {
|
||||||
|
self.push_node(&c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Serialize for &'a Node {
|
||||||
|
fn serialize<S: Serializer>(&self, serializer: &mut S,
|
||||||
|
traversal_scope: TraversalScope) -> io::Result<()> {
|
||||||
|
let node = *self;
|
||||||
|
|
||||||
|
|
||||||
|
let iter = SerializationIterator::new(node, traversal_scope == ChildrenOnly);
|
||||||
|
|
||||||
|
for cmd in iter {
|
||||||
|
match cmd {
|
||||||
|
SerializationCommand::OpenElement(n) => {
|
||||||
|
start_element(&n, serializer)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
SerializationCommand::CloseElement(n) => {
|
||||||
|
end_element(&&n, serializer)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
SerializationCommand::SerializeNonelement(n) => {
|
||||||
|
match n.type_id() {
|
||||||
|
NodeTypeId::DocumentType => {
|
||||||
|
let doctype = n.downcast::<DocumentType>().unwrap();
|
||||||
|
serializer.write_doctype(&doctype.name())?;
|
||||||
},
|
},
|
||||||
|
|
||||||
(ChildrenOnly, _) => Ok(()),
|
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => {
|
||||||
|
let cdata = n.downcast::<CharacterData>().unwrap();
|
||||||
(IncludeNode, NodeTypeId::DocumentType) => {
|
serializer.write_text(&cdata.data())?;
|
||||||
let doctype = node.downcast::<DocumentType>().unwrap();
|
|
||||||
serializer.write_doctype(&doctype.name())
|
|
||||||
},
|
},
|
||||||
|
|
||||||
(IncludeNode, NodeTypeId::CharacterData(CharacterDataTypeId::Text)) => {
|
NodeTypeId::CharacterData(CharacterDataTypeId::Comment) => {
|
||||||
let cdata = node.downcast::<CharacterData>().unwrap();
|
let cdata = n.downcast::<CharacterData>().unwrap();
|
||||||
serializer.write_text(&cdata.data())
|
serializer.write_comment(&cdata.data())?;
|
||||||
},
|
},
|
||||||
|
|
||||||
(IncludeNode, NodeTypeId::CharacterData(CharacterDataTypeId::Comment)) => {
|
NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction) => {
|
||||||
let cdata = node.downcast::<CharacterData>().unwrap();
|
let pi = n.downcast::<ProcessingInstruction>().unwrap();
|
||||||
serializer.write_comment(&cdata.data())
|
|
||||||
},
|
|
||||||
|
|
||||||
(IncludeNode, NodeTypeId::CharacterData(CharacterDataTypeId::ProcessingInstruction)) => {
|
|
||||||
let pi = node.downcast::<ProcessingInstruction>().unwrap();
|
|
||||||
let data = pi.upcast::<CharacterData>().data();
|
let data = pi.upcast::<CharacterData>().data();
|
||||||
serializer.write_processing_instruction(&pi.target(), &data)
|
serializer.write_processing_instruction(&pi.target(), &data)?;
|
||||||
},
|
},
|
||||||
|
|
||||||
(IncludeNode, NodeTypeId::DocumentFragment) => Ok(()),
|
NodeTypeId::DocumentFragment => {}
|
||||||
|
|
||||||
(IncludeNode, NodeTypeId::Document(_)) => panic!("Can't serialize Document node itself"),
|
NodeTypeId::Document(_) => panic!("Can't serialize Document node itself"),
|
||||||
|
NodeTypeId::Element(_) => panic!("Element shouldn't appear here"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -13737,6 +13737,12 @@
|
||||||
{}
|
{}
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
|
"mozilla/deep_serialization_succeeds.html": [
|
||||||
|
[
|
||||||
|
"/_mozilla/mozilla/deep_serialization_succeeds.html",
|
||||||
|
{}
|
||||||
|
]
|
||||||
|
],
|
||||||
"mozilla/deterministic-raf.html": [
|
"mozilla/deterministic-raf.html": [
|
||||||
[
|
[
|
||||||
"/_mozilla/mozilla/deterministic-raf.html",
|
"/_mozilla/mozilla/deterministic-raf.html",
|
||||||
|
@ -26886,6 +26892,10 @@
|
||||||
"67d8cdd3e3a1656ba315fcbf6dae7236680bac16",
|
"67d8cdd3e3a1656ba315fcbf6dae7236680bac16",
|
||||||
"reftest"
|
"reftest"
|
||||||
],
|
],
|
||||||
|
"mozilla/deep_serialization_succeeds.html": [
|
||||||
|
"cb3d201d577c17d19babf1f6c04ba882fa42048e",
|
||||||
|
"testharness"
|
||||||
|
],
|
||||||
"mozilla/details_ui_closed.html": [
|
"mozilla/details_ui_closed.html": [
|
||||||
"2acbe3afbec267bad4dd986803e636740a707507",
|
"2acbe3afbec267bad4dd986803e636740a707507",
|
||||||
"reftest"
|
"reftest"
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Deep Serialization Test</title>
|
||||||
|
<script src="/resources/testharness.js"></script>
|
||||||
|
<script src="/resources/testharnessreport.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="log"></div>
|
||||||
|
<script>
|
||||||
|
|
||||||
|
// The test here is that Servo doesn't crash.
|
||||||
|
|
||||||
|
test(function() {
|
||||||
|
var first = document.createElement('div');
|
||||||
|
var last = first;
|
||||||
|
for (var i = 0; i < 3000; i++) {
|
||||||
|
var e = document.createElement('div');
|
||||||
|
last.appendChild(e);
|
||||||
|
last = e;
|
||||||
|
}
|
||||||
|
last.textContent = "abcdef";
|
||||||
|
var elem = first;
|
||||||
|
assert_regexp_match(elem.outerHTML, /abcdef/);
|
||||||
|
}, "Test that deep trees can serialize without crashing.");
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Add table
Add a link
Reference in a new issue