From 85fdece31c87d9f620ae6a79cf43d245a088418e Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 19:02:18 -0500 Subject: [PATCH 1/4] fix: preserve form and icon semantics during core button lowering --- .../AuthoredFormControlBlockConverter.php | 49 +++++++++++++++++-- .../Elements/ButtonElementConverter.php | 8 +++ .../src/HtmlToBlocks/HtmlCompilation.php | 15 +++++- .../RuntimeAttributesRuntime.php | 9 +++- .../authored-form-control-block-converter.php | 4 +- .../tests/unit/photo-button-core.php | 33 +++++++++++++ .../tests/unit/plain-native-button-core.php | 33 +++++++++++++ 7 files changed, 142 insertions(+), 9 deletions(-) create mode 100644 php-transformer/tests/unit/photo-button-core.php create mode 100644 php-transformer/tests/unit/plain-native-button-core.php diff --git a/php-transformer/src/HtmlToBlocks/Elements/AuthoredFormControlBlockConverter.php b/php-transformer/src/HtmlToBlocks/Elements/AuthoredFormControlBlockConverter.php index 68117d473c..a02583391b 100644 --- a/php-transformer/src/HtmlToBlocks/Elements/AuthoredFormControlBlockConverter.php +++ b/php-transformer/src/HtmlToBlocks/Elements/AuthoredFormControlBlockConverter.php @@ -358,19 +358,25 @@ public function textarea(DOMElement $textarea, ?DOMElement $label = null, bool $ } /** - * Return a compact native button on the same terms as `input()`: a submit - * (or other form-action) control the source styles has no readable prose - * equivalent, because a Gutenberg button saves as an anchor and cannot - * submit its ancestor form. + * Return an editable native form action. Plain labels use core/button's + * native button tag; controls needing unsupported attributes or their + * original containing block retain the authored representation. * * @return array|null */ - public function button(DOMElement $button, bool $forceNative = false): ?array + public function button(DOMElement $button, bool $forceNative = false, bool $preserveContainingBlock = false): ?array { if ( ! $forceNative && array() === ($this->authoredPresentationDeclarations)($button) ) { return null; } + // Core/button supports native form actions. Plain labels do not need + // a companion merely because their source has authored presentation. + // Let the normal source projection own the wrapper/control selectors. + if (!$preserveContainingBlock && $this->hasPlainCoreButtonLabel($button)) { + return $this->plainCoreButton($button); + } + $this->session->authorSelectorProjectionState()->recordAuthoredControlCandidate($button); $generator = new AuthoredButtonBlockGenerator(); $registry = $this->session->requireGeneratedBlockRegistry(); @@ -434,6 +440,10 @@ public function runtimeButton(DOMElement $element): array /** @return array */ private function runtimeButtonBlock(DOMElement $button): array { + if ($this->hasPlainCoreButtonLabel($button)) { + return $this->plainCoreButton($button); + } + // SVG controls have no RichText label. Model their artwork as core // images and containers rather than an opaque contentParts tree. // Inline-only icons and CSS-painted controls still use the native @@ -454,6 +464,35 @@ private function runtimeButtonBlock(DOMElement $button): array return BlockFactory::leaf($registry->blockName(AuthoredButtonBlockGenerator::LOCAL_NAME), $attrs, $markup); } + private function hasPlainCoreButtonLabel(DOMElement $button): bool + { + if ('button' !== strtolower($button->tagName) || '' === trim($button->textContent) || SourceDom::childElementCount($button) > 0) return false; + // Form actions carry binding, inactive-submission and theme-paint + // semantics owned by the form pipeline. Do not bypass that lowering. + for ($ancestor = $button->parentNode; $ancestor instanceof DOMElement; $ancestor = $ancestor->parentNode) { + if ('form' === strtolower($ancestor->tagName)) return false; + } + // A synthetic wrapper changes the containing block for relative sizes, + // including those introduced by a responsive cascade or custom property. + $declarations = ($this->authoredPresentationDeclarations)($button); + if (str_contains(json_encode($declarations) ?: '', '%') || str_contains(SourceDom::attr($button, 'style'), '%')) return false; + // These native attributes have no core/button save equivalent. + foreach ($button->attributes as $attribute) { + if (!in_array(strtolower($attribute->nodeName), array('type', 'class', 'style'), true)) return false; + } + return true; + } + + private function plainCoreButton(DOMElement $button): array + { + $type = FormControlClassifier::controlType($button); + return $this->createBlock->createBlock('core/button', array( + 'tagName' => 'button', + 'type' => in_array($type, array('button', 'reset', 'submit'), true) ? $type : 'submit', + 'text' => $this->runtime->escapeHtml($button->textContent), + ), array(), $button); + } + private function isCssDrawnControl(DOMElement $element): bool { if ('svg' === strtolower($element->tagName)) { diff --git a/php-transformer/src/HtmlToBlocks/Elements/ButtonElementConverter.php b/php-transformer/src/HtmlToBlocks/Elements/ButtonElementConverter.php index 96e7f14e28..cea1416ef7 100644 --- a/php-transformer/src/HtmlToBlocks/Elements/ButtonElementConverter.php +++ b/php-transformer/src/HtmlToBlocks/Elements/ButtonElementConverter.php @@ -56,6 +56,14 @@ public function convert(DOMElement $element, string $tagName, array &$fallbacks, } } + // A photo control is a native container around editable core images, + // not an opaque authored-button contentParts tree. Keep the button's + // containing block so percentage-sized thumbnails retain their geometry. + if ( $this->hasContentScaleImage($element) && '' === trim($element->textContent) ) { + $container = $this->context->convertButtonWrapperGroup($element, $fallbacks); + if ( null !== $container ) return ConversionOutcome::handled($container); + } + if ( $this->hasAccessibleIconOnlyContent($element) ) { return ConversionOutcome::handled($this->context->runtimeButton($element)); } diff --git a/php-transformer/src/HtmlToBlocks/HtmlCompilation.php b/php-transformer/src/HtmlToBlocks/HtmlCompilation.php index 6699f64ce2..a2755c08d2 100644 --- a/php-transformer/src/HtmlToBlocks/HtmlCompilation.php +++ b/php-transformer/src/HtmlToBlocks/HtmlCompilation.php @@ -1924,7 +1924,7 @@ public function percentageSizedFormButton(DOMElement $button): ?array return null; } $this->session->authorSelectorProjectionState()->markRetainedSourcePath($button->getNodePath() ?? ''); - return $this->authoredFormControlBlockConverter->button($button, true); + return $this->authoredFormControlBlockConverter->button($button, true, true); } /** Keep containing-block-relative sizes on the original, editable control. */ @@ -2248,6 +2248,19 @@ public function convertButtonWrapperGroup(DOMElement $button, array &$fallbacks) return null; } + if ( '' === trim($button->textContent) && $button->getElementsByTagName('img')->length > 0 ) { + $attributes = array(); + foreach ($button->attributes as $attribute) { + $attributes[strtolower($attribute->nodeName)] = $attribute->nodeValue ?? ''; + } + if (!isset($attributes['type'])) $attributes['type'] = 'submit'; + return $this->createBlock('core/group', array( + 'tagName' => 'button', + 'className' => SourceDom::attr($button, 'class'), + 'metadata' => array('blocksEngineControlBox' => $attributes, 'blocksEngineControlTagName' => 'button'), + ), $children, $button); + } + return $this->layoutShells->blockForElements(array($button), $children, $button); } diff --git a/php-transformer/src/WordPressSitePlan/RuntimeAttributesRuntime.php b/php-transformer/src/WordPressSitePlan/RuntimeAttributesRuntime.php index 31b90d707c..058367680b 100644 --- a/php-transformer/src/WordPressSitePlan/RuntimeAttributesRuntime.php +++ b/php-transformer/src/WordPressSitePlan/RuntimeAttributesRuntime.php @@ -28,7 +28,14 @@ public static function source(): string foreach ( $box as $name => $value ) { if ( is_string( $name ) && is_string( $value ) && preg_match( '/^(?:class|style|id|type|name|title|tabindex|disabled|hidden|role|data-(?!wp-)[a-z0-9_.:-]+|aria-[a-z-]+)$/D', $name ) ) $processor->set_attribute( $name, $value ); } - return $processor->get_updated_html(); + $content = $processor->get_updated_html(); + if ( 'button' === ( $block['attrs']['metadata']['blocksEngineControlTagName'] ?? null ) ) { + // Core's editor keeps a selectable group; the published control + // must remain a native button, without an extra containing box. + $content = preg_replace( '/^\s*$/', '', $content, 1 ); + } + return $content; } } $attributes = $block['attrs']['metadata']['blocksEngineRuntimeAttributes'] ?? null; diff --git a/php-transformer/tests/unit/authored-form-control-block-converter.php b/php-transformer/tests/unit/authored-form-control-block-converter.php index bcbdc45882..dbd15e90de 100644 --- a/php-transformer/tests/unit/authored-form-control-block-converter.php +++ b/php-transformer/tests/unit/authored-form-control-block-converter.php @@ -111,8 +111,8 @@ $plainButton = $elementFrom('', 'button'); $assert(null === $converter->button($plainButton), 'unstyled-button-declines-authored-block'); $forcedButton = $converter->button($plainButton, true); -$assert($registry->blockName(AuthoredButtonBlockGenerator::LOCAL_NAME) === ($forcedButton['blockName'] ?? ''), 'force-native-button-uses-authored-button-block'); -$assert('' === ($forcedButton['innerHTML'] ?? ''), 'force-native-button-emits-native-markup'); +$assert('core/button' === ($forcedButton['blockName'] ?? ''), 'force-native-plain-button-uses-core-button'); +$assert('button' === ($forcedButton['attrs']['tagName'] ?? '') && 'submit' === ($forcedButton['attrs']['type'] ?? '') && 'Send' === ($forcedButton['attrs']['text'] ?? ''), 'force-native-button-retains-native-action-and-label'); $styledButton = $elementFrom('', 'button'); $buttonBlock = $converter->button($styledButton); diff --git a/php-transformer/tests/unit/photo-button-core.php b/php-transformer/tests/unit/photo-button-core.php new file mode 100644 index 0000000000..9dfae01a2f --- /dev/null +++ b/php-transformer/tests/unit/photo-button-core.php @@ -0,0 +1,33 @@ +transform( + '' + . '' +)->toArray(); +$markup = $result['serialized_blocks']; +foreach (array( + 'no opaque control' => !str_contains($markup, '/authored-button'), + 'no generated shell' => !str_contains($markup, '/layout-shell'), + 'editable image' => str_contains($markup, '