From fba2a9fe90f298f8dbe43563f484c08ed9a34678 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 20:49:37 +0000 Subject: [PATCH 1/2] fix: keep synthetic anchor paragraphs focusable across rendering contexts --- .../HtmlToBlocks/Style/EngineSupportCss.php | 6 ++--- ...synthetic-anchor-editor-hitbox-browser.mjs | 23 +++++++++++++++++++ .../synthetic-anchor-editor-hitbox.php | 23 +++++++++++++++++++ .../tests/unit/engine-support-css.php | 2 +- .../unit/synthetic-paragraph-inline-box.php | 5 ++-- 5 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 php-transformer/tests/contract/synthetic-anchor-editor-hitbox-browser.mjs create mode 100644 php-transformer/tests/contract/synthetic-anchor-editor-hitbox.php diff --git a/php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php b/php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php index e882e5d01d..ce8f3e6ce6 100644 --- a/php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php +++ b/php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php @@ -102,11 +102,11 @@ public function beforeAuthorCss(string $serializedBlocks): array // lays out as that content did (in flow it shares the line; a // flex or grid parent blockifies it into the same item), and, // unlike display:contents, it can take focus, so the editor can - // select and edit it. A paragraph that only carries a source - // anchor stays transparent: the anchor is the layout participant. + // select and edit it. Anchor carriers need that same box in both + // contexts; display:contents cannot receive RichText focus. // A lowered block-level source wrapper keeps its block box. $parts[] = ':where(p.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . ':not(.' . SourceBlockAttributeProjector::SOURCE_BOX_PARAGRAPH_CLASS . ')){display:inline}' - . "\n" . ':where(p.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . ':not(.' . SourceBlockAttributeProjector::SOURCE_BOX_PARAGRAPH_CLASS . '):has(>a)){display:contents}' + // A parent's sibling-spacing rule (`.stack > * + *`) now matches the // carrier, whose margins do nothing as display:contents. The // anchor it stands for takes them instead, as in the source. diff --git a/php-transformer/tests/contract/synthetic-anchor-editor-hitbox-browser.mjs b/php-transformer/tests/contract/synthetic-anchor-editor-hitbox-browser.mjs new file mode 100644 index 0000000000..022b68bc26 --- /dev/null +++ b/php-transformer/tests/contract/synthetic-anchor-editor-hitbox-browser.mjs @@ -0,0 +1,23 @@ +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +const { chromium } = await import(process.env.PLAYWRIGHT_MODULE || 'playwright'); +const { markup, css } = JSON.parse(readFileSync(process.env.SYNTHETIC_ANCHOR_EDITOR_ARTIFACT, 'utf8')); +const browser = await chromium.launch({ headless: true }); +try { + const page = await browser.newPage(); + for (const editor of [false, true]) { + await page.setContent(`${markup}`); + const carrier = page.locator('p:has(>a[href="/"])'); + assert.ok(await carrier.evaluate(e => e.getBoundingClientRect().width > 0), 'The native paragraph has a box on both frontend and editor'); + if (!editor) continue; + await carrier.evaluate(e => { + e.contentEditable = 'true'; + e.addEventListener('click', event => { event.preventDefault(); e.dataset.clicked = 'yes'; }); + }); + await carrier.click({ timeout: 2000 }); + assert.equal(await carrier.getAttribute('data-clicked'), 'yes', 'The RichText carrier, not its enclosing Group, receives the click'); + assert.equal(await carrier.evaluate(e => document.activeElement === e), true, 'Native RichText can receive focus'); + } +} finally { + await browser.close(); +} diff --git a/php-transformer/tests/contract/synthetic-anchor-editor-hitbox.php b/php-transformer/tests/contract/synthetic-anchor-editor-hitbox.php new file mode 100644 index 0000000000..916fae5dd0 --- /dev/null +++ b/php-transformer/tests/contract/synthetic-anchor-editor-hitbox.php @@ -0,0 +1,23 @@ +transform( + '' + . '
' +)->toArray(); +$markup = $result['serialized_blocks'] ?? ''; +$css = implode("\n", array_column($result['assets'] ?? array(), 'content')); +$dom = new DOMDocument(); +@$dom->loadHTML($markup); +$paragraph = (new DOMXPath($dom))->query('//p[a[@href="/"]]')->item(0); +if (!$paragraph instanceof DOMElement || !str_contains($paragraph->getAttribute('class'), 'blocks-engine-synthetic-paragraph')) { + throw new RuntimeException('Source brand must lower to a native synthetic paragraph.'); +} +if ($path = getenv('SYNTHETIC_ANCHOR_EDITOR_ARTIFACT')) { + file_put_contents($path, json_encode(array('markup' => $markup, 'css' => $css), JSON_THROW_ON_ERROR)); +} +echo "Synthetic anchor editor hitbox contract passed.\n"; diff --git a/php-transformer/tests/unit/engine-support-css.php b/php-transformer/tests/unit/engine-support-css.php index 81cd71f93c..5500eb14e5 100644 --- a/php-transformer/tests/unit/engine-support-css.php +++ b/php-transformer/tests/unit/engine-support-css.php @@ -59,7 +59,7 @@ $coreDefaults = implode("\n", $css->coreDefaultResetCss(SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS)); $assert(str_contains($coreDefaults, ':root :where(.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . '){margin-top:0;margin-bottom:0}'), 'synthetic paragraph margin reset cancels a core default'); $assert(str_contains($synthetic[0], ':where(p.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . ':not(.' . SourceBlockAttributeProjector::SOURCE_BOX_PARAGRAPH_CLASS . ')){display:inline}'), 'synthetic paragraph has a focusable inline box unless it is a lowered source box'); -$assert(str_contains($synthetic[0], ':where(p.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . ':not(.' . SourceBlockAttributeProjector::SOURCE_BOX_PARAGRAPH_CLASS . '):has(>a)){display:contents}'), 'synthetic paragraph that only carries a source anchor stays layout-transparent'); +$assert(! str_contains($synthetic[0], ':where(p.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . ':not(.' . SourceBlockAttributeProjector::SOURCE_BOX_PARAGRAPH_CLASS . '):has(>a)){display:contents}'), 'synthetic anchor paragraphs retain a focusable box'); $layoutShell = $css->beforeAuthorCss('', 'blocks-engine/layout-shell'); $assert(array() === $layoutShell, 'layout shells require no editor layer repair'); diff --git a/php-transformer/tests/unit/synthetic-paragraph-inline-box.php b/php-transformer/tests/unit/synthetic-paragraph-inline-box.php index bb44c4e791..f385a1b9e6 100644 --- a/php-transformer/tests/unit/synthetic-paragraph-inline-box.php +++ b/php-transformer/tests/unit/synthetic-paragraph-inline-box.php @@ -8,7 +8,7 @@ * own geometry (a flex parent blockifies it into the same item) and the * editor can focus it: an element with no box cannot take focus, so its * RichText never selects its block. A paragraph that only carries a source - * anchor stays transparent, and a text-only source div is one lowered + * anchor also keeps a focusable box, and a text-only source div is one lowered * paragraph that keeps its own padded block box. */ @@ -47,8 +47,7 @@ $assert(str_contains($tracklistMarkup, '

