Bug 232420 - Selection extend() should trigger exception with no ranges
Summary: Selection extend() should trigger exception with no ranges
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: Other
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-10-27 20:33 PDT by Brandon
Modified: 2021-11-11 12:48 PST (History)
8 users (show)

See Also:


Attachments
Patch (3.66 KB, patch)
2021-10-27 21:34 PDT, Brandon
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch (7.32 KB, patch)
2021-10-27 23:39 PDT, Brandon
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch (7.45 KB, patch)
2021-10-28 08:51 PDT, Brandon
cdumez: review-
Details | Formatted Diff | Diff
Patch (7.47 KB, patch)
2021-10-29 13:22 PDT, Brandon
no flags Details | Formatted Diff | Diff
Patch (8.39 KB, patch)
2021-10-29 14:03 PDT, Brandon
no flags Details | Formatted Diff | Diff
Patch (8.39 KB, patch)
2021-10-29 14:19 PDT, Brandon
no flags Details | Formatted Diff | Diff
Patch (8.37 KB, patch)
2021-10-29 15:55 PDT, Brandon
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Brandon 2021-10-27 20:33:28 PDT
The Selection extend method should thrown an exception when no ranges are present.

https://w3c.github.io/selection-api/#dom-selection-extend
Comment 1 Brandon 2021-10-27 20:38:00 PDT
<rdar://83931240>
Comment 2 Brandon 2021-10-27 21:34:37 PDT
Created attachment 442672 [details]
Patch
Comment 3 Brandon 2021-10-27 23:39:02 PDT
Created attachment 442678 [details]
Patch
Comment 4 Brandon 2021-10-28 08:51:42 PDT
Created attachment 442710 [details]
Patch
Comment 5 Darin Adler 2021-10-28 13:34:16 PDT
Comment on attachment 442710 [details]
Patch

If this is important for interoperability why does this have no coverage in WPT?
Comment 6 Chris Dumez 2021-10-28 15:16:39 PDT
Comment on attachment 442710 [details]
Patch

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

> LayoutTests/fast/dom/Range/missingRange.html:3
> +        <script src="../../../resources/js-test-pre.js"></script>

If you don't end up converting this to a WPT test, you'll need to include this instead:
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

> LayoutTests/fast/dom/Range/missingRange.html:12
> +            getSelection().extend(div1);

And here:
something like this:
```
test(function () {
    let div = document.createElement('div');
    document.body.appendChild(div);
    assert_throws_dom("InvalidStateError", () => {
        getSelection().extend(div);
    });
}, 'Test extend method failure due to missing range');
```

Then move it under LayoutTests/imported/w3c/web-platform-tests/selection/.

Then later on submit a PR for that test to https://github.com/web-platform-tests/wpt.
Comment 7 Chris Dumez 2021-10-28 15:39:37 PDT
Comment on attachment 442710 [details]
Patch

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

> LayoutTests/fast/dom/Range/missingRange-expected.txt:7
> +FAIL successfullyParsed should be true (of type boolean). Was undefined (of type undefined).

r- due to FAIL line in test output.

>> LayoutTests/fast/dom/Range/missingRange.html:12
>> +            getSelection().extend(div1);
> 
> And here:
> something like this:
> ```
> test(function () {
>     let div = document.createElement('div');
>     document.body.appendChild(div);
>     assert_throws_dom("InvalidStateError", () => {
>         getSelection().extend(div);
>     });
> }, 'Test extend method failure due to missing range');
> ```
> 
> Then move it under LayoutTests/imported/w3c/web-platform-tests/selection/.
> 
> Then later on submit a PR for that test to https://github.com/web-platform-tests/wpt.

And in the future, if writing such tests using js-test.js, please:
- Include js-test.js instead of js-test-pre.js / js-test-post.js which are legacy
- shouldThrowErrorName("getSelection().extend(div1);", "InvalidStateError") to properly PASS / FAIL based on the exception being thrown. Right now, the exception is throw and not caught and causes a FAIL line to be printed in the output.
Comment 8 Brandon 2021-10-29 13:22:26 PDT
Created attachment 442858 [details]
Patch
Comment 9 Brandon 2021-10-29 13:34:56 PDT
I replaced the layout test with a new WPT test.

