Bug 148483

Summary: Range.compareBoundaryPoints() should throw a NotSupportedError for invalid compareHow values
Product: WebKit Reporter: Chris Dumez <cdumez>
Component: DOMAssignee: Chris Dumez <cdumez>
Status: RESOLVED FIXED    
Severity: Normal CC: commit-queue
Priority: P2 Keywords: WebExposed
Version: Other   
Hardware: Unspecified   
OS: Unspecified   
URL: https://dom.spec.whatwg.org/#dom-range-compareboundarypoints
Attachments:
Description Flags
Patch
none
Patch none

Description Chris Dumez 2015-08-26 11:09:50 PDT
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.

W3C test suite:
http://w3c-test.org/dom/ranges/Range-compareBoundaryPoints.html
Comment 1 Chris Dumez 2015-08-26 14:15:13 PDT
Created attachment 259981 [details]
Patch
Comment 2 Geoffrey Garen 2015-08-27 14:42:55 PDT
Comment on attachment 259981 [details]
Patch

r=me
Comment 3 Chris Dumez 2015-08-27 14:46:02 PDT
Created attachment 260091 [details]
Patch
Comment 4 Chris Dumez 2015-08-27 15:33:55 PDT
Comment on attachment 260091 [details]
Patch

Clearing flags on attachment: 260091

Committed r189062: <http://trac.webkit.org/changeset/189062>
Comment 5 Chris Dumez 2015-08-27 15:34:00 PDT
All reviewed patches have been landed.  Closing bug.
Comment 6 Darin Adler 2015-08-31 18:59:47 PDT
Comment on attachment 260091 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=260091&action=review

> LayoutTests/fast/dom/Range/compareBoundaryPoints-compareHow-exception-expected.txt:10
> +PASS range.compareBoundaryPoints(65536, sourceRange) is -1

This is the desired behavior? Why not an exception in this case?
Comment 7 Chris Dumez 2015-08-31 19:33:24 PDT
Comment on attachment 260091 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=260091&action=review

>> LayoutTests/fast/dom/Range/compareBoundaryPoints-compareHow-exception-expected.txt:10
>> +PASS range.compareBoundaryPoints(65536, sourceRange) is -1
> 
> This is the desired behavior? Why not an exception in this case?

Because 65536 wraps to 0, which is the value of the START_TO_START constant. compareHow is an unsigned short so it is in the range [0, 65535] as per:
https://heycam.github.io/webidl/#idl-unsigned-short

The unsigned short conversion is documented at:
https://heycam.github.io/webidl/#es-unsigned-short
which says to call ToUint16(x) in this case:
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint16 (which specifies the wrapping around behavior)

We would only throw a TypeError if the IDL specified [EnforceRange]:
https://heycam.github.io/webidl/#EnforceRange
Comment 8 Chris Dumez 2015-08-31 19:34:04 PDT
Comment on attachment 260091 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=260091&action=review

> LayoutTests/fast/dom/Range/compareBoundaryPoints-compareHow-exception.html:20
> +shouldBe("range.compareBoundaryPoints(65536, sourceRange)", "-1"); // 65536 should wrap around to 0.

By the way, I documented the wrapping around to 0 here already.
Comment 9 Chris Dumez 2015-08-31 19:38:00 PDT
Comment on attachment 260091 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=260091&action=review

>>> LayoutTests/fast/dom/Range/compareBoundaryPoints-compareHow-exception-expected.txt:10
>>> +PASS range.compareBoundaryPoints(65536, sourceRange) is -1
>> 
>> This is the desired behavior? Why not an exception in this case?
> 
> Because 65536 wraps to 0, which is the value of the START_TO_START constant. compareHow is an unsigned short so it is in the range [0, 65535] as per:
> https://heycam.github.io/webidl/#idl-unsigned-short
> 
> The unsigned short conversion is documented at:
> https://heycam.github.io/webidl/#es-unsigned-short
> which says to call ToUint16(x) in this case:
> http://people.mozilla.org/~jorendorff/es6-draft.html#sec-touint16 (which specifies the wrapping around behavior)
> 
> We would only throw a TypeError if the IDL specified [EnforceRange]:
> https://heycam.github.io/webidl/#EnforceRange

This layout test is also passing in the latest Firefox and Chrome.