Skip to content

Implement ::selection pseudo-element - #10252

Merged
bors-servo merged 5 commits into
servo:masterfrom
emilio:selection
Mar 30, 2016
Merged

bors-servo merged 5 commits into
servo:masterfrom
emilio:selection

Conversation

@emilio

@emilio emilio commented Mar 29, 2016

Copy link
Copy Markdown
Member

It only supports color and background, for now, but it shouldn't be hard to add more properties (like text-shadow).

r? @mbrubeck


This change is Reviewable

@highfive

Copy link
Copy Markdown

Heads up! This PR modifies the following files:

@highfive

Copy link
Copy Markdown

warning Warning warning

  • These commits modify style and layout code, but no tests are modified. Please consider adding a test!

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 29, 2016
@mbrubeck

Copy link
Copy Markdown
Contributor

Reviewed 2 of 9 files at r1, 1 of 1 files at r3.
Review status: 3 of 9 files reviewed at latest revision, 2 unresolved discussions.


components/layout/construct.rs, line 590 [r3] (raw file):
Should this be node.selected_style().clone()? It can matter even for whitespace fragments, because they need to draw their background when selected.


components/layout/construct.rs, line 1870 [r3] (raw file):
You can add a selected_style field to InlineFragmentNodeInfo and read it here.


Comments from the review on Reviewable.io

@emilio

emilio commented Mar 29, 2016

Copy link
Copy Markdown
Member Author

Review status: 2 of 10 files reviewed at latest revision, 2 unresolved discussions.


components/layout/construct.rs, line 590 [r3] (raw file):
True, sorry, didn't realize node was in scope (small screen + massive function + not used to this part of the code...).

Done


components/layout/construct.rs, line 1870 [r3] (raw file):
Done.


Comments from the review on Reviewable.io

@mbrubeck

Copy link
Copy Markdown
Contributor

Reviewed 6 of 9 files at r1, 2 of 2 files at r4.
Review status: all files reviewed at latest revision, 3 unresolved discussions.


components/layout/fragment.rs, line 89 [r4] (raw file):
Any ideas for ways we could implement this without making the Fragment struct bigger, either now or in follow-up work? Maybe we could put the selected styles into a new field in the existing ComputedValues? Or compute it on demand based on the Fragment's inline_context field? (Are selected elements always in an inline flow? Hmm, probably not.)

Adding a field to inline_context also makes Fragment bigger, but there's an existing FIXME about moving that to InlineFlow.


components/layout/generated_content.rs, line 441 [r3] (raw file):
We could change this function to accept a selected_style argument. (It's probably not useful yet, since I don't think we have any way to select generated content, but it will be eventually.)


components/layout/inline.rs, line 1854 [r4] (raw file):
I know I suggested this, but now I'm having second thoughts. As mentioned above, this makes Fragment bigger which we should avoid if possible. If this is used only for control_chars_to_fragment then we could just remove it again, since these bidi control chars are not printable, and get stripped out during text layout.


Comments from the review on Reviewable.io

@emilio

emilio commented Mar 29, 2016

Copy link
Copy Markdown
Member Author

Review status: all files reviewed at latest revision, 3 unresolved discussions.


components/layout/fragment.rs, line 89 [r4] (raw file):
At first I tried to get it on-demand, but I didn't find any way to get the layout data associated with the node once we're on the display list building phase without passing it inside Fragment.

The first option I thought was making Selection a real pseudo-element, but after fiddling around with it for a while my impression is that our existing code for pseudo-elements isn't prepared for a pseudo-element that "wraps" a fragment (and possibly multiple ones) from the parent, and that ::selection seems to be a really special case for this.

Also, if we did that that way, we'd still need both the ComputedValues for ::selection and those for the node, or a "merge" of those, since ::selection should only change a few properties.

I haven't done enough layout to see if I'm right or not though, so if you know a way this could work, I'm happy to experiment with it.

Adding the selected styles in the current ComputedValues could be good enough, but I'm not sure how it could be done easily.


