Steering wheel
Details
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( + '' + . 'Details
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( ''