DOMParser.parseFromString() should support creating HTML Document with mime-type...
authorvivek.vg@samsung.com <vivek.vg@samsung.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 12 Sep 2013 01:41:46 +0000 (01:41 +0000)
committervivek.vg@samsung.com <vivek.vg@samsung.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 12 Sep 2013 01:41:46 +0000 (01:41 +0000)
https://bugs.webkit.org/show_bug.cgi?id=120826

Reviewed by Darin Adler.

Source/WebCore:

The specification http://domparsing.spec.whatwg.org/#the-domparser-interface for DOMParser.parseFromString()
allows the document creation if the mimeType is one of the supported types:
["text/html", "text/xml", "application/xml", "application/xhtml+xml", "image/svg+xml"]

Also script elements get marked unexecutable and the contents of noscript get parsed as markup.

blink review URL: https://codereview.chromium.org/23903014/

Test: fast/dom/domparser-parsefromstring-mimetype-support.html

* xml/DOMParser.cpp:
(WebCore::DOMParser::parseFromString):

LayoutTests:

* fast/dom/domparser-parsefromstring-mimetype-support-expected.txt: Added.
* fast/dom/domparser-parsefromstring-mimetype-support.html: Added.

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

LayoutTests/ChangeLog
LayoutTests/fast/dom/domparser-parsefromstring-mimetype-support-expected.txt [new file with mode: 0644]
LayoutTests/fast/dom/domparser-parsefromstring-mimetype-support.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/xml/DOMParser.cpp
Source/WebCore/xml/DOMParser.h
Source/WebCore/xml/DOMParser.idl

index 266be7d..a404c85 100644 (file)
@@ -1,3 +1,13 @@
+2013-09-11  Vivek Galatage  <vivek.vg@samsung.com>
+
+        DOMParser.parseFromString() should support creating HTML Document with mime-type text/html
+        https://bugs.webkit.org/show_bug.cgi?id=120826
+
+        Reviewed by Darin Adler.
+
+        * fast/dom/domparser-parsefromstring-mimetype-support-expected.txt: Added.
+        * fast/dom/domparser-parsefromstring-mimetype-support.html: Added.
+
 2013-09-11  Joseph Pecoraro  <pecoraro@apple.com>
 
         Flaky Test: http/tests/inspector/inspect-element.html
