Problem/Motivation

The CSS Optimizer appears to be turning valid syntax like `var(--some-variable, )` into invalid syntax like `var(--some-variable,)` (by removing the space between the comma and the closing parenthesis). The latter is invalid because what comes after the comma is a "declaration-value," which must represent at least one token (and a single whitespace-token is fine).

On `var()`: https://www.w3.org/TR/css-variables-1/#using-variables
On declaration-values: https://www.w3.org/TR/css-syntax-3/#any-value

Tailwind CSS uses this syntax for a few of its utilities, which at the moment are simply broken in Drupal when CSS optimization is enabled (see https://github.com/tailwindlabs/tailwindcss/issues/2889 for some discussion of the problem).

Steps to reproduce

Use syntax like `var(--some-variable, )` in your CSS, turn on Drupal's CSS optimization, and look in your browser's dev-tools to find `var(--some-variable,)` (no space) where you'd expect to find `var(--some-variable, )` (with space).

Proposed resolution

I suspect the regex here needs fixing: https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/lib/Drupal/C...

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

michaelallenwarner created an issue. See original summary.

michaelallenwarner’s picture

Issue summary: View changes
chalk’s picture

I've wasted a couple of hours with this issue too! The CSS aggregation breaks some styles from Tailwind CSS framework. Initially I thought that it's an AdvAgg module's issue but after some research I got that the root cause is Core's "Aggregate CSS files".

michaelallenwarner’s picture

Yes, would be nice to have this fixed. I've tried patching that regex in CssOptimizer.php but without success.

longwave’s picture

Component: CSS » asset library system

Changing component, this is code in the asset library system - CSS component is for CSS rules provided by Drupal.

longwave’s picture

Issue tags: +Needs tests

A good first step to solving this would be to write a failing test case, that someone can then use when tweaking the regex in order to get the test to pass.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

michaelallenwarner’s picture

Ideally this would be solved "robustly," but something that might work for Tailwind specifically is preserving CSS comments that consist of only an exclamation mark, like /*!*/. This is because Tailwind surrounds the vital whitespace with such comments, like: var(--tw-empty,/*!*/ /*!*/) (they do this precisely to get CSS minifiers to preserve the space, but in Drupal the comments are excised and it doesn't work).

I don't know if preserving these comments would do the trick—that depends on whether the whitespace-clearing regex targets whitespace between forward slashes (I'm optimistic because it looks to me like it doesn't: https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/lib/Drupal/C...)—but I mention it because maybe it's easier to accomplish and perhaps less risky than altering the whitespace stuff (for someone who's good with PHP, anyway, which isn't me, alas).

michaelallenwarner’s picture

I think the regex that looks for comments is here: https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/lib/Drupal/C...

It looks like this:

$comment = '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/';

I've done some testing at https://www.phpliveregex.com, and I think I can successfully prevent /*!*/ from matching by adding a negative lookahead assertion to the regex, like this:

$comment = '/\*(?!!\*/)[^*]*\*+(?:[^/*][^*]*\*+)*/';

Could use some testing, but maybe that small change would resolve this.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.