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

  1. Install Drupal using Standard profile, then enable a module which uses the off-canvas dialog (e.g. layout builder)
  2. Press a button which invokes the off-canvas dialog (e.g. "configure block" in layout builder).
  3. Use a mouse pointer to highlight some help text inside the off-canvas dialog. Screenshot example:
    Off-canvas dialog, with some help text selected.
    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.
  4. 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

  1. Remove the ::selection style from core/misc/dialog/off-canvas.reset.css
  2. 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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

andrewmacpherson created an issue. See original summary.

andrewmacpherson’s picture

Screenshot for issue summary

andrewmacpherson’s picture

Issue tags: +Novice

Should be an easy patch

Gauravvvv’s picture

I have removed the ::selection pseudo element. Please verify.

Gauravvvv’s picture

Status: Active » Needs review
andrewmacpherson’s picture

Issue summary: View changes

@Gauravmahlawat - thanks for working on this issue.

  1. The change you've made to core/misc/dialog/off-canvas-reset.css is right.
  2. The same change needs to be made in the stable and stable9 core themes. Look for files with the same name in those themes.
  3. The reason your patch failed to apply is that it has the /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 with diff --git a/core/). It sounds like you're working with a site installed by composer 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 using git clone. See the guide to Making a patch.
andrewmacpherson’s picture

Status: Needs review » Needs work
maxstarkenburg’s picture

Status: Needs work » Active

Spent 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:

One thing I discovered right away is that quite a few of the new themes available in D8 are using Bootstrap (not bootstrap base theme just the library) so I added a few things that specifically apply to elements that Bootstrap styles, for example ::selection (!?)

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.)

longwave’s picture

Is 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.

andrewmacpherson’s picture

Re. #9:

Is this a case where the CSS rule all: initial would work to reset any style overrides?

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.

  • For color the initial value varies by user-agent (e.g. currentColor), but CSS level 4 aims to standardize on the CanvasText 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).
  • For background-color the initial value is transparent.

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:
#drupal-off-canvas ::selection, #drupal-off-canvas ::-moz-selection {
    color: initial;
    background-color: initial;
}

So we can rule out the approach suggested in #9.

I also tried using revert and unset 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!

andrewmacpherson’s picture

Re. #8 - thanks for finding the issues/commits @maxstarkenberg

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.

Thanks! I confirmed this too, by checking out every tagged x.y.0 version of the twbs/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 Meyer reset.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:

/* 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);
}

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.

andrewmacpherson’s picture

Status: Active » Needs work

Back to needs-work, to remove it from stable/stable9 themes.

Gauravvvv’s picture

I have removed it from stable and stable9 themes, Please review the patch if it works.

Gauravvvv’s picture

Status: Needs work » Needs review
longwave’s picture

Status: Needs review » Needs work

Thanks, the patch works but stylelint is complaining that there is an extra blank line in each file that needs to be removed.

Gauravvvv’s picture

Gauravvvv’s picture

Status: Needs work » Needs review
ranjith_kumar_k_u’s picture

Updated the code

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! This is RTBC based on the excellent summary of the problem in #11.

andrewmacpherson’s picture

Issue summary: View changes

Updating problem section, to mention that user-agents respect OS-level colour for highlighted text, which is a configurable user-preference on most desktop OS.

ilgnerfagundes’s picture

FileSize
123.34 KB

Revisei novamente o patch, e parece tudo certo. RTBC +1

alexpott’s picture

@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!

alexpott’s picture

Crediting @longwave and @maxstarkenburg for their contributions to the issue discussion.

ilgnerfagundes’s picture

but 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

alexpott’s picture

@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.

alexpott’s picture

@ilgnerfagundes https://www.drupal.org/core/maintainers/issue-credit contains a fuller explanation of how issue credit is awarded for Drupal core.

andrewmacpherson’s picture

Forgot 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

alexpott’s picture

Version: 9.2.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 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.

  • alexpott committed 1992dda on 9.2.x
    Issue #3196430 by Gauravmahlawat, ranjith_kumar_k_u, andrewmacpherson,...

  • alexpott committed c7b83e2 on 9.1.x
    Issue #3196430 by Gauravmahlawat, ranjith_kumar_k_u, andrewmacpherson,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.