WordPress.org

Make WordPress Core

Opened 3 months ago

#43394 new defect (bug)

wpautop inserts extraneous line breaks if hard return around commented content

Reported by: davidshq Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.4
Component: General 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 (0)

Note: See TracTickets for help on using tickets.