diff --git a/php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php b/php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php index e882e5d01..a92683fe0 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. A paragraph that only carries a source - // anchor stays transparent: the anchor is the layout participant. + // 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 33d7af093..a11f0a71b 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-browser.mjs b/php-transformer/tests/contract/synthetic-anchor-editor-hitbox-browser.mjs new file mode 100644 index 000000000..022b68bc2 --- /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 000000000..ad5e1127f --- /dev/null +++ b/php-transformer/tests/contract/synthetic-anchor-editor-hitbox.php @@ -0,0 +1,23 @@ +transform( + '' + . '
Interior

Steering wheel

Details

' +)->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 81cd71f93..ab16beb66 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}'), '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 bb44c4e79..cd001b28a 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 keeps its source layout, 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), 'layout-owning anchor carriers remain transparent'); $badge = ( new HtmlTransformer() )->transform( ''