Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions php-transformer/src/HtmlToBlocks/Style/EngineSupportCss.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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(`<!doctype html><style>${css}</style><body class="${editor ? 'editor-styles-wrapper' : ''}">${markup}</body>`);
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();
}
23 changes: 23 additions & 0 deletions php-transformer/tests/contract/synthetic-anchor-editor-hitbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

require dirname(__DIR__, 2) . '/vendor/autoload.php';

use Automattic\BlocksEngine\PhpTransformer\HtmlToBlocks\HtmlTransformer;

$result = (new HtmlTransformer())->transform(
'<style>.eyebrow{font-size:12px;text-transform:uppercase}</style>'
. '<main><div><a class="eyebrow" href="/">Interior</a><div><h1>Steering wheel</h1><p>Details</p></div></div></main>'
)->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";
2 changes: 1 addition & 1 deletion php-transformer/tests/unit/engine-support-css.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<!-- wp:blocks-engine/layout-shell -->', 'blocks-engine/layout-shell');
$assert(array() === $layoutShell, 'layout shells require no editor layer repair');
Expand Down
5 changes: 2 additions & 3 deletions php-transformer/tests/unit/synthetic-paragraph-inline-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand Down Expand Up @@ -47,8 +47,7 @@
$assert(str_contains($tracklistMarkup, '<p class="track-title ' . $synthetic . '">Kindling</p>'), '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(
'<style>.card{position:relative}.featured-badge{position:absolute;top:-11px;padding:4px 14px;background:#e8a020}</style>'
Expand Down
Loading