WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 5 months ago

#30644 new enhancement

"wpautop" Enhancements

Reported by: stefanrz Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Formatting Keywords: wpautop
Focuses: Cc:
PR Number:

Description

Since there are several problems (e.g. invalid markup) with the current "wpautop" function I tried to come up with a new approach. The text gets parsed and a little analyzed in order to generate valid and comprehensible markup.

The script is not really compatible with the current implementation since whitespaces and line breaks are added differently. Performance is slightly worse, depending on the input (normal text vs. heavy html) of course. Comments in the code are still missing.

I think Shortcodes should also be considered in this markup generation process so the additional use of "shortcode_unautop" could be avoided.

Please let me know what you think and test it if you like.

Sample

Input

paragraph
<!-- comment -->
<hr> 
paragraph <strong>test</strong>

<i>italic</i>
normal

<!-- div -->

<div class="whatever">
  <blockquote>
    paragraph
  </blockquote>
  paragraph
</div>

paragraph
<ul>
  <li>line</li>
  <li>
    paragraph <!-- paragraph -->
    
    paragraph
  </li>
</ul>
paragraph

<pre>
  
  Honor
    this whitespace

</pre>

paragraph

<style><!--
Do not alter!
--></style>

paragraph
<p>paragraph</p>

<div>text</div>

Output

<p>
  paragraph
  <!-- comment -->
</p>

<hr>

<p>
  paragraph <strong>test</strong>
</p>

<p>
  <i>italic</i><br>
  normal
</p>

<!-- div -->

<div class="whatever">
  <blockquote>
    <p>
      paragraph
    </p>
  </blockquote>
  
  <p>
    paragraph
  </p>
</div>

<p>
  paragraph
</p>

<ul>
  <li>
    line
  </li>
  
  <li>
    <p>
      paragraph <!-- paragraph -->
    </p>
    
    <p>
      paragraph
    </p>
  </li>
</ul>

<p>
  paragraph
</p>

<pre>
  
  Honor
    this whitespace

</pre>

<p>
  paragraph
</p>

<style><!--
Do not alter!
--></style>

<p>
  paragraph
</p>

<p>paragraph</p>

<div>
  text
</div>

Attachments (3)

WP_Formatting_AutoP.php (11.6 KB) - added by stefanrz 5 years ago.
WP_Formatting_AutoP
30644.patch (941 bytes) - added by azaozz 5 years ago.
wpautop_replacement.php (11.0 KB) - added by azaozz 5 years ago.

Download all attachments as: .zip

Change History (5)

@stefanrz
5 years ago

WP_Formatting_AutoP

#1 @miqrogroove
5 years ago

  • Keywords wpautop added

@azaozz
5 years ago

#2 @azaozz
5 years ago

  • Milestone changed from Awaiting Review to Future Release

@stefanrz thanks for the idea and the patch. Works quite well as far as I see. Cleaned up the coding style to match the WordPress standards and made it into a plugin in wpautop_replacement.php. We probably will need to replace/improve some of the variables names to make them clearer. Also adding some inline comments explaining what is happening/how does it work would be very nice.

Think fixing/enhancing wpautop will be best done in a feature plugin. Also this is an excellent place to use the tests first approach. Having robust unit tests for wpautop() will make this much easier to get right and "tune up" in the future.

There are quite a few wpautop tests on #25856. We can probably start by cleaning them up and (conditionally) adding them.

Last edited 5 years ago by azaozz (previous) (diff)
Note: See TracTickets for help on using tickets.