Skip to content

Commit a2f9ebf

Browse files
authored
feat: enhance inspector with JSON metadata and comment parsing (#105)
* feat: enhance inspector with JSON metadata and comment parsing * style: fix formatting in while loop for comment parsing
1 parent 69f7328 commit a2f9ebf

2 files changed

Lines changed: 235 additions & 67 deletions

File tree

src/Model/TemplateEngine/Decorator/InspectorHints.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function render(BlockInterface $block, $templateFile, array $dictionary =
7171
}
7272

7373
/**
74-
* Inject data-mageforge-* attributes into HTML for inspector
74+
* Inject MageForge inspector comment markers into HTML
7575
*
7676
* @param string $html
7777
* @param BlockInterface $block
@@ -97,25 +97,30 @@ private function injectInspectorAttributes(string $html, BlockInterface $block,
9797
$blockAlias = $this->getBlockAlias($block);
9898
$isOverride = $this->isTemplateOverride($templateFile, $moduleName) ? '1' : '0';
9999

100-
// Build data attributes
101-
$dataAttributes = sprintf(
102-
'data-mageforge-template="%s" data-mageforge-block="%s" data-mageforge-module="%s" data-mageforge-id="%s" data-mageforge-viewmodel="%s" data-mageforge-parent="%s" data-mageforge-alias="%s" data-mageforge-override="%s"',
103-
htmlspecialchars($relativeTemplatePath, ENT_QUOTES, 'UTF-8'),
104-
htmlspecialchars($blockClass, ENT_QUOTES, 'UTF-8'),
105-
htmlspecialchars($moduleName, ENT_QUOTES, 'UTF-8'),
106-
htmlspecialchars($wrapperId, ENT_QUOTES, 'UTF-8'),
107-
htmlspecialchars($viewModel, ENT_QUOTES, 'UTF-8'),
108-
htmlspecialchars($parentBlock, ENT_QUOTES, 'UTF-8'),
109-
htmlspecialchars($blockAlias, ENT_QUOTES, 'UTF-8'),
110-
htmlspecialchars($isOverride, ENT_QUOTES, 'UTF-8')
111-
);
112-
113-
// Wrap content with data attributes using display:contents to avoid layout issues
100+
// Build metadata as JSON
101+
$metadata = [
102+
'id' => $wrapperId,
103+
'template' => $relativeTemplatePath,
104+
'block' => $blockClass,
105+
'module' => $moduleName,
106+
'viewModel' => $viewModel,
107+
'parent' => $parentBlock,
108+
'alias' => $blockAlias,
109+
'override' => $isOverride,
110+
];
111+
112+
// JSON encode with proper escaping for HTML comments
113+
$jsonMetadata = json_encode($metadata, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
114+
115+
// Escape any comment terminators in JSON to prevent breaking out of comment
116+
$jsonMetadata = str_replace('-->', '-->', $jsonMetadata);
117+
118+
// Wrap content with comment markers
114119
$wrappedHtml = sprintf(
115-
'<div id="%s" class="mageforge-inspectable" %s style="display:contents !important;">%s</div>',
116-
htmlspecialchars($wrapperId, ENT_QUOTES, 'UTF-8'),
117-
$dataAttributes,
118-
$html
120+
"<!-- MAGEFORGE_START %s -->\n%s\n<!-- MAGEFORGE_END %s -->",
121+
$jsonMetadata,
122+
$html,
123+
$wrapperId
119124
);
120125

121126
return $wrappedHtml;

0 commit comments

Comments
 (0)