Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep underscores in dashed-idents #13538

Open
wants to merge 5 commits into
base: next
Choose a base branch
from

Conversation

RobinMalfait
Copy link
Contributor

@RobinMalfait RobinMalfait commented Apr 17, 2024

While working on the removal of automatic var injection (#13537), I noticed that underscores in variables are converted to spaces.

Before the automatic var injection removal, the following code would work:

p-[--spacing-0_5] p-[var(--spacing-0_5)]

However, the generated CSS looked like this:

.p-\[--spacing-0_5\] {
  padding: var(--spacing-0_5);
}
.p-\[var\(--spacing-0_5\)\] {
  padding: var(--spacing-0 5);
  /*                      ^    Notice this space here */
}

One way to solve this is by escaping the underscore, but then you would have to use the variable like this:

p-[var(--spacing-0\_5)]

If you are in a JavaScript context, this could even look like this:

let className = 'p-[var(--spacing-0\\_5)]';

Which looks a bit weird.

So this PR improves the code for handling the _ conversion to by ensuring that dashed-idents width underscores are not converted to spaces.

One caveat: there are properties, such as anchor-name, that accept multiple dashed-idents but luckily for us they are separated by commas instead of spaces. E.g.:

.foo {
  anchor-name: --foo, --bar;
}

If they were separated by a space, then [anchor-name:--foo_--bar] would be a problem because it could both mean:

  • --foo --bar
  • --foo_--bar

... because _ values are valid in dashed idents.

If this ever becomes a problem, then we could special case _-- to equal -- (notice the space). But that in turn would convert legitimate values that look like --foo_--bar. Not sure if this is a real problem, because properties such as anchor-name are comma-separated right now.

Base automatically changed from remove-automatic-var-injection to next April 18, 2024 22:40
@RobinMalfait RobinMalfait force-pushed the fix/keep-variables-and-dashed-idents-as-is branch from 6cdf436 to 869bf5b Compare April 18, 2024 22:43
We will now ensure that:

1. Dashed idents are consumed as-is, meaning that underscores (which are
   valid in dashed-idents) are not converted to a space.
2. Strings are handled separately, which means that values that look
   like dashed idents containing underscores are still converted to
   spaces.
@RobinMalfait RobinMalfait force-pushed the fix/keep-variables-and-dashed-idents-as-is branch from 869bf5b to 555a33e Compare May 1, 2024 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants