Template element should parse in XHTML just as it does in HTML
authorrafaelw@chromium.org <rafaelw@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 24 Jan 2013 02:31:35 +0000 (02:31 +0000)
committerrafaelw@chromium.org <rafaelw@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 24 Jan 2013 02:31:35 +0000 (02:31 +0000)
https://bugs.webkit.org/show_bug.cgi?id=106491

Reviewed by Ryosuke Niwa.

Source/WebCore:

https://dvcs.w3.org/hg/webcomponents/raw-file/f33622c39c5e/spec/templates/index.html#parsing-xhtml-documents.
https://dvcs.w3.org/hg/webcomponents/raw-file/f33622c39c5e/spec/templates/index.html#serializing-xhtml-documents.

This patch modifies the XML parser in two ways: (1) when nodes are created, their owner document is
the owner document of the current node, rather than the containing document, and (2) when an HTMLTemplateElement
is encountered, its content document fragment is pushed onto the stack, rather than the element itself, so that children
are appended to the template content. Also, because XSLT operates on the serialized input document, transforms consider
template contents to be descendants.

Tests: fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization.xml
       fast/xpath/xpath-template-element.html
       fast/xsl/xslt-processor-template.html
       fast/xsl/xslt-xhtml-template.xml

* xml/parser/XMLDocumentParser.cpp:
(WebCore::XMLDocumentParser::enterText):
* xml/parser/XMLDocumentParserLibxml2.cpp:
(WebCore::XMLDocumentParser::startElementNs):
(WebCore::XMLDocumentParser::processingInstruction):
(WebCore::XMLDocumentParser::cdataBlock):
(WebCore::XMLDocumentParser::comment):

LayoutTests:

* fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization-expected.txt: Added.
* fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization.xml: Added.
* fast/xpath/xpath-template-element-expected.txt: Added.
* fast/xpath/xpath-template-element.html: Added.
* fast/xsl/resources/xhtml-template.xsl: Added.
* fast/xsl/xslt-processor-template-expected.txt: Added.
* fast/xsl/xslt-processor-template.html: Added.
* fast/xsl/xslt-xhtml-template-expected.txt: Added.
* fast/xsl/xslt-xhtml-template.xml: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@140631 268f45cc-cd09-0410-ab3c-d52691b4dbfc

18 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization-expected.txt [new file with mode: 0644]
LayoutTests/fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization.xml [new file with mode: 0644]
LayoutTests/fast/xpath/xpath-template-element-expected.txt [new file with mode: 0644]
LayoutTests/fast/xpath/xpath-template-element.html [new file with mode: 0644]
LayoutTests/fast/xsl/resources/xhtml-template.xsl [new file with mode: 0644]
LayoutTests/fast/xsl/xslt-processor-template-expected.txt [new file with mode: 0644]
LayoutTests/fast/xsl/xslt-processor-template.html [new file with mode: 0644]
LayoutTests/fast/xsl/xslt-xhtml-template-expected.txt [new file with mode: 0644]
LayoutTests/fast/xsl/xslt-xhtml-template.xml [new file with mode: 0644]
LayoutTests/platform/gtk/TestExpectations
LayoutTests/platform/mac/TestExpectations
LayoutTests/platform/qt/TestExpectations
LayoutTests/platform/win/TestExpectations
LayoutTests/platform/wincairo/TestExpectations
Source/WebCore/ChangeLog
Source/WebCore/xml/parser/XMLDocumentParser.cpp
Source/WebCore/xml/parser/XMLDocumentParserLibxml2.cpp

index 9841717..ce1b5c1 100644 (file)
@@ -1,3 +1,20 @@
+2013-01-23  Rafael Weinstein  <rafaelw@chromium.org>
+
+        Template element should parse in XHTML just as it does in HTML
+        https://bugs.webkit.org/show_bug.cgi?id=106491
+
+        Reviewed by Ryosuke Niwa.
+
+        * fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization-expected.txt: Added.
+        * fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization.xml: Added.
+        * fast/xpath/xpath-template-element-expected.txt: Added.
+        * fast/xpath/xpath-template-element.html: Added.
+        * fast/xsl/resources/xhtml-template.xsl: Added.
+        * fast/xsl/xslt-processor-template-expected.txt: Added.
+        * fast/xsl/xslt-processor-template.html: Added.
+        * fast/xsl/xslt-xhtml-template-expected.txt: Added.
+        * fast/xsl/xslt-xhtml-template.xml: Added.
+
 2013-01-23  Yoshifumi Inoue  <yosin@chromium.org>
 
         Unreviewew. Rebaseline for Chromium: fast/ruby/select-ruby.html