components/layout/generated_content.rs, line 441 [r3] (raw file):
I thought about it, and I saw that neither Gecko nor Blink allow selecting generated content. It's fine to add the parameter if you prefer it though.


components/layout/inline.rs, line 1854 [r4] (raw file):
Reverting, no problem :P

Anyway, I don't think that makes Fragment bigger, since InlineFragmentNodeInfo is stored on a Vec inside InlineFragmentContext, not directly.


Comments from the review on Reviewable.io

@emilio

emilio commented Mar 29, 2016

Copy link
Copy Markdown
Member Author

Review status: 8 of 9 files reviewed at latest revision, 3 unresolved discussions.


components/layout/inline.rs, line 1854 [r4] (raw file):
Pushed the revert commit instead of squashing them, just in case it needs to be re-reverted, I'll squash if r+'d


Comments from the review on Reviewable.io

@mbrubeck

Copy link
Copy Markdown
Contributor

Reviewed 2 of 2 files at r5.
Review status: all files reviewed at latest revision, 3 unresolved discussions, some commit checks failed.


components/layout/fragment.rs, line 89 [r4] (raw file):
I don't have any immediate ideas. I'm happy to leave this as a possible follow-up optimization if @pcwalton agrees.

Also, if we did that that way, we'd still need both the ComputedValues for ::selection and those for the node, or a "merge" of those, since ::selection should only change a few properties.

Yes, I envisioned adding a new style struct for selection properties, with only the handful of supported properties.

Adding the selected styles in the current ComputedValues could be good enough, but I'm not sure how it could be done easily.

@SimonSapin, any thoughts on this?


components/layout/generated_content.rs, line 441 [r3] (raw file):
Oh, in that case never mind.


components/layout/inline.rs, line 1854 [r4] (raw file):
Oops, you're right. Please un-revert. Sorry. :P


Comments from the review on Reviewable.io

@SimonSapin

Copy link
Copy Markdown
Member

Review status: 2 of 10 files reviewed at latest revision, 2 unresolved discussions.


components/layout/fragment.rs, line 89 [r4] (raw file):
I don’t have any immediate ideas either, sorry. How does Gecko do this?

Adding the selected styles in the current ComputedValues

I’m mildly opposed to this as it mixes up unrelated concepts.


Comments from the review on Reviewable.io

@emilio

emilio commented Mar 29, 2016

Copy link
Copy Markdown
Member Author

Review status: 2 of 10 files reviewed at latest revision, 2 unresolved discussions.


components/layout/fragment.rs, line 89 [r4] (raw file):
Gecko code: https://github.com/mozilla/gecko-dev/blob/e8a838517a32b4c3d6a39acdc6186d8db4641ed7/layout/generic/nsTextFrame.cpp#L3785-L3809

It appears it computes it on demand, the text frame has access to the presentation context, and also to the element it belongs to, and gathers the styles from there.

It'd be equivalent for us to have access to the node and to its style data from the text fragment, which is what I had in mind initially, but doesn't seem easy/safe to convert the opaque node into the LayoutNode to get the PseudoElement style from there.


Comments from the review on Reviewable.io

@mbrubeck

Copy link
Copy Markdown
Contributor

After #10255 lands this will require an update to tests/unit/layout/size_of.rs.

@mbrubeck mbrubeck added S-needs-decision A decision is required and has not been made. S-needs-rebase There are merge conflict errors. and removed S-awaiting-review There is new code that needs to be reviewed. labels Mar 30, 2016
@mbrubeck

Copy link
Copy Markdown
Contributor

This code all looks great to me; my only remaining concern is whether the performance impact is reasonable. Here are my thoughts:

  1. This increases the size of Fragment, but only by one word. Seems reasonable and hard to avoid.
  2. If any stylesheet (including a user or user agent stylesheet) contains a univeral ::selection { ... } rule, then this allocates an extra instance of ServoComputedValues for every node on the page. This struct is very large, and I believe pseudo-element styles never use the style sharing cache, so this could cause a whole lot of memory allocation. I think we could mitigate this by making style sharing work for pseudo elements.
  3. This adds an extra Arc::clone call per Fragment during flow construction. Not sure if it matters or not. Hard to avoid, at least for fragments affected by ::selection rules (which in many cases could be all fragments; see above).