Kindling

'), 'a text-only flex-item span is retagged as a synthetic paragraph carrying its class', $tracklistMarkup); $assert(str_contains($tracklistCss, $inlineRule), 'a synthetic paragraph gets an inline box that a flex parent blockifies like the source span'); $assert(! str_contains($tracklistCss, ':where(p.' . $synthetic . ':not(.' . $sourceBox . ')){display:contents}'), 'a synthetic paragraph is not boxless, so min-width/flex and editor focus apply to it'); -$assert(str_contains($tracklistCss, $anchorRule), 'a paragraph that only carries a source anchor stays transparent'); -$assert(strpos($tracklistCss, $anchorRule) > strpos($tracklistCss, $inlineRule), 'the anchor-carrier rule follows the inline rule at equal specificity'); +$assert(! str_contains($tracklistCss, $anchorRule), 'anchor carriers also retain an inline box for native RichText focus'); $badge = ( new HtmlTransformer() )->transform( '' From 6cb2efd7d4298e8ccaebf25138fae48118f0b0e5 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sun, 15 Nov 2026 20:49:37 +0000 Subject: [PATCH 2/2] fix: scope focusable anchor carriers to inline flow before blocks --- .../HtmlToBlocks/Style/EngineSupportCss.php | 7 ++--- .../Style/SourceBlockAttributeProjector.php | 27 +++++++++++++++++++ .../synthetic-anchor-editor-hitbox.php | 4 +-- .../tests/unit/engine-support-css.php | 2 +- .../unit/synthetic-paragraph-inline-box.php | 4 +-- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php b/php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php index ce8f3e6ce6..a92683fe0d 100644 --- a/php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php +++ b/php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php @@ -102,11 +102,12 @@ public function beforeAuthorCss(string $serializedBlocks): array // lays out as that content did (in flow it shares the line; a // flex or grid parent blockifies it into the same item), and, // unlike display:contents, it can take focus, so the editor can - // select and edit it. Anchor carriers need that same box in both - // contexts; display:contents cannot receive RichText focus. + // select and edit it. Layout-owning anchors keep their transparent + // carrier; inline-flow anchors need a focusable RichText box. // A lowered block-level source wrapper keeps its block box. $parts[] = ':where(p.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . ':not(.' . SourceBlockAttributeProjector::SOURCE_BOX_PARAGRAPH_CLASS . ')){display:inline}' - + . "\n" . ':where(p.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . ':not(.' . SourceBlockAttributeProjector::SOURCE_BOX_PARAGRAPH_CLASS . '):has(>a)){display:contents}' + . "\n" . ':where(p.blocks-engine-inline-anchor-carrier){display:inline}' // A parent's sibling-spacing rule (`.stack > * + *`) now matches the // carrier, whose margins do nothing as display:contents. The // anchor it stands for takes them instead, as in the source. diff --git a/php-transformer/src/HtmlToBlocks/Style/SourceBlockAttributeProjector.php b/php-transformer/src/HtmlToBlocks/Style/SourceBlockAttributeProjector.php index 33d7af0933..a11f0a71b5 100644 --- a/php-transformer/src/HtmlToBlocks/Style/SourceBlockAttributeProjector.php +++ b/php-transformer/src/HtmlToBlocks/Style/SourceBlockAttributeProjector.php @@ -196,6 +196,19 @@ public function project( $attrs['className'] = SourceDom::mergeClassNames((string) ($attrs['className'] ?? ''), self::SYNTHETIC_ANCHOR_BLOCK_DISPLAY_CLASS); } if ( 'a' === $sourceTagName ) { + $presentation = $this->styleResolver->structuralPresentationDeclarations($sourceElement); + $display = CssValueInspector::comparable((string) ($presentation['display'] ?? '')); + $float = CssValueInspector::comparable((string) ($presentation['float'] ?? '')); + if ( in_array($display, array('', 'inline'), true) + && in_array($float, array('', 'none'), true) + && ! $this->isDirectChildOfAuthorOwnedLayout($sourceElement) + && $this->anchorPrecedesBlock($sourceElement) ) { + // A boxless RichText root cannot receive focus. In inline + // flow before a block, its fallback hitbox can overlap the + // following Group. An inline carrier preserves the source + // line without changing flex/grid or floating link layout. + $attrs['className'] = SourceDom::mergeClassNames((string) ($attrs['className'] ?? ''), 'blocks-engine-inline-anchor-carrier'); + } $attrs = $this->withSyntheticHeaderAnchorCarrier($attrs, $sourceElement, $context->generatedStyles); } } @@ -980,6 +993,20 @@ private function sourceAnchorResolvesToBlockDisplay(DOMElement $anchor): bool return '' === $ownDisplay && $this->isDirectChildOfAuthorOwnedLayout($anchor); } + private function anchorPrecedesBlock(DOMElement $anchor): bool + { + $next = $anchor->nextSibling; + while ( $next instanceof \DOMText && '' === trim($next->textContent) ) { + $next = $next->nextSibling; + } + if ( ! $next instanceof DOMElement ) { + return false; + } + $display = CssValueInspector::comparable((string) ($this->styleResolver->structuralPresentationDeclarations($next)['display'] ?? '')); + return in_array($display, array('block', 'flex', 'grid', 'flow-root', 'table'), true) + || ( '' === $display && in_array(strtolower($next->tagName), array('div', 'section', 'article', 'aside', 'nav', 'header', 'footer', 'main', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'blockquote'), true) ); + } + private function isDirectChildOfAuthorOwnedLayout(DOMElement $element): bool { $parent = $element->parentNode; diff --git a/php-transformer/tests/contract/synthetic-anchor-editor-hitbox.php b/php-transformer/tests/contract/synthetic-anchor-editor-hitbox.php index 916fae5dd0..ad5e1127f0 100644 --- a/php-transformer/tests/contract/synthetic-anchor-editor-hitbox.php +++ b/php-transformer/tests/contract/synthetic-anchor-editor-hitbox.php @@ -6,8 +6,8 @@ use Automattic\BlocksEngine\PhpTransformer\HtmlToBlocks\HtmlTransformer; $result = (new HtmlTransformer())->transform( - '' - . '
' + '' + . '
Interior