diff --git a/LayoutTests/fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization-expected.txt b/LayoutTests/fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization-expected.txt
new file mode 100644 (file)
index 0000000..370dfb1
--- /dev/null
@@ -0,0 +1,3 @@
+This tests that content within template is lifted to the template contents when parsed as xhtml.
+The template content made it through the wormhole
+<template xmlns="http://www.w3.org/1999/xhtml"><div>Template content</div></template>
diff --git a/LayoutTests/fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization.xml b/LayoutTests/fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization.xml
new file mode 100644 (file)
index 0000000..a94e49c
--- /dev/null
@@ -0,0 +1,25 @@
+<!DOCTYPE html 
+     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xml:lang="en-us" xmlns="http://www.w3.org/1999/xhtml">
+    <body>
+        <template><div>Template content</div></template>
+        <span>This tests that content within template is lifted to the template contents when parsed as xhtml.</span>
+        <div id="output">
+        </div>
+        <div id="serialization">
+        </div>
+<script>
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+var output = document.getElementById("output");
+var template = document.querySelector("template");
+if (template.content.childNodes.length > template.childNodes.length)
+  output.textContent = "The template content made it through the wormhole\n";
+
+var serialization = document.getElementById("serialization");
+serialization.textContent = new XMLSerializer().serializeToString(template);
+</script>
+    </body>
+</html>
\ No newline at end of file
diff --git a/LayoutTests/fast/xpath/xpath-template-element-expected.txt b/LayoutTests/fast/xpath/xpath-template-element-expected.txt
new file mode 100644 (file)
index 0000000..5fdb42a
--- /dev/null
@@ -0,0 +1,8 @@
+This tests that XPath expressions do not consider (traverse into) template content
+
+A B
+PASS document.evaluate('count(//span)', test, null, XPathResult.NUMBER_TYPE, null).numberValue is 2
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/xpath/xpath-template-element.html b/LayoutTests/fast/xpath/xpath-template-element.html
new file mode 100644 (file)
index 0000000..c3c9ff5
--- /dev/null
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src="../js/resources/js-test-pre.js"></script>
+</head>
+<body>
+<p>This tests that XPath expressions do not consider (traverse into) template content</p>
+
+<div id=test>
+    <span>A</span>
+    <span>B</span>
+    <template>
+        <span>C</span>
+        <span>D</span>
+    </template>
+</div>
+
+<div id="console"></div>
+<script>
+var test = document.getElementById('test');
+var result = document.evaluate('count(//span)', test, null, XPathResult.NUMBER_TYPE, null);
+shouldBe("document.evaluate('count(//span)', test, null, XPathResult.NUMBER_TYPE, null).numberValue", "2");
+</script>
+<script src="../js/resources/js-test-post.js"></script>
+</body>
+</html>
diff --git a/LayoutTests/fast/xsl/resources/xhtml-template.xsl b/LayoutTests/fast/xsl/resources/xhtml-template.xsl
new file mode 100644 (file)
index 0000000..3460a44
--- /dev/null
@@ -0,0 +1,46 @@
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+    xmlns:xhtml="http://www.w3.org/1999/xhtml"
+    xmlns="http://www.w3.org/1999/xhtml"
+    xmlns:xs="http://www.w3.org/2001/XMLSchema"
+    exclude-result-prefixes="xhtml xsl xs">
+    <xsl:output method="xml" omit-xml-declaration="yes"/>
+    <xsl:template match="xhtml:html">
+        <html>
+            <body>
+                <template>
+                    <xsl:apply-templates select="//xhtml:body/xhtml:span" />
+                </template>
+                <xsl:apply-templates select="//xhtml:template//xhtml:span" />
+                <p>This tests that XSLT transforms can traverse into XHTML template element content when applying XSL template.
+                   If the test succeeds, the transform will have swapped the position of the body spans (A and B) with the template content spans (C and D)
+                   and replaced the spans with divs.</p>
+<script>
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+function divChildTextNodes(parent) {
+    var output = '';
+
+    for (var child = parent.firstChild; child; child = child.nextSibling) {
+        if (child.tagName == 'div') {
+            output += child.textContent;
+        }
+    }
+
+    return output;
+}
+
+var span = document.body.appendChild(document.createElement('span'));
+span.textContent = 'Body divs: ' + divChildTextNodes(document.body);
+
+span = document.body.appendChild(document.createElement('span'));
+var template = document.querySelector('template');
+span.textContent = ', Template content divs: ' + divChildTextNodes(template.content);
+</script>
+            </body>
+        </html>
+    </xsl:template>
+    <xsl:template match="xhtml:span">
+        <div><xsl:value-of select="text()" /></div>
+    </xsl:template>
+</xsl:stylesheet>
\ No newline at end of file
diff --git a/LayoutTests/fast/xsl/xslt-processor-template-expected.txt b/LayoutTests/fast/xsl/xslt-processor-template-expected.txt
new file mode 100644 (file)
index 0000000..aaf2fd9
--- /dev/null
@@ -0,0 +1,3 @@
+This tests that XSLT transforms can traverse into XHTML template element content when applying XSL template. If the test succeeds, the transform will have swapped the position of the body spans (A and B) with the template content spans (C and D) and replaced the spans with divs.
+
+Body divs: CD, Template content divs: AB
diff --git a/LayoutTests/fast/xsl/xslt-processor-template.html b/LayoutTests/fast/xsl/xslt-processor-template.html
new file mode 100644 (file)
index 0000000..55a0ddc
--- /dev/null
@@ -0,0 +1,53 @@
+<html>
+<head>
+</head>
+<body id="body">
+<p>This tests that XSLT transforms can traverse into XHTML template element content when applying XSL template.
+If the test succeeds, the transform will have swapped the position of the body spans (A and B) with the template content spans (C and D)
+and replaced the spans with divs.</p>
+<script>
+if (window.testRunner)
+       testRunner.dumpAsText();
+
+var requester = new XMLHttpRequest();
+var processor = new XSLTProcessor();
+var serializer = new XMLSerializer();
+
+function getXMLDocument(name)
+{
+       requester.open("GET", name, false);
+       requester.send(null);
+       return requester.responseXML;
+}
+
+function addStringResult(text)
+{
+       document.writeln("<span>" + text + "</span>");
+}
+
+function divChildTextNodes(parent) {
+    var output = '';
+
+    for (var child = parent.firstChild; child; child = child.nextSibling) {
+        if (child.tagName == 'div') {
+            output += child.textContent;
+        }
+    }
+
+    return output;
+}
+
+var xml = getXMLDocument("xslt-xhtml-template.xml");
+var xsl = getXMLDocument("resources/xhtml-template.xsl");
+
+processor.importStylesheet(xsl);
+
+var ownerDocument = document.implementation.createDocument("", "test", null);
+var frag = processor.transformToFragment(xml, ownerDocument);
+
+addStringResult('Body divs: ' + divChildTextNodes(frag.querySelector('body')) + 
+                ', Template content divs: ' + divChildTextNodes(frag.querySelector('template').content));
+
+</script>
+</body>
+</html>
\ No newline at end of file
diff --git a/LayoutTests/fast/xsl/xslt-xhtml-template-expected.txt b/LayoutTests/fast/xsl/xslt-xhtml-template-expected.txt
new file mode 100644 (file)
index 0000000..ee928f7
--- /dev/null
@@ -0,0 +1,5 @@
+C
+D
+This tests that XSLT transforms can traverse into XHTML template element content when applying XSL template. If the test succeeds, the transform will have swapped the position of the body spans (A and B) with the template content spans (C and D) and replaced the spans with divs.
+
+Body divs: CD, Template content divs: AB
diff --git a/LayoutTests/fast/xsl/xslt-xhtml-template.xml b/LayoutTests/fast/xsl/xslt-xhtml-template.xml
new file mode 100644 (file)
index 0000000..3d1d72b
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<?xml-stylesheet href="resources/xhtml-template.xsl" type="text/xsl"?>
+
+<!DOCTYPE html 
+     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xml:lang="en-us" xmlns="http://www.w3.org/1999/xhtml">
+    <body>
+        <span>A</span>
+        <span>B</span>
+        <template>
+            <span>C</span>
+            <span>D</span>
+        </template>
+    </body>
+</html>
\ No newline at end of file
index bf29773..d6a9f7e 100644 (file)
@@ -91,6 +91,13 @@ http/tests/xmlhttprequest/default-content-type-dashboard.html [ WontFix ]
 # ENABLE(DRAGGABLE_REGION) is disabled
 fast/css/draggable-region-parser.html [ WontFix ]
 
