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

:empty selector in Retcon removes <br> elements incorrectly** #70

Closed
romainpoirier opened this issue Sep 12, 2024 · 4 comments
Closed

Comments

@romainpoirier
Copy link

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:

{{ entry.text | striptags('<br>') | retcon([
    ['remove', ':empty']
]) | typogrify }}

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!

@mmikkel
Copy link
Owner

mmikkel commented Sep 12, 2024

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 }}

@romainpoirier
Copy link
Author

Thank you for your response, but the | retconRemoveEmpty filter is not functioning as intended.

Steps to reproduce:

The following input text in CKEditor:

lorem





ipsum

Results in the following HTML output:

<p>
    lorem
</p>
<p>
    &nbsp;
</p>
<p>
    &nbsp;
</p>
<p>
    ipsum
</p>

When using the following Twig code:

{{ entry.text | retconRemoveEmpty }}

The rendered front-end output is:

<p>lorem</p><p>&nbsp;</p><p>&nbsp;</p><p>ipsum</p>

Expected behavior:

The | retconRemoveEmpty filter should remove the empty paragraphs, but it seems to retain the line breaks (&nbsp;) as seen in the HTML output.

Could you provide a solution or clarify if this behavior is expected?

@mmikkel
Copy link
Owner

mmikkel commented Sep 12, 2024

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>&nbsp;</p>', '') | raw }}

mmikkel added a commit that referenced this issue Oct 21, 2024
@mmikkel
Copy link
Owner

mmikkel commented Oct 21, 2024

@romainpoirier Retcon v. 2.8.2 and 3.2.1 is out with a fix for this now, which should make the retconRemoveEmpty filter behave as expected with CKEditor output.

Btw, I tested the :empty selector with Retcon, and it actually appears to work – but I definitely wouldn't recommend using it for removing empty elements. For one, it matches self-closing tags, i.e. your original code would remove all img tags, for example.

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

2 participants