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
4 changes: 4 additions & 0 deletions php-transformer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
"php tests/unit/subtree-classifier.php",
"php tests/unit/custom-block-generator.php",
"php tests/unit/css-value-splitter.php",
"php tests/unit/css-stylesheet-transformer.php",
"php tests/unit/css-selector-matcher.php",
"php tests/unit/author-selector-semantics.php",
"php tests/unit/artifact-author-stylesheet-projection.php",
"php tests/unit/fallback-finding-normalizer.php",
"php tests/unit/navigation-underline-color-resolver.php",
"php tests/unit/button-signal-classifier.php",
Expand Down
254 changes: 245 additions & 9 deletions php-transformer/src/ArtifactCompiler/ArtifactCompiler.php

Large diffs are not rendered by default.

82 changes: 59 additions & 23 deletions php-transformer/src/ArtifactCompiler/ArtifactNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ public function normalize(array $artifact): array
}
}

$rawFiles = $this->withInlineScriptFiles($this->withInlineStyleFiles($this->rawFiles($artifact)));
$rawFiles = $this->rawFiles($artifact);
$reservedPaths = array();
foreach ( $rawFiles as $file ) {
$path = ArtifactPath::safeRelativePath((string) ($file['path'] ?? ''));
if ( '' !== $path ) {
$reservedPaths[$path] = true;
}
}
$rawFiles = $this->withInlineScriptFiles($this->withInlineStyleFiles($rawFiles, $reservedPaths));
$safeEntrypoints = array();
foreach ( array_unique($entrypoints) as $entrypoint ) {
$path = ArtifactPath::safeRelativePath($entrypoint);
Expand Down Expand Up @@ -125,7 +133,7 @@ public function normalize(array $artifact): array
if ( '' !== $intent ) {
$normalized['intent'] = $intent;
}
foreach ( array('placement', 'type', 'source_path', 'selector') as $field ) {
foreach ( array('placement', 'type', 'media', 'source_path', 'selector', 'stylesheet_index') as $field ) {
if ( isset($file[$field]) && is_scalar($file[$field]) && '' !== trim((string) $file[$field]) ) {
$normalized[$field] = (string) $file[$field];
}
Expand Down Expand Up @@ -217,34 +225,42 @@ private function rawFiles(array $artifact): array
* @param array<int, array<string, mixed>> $files
* @return array<int, array<string, mixed>>
*/
private function withInlineStyleFiles(array $files): array
private function withInlineStyleFiles(array $files, array &$reservedPaths): array
{
$expanded = array();
foreach ( $files as $file ) {
$expanded[] = $file;

$content = $this->payload($file, (string) ($file['path'] ?? ''))['content'];
if ( '' === trim($content) || ! $this->isHtmlLikeFile($file) || ! preg_match_all('@<style\b[^>]*>(.*?)</style>@is', $content, $matches) ) {
if ( '' === trim($content) || ! $this->isHtmlLikeFile($file) || ! preg_match_all('@<style\b([^>]*)>(.*?)</style>@is', $content, $matches, PREG_SET_ORDER) ) {
continue;
}

$css = trim(implode("\n", array_filter(array_map(
static fn (string $style): string => trim($style),
$matches[1]
), static fn (string $style): bool => '' !== $style)));
if ( '' === $css ) {
continue;
$styles = array();
foreach ( $matches as $match ) {
$attributes = (string) $match[1];
$css = trim((string) $match[2]);
if ( '' === $css || ! $this->isCssType($this->htmlAttribute($attributes, 'type')) ) {
continue;
}
$styles[] = array( 'content' => $css, 'media' => $this->htmlAttribute($attributes, 'media'), 'type' => $this->htmlAttribute($attributes, 'type') );
}
foreach ( $styles as $index => $style ) {
$path = $this->allocateGeneratedPath($this->inlineStylePath((string) ($file['path'] ?? 'index.html'), count($styles), $index + 1), $reservedPaths);
$expanded[] = array(
'path' => $path,
'content' => $style['content'],
'kind' => 'css',
'mime_type' => 'text/css',
'role' => 'stylesheet',
'intent' => 'style',
'source' => 'inline-style',
'source_path' => (string) ($file['path'] ?? 'index.html'),
'stylesheet_index' => $index + 1,
'media' => $style['media'],
'type' => $style['type'],
);
}

$expanded[] = array(
'path' => $this->inlineStylePath((string) ($file['path'] ?? 'index.html')),
'content' => $css,
'kind' => 'css',
'mime_type' => 'text/css',
'role' => 'stylesheet',
'intent' => 'style',
'source' => 'inline-style',
);
}

return $expanded;
Expand All @@ -264,20 +280,40 @@ private function isHtmlLikeFile(array $file): bool
&& ( '' === $path || preg_match('/\.html?$/', $path) || 'index.html' === $path );
}

private function inlineStylePath(string $htmlPath): string
private function inlineStylePath(string $htmlPath, int $count = 1, int $index = 1): string
{
$path = ArtifactPath::safeRelativePath($htmlPath);
if ( '' === $path ) {
return 'inline-styles.css';
return 1 === $count ? 'inline-styles.css' : 'inline-styles-' . $index . '.css';
}

$directory = trim((string) pathinfo($path, PATHINFO_DIRNAME), '.');
$filename = pathinfo($path, PATHINFO_FILENAME);
$stylePath = ('' === $filename ? 'inline' : $filename) . '.inline.css';
$stylePath = ('' === $filename ? 'inline' : $filename) . '.inline' . (1 === $count ? '' : '-' . $index) . '.css';

return '' === $directory ? $stylePath : $directory . '/' . $stylePath;
}

/** @param array<string, true> $reservedPaths */
private function allocateGeneratedPath(string $candidate, array &$reservedPaths): string
{
$path = $candidate;
$index = 1;
while ( isset($reservedPaths[$path]) ) {
$extension = pathinfo($candidate, PATHINFO_EXTENSION);
$base = '' === $extension ? $candidate : substr($candidate, 0, -strlen($extension) - 1);
$path = $base . '-generated-' . $index++ . ('' === $extension ? '' : '.' . $extension);
}
$reservedPaths[$path] = true;
return $path;
}

private function isCssType(string $type): bool
{
$type = strtolower(trim($type));
return '' === $type || 1 === preg_match("/^text\\/css(?:\\s*;\\s*[!#$%&'*+\\-.^_`|~0-9a-z]+(?:\\s*=\\s*(?:[!#$%&'*+\\-.^_`|~0-9a-z]+|\"(?:[^\"\\\\]|\\\\.)*\"))?)*\\s*$/i", $type);
}

/**
* @param array<int, array<string, mixed>> $files
* @return array<int, array<string, mixed>>
Expand Down
Loading
Loading