Range.compareBoundaryPoints() should throw a NotSupportedError for invalid compareHow...
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 27 Aug 2015 22:33:40 +0000 (22:33 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 27 Aug 2015 22:33:40 +0000 (22:33 +0000)
https://bugs.webkit.org/show_bug.cgi?id=148483

Reviewed by Geoffrey Garen.

Source/WebCore:

Range.compareBoundaryPoints() should throw a NotSupportedError for
invalid compareHow values:
https://dom.spec.whatwg.org/#dom-range-compareboundarypoints (step 1)

Firefox and Chrome conform to the specification already.

Previously, WebKit would not throw and simply cast the value to our
internal CompareHow enum type. This patch aligns WebKit's behavior with
the DOM specificaiton and other browsers.

W3C test suite:
http://w3c-test.org/dom/ranges/Range-compareBoundaryPoints.html

Test: fast/dom/Range/compareBoundaryPoints-compareHow-exception.html

* bindings/scripts/CodeGenerator.pm:
* bindings/scripts/CodeGeneratorGObject.pm:
* bindings/scripts/CodeGeneratorJS.pm:
* bindings/scripts/CodeGeneratorObjC.pm:
Drop CompareHow special casing from bindings generator as we now use
unsigned short instead in the IDL.

* dom/Range.cpp:
(WebCore::Range::compareBoundaryPointsForBindings):
* dom/Range.h:
* dom/Range.idl:
Use "unsigned short" type instead of WebCore's CompareHow for the
parameter, as per the specification. On implementation side, we
now validate the compareHow value before casting it to a CompareHow
enum type. If the value is not value, we throw and abort early.

LayoutTests:

Add new layout test to confirm that Range.compareBoundaryPoints() throws
when passed in invalid compareHow values.

* fast/dom/Range/compareBoundaryPoints-compareHow-exception-expected.txt: Added.
* fast/dom/Range/compareBoundaryPoints-compareHow-exception.html: Added.

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

LayoutTests/ChangeLog
LayoutTests/fast/dom/Range/compareBoundaryPoints-compareHow-exception-expected.txt [new file with mode: 0644]
LayoutTests/fast/dom/Range/compareBoundaryPoints-compareHow-exception.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/bindings/scripts/CodeGenerator.pm
Source/WebCore/bindings/scripts/CodeGeneratorGObject.pm
Source/WebCore/bindings/scripts/CodeGeneratorJS.pm
Source/WebCore/bindings/scripts/CodeGeneratorObjC.pm
Source/WebCore/dom/Range.cpp
Source/WebCore/dom/Range.h
Source/WebCore/dom/Range.idl

index db80d56..ffc515b 100644 (file)
@@ -1,3 +1,16 @@
+2015-08-27  Chris Dumez  <cdumez@apple.com>
+
+        Range.compareBoundaryPoints() should throw a NotSupportedError for invalid compareHow values
+        https://bugs.webkit.org/show_bug.cgi?id=148483
+
+        Reviewed by Geoffrey Garen.
+
+        Add new layout test to confirm that Range.compareBoundaryPoints() throws
+        when passed in invalid compareHow values.
+
+        * fast/dom/Range/compareBoundaryPoints-compareHow-exception-expected.txt: Added.
+        * fast/dom/Range/compareBoundaryPoints-compareHow-exception.html: Added.
+
 2015-08-27  Joseph Pecoraro  <pecoraro@apple.com>
 
         Page does not update when <link> media attribute changes to no longer apply to page
diff --git a/LayoutTests/fast/dom/Range/compareBoundaryPoints-compareHow-exception-expected.txt b/LayoutTests/fast/dom/Range/compareBoundaryPoints-compareHow-exception-expected.txt
new file mode 100644 (file)
index 0000000..ffe690b
--- /dev/null
@@ -0,0 +1,18 @@
+Checks that Range.compareBoundaryPoints() throw if the compareHow parameter is invalid
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS range.compareBoundaryPoints(Range.START_TO_START, sourceRange) is -1
+PASS range.compareBoundaryPoints(Range.START_TO_END, sourceRange) is -1
+PASS range.compareBoundaryPoints(Range.END_TO_END, sourceRange) is -1
+PASS range.compareBoundaryPoints(Range.END_TO_START, sourceRange) is -1
+PASS range.compareBoundaryPoints(65536, sourceRange) is -1
+PASS range.compareBoundaryPoints(-1, sourceRange) threw exception Error: NotSupportedError: DOM Exception 9.
+PASS range.compareBoundaryPoints(4, sourceRange) threw exception Error: NotSupportedError: DOM Exception 9.
+PASS range.compareBoundaryPoints(100, sourceRange) threw exception Error: NotSupportedError: DOM Exception 9.
+PASS range.compareBoundaryPoints(65535, sourceRange) threw exception Error: NotSupportedError: DOM Exception 9.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/dom/Range/compareBoundaryPoints-compareHow-exception.html b/LayoutTests/fast/dom/Range/compareBoundaryPoints-compareHow-exception.html
new file mode 100644 (file)
index 0000000..60c8e86
--- /dev/null
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html>
+<body>
+<div></div>
+<div></div>
+<script src="../../../resources/js-test-pre.js"></script>
+<script>
+description("Checks that Range.compareBoundaryPoints() throw if the compareHow parameter is invalid");
+
+var range = document.createRange();
+range.selectNode(document.getElementsByTagName("div")[0]);
+var sourceRange = document.createRange();
+sourceRange.selectNode(document.getElementsByTagName("div")[1]);
+
+// Valid values.
+shouldBe("range.compareBoundaryPoints(Range.START_TO_START, sourceRange)", "-1");
+shouldBe("range.compareBoundaryPoints(Range.START_TO_END, sourceRange)", "-1");
+shouldBe("range.compareBoundaryPoints(Range.END_TO_END, sourceRange)", "-1");
+shouldBe("range.compareBoundaryPoints(Range.END_TO_START, sourceRange)", "-1");
+shouldBe("range.compareBoundaryPoints(65536, sourceRange)", "-1"); // 65536 should wrap around to 0.
+
+// Invalid values.
+shouldThrow("range.compareBoundaryPoints(-1, sourceRange)", "'Error: NotSupportedError: DOM Exception 9'");
+shouldThrow("range.compareBoundaryPoints(4, sourceRange)", "'Error: NotSupportedError: DOM Exception 9'");
+shouldThrow("range.compareBoundaryPoints(100, sourceRange)", "'Error: NotSupportedError: DOM Exception 9'");
+shouldThrow("range.compareBoundaryPoints(65535, sourceRange)", "'Error: NotSupportedError: DOM Exception 9'");
+</script>
+<script src="../../../resources/js-test-post.js"></script>
+</body>
+</html>
index ca624ff..00eda5b 100644 (file)
@@ -1,3 +1,41 @@
+2015-08-27  Chris Dumez  <cdumez@apple.com>
+
+        Range.compareBoundaryPoints() should throw a NotSupportedError for invalid compareHow values
+        https://bugs.webkit.org/show_bug.cgi?id=148483
+
+        Reviewed by Geoffrey Garen.
+
+        Range.compareBoundaryPoints() should throw a NotSupportedError for
+        invalid compareHow values:
+        https://dom.spec.whatwg.org/#dom-range-compareboundarypoints (step 1)
+
+        Firefox and Chrome conform to the specification already.
+
+        Previously, WebKit would not throw and simply cast the value to our
+        internal CompareHow enum type. This patch aligns WebKit's behavior with
+        the DOM specificaiton and other browsers.
+
+        W3C test suite:
+        http://w3c-test.org/dom/ranges/Range-compareBoundaryPoints.html
+
+        Test: fast/dom/Range/compareBoundaryPoints-compareHow-exception.html
+
+        * bindings/scripts/CodeGenerator.pm:
+        * bindings/scripts/CodeGeneratorGObject.pm:
+        * bindings/scripts/CodeGeneratorJS.pm:
+        * bindings/scripts/CodeGeneratorObjC.pm:
+        Drop CompareHow special casing from bindings generator as we now use
+        unsigned short instead in the IDL.
+
+        * dom/Range.cpp:
+        (WebCore::Range::compareBoundaryPointsForBindings):
+        * dom/Range.h:
+        * dom/Range.idl:
+        Use "unsigned short" type instead of WebCore's CompareHow for the
+        parameter, as per the specification. On implementation side, we
+        now validate the compareHow value before casting it to a CompareHow
+        enum type. If the value is not value, we throw and abort early.
+
 2015-08-27  Joseph Pecoraro  <pecoraro@apple.com>
 
         Page does not update when <link> media attribute changes to no longer apply to page
index 8e18585..910a927 100644 (file)
@@ -57,14 +57,13 @@ my %stringTypeHash = ("DOMString" => 1, "AtomicString" => 1);
 
 # WebCore types used directly in IDL files.
 my %webCoreTypeHash = (
-    "CompareHow" => 1,
     "SerializedScriptValue" => 1,
     "Dictionary" => 1
 );
 
 my %enumTypeHash = ();
 
-my %nonPointerTypeHash = ("DOMTimeStamp" => 1, "CompareHow" => 1);
+my %nonPointerTypeHash = ("DOMTimeStamp" => 1);
 
 my %svgAttributesInHTMLHash = ("class" => 1, "id" => 1, "onabort" => 1, "onclick" => 1,
                                "onerror" => 1, "onload" => 1, "onmousedown" => 1,
index ad8c9ab..d6931c7 100644 (file)
@@ -411,7 +411,6 @@ sub GetGlibTypeName {
 
     my %types = ("DOMString", "gchar*",
                  "DOMTimeStamp", "guint32",
-                 "CompareHow", "gushort",
                  "SerializedScriptValue", "gchar*",
                  "float", "gfloat",
                  "unrestricted float", "gfloat",
@@ -1066,7 +1065,7 @@ sub GenerateFunction {
                 $implIncludes{"WebKitDOM${paramIDLType}Private.h"} = 1;
             }
         }
-        if ($paramIsGDOMType || ($paramIDLType eq "DOMString") || ($paramIDLType eq "CompareHow")) {
+        if ($paramIsGDOMType || ($paramIDLType eq "DOMString")) {
             $paramName = "converted" . $codeGenerator->WK_ucfirst($paramName);
         }
         if ($paramIDLType eq "NodeFilter" || $paramIDLType eq "XPathNSResolver") {
@@ -1188,8 +1187,6 @@ sub GenerateFunction {
         $convertedParamName = "converted" . $codeGenerator->WK_ucfirst($paramName);
         if ($paramIDLType eq "DOMString") {
             push(@cBody, "    WTF::String ${convertedParamName} = WTF::String::fromUTF8($paramName);\n");
-        } elsif ($paramIDLType eq "CompareHow") {
-            push(@cBody, "    WebCore::Range::CompareHow ${convertedParamName} = static_cast<WebCore::Range::CompareHow>($paramName);\n");
         } elsif ($paramIDLType eq "NodeFilter" || $paramIDLType eq "XPathNSResolver") {
             push(@cBody, "    RefPtr<WebCore::$paramIDLType> ${convertedParamName} = WebKit::core($paramName);\n");
         } elsif ($paramIsGDOMType) {
index 059ca8b..a2b3484 100644 (file)
@@ -3740,7 +3740,6 @@ sub GetNativeTypeFromSignature
 }
 
 my %nativeType = (
-    "CompareHow" => "Range::CompareHow",
     "DOMString" => "String",
     "NodeFilter" => "RefPtr<NodeFilter>",
     "SerializedScriptValue" => "RefPtr<SerializedScriptValue>",
@@ -3880,7 +3879,6 @@ sub JSValueToNative
     return "toUInt64(exec, $value, $intConversion)" if $type eq "unsigned long long";
 
     return "valueToDate(exec, $value)" if $type eq "Date";
-    return "static_cast<Range::CompareHow>($value.toInt32(exec))" if $type eq "CompareHow";
 
     if ($type eq "DOMString") {
         # FIXME: This implements [TreatNullAs=NullString] and [TreatUndefinedAs=NullString],
index 2c8c61e..c5b8779 100644 (file)
@@ -577,7 +577,6 @@ sub GetObjCType
     return "float" if $type eq "unrestricted float";
     return "id <$name>" if IsProtocolType($type);
     return $name if $codeGenerator->IsPrimitiveType($type) or $type eq "DOMTimeStamp";
-    return "unsigned short" if $type eq "CompareHow";
     return $name if IsCoreFoundationType($name);
     return "$name *";
 }
@@ -594,7 +593,7 @@ sub GetPropertyAttributes
     # FIXME: <rdar://problem/5049934> Consider using 'nonatomic' on the DOM @property declarations.
     if ($codeGenerator->IsStringType($type) || IsNativeObjCType($type)) {
         push(@attributes, "copy");
-    } elsif (!$codeGenerator->IsStringType($type) && !$codeGenerator->IsPrimitiveType($type) && $type ne "DOMTimeStamp" && $type ne "CompareHow") {
+    } elsif (!$codeGenerator->IsStringType($type) && !$codeGenerator->IsPrimitiveType($type) && $type ne "DOMTimeStamp") {
         push(@attributes, "strong");
     }
 
@@ -616,7 +615,6 @@ sub GetObjCTypeGetter
 
     return $argName if $codeGenerator->IsPrimitiveType($type) or $codeGenerator->IsStringType($type) or IsNativeObjCType($type);
     return $argName . "Node" if $type eq "EventTarget";
-    return "static_cast<WebCore::Range::CompareHow>($argName)" if $type eq "CompareHow";
     return "WTF::getPtr(nativeEventListener)" if $type eq "EventListener";
     return "WTF::getPtr(nativeNodeFilter)" if $type eq "NodeFilter";
     return "WTF::getPtr(nativeResolver)" if $type eq "XPathNSResolver";
index 9ba4814..164a655 100644 (file)
@@ -462,6 +462,15 @@ short Range::compareBoundaryPoints(CompareHow how, const Range* sourceRange, Exc
     return 0;
 }
 
+short Range::compareBoundaryPointsForBindings(unsigned short compareHow, const Range* sourceRange, ExceptionCode& ec) const
+{
+    if (compareHow > END_TO_START) {
+        ec = NOT_SUPPORTED_ERR;
+        return 0;
+    }
+    return compareBoundaryPoints(static_cast<CompareHow>(compareHow), sourceRange, ec);
+}
+
 short Range::compareBoundaryPoints(Node* containerA, int offsetA, Node* containerB, int offsetB, ExceptionCode& ec)
 {
     ASSERT(containerA);
index 08bccff..2d9e102 100644 (file)
@@ -80,6 +80,7 @@ public:
     CompareResults compareNode(Node* refNode, ExceptionCode&) const;
     enum CompareHow { START_TO_START, START_TO_END, END_TO_END, END_TO_START };
     WEBCORE_EXPORT short compareBoundaryPoints(CompareHow, const Range* sourceRange, ExceptionCode&) const;
+    short compareBoundaryPointsForBindings(unsigned short compareHow, const Range* sourceRange, ExceptionCode&) const;
     static short compareBoundaryPoints(Node* containerA, int offsetA, Node* containerB, int offsetB, ExceptionCode&);
     static short compareBoundaryPoints(const RangeBoundaryPoint& boundaryA, const RangeBoundaryPoint& boundaryB, ExceptionCode&);
     WEBCORE_EXPORT bool boundaryPointsValid() const;
index cfed94f..73e86a9 100644 (file)
@@ -50,7 +50,7 @@
     const unsigned short END_TO_END     = 2;
     const unsigned short END_TO_START   = 3;
 
-    [ObjCLegacyUnnamedParameters, RaisesException] short compareBoundaryPoints([Default=Undefined] optional CompareHow how,
+    [ObjCLegacyUnnamedParameters, RaisesException, ImplementedAs=compareBoundaryPointsForBindings] short compareBoundaryPoints([Default=Undefined] optional unsigned short how,
                                                [Default=Undefined] optional Range sourceRange);
 
     [RaisesException] void deleteContents();