diff --git a/LayoutTests/fast/dom/domparser-parsefromstring-mimetype-support-expected.txt b/LayoutTests/fast/dom/domparser-parsefromstring-mimetype-support-expected.txt
new file mode 100644 (file)
index 0000000..d5242c9
--- /dev/null
@@ -0,0 +1,37 @@
+This tests DOMParser supports creating Document for HTML content with mime-type "text/html".
+
+1. Should support mime-type = "text/html"
+HTML content:
+Scripts must be disabled for the document created using DOMParser.parseFromString()
+Sample text content
+PASS
+
+2. Should support mime-type = "text/xml"
+Root element: root
+PASS
+
+3. Should support mime-type = "application/xml"
+Root element: root
+PASS
+
+4. Should support mime-type = "application/xhtml+xml"
+Root element: html
+PASS
+
+5. Should support mime-type = "image/svg+xml"
+Root element: svg
+PASS
+
+6. Should NOT support mime-type = "text/xsl"
+PASS
+
+7. Should NOT support mime-type = "text/dummy+xml"
+PASS
+
+8. Should NOT support mime-type = "text/XML"
+PASS
+
+9. Should NOT support mime-type = "TEXT/html"
+PASS
+
+
diff --git a/LayoutTests/fast/dom/domparser-parsefromstring-mimetype-support.html b/LayoutTests/fast/dom/domparser-parsefromstring-mimetype-support.html
new file mode 100644 (file)
index 0000000..cce2f86
--- /dev/null
@@ -0,0 +1,154 @@
+<!DOCTYPE html>
+<html>
+<head>
+<style>
+.fail {
+    color: red;
+    font-weight: bold;
+}
+
+.pass {
+    color: green;
+    font-weight: bold;
+}
+</style>
+<script>
+var htmlContent =
+                "<html>" +
+                    "<head>" +
+                        "<noscript>" +
+                            "Scripts must be disabled for the document created using DOMParser.parseFromString()" +
+                        "</noscript>" +
+                    "</head>" +
+                    "<body>" +
+                        "<div id='text'>Sample text content</div>" +
+                        "<script>document.getElementById('text').textContent = 'Modified text content';<\/script>" +
+                    "</body>" +
+                "</html>";
+
+var xmlContent =
+                "<root>" +
+                "</root>";
+
+
+var xhtmlContent =
+                "<!DOCTYPE html>" +
+                "<html xmlns=\"http://www.w3.org/1999/xhtml\">" +
+                    "<head>" +
+                        "<title>Title of document</title>" +
+                        "<noscript>" +
+                            "Scripts must be disabled for the document created using DOMParser.parseFromString()" +
+                        "</noscript>" +
+                    "</head>" +
+                    "<body>" +
+                        "<div id='text'></div>" +
+                        "<script>document.getElementById('text').textContent = 'Newly added text';<\/script>" +
+                    "</body>" +
+                "</html>";
+
+var svgImageContent =
+                "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">" +
+                    "<circle cx=\"100\" cy=\"50\" r=\"40\" stroke=\"black\" stroke-width=\"2\" fill=\"red\"/>" +
+                "</svg>";
+
+var xslContent =
+                "<?xml version=\"1.0\"?>" +
+                "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
+                    "<xsl:template match=\"/\">" +
+                        "<html>" +
+                            "<head>" +
+                                "<title>XML XSL Example</title>" +
+                                "<style type=\"text/css\">" +
+                                    "body" +
+                                    "{" +
+                                        "background-color:red;" +
+                                    "}" +
+                                "</style>" +
+                            "</head>" +
+                            "<body>" +
+                                "<xsl:apply-templates/>" +
+                            "</body>" +
+                        "</html>" +
+                    "</xsl:template>" +
+                    "" +
+                    "<xsl:template match=\"tutorial\">" +
+                        "<span><xsl:value-of select=\"name\"/></span>" +
+                        "<span><xsl:value-of select=\"url\"/></span>" +
+                    "</xsl:template>" +
+                "</xsl:stylesheet>";
+
+var count = 0;
+
+function shouldSupport(content, mimeType)
+{
+    var description = document.createElement("div");
+    description.innerHTML = (++count) + ". Should support mime-type = \"" + mimeType + "\"";
+    document.body.appendChild(description);
+
+    var parser = new DOMParser();
+    try {
+        var resultDocument = parser.parseFromString(content, mimeType);
+        var content = document.createElement("div");
+        var docElement = resultDocument.documentElement;
+        if (mimeType.lastIndexOf("xml") === mimeType.length - 3)
+            content.innerHTML = "Root element: " + docElement.tagName;
+        else
+            content.innerHTML = "HTML content:<br>" + docElement.innerHTML;
+        document.body.appendChild(content);
+
+        var result = document.createElement("div");
+        result.className = "pass";
+        result.textContent = "PASS";
+        document.body.appendChild(result);
+    } catch (exception) {
+        var result = document.createElement("div");
+        result.className = "fail";
+        result.textContent = "FAIL";
+        document.body.appendChild(result);
+    }
+    document.body.appendChild(document.createElement("br"));
+}
+
+function shouldNotSupport(content, mimeType)
+{
+    var description = document.createElement("div");
+    description.innerHTML = (++count) + ". Should NOT support mime-type = \"" + mimeType + "\"";
+    document.body.appendChild(description);
+
+    var parser = new DOMParser();
+    try {
+        var resultDocument = parser.parseFromString(content, mimeType);
+        var result = document.createElement("div");
+        result.className = "fail";
+        result.textContent = "FAIL";
+        document.body.appendChild(result);
+    } catch (exception) {
+        var result = document.createElement("div");
+        result.className = "pass";
+        result.textContent = "PASS";
+        document.body.appendChild(result);
+    }
+    document.body.appendChild(document.createElement("br"));
+}
+
+function runTest()
+{
+    if (window.testRunner)
+        testRunner.dumpAsText();
+
+    shouldSupport(htmlContent, "text/html");
+    shouldSupport(xmlContent, "text/xml");
+    shouldSupport(xmlContent, "application/xml");
+    shouldSupport(xhtmlContent, "application/xhtml+xml");
+    shouldSupport(svgImageContent, "image/svg+xml");
+    shouldNotSupport(xslContent, "text/xsl");
+    shouldNotSupport(xmlContent, "text/dummy+xml");
+    shouldNotSupport(xmlContent, "text/XML");
+    shouldNotSupport(htmlContent, "TEXT/html");
+}
+</script>
+</head>
+<body onload="runTest();">
+<p>This tests DOMParser supports creating Document for HTML content with mime-type "text/html".</p>
+</body>
+</html>
index 2dbf22c..252418d 100644 (file)
@@ -1,3 +1,23 @@
+2013-09-11  Vivek Galatage  <vivek.vg@samsung.com>
+
+        DOMParser.parseFromString() should support creating HTML Document with mime-type text/html
+        https://bugs.webkit.org/show_bug.cgi?id=120826
+
+        Reviewed by Darin Adler.
+
+        The specification http://domparsing.spec.whatwg.org/#the-domparser-interface for DOMParser.parseFromString()
+        allows the document creation if the mimeType is one of the supported types:
+        ["text/html", "text/xml", "application/xml", "application/xhtml+xml", "image/svg+xml"]
+
+        Also script elements get marked unexecutable and the contents of noscript get parsed as markup.
+
+        blink review URL: https://codereview.chromium.org/23903014/
+
+        Test: fast/dom/domparser-parsefromstring-mimetype-support.html
+
+        * xml/DOMParser.cpp:
+        (WebCore::DOMParser::parseFromString):
+
 2013-09-11  Hans Muller  <hmuller@adobe.com>
 
         [CSS Shapes] Improve the performance of image valued shapes
