Auto merge of #7531 - nox:template, r=Ms2ger

Implement <template>

All tests using iframes can't currently pass, same for innerHTML-related tests with <template> elements. The latter contradicts the spec, see the links below.

Apart from this, they work, AFAICT.

https://github.com/servo/html5ever/issues/164
https://www.w3.org/Bugs/Public/show_bug.cgi?id=27314

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7531)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-09-08 02:29:00 -06:00
commit 5a0be12e43
28 changed files with 268 additions and 57 deletions

View file

@ -155,6 +155,9 @@ pub struct Document {
reflow_timeout: Cell<Option<u64>>,
/// The cached first `base` element with an `href` attribute.
base_element: MutNullableHeap<JS<HTMLBaseElement>>,
/// This field is set to the document itself for inert documents.
/// https://html.spec.whatwg.org/multipage/#appropriate-template-contents-owner-document
appropriate_template_contents_owner_document: MutNullableHeap<JS<Document>>,
}
impl PartialEq for Document {
@ -1058,6 +1061,7 @@ impl Document {
current_parser: Default::default(),
reflow_timeout: Cell::new(None),
base_element: Default::default(),
appropriate_template_contents_owner_document: Default::default(),
}
}
@ -1106,6 +1110,23 @@ impl Document {
.and_then(HTMLHtmlElementCast::to_ref)
.map(Root::from_ref)
}
/// https://html.spec.whatwg.org/multipage/#appropriate-template-contents-owner-document
pub fn appropriate_template_contents_owner_document(&self) -> Root<Document> {
self.appropriate_template_contents_owner_document.or_init(|| {
let doctype = if self.is_html_document {
IsHTMLDocument::HTMLDocument
} else {
IsHTMLDocument::NonHTMLDocument
};
let new_doc = Document::new(
&*self.window(), None, doctype, None, None,
DocumentSource::NotFromParser, DocumentLoader::new(&self.loader()));
new_doc.appropriate_template_contents_owner_document.set(
Some(JS::from_ref(&*new_doc)));
new_doc
})
}
}

View file

@ -13,19 +13,18 @@ use dom::bindings::codegen::Bindings::ElementBinding;
use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods;
use dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::InheritTypes::CharacterDataCast;
use dom::bindings::codegen::InheritTypes::DocumentDerived;
use dom::bindings::codegen::InheritTypes::HTMLAnchorElementCast;
use dom::bindings::codegen::InheritTypes::TextCast;
use dom::bindings::codegen::InheritTypes::{ElementCast, ElementDerived, EventTargetCast};
use dom::bindings::codegen::InheritTypes::{CharacterDataCast, DocumentDerived, ElementCast};
use dom::bindings::codegen::InheritTypes::{ElementDerived, EventTargetCast, HTMLAnchorElementCast};
use dom::bindings::codegen::InheritTypes::{HTMLBodyElementDerived, HTMLFontElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLIFrameElementDerived, HTMLInputElementCast};
use dom::bindings::codegen::InheritTypes::{HTMLInputElementDerived, HTMLTableElementCast};
use dom::bindings::codegen::InheritTypes::{HTMLTableElementDerived, HTMLTableCellElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLTableRowElementDerived, HTMLTextAreaElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLTableSectionElementDerived, NodeCast};
use dom::bindings::codegen::InheritTypes::{HTMLTableRowElementDerived, HTMLTableSectionElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLTemplateElementCast, HTMLTextAreaElementDerived};
use dom::bindings::codegen::InheritTypes::{NodeCast, TextCast};
use dom::bindings::codegen::UnionTypes::NodeOrString;
use dom::bindings::error::Error::NoModificationAllowed;
use dom::bindings::error::Error::{InvalidCharacter, Syntax};
@ -1280,19 +1279,25 @@ impl ElementMethods for Element {
node.get_client_rect().size.height
}
// https://dvcs.w3.org/hg/innerhtml/raw-file/tip/index.html#widl-Element-innerHTML
/// https://w3c.github.io/DOM-Parsing/#widl-Element-innerHTML
fn GetInnerHTML(&self) -> Fallible<DOMString> {
//XXX TODO: XML case
self.serialize(ChildrenOnly)
}
// https://dvcs.w3.org/hg/innerhtml/raw-file/tip/index.html#widl-Element-innerHTML
/// https://w3c.github.io/DOM-Parsing/#widl-Element-innerHTML
fn SetInnerHTML(&self, value: DOMString) -> Fallible<()> {
let context_node = NodeCast::from_ref(self);
// Step 1.
let frag = try!(context_node.parse_fragment(value));
// Step 2.
Node::replace_all(Some(NodeCast::from_ref(frag.r())), context_node);
// https://github.com/w3c/DOM-Parsing/issues/1
let target = if let Some(template) = HTMLTemplateElementCast::to_ref(self) {
NodeCast::from_root(template.Content())
} else {
Root::from_ref(context_node)
};
Node::replace_all(Some(NodeCast::from_ref(&*frag)), &target);
Ok(())
}

View file

