Title: [249079] trunk
Revision
249079
Author
[email protected]
Date
2019-08-23 19:22:28 -0700 (Fri, 23 Aug 2019)

Log Message

Implement StaticRange constructor
https://bugs.webkit.org/show_bug.cgi?id=201055

Reviewed by Wenson Hsieh.

LayoutTests/imported/w3c:

Added a test from https://github.com/web-platform-tests/wpt/pull/18619
with my review comment addressed.

* web-platform-tests/dom/interfaces-expected.txt: Rebaselined.
* web-platform-tests/dom/ranges/StaticRange-constructor-expected.txt: Added.
* web-platform-tests/dom/ranges/StaticRange-constructor.html: Added.

Source/WebCore:

Added the constructor to StaticRange per https://github.com/whatwg/dom/pull/778.

Test: imported/w3c/web-platform-tests/dom/ranges/StaticRange-constructor.html

* dom/StaticRange.cpp:
(WebCore::isDocumentTypeOrAttr):
(WebCore::StaticRange::create):
* dom/StaticRange.h:
* dom/StaticRange.idl:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (249078 => 249079)


--- trunk/LayoutTests/imported/w3c/ChangeLog	2019-08-24 00:46:56 UTC (rev 249078)
+++ trunk/LayoutTests/imported/w3c/ChangeLog	2019-08-24 02:22:28 UTC (rev 249079)
@@ -1,3 +1,17 @@
+2019-08-22  Ryosuke Niwa  <[email protected]>
+
+        Implement StaticRange constructor
+        https://bugs.webkit.org/show_bug.cgi?id=201055
+
+        Reviewed by Wenson Hsieh.
+
+        Added a test from https://github.com/web-platform-tests/wpt/pull/18619
+        with my review comment addressed.
+
+        * web-platform-tests/dom/interfaces-expected.txt: Rebaselined.
+        * web-platform-tests/dom/ranges/StaticRange-constructor-expected.txt: Added.
+        * web-platform-tests/dom/ranges/StaticRange-constructor.html: Added.
+
 2019-08-21  Ryosuke Niwa  <[email protected]>
 
         Put keygen element behind a runtime flag and disable it by default

Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/dom/interfaces-expected.txt (249078 => 249079)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/dom/interfaces-expected.txt	2019-08-24 00:46:56 UTC (rev 249078)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/dom/interfaces-expected.txt	2019-08-24 02:22:28 UTC (rev 249079)
@@ -1437,7 +1437,7 @@
 FAIL AbstractRange interface: attribute endOffset assert_own_property: self does not have own property "AbstractRange" expected property "AbstractRange" missing
 FAIL AbstractRange interface: attribute collapsed assert_own_property: self does not have own property "AbstractRange" expected property "AbstractRange" missing
 FAIL StaticRange interface: existence and properties of interface object assert_own_property: should inherit from AbstractRange, but self has no such property expected property "AbstractRange" missing
-PASS StaticRange interface object length 
+FAIL StaticRange interface object length assert_equals: wrong value for StaticRange.length expected 0 but got 1
 PASS StaticRange interface object name 
 FAIL StaticRange interface: existence and properties of interface prototype object assert_own_property: should inherit from AbstractRange, but self has no such property expected property "AbstractRange" missing
 PASS StaticRange interface: existence and properties of interface prototype object's "constructor" property 

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/dom/ranges/StaticRange-constructor-expected.txt (0 => 249079)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/dom/ranges/StaticRange-constructor-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/dom/ranges/StaticRange-constructor-expected.txt	2019-08-24 02:22:28 UTC (rev 249079)
@@ -0,0 +1,19 @@
+
+PASS Construct static range with Element container 
+PASS Construct static range with Text container 
+PASS Construct static range with Element startContainer and Text endContainer 
+PASS Construct static range with Text startContainer and Element endContainer 
+PASS Construct static range with ProcessingInstruction container 
+PASS Construct static range with Comment container 
+PASS Construct static range with CDATASection container 
+PASS Construct static range with Document container 
+PASS Construct static range with DocumentFragment container 
+PASS Construct collapsed static range 
+PASS Construct inverted static range 
+PASS Construct static range with offset greater than length 
+PASS Construct static range with standalone Node container 
+PASS Construct static range with endpoints in disconnected trees 
+PASS Construct static range with endpoints in disconnected documents 
+PASS Throw on DocumentType or Attr container 
+PASS Throw on missing or invalid arguments 
+abcdefghi