@pcwalton, any opinion on landing this as-is versus fixing the above issues first (especially number 2)?

-S-awaiting-review +S-needs-decision +S-needs-rebase


Reviewed 8 of 8 files at r6.
Review status: all files reviewed at latest revision, 1 unresolved discussion.


Comments from the review on Reviewable.io

@mbrubeck

Copy link
Copy Markdown
Contributor

If any stylesheet (including a user or user agent stylesheet) contains a univeral ::selection { ... } rule, then this allocates an extra instance of ServoComputedValues…

Oh wait, once again this is not as bad as I thought. I forgot that ServoComputedValues only holds Arcs to the actual style structs, rather than storing them inline. And it will clone these Arcs rather than copying the structs, almost all of the time. That's still a number of new allocations and Arc::clone calls, but it's not nearly as much memory as I feared.

@SimonSapin

Copy link
Copy Markdown
Member

If any stylesheet (including a user or user agent stylesheet) contains a univeral ::selection { ... }

This will likely be the case, won’t it? To set the default colors in the user-agent stylesheet.

@emilio

emilio commented Mar 30, 2016

Copy link
Copy Markdown
Member Author

This will likely be the case, won’t it? To set the default colors in the user-agent stylesheet.

Since selection only works in textareas and inputs, for now we can reduce the impact using input::selection, textarea::selection if this is really problematic.

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 30, 2016
@emilio

emilio commented Mar 30, 2016

Copy link
Copy Markdown
Member Author

Rebased and updated the test expectations

@emilio emilio removed the S-needs-rebase There are merge conflict errors. label Mar 30, 2016
@mbrubeck mbrubeck removed the S-needs-decision A decision is required and has not been made. label Mar 30, 2016
@mbrubeck

Copy link
Copy Markdown
Contributor

@bors-servo r+

Let's do this!

-S-needs-decision


Reviewed 12 of 12 files at r7.
Review status: all files reviewed at latest revision, 1 unresolved discussion, some commit checks broke.


Comments from the review on Reviewable.io

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit 46829bd has been approved by mbrubeck

@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Mar 30, 2016
@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 46829bd with merge 723989b...

bors-servo pushed a commit that referenced this pull request Mar 30, 2016
Implement ::selection pseudo-element

It only supports `color` and `background`, for now, but it shouldn't be hard to add more properties (like text-shadow).

r? @mbrubeck

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10252)
<!-- Reviewable:end -->
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - android, arm32, gonk, linux-dev, linux-rel, mac-dev-unit, mac-rel-css, mac-rel-wpt, status-appveyor

@paulrouget

Copy link
Copy Markdown
Contributor

Since selection only works in textareas and inputs, for now we can reduce the impact using input::selection, textarea::selection if this is really problematic.

Did that ever happen?

@SimonSapin

Copy link
Copy Markdown
Member

It looks like the UA stylesheets don’t have any ::selection rule at all, so selection defaults to invisible. We probably should add one in resources/servo.css.

@mbrubeck

Copy link
Copy Markdown
Contributor

On Thu, Mar 31, 2016 at 6:05 AM, Simon Sapin [email protected]
wrote:

It looks like the UA stylesheets don’t have any ::selection rule at all,
so selection defaults to invisible. We probably should add one in
resources/servo.css.

Submitted yesterday, but still waiting in the homu queue:
#10299

bors-servo pushed a commit that referenced this pull request Apr 1, 2016
Set a default ::selection style

Since we no longer have a hard-coded default after #10252.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10299)
<!-- Reviewable:end -->
@emilio
emilio deleted the selection branch July 18, 2016 04:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants