ho-nl / magento2-Ho_Templatehints

H&O Magento 2 Advanced Template Hints module
BSD 2-Clause "Simplified" License
269 stars 68 forks source link

Problem with HTML parsing #54

Open amenk opened 3 years ago

amenk commented 3 years ago

We have the following HTML

<!--Authentication Pop-Up-->
</div></header><div x-data="initHeaderNavigation()"
 x-init="$watch('activeSubmenuId', value => activateSubmenuMobile(value))"

The module inserts the hint like this

<!--Authentication Pop-Up-->
</div></header><div x-data="initHeaderNavigation()"
 x-init="$watch('activeSubmenuId', value = data-ho-hinttype="block" ....
 @menu-back.window="back"
 class="relative z-50 font-bold bg-gray-200 lg:block lg:z-40"

Reason: We should not parse HTML with regex https://github.com/ho-nl/magento2-Ho_Templatehints/blob/master/Plugin/View/LayoutPlugin.php#L117

Related to Hyvä, because we use Alpine JS there and these notations are common

https://gitlab.hyva.io/hyva-themes/hyva-compat/magento2-ho-templatehints/-/issues/2

amenk commented 3 years ago

https://stackoverflow.com/a/3983057/288568

amenk commented 3 years ago

I tried

public function decorateOuterElement($html, $attributes)
{
    if (! $html) {
        return $html;
    }

    $dom = new \DOMDocument();
    @$dom->loadHTML($html);

    foreach($dom->childNodes as $child) {
        if ($child instanceof \DOMDocumentType) {
            continue;
        }
        foreach ($attributes as $key => $value) {
            $child->setAttribute($key, $value);
        }
    }
    return $dom->saveHtml();;
}

but this does not work, as we have Parts of HTML, not full DOMdocuments - this leads to new nodes everywhere

amenk commented 3 years ago

Regexp

'/(<\b[^><=]*)>/i'

as suggests by Vinai would probably work