Added: trunk/LayoutTests/imported/w3c/web-platform-tests/dom/ranges/StaticRange-constructor.html (0 => 249079)


--- trunk/LayoutTests/imported/w3c/web-platform-tests/dom/ranges/StaticRange-constructor.html	                        (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/dom/ranges/StaticRange-constructor.html	2019-08-24 02:22:28 UTC (rev 249079)
@@ -0,0 +1,200 @@
+<!doctype html>
+<title>StaticRange constructor test</title>
+<link rel='author' title='Sanket Joshi' href=''>
+<div id='log'></div>
+<script src=''></script>
+<script src=''></script>
+<div id='testDiv'>abc<span>def</span>ghi</div>
+<script>
+'use strict';
+
+const testDiv = document.getElementById('testDiv');
+const testTextNode = testDiv.firstChild;
+const testPINode = document.createProcessingInstruction('foo', 'abc');
+const testCommentNode =  document.createComment('abc');
+document.body.append(testPINode, testCommentNode);
+
+test(function() {
+    const staticRange = new StaticRange({startContainer: testDiv, startOffset: 1, endContainer: testDiv, endOffset: 2});
+    assert_equals(staticRange.startContainer, testDiv, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 1, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testDiv, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 2, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with Element container');
+
+test(function() {
+    const staticRange = new StaticRange({startContainer: testTextNode, startOffset: 1, endContainer: testTextNode, endOffset: 2});
+    assert_equals(staticRange.startContainer, testTextNode, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 1, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testTextNode, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 2, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with Text container');
+
+test(function() {
+    const staticRange = new StaticRange({startContainer: testDiv, startOffset: 0, endContainer: testTextNode, endOffset: 1});
+    assert_equals(staticRange.startContainer, testDiv, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 0, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testTextNode, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 1, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with Element startContainer and Text endContainer');
+
+test(function() {
+    const staticRange = new StaticRange({startContainer: testTextNode, startOffset: 0, endContainer: testDiv, endOffset: 3});
+    assert_equals(staticRange.startContainer, testTextNode, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 0, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testDiv, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 3, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with Text startContainer and Element endContainer');
+
+test(function() {
+    const staticRange = new StaticRange({startContainer: testPINode, startOffset: 1, endContainer: testPINode, endOffset: 2});
+    assert_equals(staticRange.startContainer, testPINode, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 1, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testPINode, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 2, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with ProcessingInstruction container');
+
+test(function() {
+    const staticRange = new StaticRange({startContainer: testCommentNode, startOffset: 1, endContainer: testCommentNode, endOffset: 2});
+    assert_equals(staticRange.startContainer, testCommentNode, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 1, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testCommentNode, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 2, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with Comment container');
+
+test(function() {
+    const xmlDoc = new DOMParser().parseFromString('<xml></xml>', 'application/xml');
+    const testCDATASection = xmlDoc.createCDATASection('abc');
+    const staticRange = new StaticRange({startContainer: testCDATASection, startOffset: 1, endContainer: testCDATASection, endOffset: 2});
+    assert_equals(staticRange.startContainer, testCDATASection, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 1, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testCDATASection, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 2, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with CDATASection container');
+
+test(function() {
+    const staticRange = new StaticRange({startContainer: document, startOffset: 0, endContainer: document, endOffset: 1});
+    assert_equals(staticRange.startContainer, document, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 0, 'valid startOffset');
+    assert_equals(staticRange.endContainer, document, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 1, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with Document container');
+
+test(function() {
+    const testDocFrag = document.createDocumentFragment();
+    testDocFrag.append('a','b','c');
+    const staticRange = new StaticRange({startContainer: testDocFrag, startOffset: 0, endContainer: testDocFrag, endOffset: 1});
+    assert_equals(staticRange.startContainer, testDocFrag, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 0, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testDocFrag, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 1, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with DocumentFragment container');
+
+test(function() {
+    const staticRange = new StaticRange({startContainer: testDiv, startOffset: 0, endContainer: testDiv, endOffset: 0});
+    assert_equals(staticRange.startContainer, testDiv, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 0, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testDiv, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 0, 'valid endOffset');
+    assert_true(staticRange.collapsed, 'collapsed');
+}, 'Construct collapsed static range');
+
+test(function() {
+    const staticRange = new StaticRange({startContainer: testDiv, startOffset: 1, endContainer: document.body, endOffset: 0});
+    assert_equals(staticRange.startContainer, testDiv, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 1, 'valid startOffset');
+    assert_equals(staticRange.endContainer, document.body, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 0, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct inverted static range');
+
+test(function() {
+    const staticRange = new StaticRange({startContainer: testDiv, startOffset: 0, endContainer: testDiv, endOffset: 15});
+    assert_equals(staticRange.startContainer, testDiv, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 0, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testDiv, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 15, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with offset greater than length');
+
+test(function() {
+    const testNode = document.createTextNode('abc');
+    const staticRange = new StaticRange({startContainer: testNode, startOffset: 1, endContainer: testNode, endOffset: 2});
+    assert_equals(staticRange.startContainer, testNode, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 1, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testNode, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 2, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with standalone Node container');
+
+test(function() {
+    const testRoot = document.createElement('div');
+    testRoot.append('a','b');
+    const staticRange = new StaticRange({startContainer: testDiv, startOffset: 1, endContainer: testRoot, endOffset: 2});
+    assert_equals(staticRange.startContainer, testDiv, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 1, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testRoot, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 2, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with endpoints in disconnected trees');
+
+test(function() {
+    const testDocNode = document.implementation.createDocument('about:blank', 'html', null);
+    const staticRange = new StaticRange({startContainer: document, startOffset: 0, endContainer: testDocNode.documentElement, endOffset: 0});
+    assert_equals(staticRange.startContainer, document, 'valid startContainer');
+    assert_equals(staticRange.startOffset, 0, 'valid startOffset');
+    assert_equals(staticRange.endContainer, testDocNode.documentElement, 'valid endContainer');
+    assert_equals(staticRange.endOffset, 0, 'valid endOffset');
+    assert_false(staticRange.collapsed, 'not collapsed');
+}, 'Construct static range with endpoints in disconnected documents');
+
+test(function() {
+    assert_throws('INVALID_NODE_TYPE_ERR', function() {
+        const staticRange = new StaticRange({startContainer: document.doctype, startOffset: 0, endContainer: document.doctype, endOffset: 0});
+    }, 'throw a InvalidNodeTypeError when a DocumentType is passed as a startContainer or endContainer');
+
+    assert_throws('INVALID_NODE_TYPE_ERR', function() {
+        const testAttrNode = testDiv.getAttributeNode('id');
+        const staticRange = new StaticRange({startContainer: testAttrNode, startOffset: 0, endContainer: testAttrNode, endOffset: 0});
+    }, 'throw a InvalidNodeTypeError when a Attr is passed as a startContainer or endContainer');
+}, 'Throw on DocumentType or Attr container');
+
+test(function () {
+    assert_throws({'name': 'TypeError'}, function () {
+        const staticRange = new StaticRange();
+    }, 'throw a TypeError when no argument is passed');
+
+    assert_throws({'name': 'TypeError'}, function () {
+        const staticRange = new StaticRange({startOffset: 0, endContainer: testDiv, endOffset: 0});
+    }, 'throw a TypeError when a startContainer is not passed');
+
+    assert_throws({'name': 'TypeError'}, function () {
+        const staticRange = new StaticRange({startContainer: testDiv, endContainer: testDiv, endOffset: 0});
+    }, 'throw a TypeError when a startOffset is not passed');
+
+    assert_throws({'name': 'TypeError'}, function () {
+        const staticRange = new StaticRange({startContainer: testDiv, startOffset: 0, endOffset: 0});
+    }, 'throw a TypeError when an endContainer is not passed');
+
+    assert_throws({'name': 'TypeError'}, function () {
+        const staticRange = new StaticRange({startContainer: testDiv, startOffset: 0, endContainer: testDiv});
+    }, 'throw a TypeError when an endOffset is not passed');
+
+    assert_throws({'name': 'TypeError'}, function () {
+        const staticRange = new StaticRange({startContainer: null, startOffset: 0, endContainer: testDiv, endOffset: 0});
+    }, 'throw a TypeError when a null startContainer is passed');
+
+    assert_throws({'name': 'TypeError'}, function () {
+        const staticRange = new StaticRange({startContainer: testDiv, startOffset: 0, endContainer: null, endOffset: 0});
+    }, 'throw a TypeError when a null endContainer is passed');
+}, 'Throw on missing or invalid arguments');
+</script>

Modified: trunk/Source/WebCore/ChangeLog (249078 => 249079)


--- trunk/Source/WebCore/ChangeLog	2019-08-24 00:46:56 UTC (rev 249078)
+++ trunk/Source/WebCore/ChangeLog	2019-08-24 02:22:28 UTC (rev 249079)
@@ -1,3 +1,20 @@
+2019-08-22  Ryosuke Niwa  <[email protected]>
+
+        Implement StaticRange constructor
+        https://bugs.webkit.org/show_bug.cgi?id=201055
+
+        Reviewed by Wenson Hsieh.
+
+        Added the constructor to StaticRange per https://github.com/whatwg/dom/pull/778.
+
+        Test: imported/w3c/web-platform-tests/dom/ranges/StaticRange-constructor.html
+
+        * dom/StaticRange.cpp:
+        (WebCore::isDocumentTypeOrAttr):
+        (WebCore::StaticRange::create):
+        * dom/StaticRange.h:
+        * dom/StaticRange.idl:
+
 2019-08-23  Devin Rousso  <[email protected]>
 
         Web Inspector: create additional command line api functions for other console methods

Modified: trunk/Source/WebCore/dom/StaticRange.cpp (249078 => 249079)


--- trunk/Source/WebCore/dom/StaticRange.cpp	2019-08-24 00:46:56 UTC (rev 249078)
+++ trunk/Source/WebCore/dom/StaticRange.cpp	2019-08-24 02:22:28 UTC (rev 249079)
@@ -26,6 +26,7 @@
 #include "config.h"
 #include "StaticRange.h"
 
+#include "DOMException.h"
 #include "Node.h"
 #include "Range.h"
 
@@ -51,6 +52,20 @@
     return StaticRange::create(range.startContainer(), range.startOffset(), range.endContainer(), range.endOffset());
 }
 
+static inline bool isDocumentTypeOrAttr(Node& node)
+{
+    return node.isDocumentTypeNode() || node.isAttributeNode();
+}
+
+ExceptionOr<Ref<StaticRange>> StaticRange::create(Init&& init)
+{
+    ASSERT(init.startContainer);
+    ASSERT(init.endContainer);
+    if (isDocumentTypeOrAttr(*init.startContainer) || isDocumentTypeOrAttr(*init.endContainer))
+        return Exception { InvalidNodeTypeError };
+    return StaticRange::create(init.startContainer.releaseNonNull(), init.startOffset, init.endContainer.releaseNonNull(), init.endOffset);
+}
+
 Node* StaticRange::startContainer() const
 {
     return (Node*)m_startContainer.ptr();

Modified: trunk/Source/WebCore/dom/StaticRange.h (249078 => 249079)


--- trunk/Source/WebCore/dom/StaticRange.h	2019-08-24 00:46:56 UTC (rev 249078)
+++ trunk/Source/WebCore/dom/StaticRange.h	2019-08-24 02:22:28 UTC (rev 249079)
@@ -40,6 +40,15 @@
     static Ref<StaticRange> createFromRange(const Range&);
     static Ref<StaticRange> create(Ref<Node>&& startContainer, unsigned startOffset, Ref<Node>&& endContainer, unsigned endOffset);
 
+    struct Init {
+        RefPtr<Node> startContainer;
+        unsigned long startOffset { 0 };
+        RefPtr<Node> endContainer;
+        unsigned long endOffset { 0 };
+    };
+
+    static ExceptionOr<Ref<StaticRange>> create(Init&&);
+
     unsigned startOffset() const { return m_startOffset; }
     unsigned endOffset() const { return m_endOffset; }
     Node* startContainer() const;

Modified: trunk/Source/WebCore/dom/StaticRange.idl (249078 => 249079)


--- trunk/Source/WebCore/dom/StaticRange.idl	2019-08-24 00:46:56 UTC (rev 249078)
+++ trunk/Source/WebCore/dom/StaticRange.idl	2019-08-24 02:22:28 UTC (rev 249079)
@@ -26,6 +26,9 @@
 [
     EnabledAtRuntime=InputEvents,
     ImplementationLacksVTable,
+    Exposed=Window,
+    ConstructorMayThrowException,
+    Constructor(StaticRangeInit staticRangeInitDict),
 ] interface StaticRange {
     readonly attribute unsigned long startOffset;
     readonly attribute unsigned long endOffset;
@@ -33,3 +36,10 @@
     readonly attribute Node endContainer;
     readonly attribute boolean collapsed;
 };
+
+dictionary StaticRangeInit {
+  required Node startContainer;
+  required unsigned long startOffset;
+  required Node endContainer;
+  required unsigned long endOffset;
+};
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to