Skip to content

feat: data-mageforge attribute handling#189

Open
dermatz wants to merge 5 commits into
mainfrom
feat/data-mageforge-attribute
Open

feat: data-mageforge attribute handling#189
dermatz wants to merge 5 commits into
mainfrom
feat/data-mageforge-attribute

Conversation

@dermatz
Copy link
Copy Markdown
Member

@dermatz dermatz commented May 27, 2026

This pull request refactors how MageForge inspector metadata is injected and parsed in both backend PHP and frontend JavaScript. The system now uses HTML data attributes (instead of HTML comments) to mark and describe rendered blocks, improving robustness—especially when block output is embedded inside attribute values (e.g., PageBuilder URLs). The inspector can now more reliably identify blocks, including special handling for CMS/PageBuilder content, and exposes new metadata such as CMS block IDs.

Inspector metadata injection and backend logic:

  • Switched from HTML comment markers to injecting data-mageforge-id and data-mageforge-block attributes on the first root HTML element of each block, preventing markup corruption when blocks are embedded in attributes. Injection is skipped if the content doesn’t start with an HTML element. [1] [2]
  • Added detection and inclusion of the CMS block identifier (cmsBlockId) in the inspector metadata for blocks rendered via Magento CMS/PageBuilder. [1] [2]

Frontend JavaScript inspector logic:

  • Refactored the DOM parsing logic to find MageForge blocks by data-mageforge-id attributes instead of scanning for comment nodes. Added robust parsing of block metadata and improved block lookup, including fallbacks for multi-root PageBuilder content.
  • Updated the inspector tab logic to display the CMS block identifier if present, providing clearer context for PageBuilder/CMS blocks. [1] [2]
  • Improved picker logic to handle cases where no MageForge block data is available, especially for PageBuilder elements, ensuring the inspector still opens in a fallback state.

Copilot AI review requested due to automatic review settings May 27, 2026 15:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Replaces HTML-comment-based inspector markers with data-mageforge-* attributes on the first root element of each rendered block, so block output can be safely embedded inside attribute values (notably PageBuilder URLs) without corrupting markup. Also surfaces a CMS block identifier and adapts the frontend DOM/picker/tabs code to the new attribute-based discovery, including a PageBuilder multi-root fallback.

Changes:

  • Backend: inject data-mageforge-id / data-mageforge-block JSON via regex on the first opening tag and add cmsBlockId to metadata.
  • Frontend DOM: rewrite block discovery to query [data-mageforge-id], parse JSON metadata, and add a PageBuilder sibling-search fallback.
  • Frontend UI: render a "CMS Block" section when present, and let the picker still target elements inside [data-content-type] even when no block was resolved.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/Model/TemplateEngine/Decorator/InspectorHints.php Switch from comment wrappers to data-attribute injection on the first root tag; add cmsBlockId metadata.
src/view/frontend/web/js/inspector/dom.js Replace TreeWalker comment parsing with attribute-based discovery and add PageBuilder multi-root fallback.
src/view/frontend/web/js/inspector/picker.js Return inspectable target inside [data-content-type] even when no block metadata is resolved.
src/view/frontend/web/js/inspector/tabs.js Carry cmsBlockId through inherited data and render a new "CMS Block" info section.

Comment on lines +246 to +249
// Escape single quotes so JSON can be safely embedded in a single-quoted HTML attribute.
// The browser automatically decodes HTML entities when getAttribute() is called,
// so JSON.parse() on the JS side will receive the correct string.
$safeJson = str_replace("'", ''', $jsonMetadata);
Comment on lines +255 to 265
$result = preg_replace_callback(
'/^(\s*<[a-zA-Z][a-zA-Z0-9]*)/s',
function (array $matches) use ($wrapperId, $safeJson, &$replaced): string {
$replaced = true;
return $matches[0]
. ' data-mageforge-id="' . $wrapperId . '"'
. ' data-mageforge-block=\'' . $safeJson . "'";
},
$html,
$wrapperId,
1,
);
Comment on lines +222 to +223
// Detect CMS block identifier (e.g. for PageBuilder blocks rendered via Magento\Cms\Block\Block)
$cmsBlockId = method_exists($block, 'getBlockId') ? (string) $block->getBlockId() : '';
Copilot AI review requested due to automatic review settings May 27, 2026 15:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

// embedded inside HTML attribute values (e.g. PageBuilder URL blocks in href="...").
$replaced = false;
$result = preg_replace_callback(
'/^(\s*<[a-zA-Z][a-zA-Z0-9]*)/s',
Comment on lines +215 to +218
// Escape single quotes so JSON can be safely embedded in a single-quoted HTML attribute.
// The browser automatically decodes HTML entities when getAttribute() is called,
// so JSON.parse() on the JS side will receive the correct string.
$safeJson = str_replace("'", '&#39;', $jsonMetadata);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants