Make WordPress Core

Changeset 45585

Timestamp:
07/02/2019 01:30:15 AM (5 years ago)
Author:
pento
Message:

Clean up stray <p> tags added by wpautop() inside block level tags.

autop() can sometimes get confused and not clean up stray <p> or </p> tags inside block level elements, which produces sub-optimal HTML. While browsers can generally handle it, there's no need to make things harder for them if we don't have to.

Props pento, ayubi, pbearne, jond, azaozz, 1994rstefan, dionysous, MikeHansenMe, jorbin, miqrogroove, niallkennedy.
Fixes #27350.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r45580 r45585  
    493493    $pee = preg_replace( '|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee );
    494494
    495     $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
     495    $allblocks        = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)\b';
     496    $allblocksexceptp = str_replace( '|p|', '|', $allblocks );
    496497
    497498    // Add a double line break above block-level opening tags.
     
    559560    $pee = preg_replace( '|<p>\s*</p>|', '', $pee );
    560561
     562
     563
     564
    561565    // Add a closing <p> inside <div>, <address>, or <form> tag if missing.
    562566    $pee = preg_replace( '!<p>([^<]+)</(div|address|form)>!', '<p>$1</p></$2>', $pee );
    563 
    564     // If an opening or closing block element tag is wrapped in a <p>, unwrap it.
    565     $pee = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', '$1', $pee );
    566567
    567568    // In some cases <li> may get wrapped in <p>, fix them.
     
    577578    // If an opening or closing block element tag is followed by a closing <p> tag, remove it.
    578579    $pee = preg_replace( '!(</?' . $allblocks . '[^>]*>)\s*</p>!', '$1', $pee );
     580
     581
     582
     583
     584
     585
    579586
    580587    // Optionally insert line breaks.
  • trunk/tests/phpunit/tests/formatting/Autop.php

    r45577 r45585  
    601601        $this->assertEquals( $expected, trim( wpautop( $content ) ) );
    602602    }
     603
     604
     605
     606
     607
     608
     609
     610
     611
     612
     613
     614
     615
     616
     617
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
     637
     638
     639
     640
     641
     642
     643
     644
     645
     646
     647
     648
     649
     650
     651
     652
     653
     654
     655
     656
     657
     658
     659
     660
     661
    603662}
Note: See TracChangeset for help on using the changeset viewer.