Make WordPress Core

Changeset 33624


Ignore:
Timestamp:
08/17/2015 05:35:58 PM (10 years ago)
Author:
azaozz
Message:

Fix creating of extra <br /> tags in both PHP and JS variants of wpautop(). Add PHP tests to catch similar problems in the future.
Props valendesigns, azaozz. Fixes #33377.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/editor.js

    r32899 r33624  
    273273            text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' );
    274274            text = text.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' );
    275             text = text.replace( /\s*\n/gi, '<br />\n');
     275
     276            // Remove redundant spaces and line breaks after existing <br /> tags
     277            text = text.replace( /(<br[^>]*>)\s*\n/gi, '$1' );
     278
     279            // Create <br /> from the remaining line breaks
     280            text = text.replace( /\s*\n/g, '<br />\n');
     281
    276282            text = text.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' );
    277283            text = text.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' );
  • trunk/src/wp-includes/formatting.php

    r33517 r33624  
    492492    }
    493493    // Change multiple <br>s into two line breaks, which will turn into paragraphs.
    494     $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
     494    $pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee);
    495495
    496496    $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)';
     
    574574        // Replace newlines that shouldn't be touched with a placeholder.
    575575        $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
     576
     577        // Normalize <br>
     578        $pee = str_replace( array( '<br>', '<br/>' ), '<br />', $pee );
    576579
    577580        // Replace any new line characters that aren't preceded by a <br /> with a <br />.
  • trunk/tests/phpunit/tests/formatting/Autop.php

    r33469 r33624  
    445445        );
    446446    }
    447    
     447
     448    /**
     449     * wpautop() should not convert line breaks after <br /> tags
     450     *
     451     * @ticket 33377
     452     */
     453    function test_that_wpautop_skips_line_breaks_after_br() {
     454        $content = '
     455line 1<br>
     456line 2<br/>
     457line 3<br />
     458line 4
     459line 5
     460';
     461
     462        $expected = '<p>line 1<br />
     463line 2<br />
     464line 3<br />
     465line 4<br />
     466line 5</p>';
     467
     468        $this->assertEquals( $expected, trim( wpautop( $content ) ) );
     469    }
     470
     471    /**
     472     * wpautop() should convert multiple line breaks into a paragraph regarless of <br /> format
     473     *
     474     * @ticket 33377
     475     */
     476    function test_that_wpautop_adds_a_paragraph_after_multiple_br() {
     477        $content = '
     478line 1<br>
     479<br/>
     480line 2<br/>
     481<br />
     482';
     483
     484        $expected = '<p>line 1</p>
     485<p>line 2</p>';
     486
     487        $this->assertEquals( $expected, trim( wpautop( $content ) ) );
     488    }
     489
    448490}
Note: See TracChangeset for help on using the changeset viewer.