Implement adopting and cloning steps for HTMLTemplateElement

This commit is contained in:
Anthony Ramine 2015-09-02 23:50:20 +02:00
parent 663f1d65e3
commit a5cefe41d0
6 changed files with 43 additions and 31 deletions

View file

@ -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(&copy_contents_doc), CloneChildrenFlag::CloneChildren);
copy_contents.AppendChild(&copy_child).unwrap();
}
}
}

View file

@ -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)

View file

@ -1,5 +0,0 @@
[template-clone-children.html]
type: testharness
[Clone template node. Test call to cloneNode(true)]
expected: FAIL

View file

@ -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

View file

@ -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

View file

@ -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