diff --git a/src/components/script/dom/bindings/codegen/Bindings.conf b/src/components/script/dom/bindings/codegen/Bindings.conf index 5ec918b6b2c..0ef1d9d502a 100644 --- a/src/components/script/dom/bindings/codegen/Bindings.conf +++ b/src/components/script/dom/bindings/codegen/Bindings.conf @@ -291,7 +291,7 @@ DOMInterfaces = { 'nativeType': 'AbstractNode', 'concreteType': 'Node', 'pointerType': '', - 'needsAbstract': ['removeChild'] + 'needsAbstract': ['appendChild', 'removeChild'] }, 'NodeList': [ diff --git a/src/components/script/dom/bindings/utils.rs b/src/components/script/dom/bindings/utils.rs index 9dfc4e965d0..547405e13a2 100644 --- a/src/components/script/dom/bindings/utils.rs +++ b/src/components/script/dom/bindings/utils.rs @@ -797,6 +797,7 @@ impl DerivedWrapper for AbstractNode { pub enum Error { FailureUnknown, NotFound, + HierarchyRequest, } pub type ErrorResult = Result<(), Error>; diff --git a/src/components/script/dom/node.rs b/src/components/script/dom/node.rs index 5aeab579f54..e137d4c31d0 100644 --- a/src/components/script/dom/node.rs +++ b/src/components/script/dom/node.rs @@ -5,7 +5,7 @@ //! The core DOM types. Defines the basic DOM hierarchy as well as all the HTML elements. use dom::bindings::node; -use dom::bindings::utils::{WrapperCache, DOMString, null_string, str, ErrorResult, NotFound}; +use dom::bindings::utils::{WrapperCache, DOMString, null_string, str, ErrorResult, NotFound, HierarchyRequest}; use dom::bindings::utils::{BindingObject, CacheableWrapper, rust_box}; use dom::bindings; use dom::characterdata::CharacterData; @@ -263,6 +263,10 @@ impl<'self, View> AbstractNode { self.transmute_mut(f) } + pub fn is_doctype(self) -> bool { + self.type_id() == DoctypeNodeTypeId + } + pub fn is_comment(self) -> bool { self.type_id() == CommentNodeTypeId } @@ -553,8 +557,43 @@ impl Node { fail!("stub") } - pub fn AppendChild(&mut self, _node: AbstractNode, _rv: &mut ErrorResult) -> AbstractNode { - fail!("stub") + pub fn AppendChild(&mut self, + abstract_self: AbstractNode, + node: AbstractNode, + rv: &mut ErrorResult) -> AbstractNode { + fn is_hierarchy_request_err(this_node: AbstractNode, + new_child: AbstractNode) -> bool { + if new_child.is_doctype() { + return true; + } + if !this_node.is_element() { + // FIXME: This should also work for Document and DocumentFragments when they inherit from node. + // per jgraham + return true; + } + if this_node == new_child { + return true; + } + for ancestor in this_node.ancestors() { + if ancestor == new_child { + return true; + } + } + false + } + + if is_hierarchy_request_err(abstract_self, node) { + *rv = Err(HierarchyRequest); + } + + // TODO: Should we handle WRONG_DOCUMENT_ERR here? + + if rv.is_ok() { + // If the node already exists it is removed from current parent node. + node.parent_node().map(|parent| parent.remove_child(node)); + abstract_self.add_child(node); + } + node } pub fn ReplaceChild(&mut self, _node: AbstractNode, _child: AbstractNode, _rv: &mut ErrorResult) -> AbstractNode {