diff --git a/php-transformer/src/HtmlToBlocks/HtmlTransformer.php b/php-transformer/src/HtmlToBlocks/HtmlTransformer.php index 9f223c2..1933760 100644 --- a/php-transformer/src/HtmlToBlocks/HtmlTransformer.php +++ b/php-transformer/src/HtmlToBlocks/HtmlTransformer.php @@ -1202,6 +1202,7 @@ private function captureInlineSvgFallback(DOMElement $element, array &$fallbacks private function captureScriptFallback(DOMElement $element, array &$fallbacks): void { $boundedHtml = $this->boundedFallbackHtml($this->safeFallbackHtml($element)); + $boundedBody = $this->boundedFallbackText(trim($element->textContent ?? '')); $fallbacks[] = array_merge(array( 'type' => 'html', 'reason' => 'script_requires_runtime', @@ -1218,6 +1219,9 @@ private function captureScriptFallback(DOMElement $element, array &$fallbacks): 'html' => $boundedHtml['html'], 'html_bytes' => $boundedHtml['bytes'], 'html_truncated' => $boundedHtml['truncated'], + 'body' => $boundedBody['text'], + 'body_bytes' => $boundedBody['bytes'], + 'body_truncated' => $boundedBody['truncated'], ), $this->fallbackProvenance); } @@ -1258,6 +1262,27 @@ private function boundedFallbackHtml(string $html): array ); } + /** + * @return array{text: string, bytes: int, truncated: bool} + */ + private function boundedFallbackText(string $text): array + { + $bytes = strlen($text); + if ( $bytes > 2000 ) { + return array( + 'text' => substr($text, 0, 2000) . '...', + 'bytes' => $bytes, + 'truncated' => true, + ); + } + + return array( + 'text' => $text, + 'bytes' => $bytes, + 'truncated' => false, + ); + } + /** * @return array */ diff --git a/php-transformer/tests/fixtures/parity/html-h2bc-gap-primitives.json b/php-transformer/tests/fixtures/parity/html-h2bc-gap-primitives.json index 4d10957..1d7964a 100644 --- a/php-transformer/tests/fixtures/parity/html-h2bc-gap-primitives.json +++ b/php-transformer/tests/fixtures/parity/html-h2bc-gap-primitives.json @@ -13,7 +13,7 @@ }, "operation": "html_transformer.transform", "input": { - "content": "
\"Logo\"
Term
Definition

Signup

theme.json
{"version":2}
<?php add_action();
" + "content": "
\"Logo\"
Term
Definition

Signup

theme.json
{"version":2}
<?php add_action();
" }, "expected_blocks": [ { "path": "blocks.0", "name": "core/group" }, @@ -45,10 +45,15 @@ { "path": "blocks.0.innerBlocks", "assert": "count", "count": 7 }, { "path": "fallbacks", "assert": "count", "count": 2 }, { "path": "fallbacks.0.controls.0.placeholder", "assert": "equals", "value": "you@example.com" }, + { "path": "fallbacks.0.controls.1.type", "assert": "equals", "value": "checkbox" }, + { "path": "fallbacks.0.controls.1.label", "assert": "equals", "value": "Agree" }, + { "path": "fallbacks.0.controls.1.required", "assert": "equals", "value": true }, { "path": "fallbacks.0.context.parent_tag", "assert": "equals", "value": "section" }, { "path": "fallbacks.1.attributes.src", "assert": "equals", "value": "/widget.js" }, { "path": "fallbacks.1.context.parent_tag", "assert": "equals", "value": "section" }, { "path": "fallbacks.1.html", "assert": "equals", "value": "" }, + { "path": "fallbacks.1.body", "assert": "equals", "value": "hydrate()" }, + { "path": "fallbacks.1.body_truncated", "assert": "equals", "value": false }, { "path": "serialized_blocks", "assert": "contains", "value": "
\"Logo\"
" }, { "path": "serialized_blocks", "assert": "contains", "value": "
  • Parent" }, { "path": "serialized_blocks", "assert": "contains", "value": "theme.json" },