Skip to content

Selecting text in highlighted code blocks unexpectedly hides certain contents #3338

Closed
@ltrujello

Description

@ltrujello

Describe the issue/behavior that seems buggy

By default, in the base-16 themes, highlighting text (in the click and drag sense) hides some of the code. The problem is caused by the .hljs ::selection css rules in the files. It seems like they should be deleted.

Sample Code or Instructions to Reproduce

Try highlighting the code in this jsfiddle, which loads Material Darker.

Here's a screenshot for what it looks like for Material Darker.

current

Expected behavior

Here is what I expected (I achieved this by removing the style rule in Material Darker)

expected

Here is how highlighting code via mouse works on Github, for example.

github_sample

Here is how it looks on other base 16 themes. This one is Black Metal Burzum. The second picture is what happened when I removed the css rule from the browser files.

black_metal_burzum
black_metal_burzum_expected

Additional context

This is a very good and widely used project, so perhaps this is on purpose and I hope I am not misunderstanding something. However, code tends to be highlighted/selected for the purpose of copying to the clipboard, so if you are user and are trying to select up to say a specific character of text, it becomes a poor user experience if some of if it randomly disappears/becomes hard to see.

Activity

joshgoebel

joshgoebel commented on Sep 23, 2021

@joshgoebel
Member

At first glance it looks like we should change this:

.hljs ::selection {} /* existing rule */
.hljs::selection {} /* add this rule */

The problem now is only the children get the rule applied not content from the code block itself that has no other highlighting applied. Can you confirm?

ltrujello

ltrujello commented on Sep 23, 2021

@ltrujello
Author

Do you mean for example changing

.hljs ::selection {
  color: #353535;
}

to

.hljs::selection {
  color: #353535;
}

If so, it seems that change would still have the problem, this time only it hides non syntax-highlighted text, which actually hides more content. Here is what it looks like on my end for example:

suggested_change

I think it makes sense to just delete the ::selection rule, if possible.

joshgoebel

joshgoebel commented on Sep 24, 2021

@joshgoebel
Member

Do you mean for example changing ... to ...

No, BOTH rules are necessary (the parent and the children) and in my testing it looked great.

joshgoebel

joshgoebel commented on Sep 24, 2021

@joshgoebel
Member

@highlightjs/core Any philosophical thoughts on whether a theme should be able to change selection color - as a matter of policy? If we wanted to be picky we could decide this "hurts usability" or violates the users choice in some way and just disallow it... but I think technically this can be fixed as I already mention above.

ltrujello

ltrujello commented on Sep 24, 2021

@ltrujello
Author

Nothing changes on my end when I add them both, and I think that's because adding both rules is actually just what was originally the problem: only the syntax highlighted content gets the rule, not the content which has no highlighting, and because the ::selected color is very similar to the color of the background, it hides the syntax-highlighted content.

If possible, this rule should just be taken out. The default blue colors that browsers provide are perfectly fine and are what users naturally expect. Additionally, when one is selecting code via click and drag, they are probably focused and in the middle of a more important task, hence they don't need to be distracted by a random color or inconvenienced by text disappearing for a task that should be really short and simple. That's probably why most websites with syntax-highlighted code, like Github or StackOverflow, don't have any interruption or fancy coloring in the selection highlighting.

joshgoebel

joshgoebel commented on Sep 24, 2021

@joshgoebel
Member

Nothing changes on my end when I add them both

https://jsfiddle.net/pz2wkuya/

Working properly with both Safari and Chrome here. You can disagree with the visual merits, but I'm more concerned with the technical problem you first raised, which seems solvable. Now, it's worth asking if the intention of ::selected is to use the background-color, rather than color... and if so, then that's a mistake on my part I think - and something we should indeed fix (which might address some of your concerns). But these are themes - a great many of them are already a matter of personal taste, and "selection color" is an official part of the Base16 spec. If you don't like it, then don't use a base16 theme, or modify the CSS to remove ::selection.

So:

  • We'll fix the CSS flaw you originally reported with inconsistent highlights.
  • We'll see if the actual intention was background-color (vs color), which seems obvious now in hindsight.

We'll change those two things and think that will improve the situation greatly and get us back to what the theme authors originally intended.

ltrujello

ltrujello commented on Sep 24, 2021

@ltrujello
Author

The linked fiddled works and looks great! It has different CSS syntax than what one would get by just applying your initial suggestion, hence why I did not see a difference (i.e.., Material Darker's ::selection does not have a background-color value and the color value is not inherent). This works great for me though.

joshgoebel

joshgoebel commented on Sep 29, 2021

@joshgoebel
Member

@ltrujello You want to poke around in the base16-highlightjs repo and see if that looks good to you? If things are all good now we'll copy these newer versions over for the next release.

ltrujello

ltrujello commented on Oct 3, 2021

@ltrujello
Author

Yes these look great! Personally I stuck with color: inherit suggestion instead of a fixed color, so the text selection preserves the syntax highlighting, but that is just a matter of preference. Thanks for the help!

joshgoebel

joshgoebel commented on Oct 3, 2021

@joshgoebel
Member

934f2e7 Should land in 11.3

added this to the 11.3 milestone on Oct 3, 2021
allejo

allejo commented on Oct 7, 2021

@allejo
Member

@highlightjs/core Any philosophical thoughts on whether a theme should be able to change selection color - as a matter of policy? If we wanted to be picky we could decide this "hurts usability" or violates the users choice in some way and just disallow it... but I think technically this can be fixed as I already mention above.

I, personally, would allow themes to change selection color. Yes, it can lead to usability issues but there's also a chance that the browser color for highlighting can conflict with the theme. I don't think existing themes should be changed to add a selection color but if there's a problem with it, then it can be fixed.

If you're a theme author and you want to torture everyone by having red on green, we won't stop you; just nobody will use your theme 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @joshgoebel@allejo@ltrujello

        Issue actions

          Selecting text in highlighted code blocks unexpectedly hides certain contents · Issue #3338 · highlightjs/highlight.js