Open Bug 520221 Opened 15 years ago Updated 2 years ago

Investigate how to handle changes to range objects which are used in selection

Categories

(Core :: DOM: Selection, defect)

x86
All
defect

Tracking

()

People

(Reporter: smaug, Assigned: smaug)

Details

Attachments

(4 files, 1 obsolete file)

There are at least two options:
(1) Set a flag to those range objects which are used in a selection and make those
    range objects readonly for scripts.
(2) Add a pointer to the selection to those range objects which are used in a 
    selection and update selection whenever script changes the range.

I think (1) may break some websites, so I'd prefer (2).
It could be implemented so that at least when nsRange::DoSetRange is called,
range objects in the selection get re-ordered.
> I think (1) may break some websites

The ability to modify ranges in a selection is mozilla-specific, right?  How much is it used?
Don't know how much. But in any case, (2) might be even simpler to implement.
I think a fair number of sites modify selection, but do so using different UA-specific APIs in different UAs. Often time for modifying selection in editable areas to for example select the whole area of a text input when it gains focus.

Of course, what would be really nice is a real API to modify selection, then I'd imagine we can phase out whatever hacks work now.
Making ranges (which are in selection) to update selection properly would be,
IMHO, pretty nice and simple API. And web developers are already pretty
familiar with ranges.
Assignee: nobody → Olli.Pettay
Attached file a simple testcase
Trunk doesn't repaint the changed selection range automatically.
Attached patch a patch (obsolete) — Splinter Review
Something like this should work. nsTypedSelection::CopyRangeToAnchorFocus a bit ugly, but it is ugly even now :/
Needs lots of automated tests.
What's supposed to happen if a script adds two ranges to a selection, then modifies the ranges so they're the same? We'll reduce the selection to one range, I assume, so one of those ranges will just be removed from the selection as a side effect?

That's a bit weird but I guess it's OK.
(In reply to comment #7)
> What's supposed to happen if a script adds two ranges to a selection, then
> modifies the ranges so they're the same? We'll reduce the selection to one
> range, I assume, so one of those ranges will just be removed from the selection
> as a side effect?
Right, the patch doesn't change the how nsTypedSelection::AddItem handles 
overlaps. Adding an overlapping range may also just modify existing ranges.

There are two cases I especially want to fix
(1) Prevent problems like bug 520070.
(2) invalidation of modified ranges

Both cases are handled by the change to nsRange::DoSetRange:
(1) because adding a detached range to selection isn't allowed, (2) because
adding a valid range to selection calls nsTypedSelection::selectFrames, which
then marks frames selected.

And one not so important case is using the same range in several selections.
Better to just prevent that by removing the range from the old selection before
adding to new one. IMO.
Attached patch + some testsSplinter Review
Attachment #404439 - Attachment is obsolete: true
Attachment #404589 - Flags: review?(jonas)
jonas, ping.
Will this work even when the mutations happen in a node that is fully inside the range? Such as if a child is added to <bar> in the following tree:

<foo>|<bar>text here<span/></bar>|</foo>
Attachment #404589 - Flags: review?(jonas)
Attached patch v2Splinter Review
This handles also the 2nd testcase, but this is not the way it should be handled.
Frame construction or something should take care of marking right frames to be
selected, I think.
Comment on attachment 417990 [details] [diff] [review]
v2

>+         nsContentUtils::ContentIsDescendantOf(aChangedNode,
>+                                               aRange->GetStartParent()) ||
>+         nsContentUtils::ContentIsDescendantOf(aChangedNode,
>+                                               aRange->GetEndParent()))) {


This doesn't seem correct either. Consider:

<div><foo>text|here</foo><bar>more text</bar><foo>fi|nal</foo></div>

And the modification to the <bar> element. This element isn't a decedent of either the start or end parent.
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: