diff --git a/src/components/script/dom/bindings/codegen/Bindings.conf b/src/components/script/dom/bindings/codegen/Bindings.conf index afcf4319ba37..3e6d8dbe88fe 100644 --- a/src/components/script/dom/bindings/codegen/Bindings.conf +++ b/src/components/script/dom/bindings/codegen/Bindings.conf @@ -26,6 +26,7 @@ DOMInterfaces = { 'Console': {}, 'Document': { 'needsAbstract': [ + 'adoptNode', 'anchors', 'applets', 'body', @@ -40,6 +41,7 @@ DOMInterfaces = { 'getElementsByTagName', 'getElementsByTagNameNS', 'images', + 'importNode', 'links', 'plugins', 'scripts', diff --git a/src/components/script/dom/document.rs b/src/components/script/dom/document.rs index 622263458d85..3172f2f93cb2 100644 --- a/src/components/script/dom/document.rs +++ b/src/components/script/dom/document.rs @@ -28,6 +28,7 @@ use dom::htmlhtmlelement::HTMLHtmlElement; use dom::htmltitleelement::HTMLTitleElement; use dom::mouseevent::MouseEvent; use dom::node::{Node, ElementNodeTypeId, DocumentNodeTypeId, NodeHelpers, INode}; +use dom::node::{CloneChildren, DoNotCloneChildren}; use dom::text::Text; use dom::processinginstruction::ProcessingInstruction; use dom::uievent::UIEvent; @@ -294,6 +295,37 @@ impl Document { Ok(ProcessingInstruction::new(target, data, abstract_self)) } + // http://dom.spec.whatwg.org/#dom-document-importnode + pub fn ImportNode(&self, abstract_self: &JS, node: &JS, deep: bool) -> Fallible> { + // Step 1. + if node.is_document() { + return Err(NotSupported); + } + + // Step 2. + let clone_children = match deep { + true => CloneChildren, + false => DoNotCloneChildren + }; + + Ok(Node::clone(node, Some(abstract_self), clone_children)) + } + + // http://dom.spec.whatwg.org/#dom-document-adoptnode + pub fn AdoptNode(&self, abstract_self: &JS, node: &JS) -> Fallible> { + // Step 1. + if node.is_document() { + return Err(NotSupported); + } + + // Step 2. + let mut adoptee = node.clone(); + Node::adopt(&mut adoptee, abstract_self); + + // Step 3. + Ok(adoptee) + } + // http://dom.spec.whatwg.org/#dom-document-createevent pub fn CreateEvent(&self, interface: DOMString) -> Fallible> { match interface.as_slice() { diff --git a/src/components/script/dom/node.rs b/src/components/script/dom/node.rs index 8bece8fdff97..9211925edb8e 100644 --- a/src/components/script/dom/node.rs +++ b/src/components/script/dom/node.rs @@ -728,7 +728,8 @@ fn gather_abstract_nodes(cur: &JS, refs: &mut ~[JS], postorder: bool } /// Specifies whether children must be recursively cloned or not. -enum CloneChildrenFlag { +#[deriving(Eq)] +pub enum CloneChildrenFlag { CloneChildren, DoNotCloneChildren } @@ -1016,7 +1017,7 @@ impl Node { } // http://dom.spec.whatwg.org/#concept-node-adopt - fn adopt(node: &mut JS, document: &JS) { + pub fn adopt(node: &mut JS, document: &JS) { // Step 1. match node.parent_node() { Some(ref mut parent) => Node::remove(node, parent, Unsuppressed), @@ -1287,18 +1288,8 @@ impl Node { } // http://dom.spec.whatwg.org/#concept-node-clone - fn clone(node: &JS, maybe_doc: Option<&JS>, clone_children: CloneChildrenFlag) - -> JS { - fn clone_recursively(node: &JS, copy: &mut JS, doc: &JS) { - for ref child in node.get().children() { - let mut cloned = Node::clone(child, Some(doc), DoNotCloneChildren); - match Node::pre_insert(&mut cloned, copy, None) { - Ok(ref mut appended) => clone_recursively(child, appended, doc), - Err(..) => fail!("an error occurred while appending children") - } - } - } - + pub fn clone(node: &JS, maybe_doc: Option<&JS>, + clone_children: CloneChildrenFlag) -> JS { // Step 1. let mut document = match maybe_doc { Some(doc) => doc.clone(), @@ -1395,9 +1386,11 @@ impl Node { // Step 5: cloning steps. // Step 6. - match clone_children { - CloneChildren => clone_recursively(node, &mut copy, &document), - DoNotCloneChildren => () + if clone_children == CloneChildren { + for ref child in node.get().children() { + let mut child_copy = Node::clone(child, Some(&document), clone_children); + let _inserted_node = Node::pre_insert(&mut child_copy, &mut copy, None); + } } // Step 7. diff --git a/src/components/script/dom/webidls/Document.webidl b/src/components/script/dom/webidls/Document.webidl index 41d20631bde2..a10909988018 100644 --- a/src/components/script/dom/webidls/Document.webidl +++ b/src/components/script/dom/webidls/Document.webidl @@ -36,6 +36,11 @@ interface Document : Node { [Creator, Throws] ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data); + [Throws] + Node importNode(Node node, optional boolean deep = false); + [Throws] + Node adoptNode(Node node); + [Creator, Throws] Event createEvent(DOMString interface_); }; diff --git a/src/test/content/test_document_adoptNode.html b/src/test/content/test_document_adoptNode.html new file mode 100644 index 000000000000..eef34b807231 --- /dev/null +++ b/src/test/content/test_document_adoptNode.html @@ -0,0 +1,25 @@ + + + + + + + +
+ + diff --git a/src/test/content/test_document_importNode.html b/src/test/content/test_document_importNode.html new file mode 100644 index 000000000000..78cc0fa6ba69 --- /dev/null +++ b/src/test/content/test_document_importNode.html @@ -0,0 +1,36 @@ + + + + + + + +
+ +