+# ENABLE(TEMPLATE_ELEMENT) is disabled.
+webkit.org/b/103547 fast/dom/HTMLTemplateElement [ Skip ]
+webkit.org/b/103547 html5lib/run-template.html [ Skip ]
+webkit.org/b/103547 fast/xsl/xslt-xhtml-template.xml [ Skip ]
+webkit.org/b/103547 fast/xpath/xpath-template-element.html [ Skip ]
+webkit.org/b/103547 fast/xsl/xslt-processor-template.html [ Skip ]
+
 # These test -apple- and -khtml- prefixed CSS properties, which we don't support.
 inspector/styles/vendor-prefixes.html [ WontFix ]
 fast/css/apple-prefix.html [ WontFix ]
index c0d90c7..9deff54 100644 (file)
@@ -432,6 +432,9 @@ accessibility/corresponding-control-deleted-crash.html
 # ENABLE(TEMPLATE_ELEMENT) is disabled.
 fast/dom/HTMLTemplateElement
 html5lib/run-template.html
+fast/xsl/xslt-xhtml-template.xml
+fast/xpath/xpath-template-element.html
+fast/xsl/xslt-processor-template.html
 
 # JSC does not support setIsolatedWorldSecurityOrigin (http://webkit.org/b/61540)
 http/tests/security/isolatedWorld/cross-origin-xhr.html
