window.getSelection() fails when text selected in a form field
Categories
(Core :: DOM: Selection, defect, P3)
Tracking
()
Tracking | Status | |
---|---|---|
firefox138 | --- | fixed |
People
(Reporter: bugzilla, Assigned: sefeng211)
References
()
Details
(Keywords: spec-needed, webcompat:platform-bug)
User Story
platform-scheduled:2025-04-30
Attachments
(4 files)
Reporter | ||
Comment 1•24 years ago
|
||
![]() |
||
Comment 2•24 years ago
|
||
Comment 4•24 years ago
|
||
Comment 5•23 years ago
|
||
Comment 6•23 years ago
|
||
Reporter | ||
Comment 7•23 years ago
|
||
Comment 8•23 years ago
|
||
Comment 9•23 years ago
|
||
Comment 10•23 years ago
|
||
Comment 11•23 years ago
|
||
Comment 12•23 years ago
|
||
Comment 13•23 years ago
|
||
Comment 14•23 years ago
|
||
Comment 15•23 years ago
|
||
Comment 16•23 years ago
|
||
Comment 18•22 years ago
|
||
Comment 19•21 years ago
|
||
Comment 20•20 years ago
|
||
Comment 21•20 years ago
|
||
Updated•19 years ago
|
Comment 22•19 years ago
|
||
Comment 23•18 years ago
|
||
Updated•17 years ago
|
Comment 24•12 years ago
|
||
Comment 25•11 years ago
|
||
Comment 26•11 years ago
|
||
![]() |
||
Comment 27•11 years ago
|
||
Comment 28•11 years ago
|
||
![]() |
||
Comment 29•11 years ago
|
||
Comment 30•10 years ago
|
||
Comment 31•9 years ago
|
||
Comment 32•9 years ago
|
||
Comment 33•9 years ago
|
||
Comment 34•7 years ago
|
||
Comment 35•7 years ago
|
||
Updated•4 years ago
|
Comment 36•4 years ago
|
||
I have a function that checks on key press if pressing the key would cause the number input to go beyond maximum length specified. Because Firefox fails to return value on window.getSelection() in this case, the function thinks no text is selected, so if the input has already a value as long as maxLength, it will cancel user's input.
I have literally no way of working around this on Firefox while keeping this as a requirement.
See https://github.com/wojtekmaj/react-time-picker/issues/92
Comment 37•3 years ago
|
||
(In reply to Wojciech Maj from comment #36)
I have a function that checks on key press if pressing the key would cause the number input to go beyond maximum length specified. Because Firefox fails to return value on window.getSelection() in this case, the function thinks no text is selected, so if the input has already a value as long as maxLength, it will cancel user's input.
I have literally no way of working around this on Firefox while keeping this as a requirement.
See https://github.com/wojtekmaj/react-time-picker/issues/92
This looks like the same issue I reported here https://bugzilla.mozilla.org/show_bug.cgi?id=1527627, I have a workaround as:
document.activeElement.value.substring(
document.activeElement.selectionStart,
document.activeElement.selectionEnd
)
Comment 38•3 years ago
|
||
@daniel.lee1ibm, thanks. I have fight with this bug for 2 hours, till I understand that user use FF... and it simply do not pass any data in getSelection()...
BTW proper description under getSelection() page is only partially solution, cause every user expect that every browser behave exactly the same. So getSelection() should work in input fields, and laterrrr user can find out on https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection that there is more suitable command for it cause of... and then decide who is right... and how it should look/work.
TLDR I doesn't matter (at start) that chrome is wrong with getSelection(), it should work in FF.
Updated•3 years ago
|
Updated•3 years ago
|
Comment hidden (offtopic) |
Updated•3 years ago
|
Updated•3 years ago
|
Assignee | ||
Comment 42•2 years ago
|
||
Just a summary, this is an undefined thing in the spec, so browsers behave differently.
To properly fix this, we need to resolve https://github.com/w3c/selection-api/issues/83 which clarifies the Selection API behaviour when user selects contents inside a <input> or <textarea>. And https://github.com/w3c/selection-api/issues/7 which clarifies the Selection.toString()
API.
Updated•11 months ago
|
Updated•11 months ago
|
Assignee | ||
Updated•11 months ago
|
Assignee | ||
Comment 43•11 months ago
|
||
note: the provided test case also doesn't work in Safari.
Comment 44•8 months ago
|
||
window.getSelection()
does work with <textarea>
in Safari though. <textarea>
is what chatgpt.com in bug 1902004 is using. Any reason we shouldn't converge on that behaviour?
Comment 45•8 months ago
|
||
A reason would be that converging on broken behavior is not a satisfactory conclusion to a 23 years old bug, thank you.
Comment 46•8 months ago
|
||
alexander, do you consider Safari's behaviour more broken than Firefox's? If so, why?
Assignee | ||
Comment 47•8 months ago
|
||
I think there are a two undefined things that preventing us from making any changes.
- how to use DOM range to represent a text selection is not defined, so we don't know what we to do
- Gecko uses independent selection objects for each text controls, so the document could have selection ranges outside of the text controls, even if the text control has the focus. Then..what should
window.getSelection()
return in this case?
And changing these things are not trivial..On the other hand, we are also not adding any efforts to address those, and given we've been receiving webcompat reports, maybe it's the time to make compromises now?
I know Masayuki is aware all these and has been asked many times..Masayuki, thoughts on these things..again?
Comment 48•8 months ago
|
||
If there is a reasonable way, I don't have any reasons to block to change our code. I think that the document selection can have selection ranges even in the text controls at least internally. However, I don't know about what a11y module does.
Comment 49•8 months ago
|
||
(In reply to Jeff Muizelaar [:jrmuizel] from comment #46)
alexander, do you consider Safari's behaviour more broken than Firefox's? If so, why?
My mistake, apparently getSelection
works in input
in Safari in terms of being able to get the string that's actually selected (toString()
returns the selected string). I thought it was suggested to converge on that not working in input
, only in textarea
.
Assignee | ||
Comment 50•6 months ago
|
||
I am looking at what we can do to this bug.
Assignee | ||
Comment 51•6 months ago
|
||
Updated•5 months ago
|
Updated•5 months ago
|
Comment 52•5 months ago
|
||
Comment 54•5 months ago
|
||
Backed out for causing multiple dt failures
Failure log browser_rules_cycle-color.js & others
Failure log browser_rules_cycle-color.js
Failure log browser_changes_declaration_edit_value.js
Updated•5 months ago
|
Comment 57•5 months ago
|
||
Comment 58•5 months ago
|
||
bugherder |
Assignee | ||
Updated•5 months ago
|
Assignee | ||
Comment 60•5 months ago
|
||
I fixed the hang, should be good now.
Going to land this patch again
Updated•5 months ago
|
Comment 61•5 months ago
|
||
Wow, 24yrs old bug!
Comment 63•5 months ago
|
||
Comment 64•5 months ago
|
||
bugherder |
https://hg.mozilla.org/mozilla-central/rev/b6d5438a7c20
https://hg.mozilla.org/mozilla-central/rev/7ce33b38a71b
Comment 66•5 months ago
|
||
:sefeng anything here worth mentioning in the 138 release notes? Please nominate if so.
Updated•5 months ago
|
Assignee | ||
Comment 67•5 months ago
|
||
It's Nightly only, so I don't think we need an release note.
I've filed bug 1954979 for shipping it.
Updated•5 months ago
|
Comment 68•5 months ago
|
||
fwiw we support 'nightly-only relnotes' for 3 cycles if needed.
Assignee | ||
Comment 69•5 months ago
•
|
||
Ah okay, I didn't know that. Sure!
Release Note Request (optional, but appreciated)
[Why is this notable]:
- This is only enabled in Nightly.
- The fix here is that
window.getSelection().toString()
can now return the correct text serialization if the focus in a text control, and even if the focus is not in a text control, we may still return the text serialization of a previous focused text control determined by a heuristic. Blink and webkit has something like this for a long time, the idea is to mimic their behaviour forwindow.getSelection().toString()
specifically.
[Affects Firefox for Android]:
- Same as desktop
[Suggested wording]:
window.getSelection().toString()
now correctly returns the text serialization when text is selected in a text control.
[Links (documentation, blog post, etc)]:
Updated•5 months ago
|
Updated•5 months ago
|
Comment 70•5 months ago
|
||
Great! Thanks Sean!
What are the plans for how to get this to release? It's blocking at least two high-visibility webcompat issues (with chatgpt and gemini)
Assignee | ||
Comment 71•5 months ago
|
||
The plan was to let it being tested in Nightly for some time. Since we are at soft code freeze now, I am targeting 139
Comment 72•4 months ago
|
||
Removing the nightly release note in favor of the one from bug 1954979.
Description
•