Skip to content
Open
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
1 change: 1 addition & 0 deletions php-transformer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"php tests/unit/subtree-classifier.php",
"php tests/unit/custom-block-generator.php",
"php tests/unit/css-value-splitter.php",
"php tests/unit/block-style-support-conversion.php",
"php tests/unit/content-round-trip-reporter.php",
"php tests/unit/content-round-trip-form-echo.php",
"php tests/unit/corpus-detectors.php",
Expand Down
3 changes: 3 additions & 0 deletions php-transformer/src/ArtifactCompiler/ArtifactCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ private function runtimeIslandsWithMaterializedInlineScripts(array $runtimeIslan
'diagnostic_code' => 'preserved_runtime_island',
'preservation_reason' => 'script_requires_runtime',
'runtime_requirement' => 'client_script_execution',
'disposition' => 'preserve',
'preservation_status' => 'accepted_runtime_preservation',
'js_handling' => 'preserve_verbatim',
'source_snippet' => '<script' . $attributeHtml . '></script>',
'source_bytes' => strlen($content),
'source_truncated' => false,
Expand Down
3 changes: 3 additions & 0 deletions php-transformer/src/Contract/ConversionReportProjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ private static function runtimeIslandSummaryEntries(array $sourceReports): array
'tag' => $island['tag'] ?? '',
'conversion_classification' => 'runtime_island_preserved',
'preservation_strategy' => $island['preservation_strategy'] ?? 'scoped_runtime_metadata',
'disposition' => $island['disposition'] ?? '',
'preservation_status' => $island['preservation_status'] ?? '',
'js_handling' => $island['js_handling'] ?? '',
),
static fn (mixed $value): bool => '' !== $value
);
Expand Down
34 changes: 28 additions & 6 deletions php-transformer/src/CorpusDiagnostics/CorpusDetectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,14 @@ public static function varDependentStyling(array $flat): array

