From d41b6dbaeedf1dea99c0c88f6824d99c8c16743e Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 13:57:55 -0500 Subject: [PATCH 01/11] fix: investigate whole-site theme CSS weight --- .../src/ArtifactCompiler/ArtifactCompiler.php | 9 ++++++++ .../tests/unit/inline-selector-pruning.php | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 php-transformer/tests/unit/inline-selector-pruning.php diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index 467d9a4d0..3548c4357 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -2125,6 +2125,15 @@ private function withoutUnreachableSelectors(array $files): array // Metadata carries captured interaction states. if (is_array($file['metadata'] ?? null) && array() !== $file['metadata']) $texts[] = (string) json_encode($file['metadata']); $text = $file['content'] ?? (in_array($file['kind'] ?? '', array('html', 'js', 'json', 'svg', 'text'), true) && is_string($file['content_base64'] ?? null) ? base64_decode($file['content_base64'], true) : null); + if (is_string($text) && 'html' === ($file['kind'] ?? '')) { + // Inline sheets are CSS too, not evidence that their selectors + // occur in markup. Otherwise every unused inline rule proves + // itself live (and can also keep linked framework rules live). + foreach (array_reverse(StyleTagScanner::scan($text)) as $style) { + if (!StyleTagScanner::isCssType(HtmlTagScanner::attribute($style['attributes'], 'type'))) continue; + $text = substr_replace($text, '', $style['offset'], $style['end_offset'] - $style['offset']); + } + } if (is_string($text) && '' !== $text) $texts[] = $text; } if (0 === $documents) return $files; diff --git a/php-transformer/tests/unit/inline-selector-pruning.php b/php-transformer/tests/unit/inline-selector-pruning.php new file mode 100644 index 000000000..6db76db26 --- /dev/null +++ b/php-transformer/tests/unit/inline-selector-pruning.php @@ -0,0 +1,22 @@ +compile(array('entrypoint' => 'index.html', 'files' => array( + 'index.html' => '

Home

', + 'other.html' => '

Other route

', + 'site.css' => '.unused-inline{background:salmon}.other-route{background:teal}', +)))->toArray(); +$css = implode("\n", array_map(static fn (array $asset): string => (string) ($asset['content'] ?? ''), array_filter($compiled['assets'] ?? array(), static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)))); +foreach (array('salmon' => false, 'teal' => true, 'purple' => true) as $needle => $expected) { + if (str_contains($css, $needle) !== $expected) throw new RuntimeException('Unexpected inline selector reachability for ' . $needle); +} +echo "Inline selector pruning passed\n"; From a3434d9393210f36ef397927a46184ad997c8176 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 13:57:55 -0500 Subject: [PATCH 02/11] fix: restore author selector preservation during compilation --- .../src/ArtifactCompiler/ArtifactCompiler.php | 9 -------- .../tests/unit/inline-selector-pruning.php | 23 +++++++++---------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index 3548c4357..467d9a4d0 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -2125,15 +2125,6 @@ private function withoutUnreachableSelectors(array $files): array // Metadata carries captured interaction states. if (is_array($file['metadata'] ?? null) && array() !== $file['metadata']) $texts[] = (string) json_encode($file['metadata']); $text = $file['content'] ?? (in_array($file['kind'] ?? '', array('html', 'js', 'json', 'svg', 'text'), true) && is_string($file['content_base64'] ?? null) ? base64_decode($file['content_base64'], true) : null); - if (is_string($text) && 'html' === ($file['kind'] ?? '')) { - // Inline sheets are CSS too, not evidence that their selectors - // occur in markup. Otherwise every unused inline rule proves - // itself live (and can also keep linked framework rules live). - foreach (array_reverse(StyleTagScanner::scan($text)) as $style) { - if (!StyleTagScanner::isCssType(HtmlTagScanner::attribute($style['attributes'], 'type'))) continue; - $text = substr_replace($text, '', $style['offset'], $style['end_offset'] - $style['offset']); - } - } if (is_string($text) && '' !== $text) $texts[] = $text; } if (0 === $documents) return $files; diff --git a/php-transformer/tests/unit/inline-selector-pruning.php b/php-transformer/tests/unit/inline-selector-pruning.php index 6db76db26..a16964833 100644 --- a/php-transformer/tests/unit/inline-selector-pruning.php +++ b/php-transformer/tests/unit/inline-selector-pruning.php @@ -5,18 +5,17 @@ use Automattic\BlocksEngine\PhpTransformer\ArtifactCompiler\ArtifactCompiler; -// Inline CSS must not prove its own selectors live, nor keep the same -// absent selector alive in a linked sheet. Markup on another route and -// script-built classes still count as whole-site evidence. +// Author selectors also define the specificity ceiling for projected inline +// document styles. An unmatched selector cannot simply be erased before that +// ceiling is computed, even though it does not currently match the document. $compiled = (new ArtifactCompiler())->compile(array('entrypoint' => 'index.html', 'files' => array( - 'index.html' => '

Home

', - 'other.html' => '

Other route