@ -2,19 +2,28 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
use dom::bindings::codegen::Bindings::HTMLTemplateElementBinding;
use dom::bindings::codegen::InheritTypes::HTMLTemplateElementDerived;
use dom::bindings::js::Root;
use dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods;
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};
use dom::node::{CloneChildrenFlag, Node, NodeTypeId, document_from_node};
use dom::virtualmethods::VirtualMethods;
use util::str::DOMString;
#[dom_struct]
pub struct HTMLTemplateElement {
htmlelement: HTMLElement,
/// https://html.spec.whatwg.org/multipage/#template-contents
contents: MutNullableHeap<JS<DocumentFragment>>,
}
impl HTMLTemplateElementDerived for EventTarget {
@ -31,7 +40,8 @@ impl HTMLTemplateElement {
document: &Document) -> HTMLTemplateElement {
HTMLTemplateElement {
htmlelement:
HTMLElement::new_inherited(HTMLElementTypeId::HTMLTemplateElement, localName, prefix, document)
HTMLElement::new_inherited(HTMLElementTypeId::HTMLTemplateElement, localName, prefix, document),
contents: MutNullableHeap::new(None),
}
}
@ -43,3 +53,47 @@ impl HTMLTemplateElement {
Node::reflect_node(box element, document, HTMLTemplateElementBinding::Wrap)
}
}
impl HTMLTemplateElementMethods for HTMLTemplateElement {
/// https://html.spec.whatwg.org/multipage/#dom-template-content
fn Content(&self) -> Root<DocumentFragment> {
self.contents.or_init(|| {
let doc = document_from_node(self);
doc.appropriate_template_contents_owner_document().CreateDocumentFragment()
})
}
}
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

@ -1415,24 +1415,19 @@ impl Node {
// https://dom.spec.whatwg.org/#concept-node-adopt
pub fn adopt(node: &Node, document: &Document) {
// Step 1.
let parent_node = node.GetParentNode();
match parent_node {
Some(ref parent) => {
Node::remove(node, parent, SuppressObserver::Unsuppressed);
}
None => (),
}
let old_doc = node.owner_doc();
// Step 2.
let node_doc = document_from_node(node);
if node_doc.r() != document {
node.remove_self();
if &*old_doc != document {
// Step 3.
for descendant in node.traverse_preorder() {
descendant.r().set_owner_doc(document);
descendant.set_owner_doc(document);
}
// Step 4.
for descendant in node.traverse_preorder() {
vtable_for(&descendant).adopting_steps(&old_doc);
}
}
// Step 3.
// If node is an element, it is _affected by a base URL change_.
}
// https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity

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;
@ -98,7 +99,14 @@ pub trait VirtualMethods {
}
}
/// https://dom.spec.whatwg.org/#concept-node-clone (step 5)
/// https://dom.spec.whatwg.org/#concept-node-adopt-ext
fn adopting_steps(&self, old_doc: &Document) {
if let Some(ref s) = self.super_type() {
s.adopting_steps(old_doc);
}
}
/// https://dom.spec.whatwg.org/#concept-node-clone-ext
fn cloning_steps(&self, copy: &Node, maybe_doc: Option<&Document>,
clone_children: CloneChildrenFlag) {
if let Some(ref s) = self.super_type() {
@ -216,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

@ -5,5 +5,5 @@
// https://www.whatwg.org/html/#htmltemplateelement
interface HTMLTemplateElement : HTMLElement {
//readonly attribute DocumentFragment content;
readonly attribute DocumentFragment content;
};

View file

@ -6,11 +6,12 @@
use document_loader::DocumentLoader;
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
use dom::bindings::codegen::Bindings::HTMLTemplateElementBinding::HTMLTemplateElementMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::InheritTypes::ProcessingInstructionCast;
use dom::bindings::codegen::InheritTypes::{CharacterDataCast, DocumentTypeCast};
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLScriptElementCast};
use dom::bindings::codegen::InheritTypes::{HTMLFormElementDerived, NodeCast};
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLFormElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLScriptElementCast, HTMLTemplateElementCast};
use dom::bindings::codegen::InheritTypes::{NodeCast, ProcessingInstructionCast};
use dom::bindings::js::{JS, Root};
use dom::bindings::js::{RootedReference};
use dom::characterdata::CharacterDataTypeId;
@ -43,12 +44,20 @@ use util::str::DOMString;
impl<'a> TreeSink for servohtmlparser::Sink {
type Handle = JS<Node>;
fn get_document(&mut self) -> JS<Node> {
let doc = self.document.root();
let node = NodeCast::from_ref(doc.r());
JS::from_ref(node)
}
fn get_template_contents(&self, target: JS<Node>) -> JS<Node> {
let target = target.root();
let template = HTMLTemplateElementCast::to_ref(&*target)
.expect("tried to get template contents of non-HTMLTemplateElement in HTML parsing");
JS::from_ref(NodeCast::from_ref(&*template.Content()))
}
fn same_node(&self, x: JS<Node>, y: JS<Node>) -> bool {
x == y
}
@ -194,7 +203,14 @@ impl<'a> Serializable for &'a Node {
try!(serializer.start_elem(name.clone(), attr_refs));
}
for handle in node.children() {
let children = if let Some(tpl) = HTMLTemplateElementCast::to_ref(node) {
// https://github.com/w3c/DOM-Parsing/issues/1
NodeCast::from_ref(&*tpl.Content()).children()
} else {
node.children()
};
for handle in children {
try!(handle.r().serialize(serializer, IncludeNode));
}