mmikkel / Retcon-Craft

A collection of powerful Twig filters for modifying HTML
MIT License
79 stars 9 forks source link

Latest update has broken my use case #36

Closed RyanRoberts closed 2 years ago

RyanRoberts commented 2 years ago

I'm using Retcon (thanks!) to inject some links and attributes into an SVG country map, the attribute let me display a styled HTML tooltip. Since the 2.4.0 update my raw html used by Alpine.js and Tippy is being escaped which is breaking the script and preventing the tooltips showing.

You can see an example of it here if you scroll down to the map https://dmbins.com/ride-guide/#guide-map (you might need to switch to the map). It works there because I've not pushed the plugin update live yet.

Here's a wee example of the Retcon code

{% set map = map|retconAttr(
  "a#link-" ~ regionId, {
    "href" : linkUrl,
    "x-data": "{ tooltip: '<p class=ttsub>Destination</p><p class=ttheading> #{ link.regionLink.one().title }</p>' }",
  },
) %}

Everything after "tooltip:" gets escaped

x-data="{ tooltip: &#039;&lt;p class=ttsub&gt;Destination&lt;/p&gt;&lt;p class=ttheading&gt; Aberdeenshire&lt;/p&gt;&#039; }"

I've tried filtering it with |raw but that doesn't help. Is there a solution?

Thanks

mmikkel commented 2 years ago

Hi @RyanRoberts – sorry about that.

Along with support for non-primitives I added HTML encoding to the retconAttr filter in 2.4.0 – to make it a bit more safe + bring it more in-line with how the native attr filter behaves. However, this was a breaking change and in hindsight shouldn't have been made in a minor release – so I've just tagged version 2.4.1 which reverts this particular change. Hopefully this release solves your issue!

RyanRoberts commented 2 years ago

@mmikkel Cool thanks, the native attr filter doesn't appear to escape the same html or will that be the case in Craft 4? 🤔

mmikkel commented 2 years ago

the native attr filter doesn't appear to escape the same html or will that be the case in Craft 4?

@RyanRoberts Nothing's changed with the attr filter in Craft 4, but it will definitely encode HTML inside attribute values, exactly like Retcon 2.4.0 did it as per your example above. It'll only happen for the tag that the attr filter actually only operates on, though (which is the first tag it encounters).

mmikkel commented 2 years ago

Here's the method that the attr filter uses to actually render the attributes (highlighted the line that would cause the encoding in your case) – it's actually a Yii class.