Open romainpoirier opened 1 week ago
Retcon uses Symfony's CssSelector component under the hood, and I'm not sure if that supports the :empty
selector (guessing not).
There's a dedicated retconRemoveEmpty
filter for removing empty nodes, though. The default behavior for that is to retain any <br>
tags; you might want to try that and see if it works better for your use case:
{{ entry.text | striptags('<br>') | retcon([
['removeEmpty']
]) | typogrify }}
or
{{ entry.text | striptags('<br>') | retconRemoveEmpty | typogrify }}
...if you ever want to remove <br>
tags in addition to empty nodes, set the removeBr
param to true
, e.g.
{{ entry.text | striptags('<br>') | retconRemoveEmpty(removeBr=true) | typogrify }}
Thank you for your response, but the | retconRemoveEmpty
filter is not functioning as intended.
The following input text in CKEditor:
lorem
ipsum
Results in the following HTML output:
<p>
lorem
</p>
<p>
</p>
<p>
</p>
<p>
ipsum
</p>
When using the following Twig code:
{{ entry.text | retconRemoveEmpty }}
The rendered front-end output is:
<p>lorem</p><p> </p><p> </p><p>ipsum</p>
The | retconRemoveEmpty
filter should remove the empty paragraphs, but it seems to retain the line breaks (
) as seen in the HTML output.
Could you provide a solution or clarify if this behavior is expected?
That's interesting; I was not aware of that behaviour. It seems specific to CK Editor output, as I'm not able to reproduce it with Redactor, or with raw HTML (i.e. something like '<p> </p>'|retconRemoveEmpty
).
I'll investigate; in the meantime if you need a quick fix you could do
{{ entry.text | retconRemoveEmpty | replace('<p> </p>', '') | raw }}
Description:
I'm using Retcon to modify my content, specifically to remove unnecessary tags. However, it seems that the CSS
:empty
selector is also targeting self-closing<br>
elements when it shouldn't:In this case, the
<br>
elements are being removed along with genuinely empty elements, which is unexpected behavior.Is this a bug? If not, how can I remove truly empty elements (e.g.,
<p></p>
) while keeping<br>
elements intact?Versions used:
"craftcms/cms": "5.4.3"
"mmikkel/retcon": "3.2.0"
Thank you!