Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DOM selections #7492

Open
jdm opened this issue Sep 1, 2015 · 13 comments
Open

Implement DOM selections #7492

jdm opened this issue Sep 1, 2015 · 13 comments

Comments

@jdm
Copy link
Member

@jdm jdm commented Sep 1, 2015

I propose that we store the bounds of our selection in Document using a starting node, an ending node, a starting offset, and an ending offset. Additionally, we should store a vector of these to enable multiple simultaneous selections in the future, and send all of this data along with each script-initiated reflow request. When a node is part of a selection, it should have a flag set on it (c.f. NodeFlags); when a node with this flag is encountered, the layout code should check whether it's the starting or ending node; if it is not, we can go ahead and style the node entirely as selected. If it is, we need to deal with styling the node content in up to three separate parts (pre-selection, in selection, post-selection).

cc @aweinstock314

@nox
Copy link
Member

@nox nox commented Sep 1, 2015

What if we use a DOM range for the selection boundaries?

@jdm
Copy link
Member Author

@jdm jdm commented Sep 1, 2015

Seems reasonable at first glance.

@jdm
Copy link
Member Author

@jdm jdm commented Jan 4, 2017

#4409 is related and I can never find it.

@jdm
Copy link
Member Author

@jdm jdm commented Jan 4, 2017

@iamrohit7 If you're interested in trying this, I will do my best to advise you!

@ghost
Copy link

@ghost ghost commented Jan 4, 2017

@jdm Thanks. I'll get back to you with questions.

@pshaughn
Copy link
Member

@pshaughn pshaughn commented Dec 2, 2019

WPT custom-elements/reactions/Selection needs this.

@jdm jdm added this to To do in web-platform-test failures via automation Dec 2, 2019
@pshaughn
Copy link
Member

@pshaughn pshaughn commented Dec 24, 2019

Most failures under dom/ranges are also this.

@pshaughn
Copy link
Member

@pshaughn pshaughn commented Jan 23, 2020

Would implement DOM selections, up to a point that satisfies non-webdriver automated tests, be a productive thing to do in itself? I imagine they're not very useful without also having a UI to select things, but on the other hand that UI might be easier to write as its own piece on top of the already-working DOM part.

@jdm
Copy link
Member Author

@jdm jdm commented Jan 23, 2020

That would be a useful starting point, indeed.

@pshaughn
Copy link
Member

@pshaughn pshaughn commented Jan 29, 2020

Here's my understanding of what https://w3c.github.io/selection-api/ tells us to do.

Section 2:

  • This is mostly preamble but it points out that Document.open resets the selection when it resets the document, which isn't spelled out anywhere in the meatier sections. [however, WPT tests aren't treating this mention as normative]

Section 3:

  • Selection is a new DOM object, which can have references to a range and a couple nodes.
  • It has many methods that modify or test the range; these are the bulk of the new code to add, but most of them are straightforward.
  • It can return a live reference to the range and be affected by subsequent changes to it.

Section 4:

  • Document holds a reference to a selection, which is populated with an empty selection when the document's created. [empty as in no-range, not as in length-0-range]
  • Document.getSelection returns the selection (by reference) if there's a browsing context for the document, or null if not.
  • Window.getSelection calls Document.getSelection.
  • onselectstart and onselectionchange get added to the global event handlers (XRSession also uses the word onselectstart, but doesn't use a global event mixin so won't collide)

Section 5:

  • When the existing selection is empty or collapsed, a selection UI action (e.g. drag) fires a selectstart event at the node the attempted new selection is starting on, and if this event is cancelled the selection isn't made. This is the only part of the spec that prescribes any UI functionality.
  • Whenever the selection's range changes either by UI action or script, whether by setting a new range, discarding the existing range, or even a mutation to the existing range, a non-cancelable selectionchange event is fired at the document.

Could someone else give it a skim and let me know if there's anything I've left out here?

@pshaughn pshaughn self-assigned this Jan 31, 2020
@pshaughn
Copy link
Member

@pshaughn pshaughn commented Feb 1, 2020