foreach ( $flat as $block ) {
$haystack = self::blockMarkup($block);
if ( '' === $haystack ) {
continue;
}
if ( ! preg_match_all('/var\(\s*(--[A-Za-z0-9_-]+)/', $haystack, $matches) ) {
continue;
if ( '' !== $haystack && preg_match_all('/var\(\s*(--[A-Za-z0-9_-]+)/', $haystack, $matches) ) {
foreach ( $matches[1] as $name ) {
++$total;
$occurrences[$name] = ($occurrences[$name] ?? 0) + 1;
}
}
foreach ( $matches[1] as $name ) {

foreach ( self::presetVarNamesFromAttrs($block) as $name ) {
++$total;
$occurrences[$name] = ($occurrences[$name] ?? 0) + 1;
}
Expand Down Expand Up @@ -729,6 +730,27 @@ private static function blockMarkup(array $block): string
return is_string($block['innerHTML'] ?? null) ? $block['innerHTML'] : '';
}

/**
* Native preset color attrs are the valid form of CSS preset vars, so they no
* longer appear in innerHTML. Keep them in var_names for corpus visibility.
*
* @param array<string, mixed> $block
* @return array<int, string>
*/
private static function presetVarNamesFromAttrs(array $block): array
{
$attrs = is_array($block['attrs'] ?? null) ? $block['attrs'] : array();
$names = array();
foreach ( array( 'textColor', 'backgroundColor' ) as $attrName ) {
$slug = is_string($attrs[ $attrName ] ?? null) ? strtolower(trim($attrs[ $attrName ])) : '';
if ( '' !== $slug && preg_match('/^[a-z0-9_-]+$/', $slug) ) {
$names[] = '--wp--preset--color--' . $slug;
}
}

return $names;
}

/**
* RichText content for a paragraph/heading/list-item block: the explicit
* content attribute, falling back to saved innerHTML.
Expand Down
68 changes: 67 additions & 1 deletion php-transformer/src/HtmlToBlocks/BlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private function styleMapper(): StyleAttributeMapper
*/
public function create(string $name, array $attrs = array(), array $innerBlocks = array()): array
{
$attrs = $this->normalizeAttrsForBlock($name, $attrs);
$innerHtml = $this->blockHtml($name, $attrs, $innerBlocks);
if ( is_array($innerHtml) ) {
$innerContent = array( $innerHtml['opening'] );
Expand All @@ -54,6 +55,25 @@ public function create(string $name, array $attrs = array(), array $innerBlocks
);
}

/**
* @param array<string, mixed> $attrs
* @return array<string, mixed>
*/
private function normalizeAttrsForBlock(string $name, array $attrs): array
{
if ( in_array($name, array( 'core/buttons', 'core/column', 'core/columns', 'core/group', 'core/heading', 'core/list-item', 'core/paragraph' ), true) ) {
unset($attrs['style']['spacing']['blockGap']);
if ( empty($attrs['style']['spacing']) ) {
unset($attrs['style']['spacing']);
}
if ( empty($attrs['style']) ) {
unset($attrs['style']);
}
}

return $attrs;
}

/**
* @param array<string, mixed> $attrs
* @return array<string, mixed>
Expand Down Expand Up @@ -545,7 +565,9 @@ private function mergeClassNames(string ...$classNames): string
private function blockSupportAttrs(array $attrs, string $baseClass = ''): string
{
$support = $this->styleSupport($attrs['style'] ?? null);
$classes = $this->mergeClassNames($baseClass, $support['classes'], (string) ($attrs['className'] ?? ''));
$presetClasses = $this->presetColorClasses($attrs);
$layoutClasses = $this->layoutClasses($attrs['layout'] ?? null, $baseClass);
$classes = $this->mergeClassNames($baseClass, $presetClasses, $support['classes'], $layoutClasses, (string) ($attrs['className'] ?? ''));
return $this->htmlAttrs(array(
'id' => (string) ($attrs['anchor'] ?? ''),
'class' => $classes,
Expand Down Expand Up @@ -573,6 +595,50 @@ private function styleSupport(mixed $style): array
);
}

/**
* @param array<string, mixed> $attrs
*/
private function presetColorClasses(array $attrs): string
{
$classes = array();
$textColor = $this->safeSlug((string) ($attrs['textColor'] ?? ''));
if ( '' !== $textColor ) {
$classes[] = 'has-' . $textColor . '-color';
$classes[] = 'has-text-color';
}

$backgroundColor = $this->safeSlug((string) ($attrs['backgroundColor'] ?? ''));
if ( '' !== $backgroundColor ) {
$classes[] = 'has-' . $backgroundColor . '-background-color';
$classes[] = 'has-background';
}

return implode(' ', $classes);
}

private function layoutClasses(mixed $layout, string $baseClass): string
{
if ( ! is_array($layout) ) {
return '';
}

$type = $this->safeSlug((string) ($layout['type'] ?? ''));
if ( ! in_array($type, array( 'constrained', 'flex', 'flow', 'grid' ), true) ) {
return '';
}

return $this->mergeClassNames(
'is-layout-' . $type,
'' !== $baseClass ? $baseClass . '-is-layout-' . $type : ''
);
}

private function safeSlug(string $value): string
{
$value = strtolower(trim($value));
return preg_match('/^[a-z0-9_-]+$/', $value) ? $value : '';
}

/**
* @param array<string, string> $attrs
* @param array<int, string> $includeEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public function collect(
'diagnostic_class' => 'runtime_island_preserved',
'suggested_repair_class' => 'preserve_runtime_island',
'preservation_strategy' => $island['preservation_strategy'] ?? 'bounded_raw_html_runtime_island',
'disposition' => $island['disposition'] ?? null,
'preservation_status' => $island['preservation_status'] ?? null,
'js_handling' => $island['js_handling'] ?? null,
'runtime_requirement' => $island['runtime_requirement'] ?? null,
'kind' => $island['kind'] ?? null,
'reason' => $island['preservation_reason'] ?? null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@ public function recordRuntimeIsland(DOMElement $element, string $kind, string $r
'diagnostic_code' => 'preserved_runtime_island',
'preservation_reason' => $reason,
'runtime_requirement' => $runtimeRequirement,
'disposition' => 'preserve',
'preservation_status' => 'accepted_runtime_preservation',
'js_handling' => 'client_script_execution' === $runtimeRequirement ? 'preserve_verbatim' : '',
'source_snippet' => $boundedHtml['html'],
'source_bytes' => $boundedHtml['bytes'],
'source_truncated' => $boundedHtml['truncated'],
Expand Down
3 changes: 3 additions & 0 deletions php-transformer/src/HtmlToBlocks/FallbackDiagnostic.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ private static function defaults(array $fields): array
'conversion_classification' => 'runtime_island_preserved',
'loss_class' => 'runtime_island_preserved',
'diagnostic_class' => 'runtime_island_preserved',
'disposition' => 'preserve',
'preservation_status' => 'accepted_runtime_preservation',
'js_handling' => 'preserve_verbatim',
'preservation_strategy' => 'scoped_runtime_metadata',
'runtime_requirement' => 'client_script_execution',
'recoverability' => 'recoverable_with_script_enqueue_or_component_runtime',
Expand Down
Loading
Loading