index 49677ea..380643a 100644 (file)
 #include "DOMParser.h"
 
 #include "DOMImplementation.h"
-#include "Document.h"
+#include "ExceptionCode.h"
 #include <wtf/text/WTFString.h>
 
 namespace WebCore {
 
-PassRefPtr<Document> DOMParser::parseFromString(const String& str, const String& contentType)
+PassRefPtr<Document> DOMParser::parseFromString(const String& str, const String& contentType, ExceptionCode& ec)
 {
-    if (!DOMImplementation::isXMLMIMEType(contentType))
+    if (contentType != "text/html"
+        && contentType != "text/xml"
+        && contentType != "application/xml"
+        && contentType != "application/xhtml+xml"
+        && contentType != "image/svg+xml") {
+        ec = TypeError;
         return 0;
+    }
 
     RefPtr<Document> doc = DOMImplementation::createDocument(contentType, 0, KURL(), false);
     doc->setContent(str);
index 41329ba..91ed184 100644 (file)
@@ -24,6 +24,7 @@
 #include <wtf/RefPtr.h>
 
 namespace WebCore {
+typedef int ExceptionCode;
 
 class Document;
 
@@ -31,7 +32,7 @@ class DOMParser : public RefCounted<DOMParser> {
 public:
     static PassRefPtr<DOMParser> create() { return adoptRef(new DOMParser); }
 
-    PassRefPtr<Document> parseFromString(const String&, const String& contentType);
+    PassRefPtr<Document> parseFromString(const String&, const String& contentType, ExceptionCode&);
 
 private:
     DOMParser() { }
index c602a59..4e8ed09 100644 (file)
@@ -21,6 +21,6 @@
     Constructor,
     ImplementationLacksVTable
 ] interface DOMParser {
-    Document parseFromString([Default=Undefined] optional DOMString str, 
+    [RaisesException] Document parseFromString([Default=Undefined] optional DOMString str,
                              [Default=Undefined] optional DOMString contentType);
 };