From looking at the WPT extend tests they do have code that will handle a rangeCount of 0, but I am unable to find a specific test in which the exception is triggered.
Comment 10 EWS Watchlist 2021-10-29 13:36:50 PDT
This patch modifies the imported WPT tests. Please ensure that any changes on the tests (not coming from a WPT import) are exported to WPT. Please see https://trac.webkit.org/wiki/WPTExportProcess
Comment 11 Chris Dumez 2021-10-29 13:43:00 PDT
Comment on attachment 442858 [details]
Patch

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

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:2
> +<title>Selection extend() tests</title>

Please provide a more specific title

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:5
> +<body>

Please close you <body>

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:8
> +<script src=common.js></script>

What is this used for?

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:9
> +<script src=extend.js></script>

What is this used for?

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:10
> +<div id="log"></div>

Ideally, the test() would create and remove the div:
let div = document.createElement("div");
document.body.appendChild(div);

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:14
> +var selection = document.getSelection();

Should be inside the test().

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:19
> +    selection.removeAllRanges();

Which is this needed? How can there be a range already?

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:22
> +});

We normally pass a string parameter as second parameter with a test description.

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:24
> +testDiv.style.display = "none";

The test should do clean up itself. I'd recommend adding something like this inside your test():
```
this.add_cleanup(function() { div.remove() });
```
Comment 12 Brandon 2021-10-29 14:03:46 PDT
Created attachment 442859 [details]
Patch
Comment 13 Brandon 2021-10-29 14:19:13 PDT
Created attachment 442862 [details]
Patch
Comment 14 Chris Dumez 2021-10-29 14:31:21 PDT
Comment on attachment 442859 [details]
Patch

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

> LayoutTests/editing/execCommand/null_calc_primitive_value_for_css_property.html:16
> +    document.getSelection().addRange(range);

Wouldn't `document.getSelection().collapse(x, 0);` work?

> LayoutTests/editing/execCommand/transpose-backslash-with-euc.html:13
> +    getSelection().addRange(new Range());

May be better to call:
getSelection().collapse(backslashDivElement, 0);
before calling extend.

> LayoutTests/editing/inserting/insert-html-crash-02.html:16
> +    getSelection().addRange(new Range());

Similarly, would be better to call getSelection().collapse(document.body, 0) I think.

> LayoutTests/imported/w3c/ChangeLog:9
> +        contains no Range objects.

Please provide a link to the specification (https://w3c.github.io/selection-api/#dom-selection-extend) since you are aligning with the spec.

Also please mention in the change log the behavior of other browser engines. Matching the spec is good but only if other engines agree with the spec.

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:2
> +<title>Selection extend() test thrown exceptions</title>

Missing <html> tag.

> LayoutTests/imported/w3c/web-platform-tests/selection/extend-exception.html:4
> +<meta name=timeout content=long>

This is a trivial test, it doesn't need a long timeout.

> Source/WebCore/page/DOMSelection.cpp:342
> +    if (this->rangeCount() < 1)

is this-> really needed?

> Source/WebCore/page/DOMSelection.cpp:343
> +        return Exception { InvalidStateError, "The 'extend' method requires a Range object to be added to the Selection object before calling this method." };

I think I'd prefer a message like: "extend() cannot be called on an empty selection"
Comment 15 Brandon 2021-10-29 15:54:59 PDT
Comment on attachment 442859 [details]
Patch

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

>> Source/WebCore/page/DOMSelection.cpp:343
>> +        return Exception { InvalidStateError, "The 'extend' method requires a Range object to be added to the Selection object before calling this method." };
> 
> I think I'd prefer a message like: "extend() cannot be called on an empty selection"

I am good with increasing the terseness of the current message, but we should still mention the missing range object.

Other browsers make it a point to mention it too.
Comment 16 Brandon 2021-10-29 15:55:02 PDT
Comment on attachment 442859 [details]
Patch

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

>> Source/WebCore/page/DOMSelection.cpp:343
>> +        return Exception { InvalidStateError, "The 'extend' method requires a Range object to be added to the Selection object before calling this method." };
> 
> I think I'd prefer a message like: "extend() cannot be called on an empty selection"

I am good with increasing the terseness of the current message, but we should still mention the missing range object.

Other browsers make it a point to mention it too.
Comment 17 Brandon 2021-10-29 15:55:53 PDT
Created attachment 442877 [details]
Patch
Comment 18 Chris Dumez 2021-10-29 15:59:53 PDT
Comment on attachment 442877 [details]
Patch

r=me if the bots are happy
Comment 19 EWS 2021-10-30 15:58:29 PDT
Committed r285084 (243726@main): <https://commits.webkit.org/243726@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 442877 [details].