Align our implementation of Range.createContextualFragment with the specification
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 24 Feb 2016 05:15:44 +0000 (05:15 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 24 Feb 2016 05:15:44 +0000 (05:15 +0000)
https://bugs.webkit.org/show_bug.cgi?id=154627

Reviewed by Ryosuke Niwa.

LayoutTests/imported/w3c:

Rebaseline as one more check is passing.

* web-platform-tests/dom/nodes/Node-contains-xhtml-expected.txt:

Source/WebCore:

Align our implementation of Range.createContextualFragment with the
specification:
- https://w3c.github.io/DOM-Parsing/#widl-Range-createContextualFragment-DocumentFragment-DOMString-fragment

In particular, if the Range's start node is a Document / DocumentFragment,
we now create a new HTMLBodyElement and use it as context element, instead
of throwing an exception.

This also aligns our behavior with Firefox and Chrome.

Tests: imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range.html
       imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range.html
       imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range.html
       imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range.html
       imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range.xhtml
       imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran.html
       imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started.html

* dom/Range.cpp:
(WebCore::Range::createContextualFragment):

LayoutTests:

Import some more layout tests from blink to improve coverage for
Range.createContextualFragment().

* imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range-expected.txt: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range.html: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range-expected.txt: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range.html: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range-expected.txt: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range.html: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range-expected.txt: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range.html: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range-expected.txt: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range.xhtml: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran-expected.txt: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran.html: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started-expected.txt: Added.
* imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started.html: Added.

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

19 files changed:
LayoutTests/ChangeLog
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range-expected.txt [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range.html [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range-expected.txt [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range.html [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range-expected.txt [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range.html [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range-expected.txt [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range.html [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range-expected.txt [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range.xhtml [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran-expected.txt [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran.html [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started-expected.txt [new file with mode: 0644]
LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started.html [new file with mode: 0644]
LayoutTests/imported/w3c/ChangeLog
LayoutTests/imported/w3c/web-platform-tests/dom/nodes/Node-contains-xhtml-expected.txt
Source/WebCore/ChangeLog
Source/WebCore/dom/Range.cpp

index 6451d34..16ae147 100644 (file)
@@ -1,3 +1,28 @@
+2016-02-23  Chris Dumez  <cdumez@apple.com>
+
+        Align our implementation of Range.createContextualFragment with the specification
+        https://bugs.webkit.org/show_bug.cgi?id=154627
+
+        Reviewed by Ryosuke Niwa.
+
+        Import some more layout tests from blink to improve coverage for
+        Range.createContextualFragment().
+
+        * imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range-expected.txt: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range.html: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range-expected.txt: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range.html: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range-expected.txt: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range.html: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range-expected.txt: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range.html: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range-expected.txt: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range.xhtml: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran-expected.txt: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran.html: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started-expected.txt: Added.
+        * imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started.html: Added.
+
 2016-02-23  Dean Jackson  <dino@apple.com>
 
         [WebGL] iOS doesn't respect the alpha:false context creation attribute
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range-expected.txt b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range-expected.txt
new file mode 100644 (file)
index 0000000..619f270
--- /dev/null
@@ -0,0 +1,3 @@
+Test of createContextualFragment from a Range whose context is an HTML document without a body. If the test succeeds you will see the word "PASS" below.
+
+PASS
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range.html b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range.html
new file mode 100644 (file)
index 0000000..009d1f0
--- /dev/null
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<p>Test of createContextualFragment from a Range whose context is an HTML document without a body. If the test succeeds you will see the word "PASS" below.</p>
+<p id="result"></p>
+<script>
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+var doc = document.implementation.createHTMLDocument();
+var div = doc.createElement('div');
+doc.replaceChild(div, doc.documentElement);
+
+var range = doc.createRange();
+var fragment = range.createContextualFragment('<p id="fragment">Inserted fragment</p>');
+div.appendChild(fragment);
+var p = doc.getElementById('fragment');
+
+var result = document.getElementById('result');
+result.textContent = (p && p.parentElement === div && p.namespaceURI === 'http://www.w3.org/1999/xhtml') ? 'PASS' : 'FAIL';
+</script>
+
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range-expected.txt b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range-expected.txt
new file mode 100644 (file)
index 0000000..0cca5eb
--- /dev/null
@@ -0,0 +1,5 @@
+Test of createContextualFragment from a Range whose context is a detached text node. If the test succeeds you will see the word "PASS" below.
+
+PASS
+
+Inserted fragment
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range.html b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range.html
new file mode 100644 (file)
index 0000000..6d033b4
--- /dev/null
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<p>Test of createContextualFragment from a Range whose context is a detached text node. If the test succeeds you will see the word "PASS" below.</p>
+<p id="result"></p>
+<script>
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+var textNode = document.createTextNode("Text node without an element parent");
+var range = document.createRange();
+range.setStart(textNode, 0);
+var fragment = range.createContextualFragment('<p id="fragment">Inserted fragment</p>');
+document.body.appendChild(fragment);
+var p = document.getElementById('fragment');
+
+var result = document.getElementById('result');
+result.textContent = (p && p.parentElement === document.body && p.namespaceURI === 'http://www.w3.org/1999/xhtml') ? 'PASS' : 'FAIL';
+</script>
+
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range-expected.txt b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range-expected.txt
new file mode 100644 (file)
index 0000000..6a07cf7
--- /dev/null
@@ -0,0 +1,5 @@
+Test of createContextualFragment from a Range whose container is a DocumentFragment. If the test succeeds you will see the word "PASS" below.
+
+PASS
+
+Inserted fragment
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range.html b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range.html
new file mode 100644 (file)
index 0000000..814f6ee
--- /dev/null
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<p>Test of createContextualFragment from a Range whose container is a DocumentFragment. If the test succeeds you will see the word "PASS" below.</p>
+<p id="result"></p>
+<script>
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+var contextFragment = document.createDocumentFragment();
+var range = document.createRange();
+range.setStart(contextFragment, 0);
+var fragment = range.createContextualFragment('<p id="fragment">Inserted fragment</p>');
+document.body.appendChild(fragment);
+var p = document.getElementById('fragment');
+
+var result = document.getElementById('result');
+result.textContent = (p && p.parentElement === document.body && p.namespaceURI === 'http://www.w3.org/1999/xhtml') ? 'PASS' : 'FAIL';
+</script>
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range-expected.txt b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range-expected.txt
new file mode 100644 (file)
index 0000000..caaa9a6
--- /dev/null
@@ -0,0 +1,5 @@
+Test of createContextualFragment from a Range whose context is a document. If the test succeeds you will see the word "PASS" below.
+
+PASS
+
+Inserted fragment
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range.html b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range.html
new file mode 100644 (file)
index 0000000..5456eba
--- /dev/null
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<p>Test of createContextualFragment from a Range whose context is a document. If the test succeeds you will see the word "PASS" below.</p>
+<p id="result"></p>
+<script>
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+var range = document.createRange();
+var fragment = range.createContextualFragment('<p id="fragment">Inserted fragment</p>');
+document.body.appendChild(fragment);
+var p = document.getElementById('fragment');
+
+var result = document.getElementById('result');
+result.textContent = (p && p.parentElement === document.body && p.namespaceURI === 'http://www.w3.org/1999/xhtml') ? 'PASS' : 'FAIL';
+</script>
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range-expected.txt b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range-expected.txt
new file mode 100644 (file)
index 0000000..f19a612
--- /dev/null
@@ -0,0 +1,5 @@
+Test of createContextualFragment from a Range whose context is an XHTML document. If the test succeeds you will see the word "PASS" below.
+
+PASS
+
+Inserted fragment
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range.xhtml b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range.xhtml
new file mode 100644 (file)
index 0000000..c8d6fb8
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+  <title>createContextualFragment from &lt;html&gt; element range in XHTML document.</title>
+</head>
+<body>
+<p>Test of createContextualFragment from a Range whose context is an XHTML document. If the test succeeds you will see the word "PASS" below.</p>
+<p id="result"></p>
+<script>
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+var range = document.createRange();
+var fragment = range.createContextualFragment('&lt;p id="frag">Inserted fragment&lt;/p>');
+document.body.appendChild(fragment);
+var p = document.getElementById('frag');
+
+var result = document.getElementById('result');
+result.textContent = (p &amp;&amp; p.parentElement === document.body &amp;&amp; p.namespaceURI === 'http://www.w3.org/1999/xhtml') ? 'PASS' : 'FAIL';
+</script>
+</body>
+</html>
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran-expected.txt b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran-expected.txt
new file mode 100644 (file)
index 0000000..18d344d
--- /dev/null
@@ -0,0 +1,3 @@
+Tests createContextualFragment does not execute scripts immediately. You should see PASS below:
+
+PASS
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran.html b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran.html
new file mode 100644 (file)
index 0000000..6bee01d
--- /dev/null
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<body>
+<p>Tests createContextualFragment does not execute scripts immediately. You should see PASS below:</p>
+<div id="test"></div>
+<script>
+var range = document.createRange();
+var node = document.getElementById("test");
+var count = 0;
+range.selectNodeContents(node);
+range.createContextualFragment("<script> count++; </script" + ">");
+node.textContent = !count ? 'PASS' : 'FAIL - expected no execution but executed ' + count + ' times';
+
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+</script>
+</body>
+</html>
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started-expected.txt b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started-expected.txt
new file mode 100644 (file)
index 0000000..2d84fd7
--- /dev/null
@@ -0,0 +1,3 @@
+Tests createContextualFragment does not mark scripts elements it parsed as already started. You should see PASS below:
+
+PASS
diff --git a/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started.html b/LayoutTests/imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started.html
new file mode 100644 (file)
index 0000000..e00944c
--- /dev/null
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<body>
+<p>Tests createContextualFragment does not mark scripts elements it parsed as already started. You should see PASS below:</p>
+<div id="test"></div>
+<script>
+var range = document.createRange();
+var node = document.getElementById("test");
+var count = 0;
+range.selectNodeContents(node);
+node.appendChild(range.createContextualFragment("<script> count++; </script" + ">"));
+node.textContent = count == 1 ? 'PASS' : 'FAIL - expected one execution but executed ' + count + ' times';
+
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+</script>
+</body>
+</html>
index efd7459..5f32114 100644 (file)
@@ -1,3 +1,14 @@
+2016-02-23  Chris Dumez  <cdumez@apple.com>
+
+        Align our implementation of Range.createContextualFragment with the specification
+        https://bugs.webkit.org/show_bug.cgi?id=154627
+
+        Reviewed by Ryosuke Niwa.
+
+        Rebaseline as one more check is passing.
+
+        * web-platform-tests/dom/nodes/Node-contains-xhtml-expected.txt:
+
 2016-02-22  Youenn Fablet  <youenn.fablet@crf.canon.fr>
 
         Binding generator should support key value iterable
index 48a1435..d90c4c9 100644 (file)
@@ -8,6 +8,6 @@ PASS document.contains
 PASS contains with a button 
 PASS contains with a text node 
 PASS contains with a processing instruction 
-FAIL contains with a document fragment NotSupportedError: DOM Exception 9
+PASS contains with a document fragment 
 PASS contaibs with another document 
 
index 48287fc..6c611b3 100644 (file)
@@ -1,3 +1,31 @@
+2016-02-23  Chris Dumez  <cdumez@apple.com>
+
+        Align our implementation of Range.createContextualFragment with the specification
+        https://bugs.webkit.org/show_bug.cgi?id=154627
+
+        Reviewed by Ryosuke Niwa.
+
+        Align our implementation of Range.createContextualFragment with the
+        specification:
+        - https://w3c.github.io/DOM-Parsing/#widl-Range-createContextualFragment-DocumentFragment-DOMString-fragment
+
+        In particular, if the Range's start node is a Document / DocumentFragment,
+        we now create a new HTMLBodyElement and use it as context element, instead
+        of throwing an exception.
+
+        This also aligns our behavior with Firefox and Chrome.
+
+        Tests: imported/blink/fast/dom/Range/create-contextual-fragment-from-bodyless-document-range.html
+               imported/blink/fast/dom/Range/create-contextual-fragment-from-detached-text-node-range.html
+               imported/blink/fast/dom/Range/create-contextual-fragment-from-document-fragment-range.html
+               imported/blink/fast/dom/Range/create-contextual-fragment-from-document-range.html
+               imported/blink/fast/dom/Range/create-contextual-fragment-from-xhtml-document-range.xhtml
+               imported/blink/fast/dom/Range/create-contextual-fragment-script-not-ran.html
+               imported/blink/fast/dom/Range/create-contextual-fragment-script-unmark-already-started.html
+
+        * dom/Range.cpp:
+        (WebCore::Range::createContextualFragment):
+
 2016-02-23  Dan Bernstein  <mitz@apple.com>
 
         [Xcode] Linker errors display mangled names, but no longer should
index a9c6ef2..94fa4e1 100644 (file)
 #include "Event.h"
 #include "Frame.h"
 #include "FrameView.h"
+#include "HTMLBodyElement.h"
+#include "HTMLDocument.h"
 #include "HTMLElement.h"
+#include "HTMLHtmlElement.h"
 #include "HTMLNames.h"
 #include "NodeTraversal.h"
 #include "NodeWithIndex.h"
@@ -919,15 +922,26 @@ String Range::text() const
     return plainText(this);
 }
 
+// https://w3c.github.io/DOM-Parsing/#widl-Range-createContextualFragment-DocumentFragment-DOMString-fragment
 RefPtr<DocumentFragment> Range::createContextualFragment(const String& markup, ExceptionCode& ec)
 {
-    Node* element = startContainer().isElementNode() ? &startContainer() : startContainer().parentNode();
-    if (!element || !element->isHTMLElement()) {
+    Node& node = startContainer();
+    RefPtr<Element> element;
+    if (is<Document>(node) || is<DocumentFragment>(node))
+        element = nullptr;
+    else if (is<Element>(node))
+        element = &downcast<Element>(node);
+    else
+        element = node.parentElement();
+
+    if (!element || (is<HTMLDocument>(element->document()) && is<HTMLHtmlElement>(*element)))
+        element = HTMLBodyElement::create(node.document());
+    else if (!is<HTMLElement>(*element)) {
         ec = NOT_SUPPORTED_ERR;
         return nullptr;
     }
 
-    return WebCore::createContextualFragment(markup, downcast<HTMLElement>(element), AllowScriptingContentAndDoNotMarkAlreadyStarted, ec);
+    return WebCore::createContextualFragment(markup, downcast<HTMLElement>(element.get()), AllowScriptingContentAndDoNotMarkAlreadyStarted, ec);
 }