Skip to content
Merged
21 changes: 18 additions & 3 deletions php-transformer/src/ArtifactCompiler/ArtifactCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Automattic\BlocksEngine\PhpTransformer\Contract\TransformerResult;
use Automattic\BlocksEngine\PhpTransformer\FormatBridge\FormatBridge;
use Automattic\BlocksEngine\PhpTransformer\HtmlToBlocks\HtmlTransformer;
use Automattic\BlocksEngine\PhpTransformer\HtmlToBlocks\Style\CssStylesheetTransformer;
use Automattic\BlocksEngine\PhpTransformer\HtmlToBlocks\Style\FormLayoutGraphBuilder;
use Automattic\BlocksEngine\PhpTransformer\HtmlToBlocks\ShellLandmarkPolicy;
use Automattic\BlocksEngine\PhpTransformer\Path\ArtifactPath;
use Automattic\BlocksEngine\PhpTransformer\StaticSite\MaterializationPlanBuilder;
Expand Down Expand Up @@ -282,6 +284,8 @@ private function runtimeDeclarationsFromFallbacks(array $declarations, array $fa
} elseif ( 'html_form_fallback' === $code && is_array($fallback['controls'] ?? null) ) {
$selector = is_string($fallback['selector'] ?? null) ? $fallback['selector'] : '';
$form = array('selector' => $selector, 'source_path' => $sourcePath, 'form' => is_array($fallback['form'] ?? null) ? $fallback['form'] : array(), 'controls' => array_values(array_filter($fallback['controls'], 'is_array')));
if ( is_array($fallback['control_topology'] ?? null) ) $form['control_topology'] = $fallback['control_topology'];
if ( is_array($fallback['layout_graph'] ?? null) ) { FormLayoutGraphBuilder::assertValid($fallback['layout_graph']); $form['layout_graph'] = $fallback['layout_graph']; }
if ( is_array($fallback['binding'] ?? null) && 'generic/block-binding/v1' === ($fallback['binding']['schema'] ?? null) && is_string($fallback['binding']['search_block_markup'] ?? null) && '' !== trim($fallback['binding']['search_block_markup']) ) {
$form['bindings'] = array(array_merge($fallback['binding'], array('source_path' => $sourcePath)));
}
Expand Down Expand Up @@ -875,7 +879,7 @@ private function stylesheetAssetsForSource(string $html, string $sourcePath, arr
++$inlineIndex;
$file = $inline[$inlineIndex] ?? null;
if ( is_array($file) && ! isset($seenPaths[$file['path']]) ) {
$assets[] = array( 'path' => $file['path'], 'content' => $file['content'], 'source_hash' => (string) ($file['provenance']['hash'] ?? hash('sha256', $file['content']) ), 'media' => (string) ($file['media'] ?? ''), 'type' => (string) ($file['type'] ?? '') );
$assets[] = array( 'path' => $file['path'], 'source_path' => $file['source_path'] ?? $file['path'], 'content' => $file['content'], 'source_hash' => (string) ($file['provenance']['hash'] ?? hash('sha256', $file['content']) ), 'media' => (string) ($file['media'] ?? ''), 'type' => (string) ($file['type'] ?? '') );
$seenPaths[$file['path']] = true;
}
continue;
Expand All @@ -888,7 +892,7 @@ private function stylesheetAssetsForSource(string $html, string $sourcePath, arr
$path = $occurrencePaths[$sourcePathForLink][$linkOccurrences[$sourcePathForLink]] ?? '';
$file = $byPath[$path] ?? null;
if ( is_array($file) && ! isset($seenPaths[$path]) ) {
$assets[] = array( 'path' => $path, 'content' => $file['content'], 'source_hash' => (string) ($file['provenance']['hash'] ?? hash('sha256', $file['content']) ), 'media' => $this->htmlAttribute((string) $tag, 'media'), 'type' => $this->htmlAttribute((string) $tag, 'type') );
$assets[] = array( 'path' => $path, 'source_path' => $file['stylesheet_source_path'] ?? $sourcePathForLink, 'content' => $file['content'], 'source_hash' => (string) ($file['provenance']['hash'] ?? hash('sha256', $file['content']) ), 'media' => $this->htmlAttribute((string) $tag, 'media'), 'type' => $this->htmlAttribute((string) $tag, 'type') );
$seenPaths[$path] = true;
}
}
Expand Down Expand Up @@ -1011,7 +1015,18 @@ private function applyAuthorStylesheetProjections(array $files, array $projectio
if ( array() === $authoritativeContent ) {
$authoritativeContent[] = (string) ($file['content'] ?? '');
}
$content = implode("\n", array_merge(array_keys($pathProjections), $authoritativeContent));
$preambles = array();
$stylesheets = array();
foreach ( $authoritativeContent as $stylesheet ) {
$split = ( new CssStylesheetTransformer() )->splitLeadingAtRulePreamble($stylesheet);
if ( '' !== trim($split['preamble']) ) {
$preambles[] = $split['preamble'];
}
if ( '' !== trim($split['stylesheet']) ) {
$stylesheets[] = $split['stylesheet'];
}
}
$content = implode("\n", array_merge($preambles, array_keys($pathProjections), $stylesheets));
$file['content'] = $content;
// Projection rewrites the CSS text, so any base64 twin from the
// source payload is stale. Drop it and let the rewritten text be the
Expand Down
2 changes: 2 additions & 0 deletions php-transformer/src/ArtifactCompiler/RuntimeDeclarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Automattic\BlocksEngine\PhpTransformer\ArtifactCompiler;

use Automattic\BlocksEngine\PhpTransformer\Path\ArtifactPath;
use Automattic\BlocksEngine\PhpTransformer\HtmlToBlocks\Style\FormLayoutGraphBuilder;
use InvalidArgumentException;
use JsonException;

Expand Down Expand Up @@ -65,6 +66,7 @@ public static function normalizeList(mixed $raw): array
try { $encoded = self::canonicalJson($payload); } catch (InvalidArgumentException) { throw new InvalidArgumentException("Runtime declaration {$index} payload is not serializable."); }
if (strlen($encoded) > self::MAX_PAYLOAD_BYTES) throw new InvalidArgumentException("Runtime declaration {$index} payload exceeds the byte limit.");
$normalized['payload'] = $payload;
if ('entity_collection' === $kind && 'forms' === $name && 'generic/forms/v1' === ($payload['schema'] ?? null)) foreach ($payload['entities'] ?? array() as $entity) if (is_array($entity) && isset($entity['layout_graph'])) { if (!is_array($entity['layout_graph'])) throw new InvalidArgumentException("Runtime declaration {$index} form layout graph must be an object."); FormLayoutGraphBuilder::assertValid($entity['layout_graph']); }
}
if ('entity_collection' === $kind && (!isset($normalized['type'], $normalized['payload']['entities']) || !array_is_list($normalized['payload']['entities']))) throw new InvalidArgumentException("Runtime declaration {$index} entity collections require a typed entities payload.");
if (isset($declaration['required_for'])) {
Expand Down
Loading
Loading