Skip to content

Commit 8fda746

Browse files
committed
Fix bug when component has empty output like <div></div>
1 parent 6af68fe commit 8fda746

1 file changed

Lines changed: 5 additions & 6 deletions

File tree

Observer/AddHtmlAttributesToComponentBlock.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function execute(Observer $observer)
6060
return;
6161
}
6262

63-
$html = preg_replace('/^<([a-z]{2,})([^>]+)>/', '<\1 ' . $htmlAttributes . '>', $html);
63+
$html = preg_replace('/^<([a-z]{2,})([^>]{0,})>/', '<\1 ' . $htmlAttributes . '>', $html);
6464

6565
$initialDataElement = $this->getInitialDataElement($component);
6666
$html = preg_replace('/^<([^>]+)>/', '<\1>[X-LOKI-INIT-BLOCK]', $html);
@@ -81,12 +81,10 @@ private function getHtmlAttributes(ComponentInterface $component, string $curren
8181
{
8282
$block = $component->getBlock();
8383

84-
if (!preg_match('/^<([a-z]{2,})([^>]+)>/', $currentHtml, $match)) {
84+
if (!preg_match('/^<([a-z]{2,})([^>]{0,})/i', $currentHtml, $match)) {
8585
return $currentHtml;
8686
}
8787

88-
$firstTagHtml = $match[0];
89-
9088
$attributes = (array)$block->getData('html_attributes');
9189
$attributes['id'] = $this->getElementId($block);
9290
$attributes['x-data'] = $this->jsDataProvider->getComponentName($component);
@@ -96,6 +94,7 @@ private function getHtmlAttributes(ComponentInterface $component, string $curren
9694
}
9795

9896
$htmlAttributes = [];
97+
$firstTagHtml = $match[0];
9998
foreach ($attributes as $attributeName => $attributeValue) {
10099
if (str_contains($firstTagHtml, ' ' . $attributeName . '="')) {
101100
continue;
@@ -107,7 +106,7 @@ private function getHtmlAttributes(ComponentInterface $component, string $curren
107106
$allAttributes = $match[2] .' '. implode(' ', $htmlAttributes);
108107
$allAttributes = preg_replace('/@([a-z.\-]+)=/', ' x-on:\1=', $allAttributes);
109108

110-
return $allAttributes;
109+
return trim($allAttributes);
111110
}
112111

113112
private function getJsData(ComponentInterface $component): string
@@ -129,6 +128,6 @@ private function getElementId(AbstractBlock $block): string
129128
}
130129

131130
$nameInLayout = strtolower((string)$block->getNameInLayout());
132-
return preg_replace('#([^a-zA-Z0-9]{1})#', '-', $nameInLayout);
131+
return preg_replace('#([^a-zA-Z0-9]+)#', '-', $nameInLayout);
133132
}
134133
}

0 commit comments

Comments
 (0)