Open Bug 1861565 Opened 1 year ago Updated 1 year ago

window.getElement has incorrect anchorNode if there is a unselectable element inside selection

Categories

(Core :: DOM: Selection, defect)

Firefox 118
defect

Tracking

()

UNCONFIRMED

People

(Reporter: ninadsinhaa, Unassigned)

References

(Regression)

Details

(Keywords: regression)

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/118.0

Steps to reproduce:

I created 3 span elements, with the middle one being unselectable using CSS attribute (user-select: none). When selecting text and fetching it through window.getSelection, anchorNode should be the first element, and focusNode should be the last element.

Relevant jsfiddle: https://jsfiddle.net/AHarmlessPyro/japzmLwo/10/

Actual results:

On Firefox, the anchorNode is set to the container of the third/last element (of type document.ELEMENT_NODE), while the focusNode is set to the text of the third/last element (of type document.TEXT_NODE).

Expected results:

Regardless of whether the selection is of type document.ELEMENT_NODE or document.TEXT_NODE, the anchor node should belong to the first element, not the third/last element. This (expected) behavior is observed on chrome for example.

The Bugbug bot thinks this bug should belong to the 'Core::DOM: Selection' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.

Component: Untriaged → DOM: Selection
Product: Firefox → Core

Probably the nodes are collapsed into a single point. Let me check

Severity: -- → S3
Flags: needinfo?(sefeng)

So basically because there are non selectable nodes in between, we split the selection into two ranges to exclude the non-selectable nodes. You can do getSelection().getRangeAt(0) and getSelection().getRangeAt(1) to see the two ranges. getSelection() just returns second range.

It doesn't look like there's an easy fix here.

Flags: needinfo?(sefeng)
Keywords: regression
Regressed by: 739396
You need to log in before you can comment on or make changes to this bug.