Skip to content

Commit

Permalink
auto merge of #1932 : brunoabinader/servo/document-import-adopt-node,…
Browse files Browse the repository at this point in the history
  • Loading branch information
bors-servo committed Mar 26, 2014
2 parents f34a640 + 1043364 commit 3d4e157
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/components/script/dom/bindings/codegen/Bindings.conf
Expand Up @@ -26,6 +26,7 @@ DOMInterfaces = {
'Console': {},
'Document': {
'needsAbstract': [
'adoptNode',
'anchors',
'applets',
'body',
Expand All @@ -40,6 +41,7 @@ DOMInterfaces = {
'getElementsByTagName',
'getElementsByTagNameNS',
'images',
'importNode',
'links',
'plugins',
'scripts',
Expand Down
32 changes: 32 additions & 0 deletions src/components/script/dom/document.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Document>, node: &JS<Node>, deep: bool) -> Fallible<JS<Node>> {
// 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<Document>, node: &JS<Node>) -> Fallible<JS<Node>> {
// 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<JS<Event>> {
match interface.as_slice() {
Expand Down
27 changes: 10 additions & 17 deletions src/components/script/dom/node.rs
Expand Up @@ -728,7 +728,8 @@ fn gather_abstract_nodes(cur: &JS<Node>, refs: &mut ~[JS<Node>], postorder: bool
}

/// Specifies whether children must be recursively cloned or not.
enum CloneChildrenFlag {
#[deriving(Eq)]
pub enum CloneChildrenFlag {
CloneChildren,
DoNotCloneChildren
}
Expand Down Expand Up @@ -1016,7 +1017,7 @@ impl Node {
}

// http://dom.spec.whatwg.org/#concept-node-adopt
fn adopt(node: &mut JS<Node>, document: &JS<Document>) {
pub fn adopt(node: &mut JS<Node>, document: &JS<Document>) {
// Step 1.
match node.parent_node() {
Some(ref mut parent) => Node::remove(node, parent, Unsuppressed),
Expand Down Expand Up @@ -1287,18 +1288,8 @@ impl Node {
}

// http://dom.spec.whatwg.org/#concept-node-clone
fn clone(node: &JS<Node>, maybe_doc: Option<&JS<Document>>, clone_children: CloneChildrenFlag)
-> JS<Node> {
fn clone_recursively(node: &JS<Node>, copy: &mut JS<Node>, doc: &JS<Document>) {
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<Node>, maybe_doc: Option<&JS<Document>>,
clone_children: CloneChildrenFlag) -> JS<Node> {
// Step 1.
let mut document = match maybe_doc {
Some(doc) => doc.clone(),
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions src/components/script/dom/webidls/Document.webidl
Expand Up @@ -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_);
};
Expand Down
25 changes: 25 additions & 0 deletions src/test/content/test_document_adoptNode.html
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<script src="harness.js"></script>
<script>
let foo = document.getElementById("foo");
let doc = document.implementation.createHTMLDocument("title");

is(foo.ownerDocument, document);

let adopted_foo = doc.adoptNode(foo);
is(document.getElementById("foo"), null);

is(foo, adopted_foo);
is(foo.ownerDocument, doc);
is(foo.parentNode, null);
is(foo.childNodes.length, 1);

finish();
</script>
</head>
<body>
<div id="foo"><div id="bar"></div></div>
</body>
</html>
36 changes: 36 additions & 0 deletions src/test/content/test_document_importNode.html
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<script src="harness.js"></script>
<script>
let doc = document.implementation.createHTMLDocument("title");
is_not(doc, null);

let foo = document.getElementById("foo");
is(foo.ownerDocument, document);

let imported_foo = doc.importNode(foo); // deep = false
is(imported_foo.parentNode, null);
is(imported_foo.ownerDocument, doc);
is(imported_foo.childNodes.length, 0);

imported_foo = doc.importNode(foo, true);
is(imported_foo.parentNode, null);
is(imported_foo.ownerDocument, doc);
is(imported_foo.childNodes.length, 1);

let bar = document.createElement("div");
bar.appendChild(document.createElement("div"));

imported_bar = doc.importNode(bar, true);
is(imported_bar.parentNode, null);
is(imported_bar.ownerDocument, doc);
is(imported_bar.childNodes.length, 1);

finish();
</script>
</head>
<body>
<div id="foo"><div id="bar"></div></div>
</body>
</html>

0 comments on commit 3d4e157

Please sign in to comment.