diff --git a/php-transformer/src/HtmlToBlocks/Elements/AuthoredFormControlBlockConverter.php b/php-transformer/src/HtmlToBlocks/Elements/AuthoredFormControlBlockConverter.php index 68117d473..8aaf652ac 100644 --- a/php-transformer/src/HtmlToBlocks/Elements/AuthoredFormControlBlockConverter.php +++ b/php-transformer/src/HtmlToBlocks/Elements/AuthoredFormControlBlockConverter.php @@ -129,9 +129,6 @@ public function select(DOMElement $select, bool $forceNative = false, ?DOMElemen ), $select); } - $generator = new AuthoredSelectBlockGenerator(); - $registry = $this->session->requireGeneratedBlockRegistry(); - $registry->register(AuthoredSelectBlockGenerator::class, $generator->definition($registry->namespace())); $attrs = array_filter(array( 'id' => SourceDom::attr($select, 'id'), 'name' => SourceDom::attr($select, 'name'), @@ -153,19 +150,24 @@ public function select(DOMElement $select, bool $forceNative = false, ?DOMElemen 'disabled' => $select->hasAttribute('disabled'), ), static fn (mixed $value): bool => is_array($value) ? array() !== $value : '' !== $value); $attrs = array_replace($attrs, $state ?? array()); + // Capture-backed controls have an explicit state contract for the core + // option-list renderer. Ordinary authored controls must keep their saved + // native markup (including form submission and source CSS topology). + if (null !== $state) { + return $this->nativeSelect($attrs, $select); + } + $generator = new AuthoredSelectBlockGenerator(); + $registry = $this->session->requireGeneratedBlockRegistry(); + $registry->register(AuthoredSelectBlockGenerator::class, $generator->definition($registry->namespace())); $markup = $generator->markup($attrs); - $controlBlock = array( + return array( 'blockName' => $registry->blockName(AuthoredSelectBlockGenerator::LOCAL_NAME), 'attrs' => $attrs, 'innerBlocks' => array(), 'innerHTML' => $markup, - 'innerContent' => array( $markup ), - // Anchor-only identity: lets a finding bind this control's block - // without registering it as a provenance source. + 'innerContent' => array($markup), '_source_anchor_selector' => SourceDom::elementSelector($select), ); - - return $controlBlock; } public function listbox(DOMElement $trigger, ?DOMElement $panel = null): ?array @@ -214,9 +216,6 @@ public function listbox(DOMElement $trigger, ?DOMElement $panel = null): ?array if (array() === $selected && '' !== $triggerLabel && !in_array($triggerLabel, array_column($options, 'label'), true)) { array_unshift($options, array('label' => $triggerLabel, 'value' => '', 'selected' => true, 'disabled' => true, 'placeholder' => true)); } - $generator = new AuthoredSelectBlockGenerator(); - $registry = $this->session->requireGeneratedBlockRegistry(); - $registry->register(AuthoredSelectBlockGenerator::class, $generator->definition($registry->namespace())); $attrs = array_filter(array( 'id' => SourceDom::attr($trigger, 'id'), 'name' => SourceDom::attr($trigger, 'name'), @@ -226,11 +225,32 @@ public function listbox(DOMElement $trigger, ?DOMElement $panel = null): ?array 'options' => $options, 'placeholder' => $triggerLabel, ), static fn (mixed $value): bool => is_array($value) ? array() !== $value : '' !== $value); - $markup = $generator->markup($attrs); - $control = BlockFactory::leaf($registry->blockName(AuthoredSelectBlockGenerator::LOCAL_NAME), $attrs, $markup); + $control = $this->nativeSelect($attrs, $trigger); return $this->createBlock->createBlock('core/group', array('className' => 'blocks-engine-authored-select-wrapper'), array($control), null); } + /** + * Core list items own editable option labels and ordering. Native semantics + * are metadata, consumed by the theme renderer instead of a companion block. + * @param array $attrs + */ + private function nativeSelect(array $attrs, DOMElement $source): array + { + $items = array(); + foreach ($attrs['options'] ?? array() as $option) { + $label = (string) ($option['label'] ?? ''); + unset($option['label']); + $items[] = $this->createBlock->createBlock('core/list-item', array( + 'content' => $this->runtime->escapeHtml($label), + 'metadata' => array('blocksEngineSelectOption' => $option), + )); + } + unset($attrs['options'], $attrs['selectedSummary']); + return $this->createBlock->createBlock('core/list', array( + 'metadata' => array('blocksEngineSelect' => $attrs), + ), $items, $source); + } + /** * Return a compact native input only when the resolved cascade proves * authored presentation that a readable paragraph cannot retain. diff --git a/php-transformer/src/WordPressSitePlan/NativeSelectRuntime.php b/php-transformer/src/WordPressSitePlan/NativeSelectRuntime.php new file mode 100644 index 000000000..7eecffceb --- /dev/null +++ b/php-transformer/src/WordPressSitePlan/NativeSelectRuntime.php @@ -0,0 +1,47 @@ + 'class', 'ariaLabel' => 'aria-label' )[$key] ?? $key ) . '="' . esc_attr( $attrs[$key] ) . '"'; + } + foreach ( array( 'required', 'disabled', 'multiple' ) as $key ) if ( ! empty( $attrs[$key] ) ) $html .= ' ' . $key; + foreach ( $attrs['dataAttributes'] ?? array() as $name => $value ) { + if ( preg_match( '/^data-(?!wp-)[a-z0-9_.:-]+$/D', $name ) && ! str_starts_with( $name, 'data-blocks-engine-' ) ) $html .= ' ' . $name . '="' . esc_attr( $value ) . '"'; + } + $options = ''; $state = array(); $has_state = false; $group = 0; + foreach ( $block['innerBlocks'] ?? array() as $item ) { + if ( 'core/list-item' !== ( $item['blockName'] ?? '' ) ) continue; + $option = $item['attrs']['metadata']['blocksEngineSelectOption'] ?? array(); + // Read the saved core label, so editor changes are the native label too. + $label = html_entity_decode( wp_strip_all_tags( $item['innerHTML'] ?? '' ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); + $next = $option['group']['index'] ?? 0; + if ( $next !== $group ) { + if ( $group ) $options .= ''; + if ( $next ) $options .= ''; + $group = $next; + } + $options .= '' . esc_html( $label ) . ''; + $has_state = $has_state || array_key_exists( 'initialSelected', $option ); + $state[] = array( 'selected' => $option['initialSelected'] ?? ! empty( $option['selected'] ), 'defaultSelected' => ! empty( $option['selected'] ) ); + } + if ( $group ) $options .= ''; + if ( $has_state ) $html .= ' data-blocks-engine-control-state="' . esc_attr( wp_json_encode( array( 'version' => 1, 'kind' => 'select', 'options' => $state ), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) ) . '"'; + $html .= '>' . $options . ''; + if ( ! empty( $attrs['label'] ) ) $html = '' . wp_kses_post( ! empty( $attrs['labelMarkup'] ) ? $attrs['labelMarkup'] : esc_html( $attrs['label'] ) ) . $html . ''; + return $html; +}, 10, 2 ); +PHP; + } +} diff --git a/php-transformer/src/WordPressSitePlan/ThemeBootstrap.php b/php-transformer/src/WordPressSitePlan/ThemeBootstrap.php index abfd74978..f356569ec 100644 --- a/php-transformer/src/WordPressSitePlan/ThemeBootstrap.php +++ b/php-transformer/src/WordPressSitePlan/ThemeBootstrap.php @@ -516,6 +516,12 @@ public static function functionsPhp(array $assets, array $scripts = array(), arr $headerOffsetScript = NavigationHeaderOffsetRuntime::script(); $lines[] = "add_action( 'wp_enqueue_scripts', static function (): void { wp_register_script( 'blocks-engine-navigation-header-offset', false, array(), null, true ); wp_enqueue_script( 'blocks-engine-navigation-header-offset' ); wp_add_inline_script( 'blocks-engine-navigation-header-offset', " . var_export($headerOffsetScript, true) . " ); } );"; } + if (self::markupContains($documentsWithMenus, 'blocksEngineSelect')) { + $lines[] = NativeSelectRuntime::source(); + $selectStateScript = \Automattic\BlocksEngine\PhpTransformer\WordPress\AuthoredControlStateScript::viewScript(); + $lines[] = "add_action( 'wp_enqueue_scripts', static function (): void { wp_register_script( 'blocks-engine-select-state', false, array(), null, true ); wp_enqueue_script( 'blocks-engine-select-state' ); wp_add_inline_script( 'blocks-engine-select-state', " . var_export($selectStateScript, true) . " ); } );"; + $lines[] = "add_action( 'wp_enqueue_scripts', static function (): void { wp_register_style( 'blocks-engine-select-wrapper', false ); wp_enqueue_style( 'blocks-engine-select-wrapper' ); wp_add_inline_style( 'blocks-engine-select-wrapper', '.wp-block-group.blocks-engine-authored-select-wrapper{display:contents}' ); } );"; + } $breadcrumbLabels = BreadcrumbLabelRuntime::labels($documents, $pages); $breadcrumbPostTrails = BreadcrumbLabelRuntime::postTrails($pages); if (array() !== $breadcrumbLabels || array() !== $breadcrumbPostTrails) $lines[] = BreadcrumbLabelRuntime::source($breadcrumbLabels, $breadcrumbPostTrails); diff --git a/php-transformer/tests/Support/NativeSelectRendererFixture.php b/php-transformer/tests/Support/NativeSelectRendererFixture.php new file mode 100644 index 000000000..a8bc57941 --- /dev/null +++ b/php-transformer/tests/Support/NativeSelectRendererFixture.php @@ -0,0 +1,40 @@ +parseBlocks($markup))); + } +} + +function add_filter($name, $callback, ...$args) { NativeSelectRendererFixture::$filter = $callback; } +function esc_attr($text) { return htmlspecialchars((string) $text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); } +function esc_html($text) { return esc_attr($text); } +function wp_strip_all_tags($text) { return strip_tags($text); } +function wp_json_encode($data, $flags = 0) { return json_encode($data, $flags); } +function wp_kses_post($text) { return $text; } diff --git a/php-transformer/tests/contract/component-interactions.php b/php-transformer/tests/contract/component-interactions.php index 87d75ef19..87a84f5e4 100644 --- a/php-transformer/tests/contract/component-interactions.php +++ b/php-transformer/tests/contract/component-interactions.php @@ -23,12 +23,14 @@ $html = $markup($result); $assert(!isset($report['pages'][0]['states']), 'Real emitter fixture has one component inventory.'); $assert(array('dialog', 'listbox', 'tabs', 'disclosure', 'carousel', 'unsupported') === array_column($report['pages'][0]['components'], 'kind'), 'Real capture covers all six surfaces.'); -foreach (array('custom/authored-select', 'tabs', 'tab-list', 'tab-panels', 'tab-panel', 'details', 'custom/authored-carousel') as $block) { +foreach (array('tabs', 'tab-list', 'tab-panels', 'tab-panel', 'details', 'custom/authored-carousel') as $block) { $assert(str_contains($html, ''), $block . ' keeps its pre-migration native block family.'); } $assert(!str_contains($html, '