Bug 1011831 - Support <template> element in XHTML parser. r=hsivonen

This commit is contained in:
William Chen 2014-06-04 15:21:23 -07:00
Родитель 109c1ee755
Коммит b031491de9
5 изменённых файлов: 83 добавлений и 8 удалений

Просмотреть файл

@ -2597,9 +2597,10 @@ Serialize(FragmentOrElement* aRoot, bool aDescendentsOnly, nsAString& aOut)
current = current->GetParentNode();
// Template case, if we are in a template's content, then the parent
// should be the host template element.
if (current->NodeType() == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) {
// Handle template element. If the parent is a template's content,
// then adjust the parent to be the template element.
if (current != aRoot &&
current->NodeType() == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) {
DocumentFragment* frag = static_cast<DocumentFragment*>(current);
nsIContent* fragHost = frag->GetHost();
if (fragHost && nsNodeUtils::IsTemplateElement(fragHost)) {

Просмотреть файл

@ -522,12 +522,12 @@ nsDocumentEncoder::SerializeToStringIterative(nsINode* aNode,
{
nsresult rv;
nsINode* node = aNode->GetFirstChild();
nsINode* node = nsNodeUtils::GetFirstChildOfTemplateOrNode(aNode);
while (node) {
nsINode* current = node;
rv = SerializeNodeStart(current, 0, -1, aStr, current);
NS_ENSURE_SUCCESS(rv, rv);
node = current->GetFirstChild();
node = nsNodeUtils::GetFirstChildOfTemplateOrNode(current);
while (!node && current && current != aNode) {
rv = SerializeNodeEnd(current, aStr);
NS_ENSURE_SUCCESS(rv, rv);
@ -536,6 +536,17 @@ nsDocumentEncoder::SerializeToStringIterative(nsINode* aNode,
if (!node) {
// Perhaps parent node has siblings.
current = current->GetParentNode();
// Handle template element. If the parent is a template's content,
// then adjust the parent to be the template element.
if (current && current != aNode &&
current->NodeType() == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) {
DocumentFragment* frag = static_cast<DocumentFragment*>(current);
nsIContent* host = frag->GetHost();
if (host && host->IsHTML(nsGkAtoms::_template)) {
current = host;
}
}
}
}
}

Просмотреть файл

@ -60,6 +60,7 @@
#include "mozilla/dom/CDATASection.h"
#include "mozilla/dom/Comment.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/HTMLTemplateElement.h"
#include "mozilla/dom/ProcessingInstruction.h"
using namespace mozilla::dom;
@ -848,7 +849,17 @@ nsXMLContentSink::PushContent(nsIContent *aContent)
StackNode *sn = mContentStack.AppendElement();
NS_ENSURE_TRUE(sn, NS_ERROR_OUT_OF_MEMORY);
sn->mContent = aContent;
nsIContent* contentToPush = aContent;
// When an XML parser would append a node to a template element, it
// must instead append it to the template element's template contents.
if (contentToPush->IsHTML(nsGkAtoms::_template)) {
HTMLTemplateElement* templateElement =
static_cast<HTMLTemplateElement*>(contentToPush);
contentToPush = templateElement->Content();
}
sn->mContent = contentToPush;
sn->mNumFlushed = 0;
return NS_OK;
}
@ -1076,8 +1087,13 @@ nsXMLContentSink::HandleEndElement(const char16_t *aName,
nsContentUtils::SplitExpatName(aName, getter_AddRefs(debugNameSpacePrefix),
getter_AddRefs(debugTagAtom),
&debugNameSpaceID);
NS_ASSERTION(content->NodeInfo()->Equals(debugTagAtom, debugNameSpaceID),
"Wrong element being closed");
// Check if we are closing a template element because template
// elements do not get pushed on the stack, the template
// element content is pushed instead.
bool isTemplateElement = debugTagAtom == nsGkAtoms::_template &&
debugNameSpaceID == kNameSpaceID_XHTML;
NS_ASSERTION(content->NodeInfo()->Equals(debugTagAtom, debugNameSpaceID) ||
isTemplateElement, "Wrong element being closed");
#endif
result = CloseElement(content);

Просмотреть файл

@ -17,6 +17,7 @@ support-files =
[test_document_register_stack.html]
[test_document_shared_registry.html]
[test_template.html]
[test_template_xhtml.html]
[test_shadowroot.html]
[test_shadowroot_inert_element.html]
[test_shadowroot_style.html]

Просмотреть файл

@ -0,0 +1,46 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1011831
-->
<head>
<title>Test for template element</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1011831">Bug 1011831</a>
<script>
var docSrc =
'<!DOCTYPE html>' +
'<html xmlns="http://www.w3.org/1999/xhtml">' +
'<body>' +
'<template id="t">Content<span>Content</span></template>' +
'<div id="container"><template>One</template><div>Two</div></div>' +
'<template id="t2"></template>' +
'</body>' +
'</html>';
var doc = (new DOMParser()).parseFromString(docSrc, 'application/xhtml+xml');
var t = doc.getElementById("t");
is(t.childNodes.length, 0, "Template should have no children.");
is(t.content.childNodes.length, 2, "Template content should have two children, text node and a span.");
// Test serialization of template element.
is(t.innerHTML, 'Content<span xmlns="http://www.w3.org/1999/xhtml">Content</span>', "Template contents should be serialized.");
is(t.outerHTML, '<template xmlns="http://www.w3.org/1999/xhtml" id="t">Content<span>Content</span></template>', "Template contents should be serialized.");
var c = doc.getElementById("container");
is(c.innerHTML, '<template xmlns="http://www.w3.org/1999/xhtml">One</template><div xmlns="http://www.w3.org/1999/xhtml">Two</div>', "Template contents should be serialized.");
is(c.outerHTML, '<div xmlns="http://www.w3.org/1999/xhtml" id="container"><template>One</template><div>Two</div></div>', "Template contents should be serialized.");
// Test setting innerHTML on template element.
var t2 = doc.getElementById("t2");
t2.innerHTML = 'Three<span>Four</span>';
is(t2.childNodes.length, 0, "Setting innerHTML should append children into template content.");
is(t2.content.childNodes.length, 2, "Setting innerHTML should append children into template content.");
</script>
</body>
</html>