', - 'site.css' => '.unused-inline{background:salmon}.other-route{background:teal}', + 'index.html' => '
Home
', )))->toArray(); -$css = implode("\n", array_map(static fn (array $asset): string => (string) ($asset['content'] ?? ''), array_filter($compiled['assets'] ?? array(), static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)))); -foreach (array('salmon' => false, 'teal' => true, 'purple' => true) as $needle => $expected) { - if (str_contains($css, $needle) !== $expected) throw new RuntimeException('Unexpected inline selector reachability for ' . $needle); +$frontend = ''; +foreach ($compiled['source_reports']['wordpress_site_plan']['assets'] as $asset) { + if ('frontend' === ($asset['stylesheet_target'] ?? 'both')) $frontend .= $asset['content'] ?? ''; } -echo "Inline selector pruning passed\n"; +if (!preg_match('/,(#[a-zA-Z0-9_-]+)\1\1\)/', $frontend)) { + throw new RuntimeException('Inline root carrier must retain the author specificity ceiling, including unmatched author IDs'); +} +echo "Inline selector specificity preservation passed\n"; From 9e96d683242a49a43e2d860d6ebadef0a278d680 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 13:57:55 -0500 Subject: [PATCH 03/11] fix: prune inline selector evidence while preserving author specificity --- .../src/ArtifactCompiler/ArtifactCompiler.php | 13 ++++++++++--- .../tests/unit/inline-selector-pruning.php | 13 ++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index 467d9a4d0..e44724166 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -551,7 +551,7 @@ private function finalizeArtifact(array $artifact, array $reduction, ?\Automatti if (isset($generatedAssetIdentities[$identity])) continue; $generatedAssetIdentities[$identity] = true; $generatedAssets[] = $asset; } - $rootInlineAsset = $this->documentInlineStyleAsset($normalized['files']); + $rootInlineAsset = $this->documentInlineStyleAsset($normalized['files'], $authoredFiles); if (null !== $rootInlineAsset) $generatedAssets[] = $rootInlineAsset; $presentationalHintAsset = $this->documentPresentationalHintAsset($normalized['files']); if (null !== $presentationalHintAsset) $generatedAssets[] = $presentationalHintAsset; @@ -2125,6 +2125,13 @@ private function withoutUnreachableSelectors(array $files): array // Metadata carries captured interaction states. if (is_array($file['metadata'] ?? null) && array() !== $file['metadata']) $texts[] = (string) json_encode($file['metadata']); $text = $file['content'] ?? (in_array($file['kind'] ?? '', array('html', 'js', 'json', 'svg', 'text'), true) && is_string($file['content_base64'] ?? null) ? base64_decode($file['content_base64'], true) : null); + if (is_string($text) && 'html' === ($file['kind'] ?? '')) { + // Stylesheet selectors are not evidence of markup reachability. + foreach (array_reverse(StyleTagScanner::scan($text)) as $style) { + if (!StyleTagScanner::isCssType(HtmlTagScanner::attribute($style['attributes'], 'type'))) continue; + $text = substr_replace($text, '', $style['offset'], $style['end_offset'] - $style['offset']); + } + } if (is_string($text) && '' !== $text) $texts[] = $text; } if (0 === $documents) return $files; @@ -2346,7 +2353,7 @@ private function documentPresentationalHintAsset(array $files): ?array } /** Front-end-only document mechanics; never part of the editor carrier. */ - private function documentInlineStyleAsset(array $files): ?array + private function documentInlineStyleAsset(array $files, array $authoredFiles): ?array { $documents = array(); foreach ($files as $file) { @@ -2362,7 +2369,7 @@ private function documentInlineStyleAsset(array $files): ?array // ID weight without an authored ID, like InlineGeometry's carriers. $ids = 0; $transformer = new CssStylesheetTransformer(); - foreach ($files as $file) { + foreach (array_merge($files, $authoredFiles) as $file) { if ('css' !== ($file['kind'] ?? '')) continue; $transformer->visitStyleRules((string) ($file['content'] ?? ''), static function (string $prelude) use (&$ids): void { foreach (CssStylesheetTransformer::splitSelectorList($prelude) ?? array() as $selector) { diff --git a/php-transformer/tests/unit/inline-selector-pruning.php b/php-transformer/tests/unit/inline-selector-pruning.php index a16964833..6dac15efe 100644 --- a/php-transformer/tests/unit/inline-selector-pruning.php +++ b/php-transformer/tests/unit/inline-selector-pruning.php @@ -18,4 +18,15 @@ if (!preg_match('/,(#[a-zA-Z0-9_-]+)\1\1\)/', $frontend)) { throw new RuntimeException('Inline root carrier must retain the author specificity ceiling, including unmatched author IDs'); } -echo "Inline selector specificity preservation passed\n"; +// The absent author IDs still set the ceiling, but neither inline nor linked +// rules may use the inline sheet itself as evidence that an absent class lives. +$compiled = (new ArtifactCompiler())->compile(array('entrypoint' => 'index.html', 'files' => array( + 'index.html' => '
Home
', + 'other.html' => '
Other
', + 'site.css' => '.unused-inline{background:salmon}.other-route{background:teal}', +)))->toArray(); +$css = implode("\n", array_map(static fn (array $asset): string => (string) ($asset['content'] ?? ''), array_filter($compiled['assets'] ?? array(), static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)))); +foreach (array('salmon' => false, 'teal' => true, 'purple' => true) as $needle => $expected) { + if (str_contains($css, $needle) !== $expected) throw new RuntimeException('Unexpected inline selector reachability for ' . $needle); +} +echo "Inline selector pruning and specificity preservation passed\n"; From 21c699207bc577089194070769ea56f5b141a806 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 13:57:55 -0500 Subject: [PATCH 04/11] fix: preserve author selectors and deduplicate coalesced alternatives --- .../src/ArtifactCompiler/ArtifactCompiler.php | 10 ++------ .../src/Css/CssSelectorListCompactor.php | 24 +++++++++++++++++++ .../Style/DeclarationAddressedClasses.php | 4 ++-- .../unit/coalesced-selector-deduplication.php | 16 +++++++++++++ .../tests/unit/inline-selector-pruning.php | 6 ++--- 5 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 php-transformer/tests/unit/coalesced-selector-deduplication.php diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index e44724166..927da91e6 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -2088,7 +2088,8 @@ private function projectedStylesheetFile(array $file, string $path, string $cont { $file['provenance']['projected_from_bytes'] ??= (int) ($file['bytes'] ?? strlen((string) ($file['content'] ?? ''))); $file['path'] = $path; - $file = ArtifactNormalizer::withTextPayload($file, $content); + $file = ArtifactNormalizer::withTextPayload($file, CssSelectorListCompactor::stylesheet($content)); + $content = $file['content']; $file['provenance']['projected_from_hash'] = $file['provenance']['hash'] ?? ''; $file['provenance']['hash'] = hash('sha256', $content); return $file; @@ -2125,13 +2126,6 @@ private function withoutUnreachableSelectors(array $files): array // Metadata carries captured interaction states. if (is_array($file['metadata'] ?? null) && array() !== $file['metadata']) $texts[] = (string) json_encode($file['metadata']); $text = $file['content'] ?? (in_array($file['kind'] ?? '', array('html', 'js', 'json', 'svg', 'text'), true) && is_string($file['content_base64'] ?? null) ? base64_decode($file['content_base64'], true) : null); - if (is_string($text) && 'html' === ($file['kind'] ?? '')) { - // Stylesheet selectors are not evidence of markup reachability. - foreach (array_reverse(StyleTagScanner::scan($text)) as $style) { - if (!StyleTagScanner::isCssType(HtmlTagScanner::attribute($style['attributes'], 'type'))) continue; - $text = substr_replace($text, '', $style['offset'], $style['end_offset'] - $style['offset']); - } - } if (is_string($text) && '' !== $text) $texts[] = $text; } if (0 === $documents) return $files; diff --git a/php-transformer/src/Css/CssSelectorListCompactor.php b/php-transformer/src/Css/CssSelectorListCompactor.php index c00bee12a..19b4deaf8 100644 --- a/php-transformer/src/Css/CssSelectorListCompactor.php +++ b/php-transformer/src/Css/CssSelectorListCompactor.php @@ -250,6 +250,7 @@ public static function compact(string $prelude): string if ( ! str_contains($prelude, ',') ) { return $prelude; } + $prelude = self::uniqueSelectorArms($prelude); $prelude = self::mergeNthChildRuns($prelude); if ( ! str_contains($prelude, ',') ) { return $prelude; @@ -325,6 +326,29 @@ public static function compact(string $prelude): string return implode(',', $output); } + /** Duplicate alternatives do not affect matching or specificity, including inside :is(). */ + private static function uniqueSelectorArms(string $selector): string + { + for ($i = 0; $i < strlen($selector); ++$i) { + if ('[' === $selector[$i]) { + $close = CssSyntaxScanner::matchingClose($selector, $i); + if (null === $close) return $selector; + $i = $close; + } elseif ('(' === $selector[$i]) { + $close = CssSyntaxScanner::matchingClose($selector, $i); + if (null === $close) return $selector; + if (preg_match('/:(?:is|where|not|has)$/i', substr($selector, 0, $i))) { + $inner = self::uniqueSelectorArms(substr($selector, $i + 1, $close - $i - 1)); + $selector = substr_replace($selector, $inner, $i + 1, $close - $i - 1); + $close = $i + strlen($inner) + 1; + } + $i = $close; + } + } + $arms = CssStylesheetTransformer::splitSelectorList($selector); + return null === $arms ? $selector : implode(',', array_unique(array_map('trim', $arms))); + } + /** @var array Bounded syntax-only cache; never holds document state. */ private static array $weights = array(); diff --git a/php-transformer/src/HtmlToBlocks/Style/DeclarationAddressedClasses.php b/php-transformer/src/HtmlToBlocks/Style/DeclarationAddressedClasses.php index 0714261ef..47ab73b66 100644 --- a/php-transformer/src/HtmlToBlocks/Style/DeclarationAddressedClasses.php +++ b/php-transformer/src/HtmlToBlocks/Style/DeclarationAddressedClasses.php @@ -169,9 +169,9 @@ public function rewrite(mixed $value, array $replacements): mixed /** Coalesced identities must not leave repeated alternatives in published CSS. */ private function deduplicateMarkerArms(string $css): string { - if (!str_contains($css, 'blocks-engine-control-')) return $css; + if (!str_contains($css, 'blocks-engine-')) return $css; return (new CssStylesheetTransformer())->transformStyleRules($css, static function (string $selector, string $body): string { - if (!str_contains($selector, 'blocks-engine-control-')) return $selector . '{' . $body . '}'; + if (!str_contains($selector, 'blocks-engine-')) return $selector . '{' . $body . '}'; $arms = CssStylesheetTransformer::splitSelectorList(CssSelectorListCompactor::expand($selector)); return CssSelectorListCompactor::compact(implode(',', array_values(array_unique($arms ?? array($selector))))) . '{' . $body . '}'; }); diff --git a/php-transformer/tests/unit/coalesced-selector-deduplication.php b/php-transformer/tests/unit/coalesced-selector-deduplication.php new file mode 100644 index 000000000..629fddcad --- /dev/null +++ b/php-transformer/tests/unit/coalesced-selector-deduplication.php @@ -0,0 +1,16 @@ + img,:is(.same,.same) > img{color:red}'; +$output = CssSelectorListCompactor::stylesheet($input); +if (':is(.same) > img{color:red}' !== $output) { + throw new RuntimeException('Coalesced identities must not repeat top-level or logical selector alternatives: ' . $output); +} +$input = '.x[data-value="a,b"],.x[data-value="a,b"]{color:blue}'; +if ('.x[data-value="a,b"]{color:blue}' !== CssSelectorListCompactor::stylesheet($input)) { + throw new RuntimeException('Commas inside attribute values are not selector boundaries'); +} +echo "Coalesced selector deduplication passed\n"; diff --git a/php-transformer/tests/unit/inline-selector-pruning.php b/php-transformer/tests/unit/inline-selector-pruning.php index 6dac15efe..285dddc8a 100644 --- a/php-transformer/tests/unit/inline-selector-pruning.php +++ b/php-transformer/tests/unit/inline-selector-pruning.php @@ -18,15 +18,15 @@ if (!preg_match('/,(#[a-zA-Z0-9_-]+)\1\1\)/', $frontend)) { throw new RuntimeException('Inline root carrier must retain the author specificity ceiling, including unmatched author IDs'); } -// The absent author IDs still set the ceiling, but neither inline nor linked -// rules may use the inline sheet itself as evidence that an absent class lives. +// Inline selectors remain author evidence: incomplete markup and responsive +// variants must not cause authored rules to disappear. $compiled = (new ArtifactCompiler())->compile(array('entrypoint' => 'index.html', 'files' => array( 'index.html' => '
Home
', 'other.html' => '
Other
', 'site.css' => '.unused-inline{background:salmon}.other-route{background:teal}', )))->toArray(); $css = implode("\n", array_map(static fn (array $asset): string => (string) ($asset['content'] ?? ''), array_filter($compiled['assets'] ?? array(), static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)))); -foreach (array('salmon' => false, 'teal' => true, 'purple' => true) as $needle => $expected) { +foreach (array('salmon' => true, 'teal' => true, 'purple' => true) as $needle => $expected) { if (str_contains($css, $needle) !== $expected) throw new RuntimeException('Unexpected inline selector reachability for ' . $needle); } echo "Inline selector pruning and specificity preservation passed\n"; From 082f9b00cc797ab2a3987552e4a84b73a8d5e35e Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 13:57:55 -0500 Subject: [PATCH 05/11] fix: restore selector compaction boundaries --- .../src/ArtifactCompiler/ArtifactCompiler.php | 3 +-- .../src/Css/CssSelectorListCompactor.php | 24 ------------------- .../Style/DeclarationAddressedClasses.php | 4 ++-- .../unit/coalesced-selector-deduplication.php | 16 ------------- 4 files changed, 3 insertions(+), 44 deletions(-) delete mode 100644 php-transformer/tests/unit/coalesced-selector-deduplication.php diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index 927da91e6..9b4341f17 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -2088,8 +2088,7 @@ private function projectedStylesheetFile(array $file, string $path, string $cont { $file['provenance']['projected_from_bytes'] ??= (int) ($file['bytes'] ?? strlen((string) ($file['content'] ?? ''))); $file['path'] = $path; - $file = ArtifactNormalizer::withTextPayload($file, CssSelectorListCompactor::stylesheet($content)); - $content = $file['content']; + $file = ArtifactNormalizer::withTextPayload($file, $content); $file['provenance']['projected_from_hash'] = $file['provenance']['hash'] ?? ''; $file['provenance']['hash'] = hash('sha256', $content); return $file; diff --git a/php-transformer/src/Css/CssSelectorListCompactor.php b/php-transformer/src/Css/CssSelectorListCompactor.php index 19b4deaf8..c00bee12a 100644 --- a/php-transformer/src/Css/CssSelectorListCompactor.php +++ b/php-transformer/src/Css/CssSelectorListCompactor.php @@ -250,7 +250,6 @@ public static function compact(string $prelude): string if ( ! str_contains($prelude, ',') ) { return $prelude; } - $prelude = self::uniqueSelectorArms($prelude); $prelude = self::mergeNthChildRuns($prelude); if ( ! str_contains($prelude, ',') ) { return $prelude; @@ -326,29 +325,6 @@ public static function compact(string $prelude): string return implode(',', $output); } - /** Duplicate alternatives do not affect matching or specificity, including inside :is(). */ - private static function uniqueSelectorArms(string $selector): string - { - for ($i = 0; $i < strlen($selector); ++$i) { - if ('[' === $selector[$i]) { - $close = CssSyntaxScanner::matchingClose($selector, $i); - if (null === $close) return $selector; - $i = $close; - } elseif ('(' === $selector[$i]) { - $close = CssSyntaxScanner::matchingClose($selector, $i); - if (null === $close) return $selector; - if (preg_match('/:(?:is|where|not|has)$/i', substr($selector, 0, $i))) { - $inner = self::uniqueSelectorArms(substr($selector, $i + 1, $close - $i - 1)); - $selector = substr_replace($selector, $inner, $i + 1, $close - $i - 1); - $close = $i + strlen($inner) + 1; - } - $i = $close; - } - } - $arms = CssStylesheetTransformer::splitSelectorList($selector); - return null === $arms ? $selector : implode(',', array_unique(array_map('trim', $arms))); - } - /** @var array Bounded syntax-only cache; never holds document state. */ private static array $weights = array(); diff --git a/php-transformer/src/HtmlToBlocks/Style/DeclarationAddressedClasses.php b/php-transformer/src/HtmlToBlocks/Style/DeclarationAddressedClasses.php index 47ab73b66..0714261ef 100644 --- a/php-transformer/src/HtmlToBlocks/Style/DeclarationAddressedClasses.php +++ b/php-transformer/src/HtmlToBlocks/Style/DeclarationAddressedClasses.php @@ -169,9 +169,9 @@ public function rewrite(mixed $value, array $replacements): mixed /** Coalesced identities must not leave repeated alternatives in published CSS. */ private function deduplicateMarkerArms(string $css): string { - if (!str_contains($css, 'blocks-engine-')) return $css; + if (!str_contains($css, 'blocks-engine-control-')) return $css; return (new CssStylesheetTransformer())->transformStyleRules($css, static function (string $selector, string $body): string { - if (!str_contains($selector, 'blocks-engine-')) return $selector . '{' . $body . '}'; + if (!str_contains($selector, 'blocks-engine-control-')) return $selector . '{' . $body . '}'; $arms = CssStylesheetTransformer::splitSelectorList(CssSelectorListCompactor::expand($selector)); return CssSelectorListCompactor::compact(implode(',', array_values(array_unique($arms ?? array($selector))))) . '{' . $body . '}'; }); diff --git a/php-transformer/tests/unit/coalesced-selector-deduplication.php b/php-transformer/tests/unit/coalesced-selector-deduplication.php deleted file mode 100644 index 629fddcad..000000000 --- a/php-transformer/tests/unit/coalesced-selector-deduplication.php +++ /dev/null @@ -1,16 +0,0 @@ - img,:is(.same,.same) > img{color:red}'; -$output = CssSelectorListCompactor::stylesheet($input); -if (':is(.same) > img{color:red}' !== $output) { - throw new RuntimeException('Coalesced identities must not repeat top-level or logical selector alternatives: ' . $output); -} -$input = '.x[data-value="a,b"],.x[data-value="a,b"]{color:blue}'; -if ('.x[data-value="a,b"]{color:blue}' !== CssSelectorListCompactor::stylesheet($input)) { - throw new RuntimeException('Commas inside attribute values are not selector boundaries'); -} -echo "Coalesced selector deduplication passed\n"; From 66b785b00270ce5cabdb2a77124146d7d1bf6508 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 13:57:55 -0500 Subject: [PATCH 06/11] fix: exclude inline styles from selector liveness evidence --- php-transformer/src/ArtifactCompiler/ArtifactCompiler.php | 6 ++++++ php-transformer/tests/unit/inline-selector-pruning.php | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index 9b4341f17..2ce50a042 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -2125,6 +2125,12 @@ private function withoutUnreachableSelectors(array $files): array // Metadata carries captured interaction states. if (is_array($file['metadata'] ?? null) && array() !== $file['metadata']) $texts[] = (string) json_encode($file['metadata']); $text = $file['content'] ?? (in_array($file['kind'] ?? '', array('html', 'js', 'json', 'svg', 'text'), true) && is_string($file['content_base64'] ?? null) ? base64_decode($file['content_base64'], true) : null); + // Stylesheet declarations are not evidence that their selector + // subjects exist. Inline sheets must obey the same boundary as + // linked CSS; retain scripts and markup as interaction evidence. + if ('html' === ($file['kind'] ?? '') && is_string($text)) { + $text = (string) preg_replace('#]*>.*?#is', '', $text); + } if (is_string($text) && '' !== $text) $texts[] = $text; } if (0 === $documents) return $files; diff --git a/php-transformer/tests/unit/inline-selector-pruning.php b/php-transformer/tests/unit/inline-selector-pruning.php index 285dddc8a..b45373bfe 100644 --- a/php-transformer/tests/unit/inline-selector-pruning.php +++ b/php-transformer/tests/unit/inline-selector-pruning.php @@ -18,15 +18,15 @@ if (!preg_match('/,(#[a-zA-Z0-9_-]+)\1\1\)/', $frontend)) { throw new RuntimeException('Inline root carrier must retain the author specificity ceiling, including unmatched author IDs'); } -// Inline selectors remain author evidence: incomplete markup and responsive -// variants must not cause authored rules to disappear. +// Inline sheets are not subject evidence. Cross-route markup and dynamic +// scripts still keep the selectors they can render alive. $compiled = (new ArtifactCompiler())->compile(array('entrypoint' => 'index.html', 'files' => array( 'index.html' => '
Home
', 'other.html' => '
Other
', 'site.css' => '.unused-inline{background:salmon}.other-route{background:teal}', )))->toArray(); $css = implode("\n", array_map(static fn (array $asset): string => (string) ($asset['content'] ?? ''), array_filter($compiled['assets'] ?? array(), static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)))); -foreach (array('salmon' => true, 'teal' => true, 'purple' => true) as $needle => $expected) { +foreach (array('salmon' => false, 'teal' => true, 'purple' => true) as $needle => $expected) { if (str_contains($css, $needle) !== $expected) throw new RuntimeException('Unexpected inline selector reachability for ' . $needle); } echo "Inline selector pruning and specificity preservation passed\n"; From de7b93d6b270d165aac8a581c7180ba9ce39e850 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 13:57:55 -0500 Subject: [PATCH 07/11] fix: prune unreachable escaped CSS selectors --- .../src/Css/UnreachableSelectorPruner.php | 20 ++++++++++++++++++- .../tests/unit/escaped-selector-pruning.php | 15 ++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 php-transformer/tests/unit/escaped-selector-pruning.php diff --git a/php-transformer/src/Css/UnreachableSelectorPruner.php b/php-transformer/src/Css/UnreachableSelectorPruner.php index 0d931aa2b..2a1887d8e 100644 --- a/php-transformer/src/Css/UnreachableSelectorPruner.php +++ b/php-transformer/src/Css/UnreachableSelectorPruner.php @@ -31,10 +31,12 @@ final class UnreachableSelectorPruner private array $live = array(); /** @var list */ private array $livePrefixes = array(); + private string $evidenceText; /** @param list $texts Every non-stylesheet file of the site, including its metadata. */ public function __construct(array $texts) { + $this->evidenceText = implode("\n", $texts); $prefixes = array(); foreach ($texts as $text) { if (preg_match_all('/[\w-]+/', $text, $matches)) $this->live += array_fill_keys($matches[0], true); @@ -67,7 +69,16 @@ public function prune(string $css): string public function reachable(string $selector): bool { $selector = (string) preg_replace('#/\*.*?\*/#s', ' ', $selector); - if (str_contains($selector, '\\') || 1 === preg_match('/[^\x20-\x7E\t\n\r]/', $selector)) return true; + if (1 === preg_match('/[^\x20-\x7E\t\n\r]/', $selector)) return true; + // Escaped punctuation belongs to the identifier, not selector syntax. + // Mask it before walking brackets and functional pseudo-classes so + // utility names such as md:grid and w-[50%] can be checked too. + $selector = (string) preg_replace_callback('/\\\\(?:[0-9a-fA-F]{1,6}\s?|[^\r\n\f])/u', static function (array $match): string { + $escape = substr($match[0], 1); + $value = preg_match('/^[0-9a-fA-F]/', $escape) ? mb_chr(hexdec(trim($escape)), 'UTF-8') : $escape; + return 'BEESC' . bin2hex($value) . 'X'; + }, $selector); + if (str_contains($selector, '\\')) return true; foreach (self::requiredTokens($selector) as $token) { if (!$this->sourceNamed($token)) return false; } @@ -95,6 +106,13 @@ public function reachable(string $selector): bool private function sourceNamed(string $token): bool { + if (str_contains($token, 'BEESC')) { + $decoded = (string) preg_replace_callback('/BEESC([0-9a-f]+)X/', static fn (array $match): string => (string) hex2bin($match[1]), $token); + // Substring evidence is deliberately conservative: scripts may + // construct these names without literal class attributes. + if (str_contains($this->evidenceText, $decoded)) return true; + $token = $decoded; + } if (isset($this->live[$token]) || 1 === preg_match(self::MINTED, $token) || 1 === preg_match('/^' . SourceClassIdentity::REVEAL_STATE . '$/i', $token) || SourceClassIdentity::containsCurrentStateToken($token)) return true; foreach ($this->livePrefixes as $prefix) { diff --git a/php-transformer/tests/unit/escaped-selector-pruning.php b/php-transformer/tests/unit/escaped-selector-pruning.php new file mode 100644 index 000000000..ac808a4d5 --- /dev/null +++ b/php-transformer/tests/unit/escaped-selector-pruning.php @@ -0,0 +1,15 @@ +compile(array('entrypoint' => 'index.html', 'files' => array( + 'index.html' => '
Home
', + 'site.css' => '.md\\:grid{display:grid}.w-\\[50\\%\\]{width:50%}.md\\:absent{color:salmon}.w-\\[123px\\]{width:123px}', +)))->toArray(); +$css = implode("\n", array_map(static fn (array $asset): string => (string) ($asset['content'] ?? ''), array_filter($result['assets'], static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)))); +foreach (array('display:grid' => true, 'width:50%' => true, 'salmon' => false, '123px' => false) as $needle => $expected) { + if (str_contains($css, $needle) !== $expected) throw new RuntimeException('Unexpected escaped selector reachability: ' . $needle); +} +echo "Escaped selector pruning passed\n"; From 73cef71937851b969b9ad584d5ff3bee81eeb723 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 18:57:55 +0000 Subject: [PATCH 08/11] fix: preserve inline author evidence during CSS pruning --- .../src/ArtifactCompiler/ArtifactCompiler.php | 8 ++---- .../unit/inline-author-liveness-boundary.php | 25 +++++++++++++++++++ .../tests/unit/inline-selector-pruning.php | 6 ++--- 3 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 php-transformer/tests/unit/inline-author-liveness-boundary.php diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index 2ce50a042..e61a43860 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -2125,12 +2125,8 @@ private function withoutUnreachableSelectors(array $files): array // Metadata carries captured interaction states. if (is_array($file['metadata'] ?? null) && array() !== $file['metadata']) $texts[] = (string) json_encode($file['metadata']); $text = $file['content'] ?? (in_array($file['kind'] ?? '', array('html', 'js', 'json', 'svg', 'text'), true) && is_string($file['content_base64'] ?? null) ? base64_decode($file['content_base64'], true) : null); - // Stylesheet declarations are not evidence that their selector - // subjects exist. Inline sheets must obey the same boundary as - // linked CSS; retain scripts and markup as interaction evidence. - if ('html' === ($file['kind'] ?? '') && is_string($text)) { - $text = (string) preg_replace('#]*>.*?#is', '', $text); - } + // Preserve inline author sheets as conservative evidence: partial + // documents and responsive variants need not contain their subjects. if (is_string($text) && '' !== $text) $texts[] = $text; } if (0 === $documents) return $files; diff --git a/php-transformer/tests/unit/inline-author-liveness-boundary.php b/php-transformer/tests/unit/inline-author-liveness-boundary.php new file mode 100644 index 000000000..57605a73f --- /dev/null +++ b/php-transformer/tests/unit/inline-author-liveness-boundary.php @@ -0,0 +1,25 @@ +compile(array( + 'entrypoint' => 'index.html', + 'files' => array( + 'index.html' => '
Partial document
', + 'framework.css' => '.partial-label{background:teal}.absent-widget{color:purple}', + ), +))->toArray(); +$css = implode("\n", array_map( + static fn (array $asset): string => (string) ($asset['content'] ?? ''), + array_filter($result['assets'], static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)) +)); +foreach (array('salmon' => true, 'teal' => true, 'purple' => false) as $value => $expected) { + if (str_contains($css, $value) !== $expected) { + throw new RuntimeException('Inline author liveness boundary: ' . $value); + } +} +echo "Inline author liveness boundary passed\n"; diff --git a/php-transformer/tests/unit/inline-selector-pruning.php b/php-transformer/tests/unit/inline-selector-pruning.php index b45373bfe..cf70760c0 100644 --- a/php-transformer/tests/unit/inline-selector-pruning.php +++ b/php-transformer/tests/unit/inline-selector-pruning.php @@ -18,15 +18,15 @@ if (!preg_match('/,(#[a-zA-Z0-9_-]+)\1\1\)/', $frontend)) { throw new RuntimeException('Inline root carrier must retain the author specificity ceiling, including unmatched author IDs'); } -// Inline sheets are not subject evidence. Cross-route markup and dynamic -// scripts still keep the selectors they can render alive. +// Inline sheets remain conservative evidence for partial documents and +// responsive variants. Cross-route markup and dynamic scripts stay live too. $compiled = (new ArtifactCompiler())->compile(array('entrypoint' => 'index.html', 'files' => array( 'index.html' => '
Home
', 'other.html' => '
Other
', 'site.css' => '.unused-inline{background:salmon}.other-route{background:teal}', )))->toArray(); $css = implode("\n", array_map(static fn (array $asset): string => (string) ($asset['content'] ?? ''), array_filter($compiled['assets'] ?? array(), static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)))); -foreach (array('salmon' => false, 'teal' => true, 'purple' => true) as $needle => $expected) { +foreach (array('salmon' => true, 'teal' => true, 'purple' => true) as $needle => $expected) { if (str_contains($css, $needle) !== $expected) throw new RuntimeException('Unexpected inline selector reachability for ' . $needle); } echo "Inline selector pruning and specificity preservation passed\n"; From 0c76dc9ac72521dc2895d58e524e94ea73e95015 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 18:57:55 +0000 Subject: [PATCH 09/11] fix: exclude inline CSS inventory from document selector evidence --- .../src/ArtifactCompiler/ArtifactCompiler.php | 9 +++++-- .../complete-document-selector-liveness.php | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 php-transformer/tests/unit/complete-document-selector-liveness.php diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index e61a43860..bdf7cb3e3 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -2125,8 +2125,13 @@ private function withoutUnreachableSelectors(array $files): array // Metadata carries captured interaction states. if (is_array($file['metadata'] ?? null) && array() !== $file['metadata']) $texts[] = (string) json_encode($file['metadata']); $text = $file['content'] ?? (in_array($file['kind'] ?? '', array('html', 'js', 'json', 'svg', 'text'), true) && is_string($file['content_base64'] ?? null) ? base64_decode($file['content_base64'], true) : null); - // Preserve inline author sheets as conservative evidence: partial - // documents and responsive variants need not contain their subjects. + // A complete document supplies subject evidence, not its style + // inventory. Otherwise an inline framework sheet keeps every one + // of its own unused selectors alive. Fragments remain conservative: + // their author rules may target subjects outside the supplied tree. + if ('html' === ($file['kind'] ?? '') && is_string($text) && preg_match('/]*>.*?#is', '', $text); + } if (is_string($text) && '' !== $text) $texts[] = $text; } if (0 === $documents) return $files; diff --git a/php-transformer/tests/unit/complete-document-selector-liveness.php b/php-transformer/tests/unit/complete-document-selector-liveness.php new file mode 100644 index 000000000..088af22fe --- /dev/null +++ b/php-transformer/tests/unit/complete-document-selector-liveness.php @@ -0,0 +1,26 @@ +compile(array( + 'entrypoint' => 'index.html', + 'files' => array( + 'index.html' => '
Home
', + 'other.html' => '
Other
', + 'framework.css' => '.absent-widget{background:salmon}.other-route{background:teal}.dynamic-item{background:purple}', + ), +))->toArray(); +$css = implode("\n", array_map( + static fn (array $asset): string => (string) ($asset['content'] ?? ''), + array_filter($result['assets'], static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)) +)); +foreach (array('salmon' => false, 'teal' => true, 'purple' => true) as $value => $expected) { + if (str_contains($css, $value) !== $expected) { + throw new RuntimeException('Complete document selector liveness: ' . $value); + } +} +echo "Complete document selector liveness passed\n"; From dd62d419548c98213c9b796e7847ff379ccdcdce Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Mon, 16 Nov 2026 09:55:15 +0000 Subject: [PATCH 10/11] fix(css): prune unreachable framework subjects before projection --- .../src/ArtifactCompiler/ArtifactCompiler.php | 8 +++-- .../src/ArtifactCompiler/StagedTransport.php | 1 + .../src/Css/UnreachableSelectorPruner.php | 17 ++++++---- .../unit/state-pseudo-selector-pruning.php | 34 +++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 php-transformer/tests/unit/state-pseudo-selector-pruning.php diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index afe90d760..4573b1cea 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -297,6 +297,7 @@ private function compileArtifact(array $artifact): TransformerResult $choiceGroups = (new CapturedChoiceGroupProjector())->project($selectableSets['files']); $scrollStates = (new ScrollStateProjector())->project($choiceGroups['files']); $normalized['files'] = (new RouteStateProjector())->project($scrollStates['files'])['files']; + $normalized['files'] = $this->withoutUnreachableSelectors($normalized['files'], false); return $this->finalizeArtifact($artifact, array( 'normalized' => $normalized, 'inline_compilation' => true, @@ -2125,7 +2126,7 @@ private function pageStylesheetPath(string $path, string $owner, array &$reserve * @param array> $files * @return array> */ - private function withoutUnreachableSelectors(array $files): array + private function withoutUnreachableSelectors(array $files, bool $projectedOnly = true): array { $texts = array(); $documents = 0; @@ -2159,7 +2160,10 @@ private function withoutUnreachableSelectors(array $files): array $pruner = new UnreachableSelectorPruner($texts); foreach ($files as &$file) { // Only the projected sheets the pages load; other CSS is inventory. - if ('css' !== ($file['kind'] ?? '') || !isset($file['provenance']['projected_from_hash']) || !is_string($file['content'] ?? null)) continue; + if ('css' !== ($file['kind'] ?? '') || ($projectedOnly && !isset($file['provenance']['projected_from_hash'])) || !is_string($file['content'] ?? null)) continue; + // Inline rules also establish the specificity ceiling for inline + // carriers; keep their inventory until that projection is complete. + if (!$projectedOnly && 'inline-style' === ($file['source'] ?? null)) continue; $content = $pruner->prune($file['content']); if ($content !== $file['content']) $file = $this->projectedStylesheetFile($file, $file['path'], $content); } diff --git a/php-transformer/src/ArtifactCompiler/StagedTransport.php b/php-transformer/src/ArtifactCompiler/StagedTransport.php index 75a0a107d..8eae4133c 100644 --- a/php-transformer/src/ArtifactCompiler/StagedTransport.php +++ b/php-transformer/src/ArtifactCompiler/StagedTransport.php @@ -670,6 +670,7 @@ private function stagePartition(array $artifact, string $scope, string $pageId = $capturedDialogs['projected_choice_group_count'] = $choiceGroupsProjection['projected_count']; } $rawFiles = (new RouteStateProjector())->project($scrollStatesProjection['files'])['files']; + $rawFiles = $this->withoutUnreachableSelectors($rawFiles, false); // A later partition-envelope normalization must not lose the implicit // page ownership of already-expanded inline assets. foreach ($rawFiles as &$file) { diff --git a/php-transformer/src/Css/UnreachableSelectorPruner.php b/php-transformer/src/Css/UnreachableSelectorPruner.php index 566f31b43..66be544d7 100644 --- a/php-transformer/src/Css/UnreachableSelectorPruner.php +++ b/php-transformer/src/Css/UnreachableSelectorPruner.php @@ -50,14 +50,17 @@ public function prune(string $css): string $pruned = (new CssStylesheetTransformer())->transformStyleRules($css, function (string $prelude, string $body): string { $arms = CssStylesheetTransformer::splitSelectorList($prelude); if (null === $arms) return $prelude . '{' . $body . '}'; - // An unparseable arm makes the entire list uncertain. Keep the - // authored rule intact, just as stylesheet scope projection does. + // Matcher support is not selector validity: generated content and + // browser state pseudos can still have provably absent subjects. + // Malformed attribute syntax, however, makes a whole list uncertain. foreach ($arms as $arm) { - // Pseudo-elements do not add class/id requirements. Validate - // their subject rather than treating valid generated content - // as an uncertain selector list. - $subject = (string) preg_replace('/::?(?:before|after|first-line|first-letter|marker|placeholder|selection|backdrop)$/i', '', $arm); - if (!CssSelectorMatcher::parse($arm)['supported'] && ($subject === $arm || !CssSelectorMatcher::parse($subject)['supported'])) return $prelude . '{' . $body . '}'; + for ($i = 0, $length = strlen($arm); $i < $length; ++$i) { + if ('\\' === $arm[$i]) { ++$i; continue; } + if ('[' !== $arm[$i]) continue; + $close = CssSyntaxScanner::matchingClose($arm, $i); + if (null === $close || !CssSelectorMatcher::parse('*' . substr($arm, $i, $close - $i + 1))['supported']) return $prelude . '{' . $body . '}'; + $i = $close; + } } $kept = array_values(array_filter($arms, fn (string $arm): bool => $this->reachable($arm))); if (count($kept) === count($arms)) return $prelude . '{' . $body . '}'; diff --git a/php-transformer/tests/unit/state-pseudo-selector-pruning.php b/php-transformer/tests/unit/state-pseudo-selector-pruning.php new file mode 100644 index 000000000..1c15d21bf --- /dev/null +++ b/php-transformer/tests/unit/state-pseudo-selector-pruning.php @@ -0,0 +1,34 @@ + 'index.html', + 'files' => array( + 'index.html' => '

Home

', + 'other.html' => '
Other
', + 'site.css' => '.absent:focus-within{color:salmon}.present:focus-within{color:teal}.other-route{color:purple}.unused,.broken[foo=bar baz]{color:orchid}', + ), +); +$compiler = new ArtifactCompiler(); +$shared = $compiler->prepareShared($artifact); +$pages = $compiler->preparePages($artifact, $shared); +$receipts = $compiler->compilePreparedPages($shared, $pages); +foreach (array( + $compiler->compile($artifact)->toArray(), + $compiler->compose($shared, array_values($receipts))->toArray(), +) as $result) { + $css = implode("\n", array_map( + static fn (array $asset): string => (string) ($asset['content'] ?? ''), + array_filter($result['assets'], static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)) + )); + foreach (array('salmon' => false, 'teal' => true, 'purple' => true, 'orchid' => true) as $needle => $expected) { + if (str_contains($css, $needle) !== $expected) throw new RuntimeException('Source selector pruning: ' . $needle); + } +} +echo "Source selector pruning before projection passed\n"; From feff5d7e26651ddb3ba8b094018972318a63ef6f Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Mon, 16 Nov 2026 09:55:15 +0000 Subject: [PATCH 11/11] fix(css): prune published projections without mutating source inventory --- .../src/ArtifactCompiler/ArtifactCompiler.php | 11 +++++------ .../src/ArtifactCompiler/StagedTransport.php | 1 - .../tests/unit/state-pseudo-selector-pruning.php | 10 ++++++++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index 4573b1cea..c7a183cfd 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -297,7 +297,6 @@ private function compileArtifact(array $artifact): TransformerResult $choiceGroups = (new CapturedChoiceGroupProjector())->project($selectableSets['files']); $scrollStates = (new ScrollStateProjector())->project($choiceGroups['files']); $normalized['files'] = (new RouteStateProjector())->project($scrollStates['files'])['files']; - $normalized['files'] = $this->withoutUnreachableSelectors($normalized['files'], false); return $this->finalizeArtifact($artifact, array( 'normalized' => $normalized, 'inline_compilation' => true, @@ -509,6 +508,9 @@ private function finalizeArtifact(array $artifact, array $reduction, ?\Automatti foreach (array_unique(array_map(static fn (array $part): string => (string) ($part['block_markup'] ?? ''), $parts)) as $partMarkup) $sharedMarkup .= "\n" . $partMarkup; foreach ($pageMarkup as &$markup) $markup .= "\n" . $sharedMarkup; unset($markup); + // Keep source bytes intact through compilation: provenance, orphan + // assets and cross-route factoring all depend on the authored inventory. + // Only prune the published projections after that factoring is complete. $authoredFiles = $normalized['files']; $normalized['files'] = $this->applyAuthorStylesheetProjections($normalized['files'], $authorStylesheetProjections, $entryBlocks['author_stylesheet_projections'], (string) ($ownedBy(array(array()), $entryPath)[0]['owner'] ?? ''), $pageMarkup, $allDiagnostics, $sharedMarkup, $timer); $normalized['files'] = $this->withoutUnreachableSelectors($normalized['files']); @@ -2126,7 +2128,7 @@ private function pageStylesheetPath(string $path, string $owner, array &$reserve * @param array> $files * @return array> */ - private function withoutUnreachableSelectors(array $files, bool $projectedOnly = true): array + private function withoutUnreachableSelectors(array $files): array { $texts = array(); $documents = 0; @@ -2160,10 +2162,7 @@ private function withoutUnreachableSelectors(array $files, bool $projectedOnly = $pruner = new UnreachableSelectorPruner($texts); foreach ($files as &$file) { // Only the projected sheets the pages load; other CSS is inventory. - if ('css' !== ($file['kind'] ?? '') || ($projectedOnly && !isset($file['provenance']['projected_from_hash'])) || !is_string($file['content'] ?? null)) continue; - // Inline rules also establish the specificity ceiling for inline - // carriers; keep their inventory until that projection is complete. - if (!$projectedOnly && 'inline-style' === ($file['source'] ?? null)) continue; + if ('css' !== ($file['kind'] ?? '') || !isset($file['provenance']['projected_from_hash']) || !is_string($file['content'] ?? null)) continue; $content = $pruner->prune($file['content']); if ($content !== $file['content']) $file = $this->projectedStylesheetFile($file, $file['path'], $content); } diff --git a/php-transformer/src/ArtifactCompiler/StagedTransport.php b/php-transformer/src/ArtifactCompiler/StagedTransport.php index 8eae4133c..75a0a107d 100644 --- a/php-transformer/src/ArtifactCompiler/StagedTransport.php +++ b/php-transformer/src/ArtifactCompiler/StagedTransport.php @@ -670,7 +670,6 @@ private function stagePartition(array $artifact, string $scope, string $pageId = $capturedDialogs['projected_choice_group_count'] = $choiceGroupsProjection['projected_count']; } $rawFiles = (new RouteStateProjector())->project($scrollStatesProjection['files'])['files']; - $rawFiles = $this->withoutUnreachableSelectors($rawFiles, false); // A later partition-envelope normalization must not lose the implicit // page ownership of already-expanded inline assets. foreach ($rawFiles as &$file) { diff --git a/php-transformer/tests/unit/state-pseudo-selector-pruning.php b/php-transformer/tests/unit/state-pseudo-selector-pruning.php index 1c15d21bf..eb845e1f8 100644 --- a/php-transformer/tests/unit/state-pseudo-selector-pruning.php +++ b/php-transformer/tests/unit/state-pseudo-selector-pruning.php @@ -6,13 +6,15 @@ // Framework selectors unsupported by the matcher still have required source // classes. Neither generated native identities nor state pseudos make those -// absent classes live. Malformed lists remain untouched. +// absent classes live. Malformed lists remain untouched. Pruning belongs to +// published projections, not source inventory: orphan assets keep their bytes. $artifact = array( 'entrypoint' => 'index.html', 'files' => array( 'index.html' => '

Home

', 'other.html' => '
Other
', 'site.css' => '.absent:focus-within{color:salmon}.present:focus-within{color:teal}.other-route{color:purple}.unused,.broken[foo=bar baz]{color:orchid}', + 'orphan.css' => '.inventory-only:focus-within{color:gold}', ), ); $compiler = new ArtifactCompiler(); @@ -23,6 +25,10 @@ $compiler->compile($artifact)->toArray(), $compiler->compose($shared, array_values($receipts))->toArray(), ) as $result) { + $orphans = array_values(array_filter($result['assets'], static fn (array $asset): bool => 'orphan.css' === ($asset['path'] ?? null))); + if ('.inventory-only:focus-within{color:gold}' !== ($orphans[0]['content'] ?? null)) { + throw new RuntimeException('Selector pruning must preserve unreferenced source inventory'); + } $css = implode("\n", array_map( static fn (array $asset): string => (string) ($asset['content'] ?? ''), array_filter($result['assets'], static fn (array $asset): bool => 'css' === ($asset['kind'] ?? null)) @@ -31,4 +37,4 @@ if (str_contains($css, $needle) !== $expected) throw new RuntimeException('Source selector pruning: ' . $needle); } } -echo "Source selector pruning before projection passed\n"; +echo "Projected selector pruning preserves source inventory\n";