Problem/Motivation
The off canvas dialog overrides the style for the ::selection
pseudo-element:
/* core/misc/dialog/off-canvas-reset.css */
/* To standardize off-canvas selection color. */
#drupal-off-canvas ::-moz-selection,
#drupal-off-canvas ::selection {
color: inherit;
background-color: rgba(175, 175, 175, 0.5);
}
I can't see a good justification for this; the reason given ("standardize") is vague. Unless there's a very good reason to style it ourselves, it's best left to user-agents to decide. The selection indicator is a tool which really belongs to the user (not the author), rather like the mouse pointer. Cosmetics isn't a good reason here.
By default, user agents respect the OS-level highlighted text colour, which is a configurable user preference on most desktop OS. Some users employ the text selection as a reading aid, and the ability to configure the colour is very useful. Sadly, when author stylesheets override the colours with the ::selection
pseudo-element, users lose the ability to read highlighted text in a colour of their own choosing. The MDN article on ::selection
advises against doing this in the accessibility concerns section.
What's more, the colour chosen here fails WCAG success criterion 1.4.11 Non-text contrast. While the selected text has good contrast against the selection background, the selection background has poor contrast against the dialog container background. So it's hard to be sure which fragment of text is selected.
Steps to reproduce
- Install Drupal using Standard profile, then enable a module which uses the off-canvas dialog (e.g. layout builder)
- Press a button which invokes the off-canvas dialog (e.g. "configure block" in layout builder).
- Use a mouse pointer to highlight some help text inside the off-canvas dialog. Screenshot example:
Here we see the configuration settings for a comments field, used in layout builder. There's a lot of help text here, and one sentence is selected. It's not easy to distinguish the selected sentence from the rest of the help text. - The resulting background colour for the selection (after taking translucency into account) is #5b5b5b. This has a contrast ration of 1.43:1 against the surrounding dialog background (#444).
Proposed resolution
Remove the ::selection
style from the off-canvas dialog.
Remaining tasks
- Remove the
::selection
style fromcore/misc/dialog/off-canvas.reset.css
- Also remove it from the corresponding stylesheets included in the stable and stable9 themes.
User interface changes
Use the user-agent defaults for selected text in the off-canvas dialog.
API changes
None.
Data model changes
None.
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#21 | SIZ1g71 - Imgur.png | 123.34 KB | ilgnerfagundes |
#18 | 3196430-18.patch | 1.54 KB | ranjith_kumar_k_u |
#16 | 3196430-16.patch | 1.47 KB | Gauravvvv |
#13 | 3196430-14.patch | 1.47 KB | Gauravvvv |
#2 | 3196430-off-canvas-dialog-selection-comments-field-layoutbuilder.png | 23.32 KB | andrewmacpherson |
Comments
Comment #2
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedScreenshot for issue summary
Comment #3
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedShould be an easy patch
Comment #4
Gauravvvv CreditAttribution: Gauravvvv at OpenSense Labs commentedI have removed the ::selection pseudo element. Please verify.
Comment #5
Gauravvvv CreditAttribution: Gauravvvv at OpenSense Labs commentedComment #6
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commented@Gauravmahlawat - thanks for working on this issue.
core/misc/dialog/off-canvas-reset.css
is right./web/
step in the paths (in the first line of the patch). Core patches need to use paths relative to the git repo root (so they should begin withdiff --git a/core/
). It sounds like you're working with a site installed bycomposer create-project drupal/recommended-project
? That's the recommended approach for managing a website, but for core contribution you'll want to start with a separate repo by usinggit clone
. See the guide to Making a patch.Comment #7
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedComment #8
maxstarkenburgSpent a bit of time trying to track down the history of why this got added, regardless of its merits, to know if certain things might be affected. From this patch from #2899724: Fixes for settings tray CSS and JavaScript minor issues, the "standardize" comment originally read "Bootstrap and other frameworks add color to selection." That led me to finding #2826722: Add a 'fence' around settings tray with aggressive CSS reset., particularly comment 26, where it was noted:
So I would guess that a potential "consequence" of removing this would be that some site using Bootstrap-based themes, for example, might have Bootstrap's
::selection
colors start to seep through in off-canvas contexts. (Though doing some cursory grepping in some versions/varieties of Bootstrap, I'm not finding setting of::selection
color, but I could just be looking in the wrong places.)Comment #9
longwaveIs this a case where the CSS rule
all: initial
would work to reset any style overrides?We are looking to remove that in another off-canvas issue #2958588: Off-canvas style resets are overriding styles (especially SVGs) resulting in display issues but this feels like a case where it might be OK.
Comment #10
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedRe. #9:
Unsure, haven't tried it. However it sounds very risky to me. Remember that
initial
is supposed to set it back to the initial value specified in the CSS recommendations, which isn't the same as the user-agent stylesheet.color
the initial value varies by user-agent (e.g.currentColor
), but CSS level 4 aims to standardize on theCanvasText
system colour keyword, i.e. normal text. That often computes to black (but will differ if OS features like high-contrast mode, dark mode, or other native widget theme/skins are used).background-color
the initial value istransparent
.These are almost certainly NOT what we hope for in a selection style.
What I'm unsure of is whether user-agents would actually treat this as CanvasText-on-transparent, or decide that it needed to use a UA style.
Update (5th Feb 2021): I tested the
initial
keyword, and my fear was correct. Adding this via the Firefox dev tools resulted in the text selection appearing black-on-transparent:So we can rule out the approach suggested in #9.
I also tried using
revert
andunset
which aren't any better. Colours are inherited properties, so both of these result in the selected text looking exactly the same as the unselected text. So the selection is effectively invisible!Comment #11
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedRe. #8 - thanks for finding the issues/commits @maxstarkenberg
Thanks! I confirmed this too, by checking out every tagged
x.y.0
version of thetwbs/bootstrap
repo. Grepping for "selection", I found no instances of::selection
or-moz-selection
. So the core Bootstrap library has never done this. Maybe the various Bootstrap "themes" set::selection
styles? I wouldn't relish doing a survey of those.normalize.css
and the Meyerreset.css
don't use the selection pseudo-element either.The HTML5 Boilerplate starter DOES style the
::selection
though, so perhaps these styles are present in some Drupal themes. Versions 1-3 used a hot/bubblegum pink selection background, and versions 4-8 use a light blue background. The doc comments explain the motivation is to remove a text-shadow from some user-agent styles, but that doesn't explain their use of colour properties.So the concern in the original issue may be warranted, but we have no way of knowing how widespread the use of custom selection styles is. It certainly isn't "all Bootstrap sites" though.
Meanwhile, we're missing the glaringly obvious fact that this rule in
off-canvas.reset.css
doesn't actually work in Blink, Webkit, IE, or old-Edge:In fact this has only ever worked in Gecko-based browsers, because when browsers don't recognize one of the selectors (
::-moz-selection
), the entire rule is ignored.I confirmed this by shoving
::selection, ::-moz-selection { background-color: orange; }
into Bartik. Sure enough, selected text in the off-canvas dialog gets an orange background in Chrome, but a grey background in Firefox.So, it's never worked in most of our supported browsers (in any stable Drupal core release since it was added in mid-2017), while it leads to a WCAG contrast failure in the only browser where it does work. That gives me some confidence that just removing it outright won't be a disaster.
Comment #12
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedBack to needs-work, to remove it from stable/stable9 themes.
Comment #13
Gauravvvv CreditAttribution: Gauravvvv at OpenSense Labs commentedI have removed it from stable and stable9 themes, Please review the patch if it works.
Comment #14
Gauravvvv CreditAttribution: Gauravvvv at OpenSense Labs commentedComment #15
longwaveThanks, the patch works but stylelint is complaining that there is an extra blank line in each file that needs to be removed.
Comment #16
Gauravvvv CreditAttribution: Gauravvvv at OpenSense Labs commentedI have updated the patch as per your comment please review.
Comment #17
Gauravvvv CreditAttribution: Gauravvvv at OpenSense Labs commentedComment #18
ranjith_kumar_k_u CreditAttribution: ranjith_kumar_k_u at Zyxware Technologies commentedUpdated the code
Comment #19
longwaveThanks! This is RTBC based on the excellent summary of the problem in #11.
Comment #20
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedUpdating problem section, to mention that user-agents respect OS-level colour for highlighted text, which is a configurable user-preference on most desktop OS.
Comment #21
ilgnerfagundes CreditAttribution: ilgnerfagundes at CI&T commentedRevisei novamente o patch, e parece tudo certo. RTBC +1
Comment #22
alexpott@ilgnerfagundes please don't upload screenshots of dreditor. This has happened on multiple issues. For example #3197135: hook_validation_constraint_alter() example code and #3100386: Create contrib update module test cases that use semantic versioning. Posting screenshots of your codebase or CLI does not advance the issue, since the automated testing infrastructure tells us whether the patch applies correctly. So, I've removed the issue credit for that screenshot. In the future, you can get credit for issues by reading the issue to understand its purpose, and posting your review or testing of that purpose. Thank you!
Comment #23
alexpottCrediting @longwave and @maxstarkenburg for their contributions to the issue discussion.
Comment #24
ilgnerfagundes CreditAttribution: ilgnerfagundes at CI&T commentedbut alex i just put the screenshot to prove that i downloaded the patch, i ran it and did the tests, people above didn't even put any print and still proved it worked and i didn't? @alexpot
Comment #25
alexpott@ilgnerfagundes - @longwave contributed to the issue discussion by discussing possible solutions in #9 and helping a contributor with their patch in #15. @maxstarkenburg did the git archaeology to link issues where the css under discussion was added in #8. All I can see from #21 is that you've taken a screenshot with dreditor. There is no contribution to the issue discussion, no discussion of what you've reviewed and how. DrupalCI applies the change and runs tests so doing that locally is not enough for issue credit.
Comment #26
alexpott@ilgnerfagundes https://www.drupal.org/core/maintainers/issue-credit contains a fuller explanation of how issue credit is awarded for Drupal core.
Comment #28
andrewmacpherson CreditAttribution: andrewmacpherson as a volunteer commentedForgot to say earlier, that @Lendude deserves a credit here too. He helped out with some macOS browser testing, when I asked in the Slack #contribute channel. My mac is broken so I needed someome to check whether browsers respect the user-specified highlight colour from macOS System Preferences. They confirmed that Firefox, Safari, Opera, and Chrome, all respect the macOS highlight colour choice unless the author stylesheet has overridden the colours of
::selection
. So that guided the proposed resolution to remove this rule.If you are logged in to Slack, you can see a record of the contribution in this thread - https://drupal.slack.com/archives/C1BMUQ9U6/p1612515039234300
Comment #29
alexpottCommitted and pushed 1992dda93b to 9.2.x and c7b83e20b8 to 9.1.x. Thanks!
Discussed with @lauriii and we agreed that the fix is good to go.
Backporting to 9.1.x since this is a low risk accessibility bug fix.