diff --git a/php-transformer/src/ArtifactCompiler/RouteStateProjector.php b/php-transformer/src/ArtifactCompiler/RouteStateProjector.php index 802202bba..cf3037ae1 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,21 @@ 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 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); - 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_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); + $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 000000000..8419e6265 --- /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 000000000..626858f8a --- /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";