From 9bcf5468e4a2b648634a2238fa0c45f9f21860c2 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Wed, 30 Oct 2013 11:45:21 +0100 Subject: [PATCH] Rewrite replace_all. --- src/components/script/dom/node.rs | 57 +++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/src/components/script/dom/node.rs b/src/components/script/dom/node.rs index e2369b786b9..c3ec9a136f5 100644 --- a/src/components/script/dom/node.rs +++ b/src/components/script/dom/node.rs @@ -889,18 +889,45 @@ impl Node { } // http://dom.spec.whatwg.org/#concept-node-replace-all - pub fn replace_all(&mut self, - abstract_self: AbstractNode, - node: Option>) { - //FIXME: We should batch document notifications that occur here - for child in abstract_self.children() { - self.RemoveChild(abstract_self, child); - } + pub fn replace_all(node: Option>, + parent: AbstractNode) { + // Step 1. match node { - None => {}, - Some(node) => { - self.AppendChild(abstract_self, node); - } + Some(node) => Node::adopt(node, parent.node().owner_doc()), + None => (), + } + + // Step 2. + let removedNodes: ~[AbstractNode] = parent.children().collect(); + + // Step 3. + let addedNodes = match node { + None => ~[], + Some(node) => match node.type_id() { + DocumentFragmentNodeTypeId => node.children().collect(), + _ => ~[node], + }, + }; + + // Step 4. + for child in parent.children() { + Node::remove(child, parent, true); + } + + // Step 5. + match node { + Some(node) => Node::insert(node, parent, None, true), + None => (), + } + + // Step 6: mutation records. + + // Step 7. + for removedNode in removedNodes.iter() { + removedNode.node_removed(); + } + for addedNode in addedNodes.iter() { + addedNode.node_inserted(); } } @@ -939,20 +966,22 @@ impl Node { pub fn SetTextContent(&mut self, abstract_self: AbstractNode, value: &Option) -> ErrorResult { + self.wait_until_safe_to_modify_dom(); + let value = null_str_as_empty(value); match self.type_id { DocumentFragmentNodeTypeId | ElementNodeTypeId(*) => { + // Step 1-2. let node = if value.len() == 0 { None } else { let document = self.owner_doc(); Some(document.document().CreateTextNode(document, &value)) }; - self.replace_all(abstract_self, node); + // Step 3. + Node::replace_all(node, abstract_self); } CommentNodeTypeId | TextNodeTypeId => { - self.wait_until_safe_to_modify_dom(); - do abstract_self.with_mut_characterdata() |characterdata| { characterdata.data = value.clone();