index 775e4c1..0b554e2 100644 (file)
@@ -195,6 +195,9 @@ fast/css/cursor-parsing-image-set.html
 # ENABLE(TEMPLATE_ELEMENT) is disabled.
 fast/dom/HTMLTemplateElement
 html5lib/run-template.html
+fast/xsl/xslt-xhtml-template.xml
+fast/xpath/xpath-template-element.html
+fast/xsl/xslt-processor-template.html
 
 # Fail until SUBPIXEL_LAYOUT is enabled
 # https://bugs.webkit.org/show_bug.cgi?id=85532
index 9191501..67b2521 100644 (file)
@@ -1537,6 +1537,9 @@ fast/dom/shadow
 # ENABLE(TEMPLATE_ELEMENT) is disabled.
 fast/dom/HTMLTemplateElement
 html5lib/run-template.html
+fast/xsl/xslt-xhtml-template.xml
+fast/xpath/xpath-template-element.html
+fast/xsl/xslt-processor-template.html
 
 # CSS Regions support not yet enabled. http://webkit.org/b/57312
 fast/regions
index 5f55e7f..2accc1c 100644 (file)
@@ -2066,6 +2066,9 @@ fast/dom/shadow
 # ENABLE(TEMPLATE_ELEMENT) is disabled.
 fast/dom/HTMLTemplateElement
 html5lib/run-template.html
+fast/xsl/xslt-xhtml-template.xml
+fast/xpath/xpath-template-element.html
+fast/xsl/xslt-processor-template.html
 
 # CSS Regions support not yet enabled. http://webkit.org/b/57312
 fast/regions
