Make WordPress Core

Opened 7 years ago

Last modified 4 years ago

#43394 new defect (bug)

wpautop inserts extraneous line breaks if hard return around commented content

Reported by: davidshq's profile davidshq Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.4
Component: Formatting Keywords:
Focuses: Cc:

Description

Problem: If one has code comments before content in a WordPress post/page and there are hard returns before and after the code comment, WordPress will insert an extraneous line break. Further, if there are hard returns between multiple comments an extraneous line break will be added.

This problem can be reproduced by doing the following:

  1. Edit a WordPress post or page.
  2. Add a code comment with hard returns before and after it such as:
<!-- My Code Comment-->

  1. Publish/preview the page.
  2. Use Inspect to examine the area where you placed the above comment and you'll notice an extra line break.

This may seem like a very edge case and a minor annoyance, but we ran into this issue recently when we had multiple code comments following each other like so:

<p>Some regular content here.</p>
<!-- This is Important -->
<!-- Don't Forget This -->
<!-- Why are you Forgetting? -->
<!-- Adding individual comments for each line is -->
<!-- fun! -->
<p>Some regular content here.</p>

An extraneous line break now becomes four line breaks.

This becomes quite confusing for editors as when they look at the post the extra returns are not apparent yet inserted when the page is actually posted.

I'm thinking that resolving this issue might also resolve many other cases of wpautop gone rogue.

My suggestion would be that wpautop be changed so that:

  1. If there is a code comment with a hard return before and after the comment, it does not automatically add wpautop. (one can argue that this is behaving as expected, but I doubt many are thinking when they add a code comment that it will add a nice line break for them...and in any case, this can be much more obviously/easily remedied by inserting a <br> in the editor if one truly did want a line break caused in this manner, whereas it can be quite time consuming to discover why extra line breaks are mysteriously appearing)
  2. Similarly, with shortcodes, if the shortcode is tested valid then don't add line breaks with wpautop around the shortcode.

Change History (1)

#1 @sabernhardt
4 years ago

  • Component changed from General to Formatting

@davidshq Hi and thanks for the report!

In addition to the line break tags, the comments in your example are wrapped in a paragraph for me:

<p>Some regular content here.</p>
<p><!-- This is Important --><br />
<!-- Don't Forget This --><br />
<!-- Why are you Forgetting? --><br />
<!-- Adding individual comments for each line is --><br />
<!-- fun! --></p>
<p>Some regular content here.</p>

If no one uses the Visual editor for your site, you might want to try combining the comments into one and indenting each line of text within it (for now). Switching to Visual, though, would edit the comment to one line. The comment still wraps in the paragraph tag, so this option would only remove the line breaks.

<p>Some regular content here.</p>
<!-- 
     This is Important 
     Don't Forget This 
     Why are you Forgetting? 
     Adding individual comments for each line is 
     fun!
-->
<p>Some regular content here.</p>
Note: See TracTickets for help on using tickets.