Open tkent-google opened 7 years ago
The biggest difference between WebKit/Blink and Gecko is that the former two will update the selection inside a text field based on APIs on Selection
whereas Gecko doesn't.
<!DOCTYPE html>
<body><div></div><input value="initial"><div></div>
<script>
var sel = getSelection();
sel.removeAllRanges();
var i = document.querySelector('input');
i.setSelectionRange(1, 1);
alert('before focus: ' + sel.anchorNode + ' ' + sel.anchorOffset +
' ' + sel.focusNode + ' ' + sel.focusOffset);
i.focus();
alert('after focus: ' + sel.anchorNode + ' ' + sel.anchorOffset +
' ' + sel.focusNode + ' ' + sel.focusOffset);
</script>
</div>
Firefox | Chrome | Safari | Edge | |
---|---|---|---|---|
before focus | null 0 null 0 | null 0 null 0 | body 1 body 1 | body 1 body 1 |
after focus | body 1 body 1 | body 1 body 1 | body 1 body 1 | body 1 body 1 |
It seems a difference is only how text control selection API works without focus.
As for Selection.toString(), Chrome and Safari return selected string in a text control, and Edge and Firefox returns nothing.
I was testing the selection range with input and textarea, and there is also a difference between Blink/WebKit and Gecko. If some text in a text field or textarea is selected, the selection range in Blink and WebKit seems to be equivalent to a caret selection immediately before the input or textarea (so range.startContainer === input.parentNode
). Gecko's behavior is much stranger: it seems like if the last non-text-field selection was a range selection, it treats the new selection as empty; but if the last non-text-field selection was a caret selection, it keeps it.
Gecko's behavior is almost certainly unintentional and the result of an implementation bug, but I wonder whether Blink/WebKit's behavior is worth keeping.
The relationship between
Selection
interface and text control selections is unclear. Let's clarify, and ask for updating HTML specification if needed.