index 5cf1f1d..5ad7720 100644 (file)
@@ -1,3 +1,32 @@
+2013-01-23  Rafael Weinstein  <rafaelw@chromium.org>
+
+        Template element should parse in XHTML just as it does in HTML
+        https://bugs.webkit.org/show_bug.cgi?id=106491
+
+        Reviewed by Ryosuke Niwa.
+
+        https://dvcs.w3.org/hg/webcomponents/raw-file/f33622c39c5e/spec/templates/index.html#parsing-xhtml-documents.
+        https://dvcs.w3.org/hg/webcomponents/raw-file/f33622c39c5e/spec/templates/index.html#serializing-xhtml-documents.
+
+        This patch modifies the XML parser in two ways: (1) when nodes are created, their owner document is
+        the owner document of the current node, rather than the containing document, and (2) when an HTMLTemplateElement
+        is encountered, its content document fragment is pushed onto the stack, rather than the element itself, so that children
+        are appended to the template content. Also, because XSLT operates on the serialized input document, transforms consider
+        template contents to be descendants.
+
+        Tests: fast/dom/HTMLTemplateElement/xhtml-parsing-and-serialization.xml
+               fast/xpath/xpath-template-element.html
+               fast/xsl/xslt-processor-template.html
+               fast/xsl/xslt-xhtml-template.xml
+
+        * xml/parser/XMLDocumentParser.cpp:
+        (WebCore::XMLDocumentParser::enterText):
+        * xml/parser/XMLDocumentParserLibxml2.cpp:
+        (WebCore::XMLDocumentParser::startElementNs):
+        (WebCore::XMLDocumentParser::processingInstruction):
+        (WebCore::XMLDocumentParser::cdataBlock):
+        (WebCore::XMLDocumentParser::comment):
+
 2013-01-23  Tony Gentilcore  <tonyg@chromium.org>
 
         Teach threaded HTML parser to update InspectorInstrumentation when writing HTML
index 53b2c4b..447ab1e 100644 (file)
@@ -146,7 +146,7 @@ void XMLDocumentParser::enterText()
     ASSERT(m_bufferedText.size() == 0);
 #endif
     ASSERT(!m_leafTextNode);
-    m_leafTextNode = Text::create(document(), "");
+    m_leafTextNode = Text::create(m_currentNode->document(), "");
     m_currentNode->parserAppendChild(m_leafTextNode.get());
 }
 
index 88d23ee..c72a52d 100644 (file)
@@ -42,6 +42,7 @@
 #include "HTMLLinkElement.h"
 #include "HTMLNames.h"
 #include "HTMLStyleElement.h"
+#include "HTMLTemplateElement.h"
 #include "ProcessingInstruction.h"
 #include "ResourceError.h"
 #include "ResourceHandle.h"
@@ -789,7 +790,7 @@ void XMLDocumentParser::startElementNs(const xmlChar* xmlLocalName, const xmlCha
     m_sawFirstElement = true;
 
     QualifiedName qName(prefix, localName, uri);
-    RefPtr<Element> newElement = document()->createElement(qName, true);
+    RefPtr<Element> newElement = m_currentNode->document()->createElement(qName, true);
     if (!newElement) {
         stopParsing();
         return;
@@ -819,7 +820,15 @@ void XMLDocumentParser::startElementNs(const xmlChar* xmlLocalName, const xmlCha
 
     m_currentNode->parserAppendChild(newElement.get());
 
+#if ENABLE(TEMPLATE_ELEMENT)
+    if (newElement->hasTagName(HTMLNames::templateTag))
+        pushCurrentNode(toHTMLTemplateElement(newElement.get())->content());
+    else
+        pushCurrentNode(newElement.get());
+#else
     pushCurrentNode(newElement.get());
+#endif
+
     if (m_view && !newElement->attached())
         newElement->attach();
 
@@ -962,7 +971,7 @@ void XMLDocumentParser::processingInstruction(const xmlChar* target, const xmlCh
 
     // ### handle exceptions
     ExceptionCode ec = 0;
-    RefPtr<ProcessingInstruction> pi = document()->createProcessingInstruction(
+    RefPtr<ProcessingInstruction> pi = m_currentNode->document()->createProcessingInstruction(
         toString(target), toString(data), ec);
     if (ec)
         return;
@@ -996,7 +1005,7 @@ void XMLDocumentParser::cdataBlock(const xmlChar* s, int len)
 
     exitText();
 
-    RefPtr<CDATASection> newNode = CDATASection::create(document(), toString(s, len));
+    RefPtr<CDATASection> newNode = CDATASection::create(m_currentNode->document(), toString(s, len));
     m_currentNode->parserAppendChild(newNode.get());
     if (m_view && !newNode->attached())
         newNode->attach();
@@ -1014,7 +1023,7 @@ void XMLDocumentParser::comment(const xmlChar* s)
 
     exitText();
 
-    RefPtr<Comment> newNode = Comment::create(document(), toString(s));
+    RefPtr<Comment> newNode = Comment::create(m_currentNode->document(), toString(s));
     m_currentNode->parserAppendChild(newNode.get());
     if (m_view && !newNode->attached())
         newNode->attach();