Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with HTML parsing #54

Open
amenk opened this issue May 25, 2021 · 3 comments
Open

Problem with HTML parsing #54

amenk opened this issue May 25, 2021 · 3 comments

Comments

@amenk
Copy link
Contributor

amenk commented May 25, 2021

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
Copy link
Contributor Author

amenk commented May 25, 2021

@amenk
Copy link
Contributor Author

amenk commented May 25, 2021

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
Copy link
Contributor Author

amenk commented May 25, 2021

Regexp

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

as suggests by Vinai would probably work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant