From 8a457a2caa6df536f888ebd5cd12fae129e87d5b Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 17 Mar 2014 00:55:57 -0400 Subject: [PATCH 1/5] Implemented Document.importNode Spec: http://dom.spec.whatwg.org/#dom-document-importnode --- .../script/dom/bindings/codegen/Bindings.conf | 1 + src/components/script/dom/document.rs | 17 +++++++++++++++++ src/components/script/dom/node.rs | 6 +++--- .../script/dom/webidls/Document.webidl | 3 +++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/components/script/dom/bindings/codegen/Bindings.conf b/src/components/script/dom/bindings/codegen/Bindings.conf index afcf4319ba37..3024190100ba 100644 --- a/src/components/script/dom/bindings/codegen/Bindings.conf +++ b/src/components/script/dom/bindings/codegen/Bindings.conf @@ -40,6 +40,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..8ece312e9070 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,22 @@ 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-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..1905b77e5160 100644 --- a/src/components/script/dom/node.rs +++ b/src/components/script/dom/node.rs @@ -728,7 +728,7 @@ fn gather_abstract_nodes(cur: &JS, refs: &mut ~[JS], postorder: bool } /// Specifies whether children must be recursively cloned or not. -enum CloneChildrenFlag { +pub enum CloneChildrenFlag { CloneChildren, DoNotCloneChildren } @@ -1287,8 +1287,8 @@ impl Node { } // http://dom.spec.whatwg.org/#concept-node-clone - fn clone(node: &JS, maybe_doc: Option<&JS>, clone_children: CloneChildrenFlag) - -> JS { + pub 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); diff --git a/src/components/script/dom/webidls/Document.webidl b/src/components/script/dom/webidls/Document.webidl index 41d20631bde2..b12c5525e921 100644 --- a/src/components/script/dom/webidls/Document.webidl +++ b/src/components/script/dom/webidls/Document.webidl @@ -36,6 +36,9 @@ interface Document : Node { [Creator, Throws] ProcessingInstruction createProcessingInstruction(DOMString target, DOMString data); + [Throws] + Node importNode(Node node, optional boolean deep = false); + [Creator, Throws] Event createEvent(DOMString interface_); }; From 990545c310ab801b6ec74b3765e5308ca2926cae Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 17 Mar 2014 00:56:21 -0400 Subject: [PATCH 2/5] Implemented Node.adoptNode Spec: http://dom.spec.whatwg.org/#dom-document-adoptnode --- .../script/dom/bindings/codegen/Bindings.conf | 1 + src/components/script/dom/document.rs | 15 +++++++++++++++ src/components/script/dom/node.rs | 2 +- src/components/script/dom/webidls/Document.webidl | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/components/script/dom/bindings/codegen/Bindings.conf b/src/components/script/dom/bindings/codegen/Bindings.conf index 3024190100ba..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', diff --git a/src/components/script/dom/document.rs b/src/components/script/dom/document.rs index 8ece312e9070..3172f2f93cb2 100644 --- a/src/components/script/dom/document.rs +++ b/src/components/script/dom/document.rs @@ -311,6 +311,21 @@ impl Document { 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 1905b77e5160..95c09d309f2d 100644 --- a/src/components/script/dom/node.rs +++ b/src/components/script/dom/node.rs @@ -1016,7 +1016,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), diff --git a/src/components/script/dom/webidls/Document.webidl b/src/components/script/dom/webidls/Document.webidl index b12c5525e921..a10909988018 100644 --- a/src/components/script/dom/webidls/Document.webidl +++ b/src/components/script/dom/webidls/Document.webidl @@ -38,6 +38,8 @@ interface Document : Node { [Throws] Node importNode(Node node, optional boolean deep = false); + [Throws] + Node adoptNode(Node node); [Creator, Throws] Event createEvent(DOMString interface_); From b05e3666e4b48f647306c580d87a09f88a552f41 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 17 Mar 2014 23:39:08 -0400 Subject: [PATCH 3/5] Cleaned up recursive code in Node.clone --- src/components/script/dom/node.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/components/script/dom/node.rs b/src/components/script/dom/node.rs index 95c09d309f2d..9211925edb8e 100644 --- a/src/components/script/dom/node.rs +++ b/src/components/script/dom/node.rs @@ -728,6 +728,7 @@ fn gather_abstract_nodes(cur: &JS, refs: &mut ~[JS], postorder: bool } /// Specifies whether children must be recursively cloned or not. +#[deriving(Eq)] pub enum CloneChildrenFlag { CloneChildren, DoNotCloneChildren @@ -1289,16 +1290,6 @@ impl Node { // http://dom.spec.whatwg.org/#concept-node-clone pub 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") - } - } - } - // 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. From 2ca3d9b138029959592ea417272e33b70e496f9d Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 17 Mar 2014 23:58:51 -0400 Subject: [PATCH 4/5] Added Document.adoptNode content tests --- src/test/content/test_document_adoptNode.html | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/test/content/test_document_adoptNode.html 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 @@ + + + + + + + +
+ + From 10433648d3ff289cece282b09ca29dc80680656d Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 17 Mar 2014 23:59:21 -0400 Subject: [PATCH 5/5] Added Document.importNode content tests --- .../content/test_document_importNode.html | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/test/content/test_document_importNode.html 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 @@ + + + + + + + +
+ +