From a5cefe41d0c853ca6bbaf966d17d107f6987cda2 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Wed, 2 Sep 2015 23:50:20 +0200 Subject: [PATCH] Implement adopting and cloning steps for HTMLTemplateElement --- components/script/dom/htmltemplateelement.rs | 41 ++++++++++++++++++- components/script/dom/virtualmethods.rs | 4 ++ .../template-clone-children.html.ini | 5 --- .../templates-copy-document-owner.html.ini | 12 ------ .../definitions/template-contents.html.ini | 3 -- .../node-document-changes.html.ini | 9 ---- 6 files changed, 43 insertions(+), 31 deletions(-) delete mode 100644 tests/wpt/metadata/html/semantics/scripting-1/the-template-element/additions-to-the-steps-to-clone-a-node/template-clone-children.html.ini diff --git a/components/script/dom/htmltemplateelement.rs b/components/script/dom/htmltemplateelement.rs index e18166c8074..0a4aa7b8786 100644 --- a/components/script/dom/htmltemplateelement.rs +++ b/components/script/dom/htmltemplateelement.rs @@ -5,14 +5,17 @@ use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods; use dom::bindings::codegen::Bindings::HTMLTemplateElementBinding; use dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods; -use dom::bindings::codegen::InheritTypes::HTMLTemplateElementDerived; +use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; +use dom::bindings::codegen::InheritTypes::{HTMLElementCast, HTMLTemplateElementCast}; +use dom::bindings::codegen::InheritTypes::{HTMLTemplateElementDerived, NodeCast}; use dom::bindings::js::{JS, MutNullableHeap, Root}; use dom::document::Document; use dom::documentfragment::DocumentFragment; use dom::element::ElementTypeId; use dom::eventtarget::{EventTarget, EventTargetTypeId}; use dom::htmlelement::{HTMLElement, HTMLElementTypeId}; -use dom::node::{Node, NodeTypeId, document_from_node}; +use dom::node::{CloneChildrenFlag, Node, NodeTypeId, document_from_node}; +use dom::virtualmethods::VirtualMethods; use util::str::DOMString; #[dom_struct] @@ -60,3 +63,37 @@ impl HTMLTemplateElementMethods for HTMLTemplateElement { }) } } + +impl VirtualMethods for HTMLTemplateElement { + fn super_type(&self) -> Option<&VirtualMethods> { + Some(HTMLElementCast::from_ref(self) as &VirtualMethods) + } + + /// https://html.spec.whatwg.org/multipage/#template-adopting-steps + fn adopting_steps(&self, old_doc: &Document) { + self.super_type().unwrap().adopting_steps(old_doc); + // Step 1. + let doc = document_from_node(self).appropriate_template_contents_owner_document(); + // Step 2. + Node::adopt(NodeCast::from_ref(&*self.Content()), &doc); + } + + /// https://html.spec.whatwg.org/multipage/#the-template-element:concept-node-clone-ext + fn cloning_steps(&self, copy: &Node, maybe_doc: Option<&Document>, + clone_children: CloneChildrenFlag) { + self.super_type().unwrap().cloning_steps(copy, maybe_doc, clone_children); + if clone_children == CloneChildrenFlag::DoNotCloneChildren { + // Step 1. + return; + } + let copy = HTMLTemplateElementCast::to_ref(copy).unwrap(); + // Steps 2-3. + let copy_contents = NodeCast::from_root(copy.Content()); + let copy_contents_doc = copy_contents.owner_doc(); + for child in NodeCast::from_root(self.Content()).children() { + let copy_child = Node::clone( + &child, Some(©_contents_doc), CloneChildrenFlag::CloneChildren); + copy_contents.AppendChild(©_child).unwrap(); + } + } +} diff --git a/components/script/dom/virtualmethods.rs b/components/script/dom/virtualmethods.rs index 7afb3be4624..135670cbbc3 100644 --- a/components/script/dom/virtualmethods.rs +++ b/components/script/dom/virtualmethods.rs @@ -30,6 +30,7 @@ use dom::bindings::codegen::InheritTypes::HTMLTableCellElementCast; use dom::bindings::codegen::InheritTypes::HTMLTableElementCast; use dom::bindings::codegen::InheritTypes::HTMLTableRowElementCast; use dom::bindings::codegen::InheritTypes::HTMLTableSectionElementCast; +use dom::bindings::codegen::InheritTypes::HTMLTemplateElementCast; use dom::bindings::codegen::InheritTypes::HTMLTextAreaElementCast; use dom::bindings::codegen::InheritTypes::HTMLTitleElementCast; use dom::document::Document; @@ -223,6 +224,9 @@ pub fn vtable_for<'a>(node: &'a Node) -> &'a (VirtualMethods + 'a) { HTMLTableSectionElementCast::to_ref(node).unwrap(); element as &'a (VirtualMethods + 'a) } + NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTemplateElement)) => { + HTMLTemplateElementCast::to_ref(node).unwrap() as &'a (VirtualMethods + 'a) + } NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLTextAreaElement)) => { let element = HTMLTextAreaElementCast::to_ref(node).unwrap(); element as &'a (VirtualMethods + 'a) diff --git a/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/additions-to-the-steps-to-clone-a-node/template-clone-children.html.ini b/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/additions-to-the-steps-to-clone-a-node/template-clone-children.html.ini deleted file mode 100644 index 2901b20f25f..00000000000 --- a/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/additions-to-the-steps-to-clone-a-node/template-clone-children.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[template-clone-children.html] - type: testharness - [Clone template node. Test call to cloneNode(true)] - expected: FAIL - diff --git a/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/additions-to-the-steps-to-clone-a-node/templates-copy-document-owner.html.ini b/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/additions-to-the-steps-to-clone-a-node/templates-copy-document-owner.html.ini index 60fb06a98d4..40f9199c5df 100644 --- a/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/additions-to-the-steps-to-clone-a-node/templates-copy-document-owner.html.ini +++ b/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/additions-to-the-steps-to-clone-a-node/templates-copy-document-owner.html.ini @@ -1,17 +1,5 @@ [templates-copy-document-owner.html] type: testharness - [ownerDocument of cloned template content is set to template content owner. Test cloning with children] - expected: FAIL - - [ownerDocument of cloned template content is set to template content owner. Test cloning without children] - expected: FAIL - - [ownerDocument of cloned template content is set to template content owner. Test cloneNode() with no arguments (false by default)] - expected: FAIL - - [ownerDocument of cloned template content is set to template content owner. Test cloning nested template] - expected: FAIL - [ownerDocument of cloned template content is set to template content owner. Test loading HTML document from file] expected: FAIL diff --git a/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/definitions/template-contents.html.ini b/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/definitions/template-contents.html.ini index aa1e06b4138..f394f7448a2 100644 --- a/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/definitions/template-contents.html.ini +++ b/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/definitions/template-contents.html.ini @@ -1,8 +1,5 @@ [template-contents.html] type: testharness - [The template contents must be a DocumentFragment (nested template containing a text node)] - expected: FAIL - [The template contents must be a DocumentFragment (the empty template tag inside HTML file loaded in iframe)] expected: FAIL diff --git a/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/template-element/node-document-changes.html.ini b/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/template-element/node-document-changes.html.ini index b5f147962e2..a3bbe0e13e9 100644 --- a/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/template-element/node-document-changes.html.ini +++ b/tests/wpt/metadata/html/semantics/scripting-1/the-template-element/template-element/node-document-changes.html.ini @@ -1,14 +1,5 @@ [node-document-changes.html] type: testharness - [Changing of template element's node document. Test that ownerDocument of an empty template and its content changes] - expected: FAIL - - [Changing of template element's node document. Test that ownerDocument of a not empty template and its content changes] - expected: FAIL - - [Changing of template element's node document. Test that ownerDocument of nested template and its content changes] - expected: FAIL - [Changing of template element's node document. Test document loaded from a file] expected: FAIL