highlightjs / highlightjs/highlight.js

(css) Escaped special character in selector class name breaks highlighting

Open
#3,965 2 comments 0 reactions 0 assignees View on GitHub
bug help welcome language
Dominant language
JavaScript
Stars
25k
Forks
3.8k
Avg merge
11h 35m
Merged PRs (30d)
3

Description

**Describe the issue**

CSS class and ID selectors contain arbitrary strings (e.g. `.myclassname`), which can contain escaped special characters (e.g. `.foo\:myclassname` to match `class="foo:myclassname"`). Currently, the backslash that escapes a special character is not recognised by highlight.js as a valid part of the class name or ID.

**Which language seems to have the issue?**

css

**Are you using `highlight` or `highlightAuto`?**

`highlight`

**Sample Code to Reproduce**

```css
.dark\:hover\:bg-sky-500:hover {
--tw-bg-opacity: 1;
background-color: rgb(14 165 233 / var(--tw-bg-opacity));
}
```

Current highlighting:

![CleanShot 2024-01-16 at 11 45 54](https://github.com/highlightjs/highlight.js/assets/89772/7f0ed269-db13-4dd2-b27d-7298e963067a)

**Expected behavior**

The escaped special characters (`\:`) should not interfere with the highlighting of the class name, but actual pseudoclasses (`:hover`) should still be highlighted.

GitHub:

![CleanShot 2024-01-16 at 11 43 34](https://github.com/highlightjs/highlight.js/assets/89772/f4146b25-1e38-4926-ad56-fb55bf05fa4e)

VS Code:

![CleanShot 2024-01-16 at 11 45 17](https://github.com/highlightjs/highlight.js/assets/89772/d3be1458-b632-471e-8561-9732f5c59f3e)

**Additional context**

TailwindCSS makes extensive use of escaped characters in CSS selectors for its [modifiers](https://tailwindcss.com/docs/hover-focus-and-other-states).

Other special characters are escaped for features like [color opacity](https://tailwindcss.com/docs/text-color#changing-the-opacity):

```css
.dark\:hover\:bg-sky-500\/25:hover {
background-color: rgb(14 165 233 / 0.25);
}
```

…and [arbitrary values](https://tailwindcss.com/docs/adding-custom-styles#using-arbitrary-values):

```css
.dark\:hover\:bg-\[\#ff0000\]\/25:hover {
background-color: rgb(255 0 0 / 0.25);
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.