[w3c/selection-api] Define `Selection.rangeCount` when an input field is focused (Issue #166)

Hello!

In the process of investigating a [web compat issue](https://github.com/WebKit/WebKit/pull/16492) in WebKit that affects Quip's editor, I stumbled into the fact that `Selection.rangeCount` behaves differently across FF, Chrome and Safari when the selection is moved into a text field.

Consider the following reduced test case:

```
<input />
<script>
document.querySelector("input").focus();
document.write(`rangeCount := ${getSelection().rangeCount}`);
</script>
```

Safari 16 and Chrome will show a `rangeCount` of 1. However, Safari 17 (with Live Range Selection enabled, at time of writing) and Firefox both show a `rangeCount` of 0. However, if I adjust the test case to additionally set the DOM selection prior to focusing the input:

```
<input />
<script>
getSelection().setPosition(document.body, 0);
document.querySelector("input").focus();
document.write(`rangeCount := ${getSelection().rangeCount}`);
</script>
```

...then Firefox outputs `1` instead of `0`. The behavior in Firefox seems to be that moving focus into an input field doesn't affect selection ranges, whereas behavior in Safari 16 and Chrome is that the selection will move to the start of the containing shadow host (in this case, the `input` element). In Safari 17, with Live Range Selection enabled, we'll now expose a `rangeCount` of 0 after moving focus into a text field.

Edit: this is essentially a small piece of <https://github.com/w3c/selection-api/issues/83>, that's just about the `rangeCount`.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/w3c/selection-api/issues/166
You are receiving this because you are subscribed to this thread.

Message ID: <w3c/selection-api/issues/166@github.com>

Received on Wednesday, 9 August 2023 21:10:22 UTC