From c82c3d2f2196948b0fe0986bd8f6e657580fe3ea Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Mon, 16 Nov 2026 02:33:08 -0500 Subject: [PATCH 1/2] fix(php-transformer): keep shared stylesheet classes and compound aliases in route class alignment Route class alignment (#5970) began reading every linked stylesheet file as the linking route's own sheet. A shared runtime or widget file linked by several routes then made its classes read differently on any route that also linked one more widget sheet, so shared vocabulary classes were renamed. Two losses followed: nested rules in the shared file were not copied for the alias, and a selector naming two aliased classes only gained copies that renamed one of them, matching nothing. A file linked by several chrome documents is now shared vocabulary, read like an unlinked file. A selector naming several aliased classes gains a copy for each set of them. --- .../ArtifactCompiler/RouteStateProjector.php | 36 ++++++++++--- ...ligned-chrome-class-compound-selectors.php | 51 +++++++++++++++++++ ...aligned-chrome-class-shared-stylesheet.php | 44 ++++++++++++++++ 3 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 php-transformer/tests/contract/route-aligned-chrome-class-compound-selectors.php create mode 100644 php-transformer/tests/contract/route-aligned-chrome-class-shared-stylesheet.php diff --git a/php-transformer/src/ArtifactCompiler/RouteStateProjector.php b/php-transformer/src/ArtifactCompiler/RouteStateProjector.php index 802202bba4..3822fbda90 100644 --- a/php-transformer/src/ArtifactCompiler/RouteStateProjector.php +++ b/php-transformer/src/ArtifactCompiler/RouteStateProjector.php @@ -626,12 +626,21 @@ private static function withRouteAlignedChromeClasses(array $files): array if (null !== $linked && !in_array($linked, $sheets[$path] ?? array(), true)) $sheets[$path][] = $linked; } } + // A file only one chrome document links is that route's own sheet. A + // file several routes link is shared vocabulary, read like a file + // no chrome document links: its classes mean the same everywhere. + $links = array(); + foreach (array_keys($documents) as $index) foreach ($sheets[(string) ($files[$index]['path'] ?? '')] ?? array() as $sheet) $links[$sheet] = ($links[$sheet] ?? 0) + 1; + $global = ''; + foreach ($stylesheetFiles as $sheet) { + if (1 === ($links[$sheet] ?? 0)) continue; + $global .= "\n" . $files[$sheet]['content']; + foreach ($sheets as &$own) $own = array_values(array_filter($own, static fn(int $held): bool => $held !== $sheet)); + unset($own); + } + $sheets = array_filter($sheets); $documents = array_filter($documents, static fn($unused, int $index): bool => isset($sheets[(string) ($files[$index]['path'] ?? '')]), ARRAY_FILTER_USE_BOTH); if (count($documents) < 2) return $files; - $linkedSheets = array(); - foreach (array_keys($documents) as $index) foreach ($sheets[(string) $files[$index]['path']] as $sheet) $linkedSheets[$sheet] = true; - $global = ''; - foreach ($stylesheetFiles as $sheet) if (!isset($linkedSheets[$sheet])) $global .= "\n" . $files[$sheet]['content']; $transformer = new CssStylesheetTransformer(); $classPattern = '/\.(-?[A-Za-z_][A-Za-z0-9_-]*)/'; $named = static fn(string $class): string => '/\.' . preg_quote($class, '/') . '(?![A-Za-z0-9_-])/'; @@ -713,12 +722,27 @@ private static function withRouteAlignedChromeClasses(array $files): array if (is_string($html)) $files[$index]['content'] = $html; } // Each rule naming an aliased class gains a copy for its alias right - // after it, so both names keep their place in the cascade. + // after it, so both names keep their place in the cascade. A selector + // naming several aliased classes gains a copy for each set of them: + // aligned chrome wears every alias at once, while unaligned elements + // keep the source names. $withCopies = static fn(string $css, array $aliases): string => $transformer->transform($css, static function (string $prelude, string $body) use ($aliases, $classPattern, $named): string|array { $copies = array(); foreach (CssStylesheetTransformer::splitSelectorList($prelude) ?? array() as $selector) { preg_match_all($classPattern, $selector, $classes); - foreach (array_unique($classes[1]) as $class) if (isset($aliases[$class])) $copies[] = preg_replace($named($class), '.' . $aliases[$class], trim($selector)); + $aliased = array_values(array_filter(array_unique($classes[1]), static fn(string $class): bool => isset($aliases[$class]))); + $sets = array(); + if (count($aliased) <= 4) { + for ($mask = 1; $mask < 1 << count($aliased); ++$mask) $sets[] = array_values(array_filter($aliased, static fn(int $bit): bool => 0 !== ($mask & 1 << $bit), ARRAY_FILTER_USE_KEY)); + usort($sets, static fn(array $a, array $b): int => count($a) <=> count($b)); + } else { + $sets = array_merge(array_map(static fn(string $class): array => array($class), $aliased), array($aliased)); + } + foreach ($sets as $set) { + $copy = trim($selector); + foreach ($set as $class) $copy = (string) preg_replace($named($class), '.' . $aliases[$class], $copy); + $copies[] = $copy; + } } return array() === $copies ? $prelude : array(array('prelude' => $prelude, 'body' => $body), array('prelude' => implode(',', array_unique($copies)), 'body' => $body)); }); diff --git a/php-transformer/tests/contract/route-aligned-chrome-class-compound-selectors.php b/php-transformer/tests/contract/route-aligned-chrome-class-compound-selectors.php new file mode 100644 index 0000000000..8419e62659 --- /dev/null +++ b/php-transformer/tests/contract/route-aligned-chrome-class-compound-selectors.php @@ -0,0 +1,51 @@ +
Monday
9 to 5
Tuesday
Closed
'; +$files = array( + // The second route's sheet carries one more rule for the same list, so + // both class names read differently there and are aligned. + array('path' => 'assets/home.css', 'kind' => 'css', 'content' => $columns), + array('path' => 'assets/contact.css', 'kind' => 'css', 'content' => $columns . '.hours .hours-list{margin:0}'), +); +foreach (array('index.html' => 'home', 'contact/index.html' => 'contact') as $path => $sheet) { + $files[] = array('path' => $path, 'kind' => 'html', 'content' => '

' . $sheet . '

' . $footer . ''); +} + +$projected = (new RouteStateProjector())->project($files)['files']; +$sheets = array(); +foreach ($projected as $file) if ('css' === $file['kind']) $sheets[$file['path']] = (string) $file['content']; +foreach ($projected as $file) { + if ('html' !== $file['kind']) continue; + $document = new DOMDocument(); + @$document->loadHTML((string) $file['content']); + $xpath = new DOMXPath($document); + $box = (string) $xpath->evaluate('string(//footer/div/@class)'); + $list = (string) $xpath->evaluate('string(//footer//dl/@class)'); + $assert(str_starts_with($box, 'route-class-') && str_starts_with($list, 'route-class-'), 'Both footer classes are aligned in ' . $file['path'] . '.', (string) $file['content']); + $css = $sheets['index.html' === $file['path'] ? 'assets/home.css' : 'assets/contact.css']; + // The aligned footer wears only the aliases, so a rule reaches its list + // terms only through a copy that names both. + $assert(str_contains($css, '.' . $box . ' .' . $list . ' dt'), 'The aligned list terms keep their column rule in ' . $file['path'] . '.', $css); + $assert(str_contains($css, '.' . $box . ' .' . $list . ' dd'), 'The aligned list details keep their column rule in ' . $file['path'] . '.', $css); +} + +echo "route-aligned-chrome-class-compound-selectors contract passed\n"; diff --git a/php-transformer/tests/contract/route-aligned-chrome-class-shared-stylesheet.php b/php-transformer/tests/contract/route-aligned-chrome-class-shared-stylesheet.php new file mode 100644 index 0000000000..626858f8ad --- /dev/null +++ b/php-transformer/tests/contract/route-aligned-chrome-class-shared-stylesheet.php @@ -0,0 +1,44 @@ +.menu-item:after{border-top:1px solid #d1d1d1;content:""}}.menu .menu-top>.menu-item{display:flex;flex-direction:column}'; +$files = array( + array('path' => 'assets/runtime.css', 'kind' => 'css', 'content' => $runtime), + // Only the contact route links this widget sheet. + array('path' => 'assets/contact-widget.css', 'kind' => 'css', 'content' => '.menu-top{margin:0}.contact-form{padding:8px}'), +); +$menu = ''; +foreach (array('index.html' => '', 'contact/index.html' => '') as $path => $widget) { + $files[] = array('path' => $path, 'kind' => 'html', 'content' => '' . $widget . '
' . $menu . ''); +} + +$projected = (new RouteStateProjector())->project($files)['files']; +foreach ($projected as $file) { + if ('css' === $file['kind']) { + $assert($file['content'] === $files['assets/runtime.css' === $file['path'] ? 0 : 1]['content'], 'Route alignment leaves ' . $file['path'] . ' as written.', (string) $file['content']); + continue; + } + $document = new DOMDocument(); + @$document->loadHTML((string) $file['content']); + $list = (string) (new DOMXPath($document))->evaluate('string(//nav//ul/@class)'); + $assert('menu-top' === $list, 'The menu list keeps the class its shared rules name in ' . $file['path'] . ', got "' . $list . '".', (string) $file['content']); +} + +echo "route-aligned-chrome-class-shared-stylesheet contract passed\n"; From 4c157985c5c6d5fca59552f0901cbc02674d4abe Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Mon, 16 Nov 2026 03:10:09 -0500 Subject: [PATCH 2/2] Copy multi-alias selectors once per alias plus once with every alias --- .../src/ArtifactCompiler/RouteStateProjector.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/php-transformer/src/ArtifactCompiler/RouteStateProjector.php b/php-transformer/src/ArtifactCompiler/RouteStateProjector.php index 3822fbda90..cf3037ae19 100644 --- a/php-transformer/src/ArtifactCompiler/RouteStateProjector.php +++ b/php-transformer/src/ArtifactCompiler/RouteStateProjector.php @@ -723,21 +723,15 @@ private static function withRouteAlignedChromeClasses(array $files): array } // Each rule naming an aliased class gains a copy for its alias right // after it, so both names keep their place in the cascade. A selector - // naming several aliased classes gains a copy for each set of them: - // aligned chrome wears every alias at once, while unaligned elements - // keep the source names. + // naming several aliased classes also gains one copy naming all of + // them, because aligned chrome wears every alias at once. $withCopies = static fn(string $css, array $aliases): string => $transformer->transform($css, static function (string $prelude, string $body) use ($aliases, $classPattern, $named): string|array { $copies = array(); foreach (CssStylesheetTransformer::splitSelectorList($prelude) ?? array() as $selector) { preg_match_all($classPattern, $selector, $classes); $aliased = array_values(array_filter(array_unique($classes[1]), static fn(string $class): bool => isset($aliases[$class]))); - $sets = array(); - if (count($aliased) <= 4) { - for ($mask = 1; $mask < 1 << count($aliased); ++$mask) $sets[] = array_values(array_filter($aliased, static fn(int $bit): bool => 0 !== ($mask & 1 << $bit), ARRAY_FILTER_USE_KEY)); - usort($sets, static fn(array $a, array $b): int => count($a) <=> count($b)); - } else { - $sets = array_merge(array_map(static fn(string $class): array => array($class), $aliased), array($aliased)); - } + $sets = array_map(static fn(string $class): array => array($class), $aliased); + if (count($aliased) > 1) $sets[] = $aliased; foreach ($sets as $set) { $copy = trim($selector); foreach ($set as $class) $copy = (string) preg_replace($named($class), '.' . $aliases[$class], $copy);