The selectionchange event has no tests and some underspecification (w3c/selection-api#117) but I'll try to make some version of it work. Aside from that, I have a build that seems to be otherwise working, passing WPT tests that incidentally use getSelection, but I'm also getting some unexpected harness timeouts and I don't know how they relate to my changes yet. I'll push a draft of whatever I've got today, hopefully in a state where someone else could pick up the UI part.

@jdm
Copy link
Member Author

@jdm jdm commented Feb 1, 2020

In response to your task source question, 7b3cf27#diff-a1afe92a339a20989be95f03c80740e5 is a nice example of adding a new task source. That being said, the DOM manipulation task source is generally a safe choice when a spec doesn't explicitly state one.

bors-servo added a commit that referenced this issue Feb 2, 2020
Selection DOM interface (but not actual UI selections)

This is work towards #7492.

Things that aren't done here:
- The spec doesn't describe what tests/wpt/metadata/selection/script-and-style-elements.html.ini wants, and what it wants seems to require some sort of layout query.
- #25673 means I'm not sure about some tests being OK.
- I haven't checked that the selectionchange event is actually working, and it has no WPT tests other than that the handler exists.
- Actual UI interactions are not present at all; selection.rs has a few TODOs saying methods what I understand the UI should call for what actions, but there are a lot of fine points here that I don't know about.
bors-servo added a commit that referenced this issue Feb 3, 2020
Selection DOM interface (but not actual UI selections)

This is work towards #7492.

I put new tests in the mozilla-specific directory rather than the main tree because I'm not sure how upstreamable they are; I'd like opinions on that point.

This adds a new exposed interface, which I understand from tests/mozilla/interfaces.html is something requiring specific approval from someone allowed to give that approval.

Things that aren't done here:
- The spec doesn't describe what selection/script-and-style-elements.html wants, and what it wants seems to require some sort of layout query.
- Actual UI interactions are not present at all; selection.rs has a few TODOs saying which methods I believe the eventual UI code should call for what actions, but there are a lot of fine points here that I don't know about.
- I haven't touched Node; you can ask if a node's in the Selection's Range the same way you'd ask about any other Range, but there's not a faster path just for selection layout.
@pshaughn
Copy link
Member

@pshaughn pshaughn commented Feb 14, 2020

The next big step here is figuring out how to convert a click into a range boundary, in the general case where we don't know in advance if it's going to be counting characters or counting child nodes.

@pshaughn pshaughn removed their assignment Feb 14, 2020
bors-servo added a commit that referenced this issue Feb 14, 2020
Selection DOM interface (but not actual UI selections)

This is work towards #7492.

I put new tests in the mozilla-specific directory rather than the main tree because I'm not sure how upstreamable they are; I'd like opinions on that point.

This adds a new exposed interface, which I understand from tests/mozilla/interfaces.html is something requiring specific approval from someone allowed to give that approval.

Things that aren't done here:
- The spec doesn't describe what selection/script-and-style-elements.html wants, and what it wants seems to require some sort of layout query.
- Actual UI interactions are not present at all; selection.rs has a few TODOs saying which methods I believe the eventual UI code should call for what actions, but there are a lot of fine points here that I don't know about.
- I haven't touched Node; you can ask if a node's in the Selection's Range the same way you'd ask about any other Range, but there's not a faster path just for selection layout.
bors-servo added a commit that referenced this issue Feb 14, 2020
Selection DOM interface (but not actual UI selections)

This is work towards #7492.

I put new tests in the mozilla-specific directory rather than the main tree because I'm not sure how upstreamable they are; I'd like opinions on that point.

This adds a new exposed interface, which I understand from tests/mozilla/interfaces.html is something requiring specific approval from someone allowed to give that approval.

Things that aren't done here:
- The spec doesn't describe what selection/script-and-style-elements.html wants, and what it wants seems to require some sort of layout query.
- Actual UI interactions are not present at all; selection.rs has a few TODOs saying which methods I believe the eventual UI code should call for what actions, but there are a lot of fine points here that I don't know about.
- I haven't touched Node; you can ask if a node's in the Selection's Range the same way you'd ask about any other Range, but there's not a faster path just for selection layout.
bors-servo added a commit that referenced this issue Feb 14, 2020
Selection DOM interface (but not actual UI selections)

This is work towards #7492.

I put new tests in the mozilla-specific directory rather than the main tree because I'm not sure how upstreamable they are; I'd like opinions on that point.

This adds a new exposed interface, which I understand from tests/mozilla/interfaces.html is something requiring specific approval from someone allowed to give that approval.

Things that aren't done here:
- The spec doesn't describe what selection/script-and-style-elements.html wants, and what it wants seems to require some sort of layout query.
- Actual UI interactions are not present at all; selection.rs has a few TODOs saying which methods I believe the eventual UI code should call for what actions, but there are a lot of fine points here that I don't know about.
- I haven't touched Node; you can ask if a node's in the Selection's Range the same way you'd ask about any other Range, but there's not a faster path just for selection layout.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.