diff --git a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php index 195626422..c7a183cfd 100644 --- a/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php +++ b/php-transformer/src/ArtifactCompiler/ArtifactCompiler.php @@ -508,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']); @@ -2135,8 +2138,24 @@ 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('/]*>/i', $text, $open, PREG_OFFSET_CAPTURE, $offset)) { + $start = $open[0][1]; + $bodyStart = $start + strlen($open[0][0]); + if (!preg_match('/<\/style\s*>/i', $text, $close, PREG_OFFSET_CAPTURE, $bodyStart)) break; + $end = $close[0][1] + strlen($close[0][0]); + $text = substr_replace($text, '', $start, $end - $start); + $offset = $start; + } + } if (is_string($text) && '' !== $text) $texts[] = $text; } if (0 === $documents) return $files; diff --git a/php-transformer/src/Css/UnreachableSelectorPruner.php b/php-transformer/src/Css/UnreachableSelectorPruner.php index 2a1887d8e..66be544d7 100644 --- a/php-transformer/src/Css/UnreachableSelectorPruner.php +++ b/php-transformer/src/Css/UnreachableSelectorPruner.php @@ -50,6 +50,18 @@ 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 . '}'; + // 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) { + 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 . '}'; // Comments ahead of the rule (licenses, source maps) stay in place. 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..5c21b635a --- /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