Steering wheel

Details

' )->toArray(); $markup = $result['serialized_blocks'] ?? ''; $css = implode("\n", array_column($result['assets'] ?? array(), 'content')); diff --git a/php-transformer/tests/unit/engine-support-css.php b/php-transformer/tests/unit/engine-support-css.php index 5500eb14e5..ab16beb662 100644 --- a/php-transformer/tests/unit/engine-support-css.php +++ b/php-transformer/tests/unit/engine-support-css.php @@ -59,7 +59,7 @@ $coreDefaults = implode("\n", $css->coreDefaultResetCss(SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS)); $assert(str_contains($coreDefaults, ':root :where(.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . '){margin-top:0;margin-bottom:0}'), 'synthetic paragraph margin reset cancels a core default'); $assert(str_contains($synthetic[0], ':where(p.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . ':not(.' . SourceBlockAttributeProjector::SOURCE_BOX_PARAGRAPH_CLASS . ')){display:inline}'), 'synthetic paragraph has a focusable inline box unless it is a lowered source box'); -$assert(! str_contains($synthetic[0], ':where(p.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . ':not(.' . SourceBlockAttributeProjector::SOURCE_BOX_PARAGRAPH_CLASS . '):has(>a)){display:contents}'), 'synthetic anchor paragraphs retain a focusable box'); +$assert(str_contains($synthetic[0], ':where(p.' . SourceBlockAttributeProjector::SYNTHETIC_PARAGRAPH_CLASS . ':not(.' . SourceBlockAttributeProjector::SOURCE_BOX_PARAGRAPH_CLASS . '):has(>a)){display:contents}'), 'layout-owning anchors retain transparent carriers'); $layoutShell = $css->beforeAuthorCss('', 'blocks-engine/layout-shell'); $assert(array() === $layoutShell, 'layout shells require no editor layer repair'); diff --git a/php-transformer/tests/unit/synthetic-paragraph-inline-box.php b/php-transformer/tests/unit/synthetic-paragraph-inline-box.php index f385a1b9e6..cd001b28a7 100644 --- a/php-transformer/tests/unit/synthetic-paragraph-inline-box.php +++ b/php-transformer/tests/unit/synthetic-paragraph-inline-box.php @@ -8,7 +8,7 @@ * own geometry (a flex parent blockifies it into the same item) and the * editor can focus it: an element with no box cannot take focus, so its * RichText never selects its block. A paragraph that only carries a source - * anchor also keeps a focusable box, and a text-only source div is one lowered + * anchor keeps its source layout, and a text-only source div is one lowered * paragraph that keeps its own padded block box. */ @@ -47,7 +47,7 @@ $assert(str_contains($tracklistMarkup, '

Kindling

'), 'a text-only flex-item span is retagged as a synthetic paragraph carrying its class', $tracklistMarkup); $assert(str_contains($tracklistCss, $inlineRule), 'a synthetic paragraph gets an inline box that a flex parent blockifies like the source span'); $assert(! str_contains($tracklistCss, ':where(p.' . $synthetic . ':not(.' . $sourceBox . ')){display:contents}'), 'a synthetic paragraph is not boxless, so min-width/flex and editor focus apply to it'); -$assert(! str_contains($tracklistCss, $anchorRule), 'anchor carriers also retain an inline box for native RichText focus'); +$assert(str_contains($tracklistCss, $anchorRule), 'layout-owning anchor carriers remain transparent'); $badge = ( new HtmlTransformer() )->transform( ''