Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions php-transformer/src/ArtifactCompiler/RouteStateProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_-])/';
Expand Down Expand Up @@ -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));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);

/**
* Route alignment renames a chrome class whose rules read differently on
* another route. A selector can name two such classes, an ancestor and its
* descendant, and chrome then wears both aliases and neither source name.
* The aliased copy of that rule renames every aligned class it names, so it
* still matches the aligned chrome: a footer's two-column definition list
* keeps its floats and widths on every route.
*/

require dirname(__DIR__, 2) . '/vendor/autoload.php';

use Automattic\BlocksEngine\PhpTransformer\ArtifactCompiler\RouteStateProjector;

$assert = static function (bool $condition, string $message, string $detail = ''): void {
if (!$condition) throw new RuntimeException($message . ('' === $detail ? '' : "\n" . substr($detail, 0, 3000)));
};

$columns = '.hours .hours-list dt{float:left;width:40%}.hours .hours-list dd{float:right;width:59%}';
$footer = '<footer><div class="hours"><dl class="hours-list"><div><dt>Monday</dt><dd>9 to 5</dd></div><div><dt>Tuesday</dt><dd>Closed</dd></div></dl></div></footer>';
$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' => '<!doctype html><html><head><link rel="stylesheet" href="/assets/' . $sheet . '.css"></head><body><main><h1>' . $sheet . '</h1></main>' . $footer . '</body></html>');
}

$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";
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

/**
* Route alignment reads a stylesheet file that only one route links as that
* route's own sheet. A file several routes link is shared vocabulary: its
* classes mean the same on every route, even when one route also links a
* widget sheet that adds a rule for one of them. Chrome keeps those source
* names, so the shared file's nested, attribute-gated rules (a vertical
* menu's item separators) still reach the menu they were written for.
*/

require dirname(__DIR__, 2) . '/vendor/autoload.php';

use Automattic\BlocksEngine\PhpTransformer\ArtifactCompiler\RouteStateProjector;

$assert = static function (bool $condition, string $message, string $detail = ''): void {
if (!$condition) throw new RuntimeException($message . ('' === $detail ? '' : "\n" . substr($detail, 0, 3000)));
};

$runtime = '.menu[data-structure=vertical]{display:block;&[data-layout=lined] .menu-top>.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 = '<footer><nav class="menu" data-structure="vertical" data-layout="lined"><ul class="menu-top"><li class="menu-item"><a href="/">Home</a></li><li class="menu-item"><a href="/contact/">Contact</a></li></ul></nav></footer>';
foreach (array('index.html' => '', 'contact/index.html' => '<link rel="stylesheet" href="/assets/contact-widget.css">') as $path => $widget) {
$files[] = array('path' => $path, 'kind' => 'html', 'content' => '<!doctype html><html><head><link rel="stylesheet" href="/assets/runtime.css">' . $widget . '</head><body><main><form class="contact-form"></form></main>' . $menu . '</body></html>');
}

$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";
Loading