From 37787d55d0008879fd776b012b5d7994cff99fe9 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Mon, 16 Sep 2013 23:29:05 -0400 Subject: [PATCH] Pass the AbstractNode into the Node.textContent setter. --- .../script/dom/bindings/codegen/Bindings.conf | 2 +- src/components/script/dom/document.rs | 11 +--- src/components/script/dom/node.rs | 56 ++++++++++++------- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/components/script/dom/bindings/codegen/Bindings.conf b/src/components/script/dom/bindings/codegen/Bindings.conf index 4942b59b132..9d55b8e6038 100644 --- a/src/components/script/dom/bindings/codegen/Bindings.conf +++ b/src/components/script/dom/bindings/codegen/Bindings.conf @@ -293,7 +293,7 @@ DOMInterfaces = { 'nativeType': 'AbstractNode', 'concreteType': 'Node', 'pointerType': '', - 'needsAbstract': ['appendChild', 'removeChild'] + 'needsAbstract': ['appendChild', 'removeChild', 'textContent'] }, 'NodeList': [ diff --git a/src/components/script/dom/document.rs b/src/components/script/dom/document.rs index 60d611f3fde..a6aba2a7a95 100644 --- a/src/components/script/dom/document.rs +++ b/src/components/script/dom/document.rs @@ -331,7 +331,7 @@ impl Document { for title_child in child.children() { child.remove_child(title_child); } - child.add_child(self.createText(title.to_str())); + child.add_child(self.CreateTextNode(title)); break; } if !has_title { @@ -341,7 +341,7 @@ impl Document { let new_title = unsafe { Node::as_abstract_node(cx, new_title) }; - new_title.add_child(self.createText(title.to_str())); + new_title.add_child(self.CreateTextNode(title)); node.add_child(new_title); } break; @@ -438,13 +438,6 @@ impl Document { elem.get_attr("name").is_some() && eq_slice(elem.get_attr("name").unwrap(), name.to_str())) } - pub fn createText(&self, data: ~str) -> AbstractNode { - let (_scope, cx) = self.get_scope_and_cx(); - unsafe { - Node::as_abstract_node(cx, @Text::new(data)) - } - } - pub fn createHTMLCollection(&self, callback: &fn(elem: &Element) -> bool) -> @mut HTMLCollection { let mut elements = ~[]; let _ = for child in self.root.traverse_preorder() { diff --git a/src/components/script/dom/node.rs b/src/components/script/dom/node.rs index 396d07ac343..335825b5cd5 100644 --- a/src/components/script/dom/node.rs +++ b/src/components/script/dom/node.rs @@ -562,11 +562,11 @@ impl Node { pub fn SetNodeValue(&mut self, _val: &DOMString, _rv: &mut ErrorResult) { } - pub fn GetTextContent(&self) -> DOMString { + pub fn GetTextContent(&self, abstract_self: AbstractNode) -> DOMString { match self.type_id { ElementNodeTypeId(*) => { let mut content = ~""; - for node in self.abstract.unwrap().traverse_preorder() { + for node in abstract_self.traverse_preorder() { if node.is_text() { do node.with_imm_text() |text| { let s = text.parent.Data(); @@ -577,7 +577,7 @@ impl Node { str(content) } CommentNodeTypeId | TextNodeTypeId => { - do self.abstract.unwrap().with_imm_characterdata() |characterdata| { + do abstract_self.with_imm_characterdata() |characterdata| { characterdata.Data() } } @@ -588,38 +588,52 @@ impl Node { } // http://dom.spec.whatwg.org/#concept-node-replace-all - pub fn replace_all(&mut self, node: Option>) { - let this = self.abstract.unwrap(); - for child in this.children() { - this.remove_child(child); + pub fn replace_all(&mut self, + abstract_self: AbstractNode, + node: Option>) { + //FIXME: We should batch document notifications that occur here + let mut rv = Ok(()); + for child in abstract_self.children() { + self.RemoveChild(abstract_self, child, &mut rv); } match node { - None => {}, - Some(node) => this.add_child(node) + None => {}, + Some(node) => { + self.AppendChild(abstract_self, node, &mut rv); + } } } - pub fn SetTextContent(&mut self, value: &DOMString, _rv: &mut ErrorResult) { - let text_content = match value { - &str(ref s) => s.as_slice(), - &null_string => &"" + pub fn SetTextContent(&mut self, + abstract_self: AbstractNode, + value: &DOMString, + _rv: &mut ErrorResult) { + let is_empty = match value { + &str(~"") | &null_string => true, + _ => false }; match self.type_id { ElementNodeTypeId(*) => { - let node = match text_content { - "" => None, - s => { + let node = if is_empty { + None + } else { let text_node = do self.owner_doc.unwrap().with_base |document| { - document.createText(s.to_str()) + document.CreateTextNode(value) }; Some(text_node) - } }; - self.replace_all(node); + self.replace_all(abstract_self, node); } CommentNodeTypeId | TextNodeTypeId => { - do self.abstract.unwrap().with_mut_characterdata() |characterdata| { - characterdata.data = text_content.to_str(); + do abstract_self.with_mut_characterdata() |characterdata| { + characterdata.data = value.to_str(); + + // Notify the document that the content of this node is different + for doc in self.owner_doc.iter() { + do doc.with_base |doc| { + doc.content_changed(); + } + } } } DoctypeNodeTypeId => {}