WordPress.org

Make WordPress Core

Ticket #38656: 38656.diff

File 38656.diff, 3.8 KB (added by pento, 5 years ago)
  • src/wp-includes/formatting.php

     
    466466        // Change multiple <br>s into two line breaks, which will turn into paragraphs.
    467467        $pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee);
    468468
    469         $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)';
     469        // Block level elements that can contain a <p>.
     470        $peeblocks = 'caption|td|th|div|dd|dt|li|form|map|blockquote|address|fieldset|section|article|aside|header|footer|nav|figure|figcaption|details|menu';
     471
     472        // Block level elements that cannot contain a <p>.
     473        $peefreeblocks = 'table|thead|tfoot|col|colgroup|tbody|tr|dl|ul|ol|pre|area|math|style|p|h[1-6]|hr|legend|hgroup|summary';
     474
     475        $allblocks     = "(?:$peeblocks|$peefreeblocks)";
     476        $peeblocks     = "(?:$peeblocks)";
     477        $peefreeblocks = "(?:$peefreeblocks)";
     478
     479        // Preserve newlines within pee-free blocks.
     480        $pee = preg_replace_callback('/<(' . $peefreeblocks . ')( [^>]*)?>.*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee);
     481
     482        // ... but not the ones inside pee blocks inside pee free blocks.
     483        $pee = preg_replace( '%(<(' . $peeblocks . ")(?: [^>]*)?>)>(.*?)<WPPreserveNewline />(.*?)(</\\2>)%s", "$1$3\n$4$5", $pee );
    470484
    471485        // Add a double line break above block-level opening tags.
    472486        $pee = preg_replace('!(<' . $allblocks . '[\s/>])!', "\n\n$1", $pee);
     
    474488        // Add a double line break below block-level closing tags.
    475489        $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
    476490
     491        // Add a double line break below block-level opening tags that are allowed to contain a <p>.
     492        $pee = preg_replace('%(<' . $peeblocks . '(?: [^>]*)?>)%', "$1\n\n", $pee);
     493
     494        // Add a double line break above block-level closing tags that are allowed to contain a <p>.
     495        $pee = preg_replace('%(</' . $peeblocks . '>)%', "\n\n$1", $pee);
     496
    477497        // Standardize newline characters to "\n".
    478498        $pee = str_replace(array("\r\n", "\r"), "\n", $pee);
    479499
     
    506526                $pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee );
    507527        }
    508528
     529        // If there's only one paragraph inside a pee block, remove the newlines.
     530        $pee = preg_replace( '%(<(' . $peeblocks . ")(?: [^>]*)?>)\n\n((?(?!\n\n).)*)\n\n(</\\2>)%s", '$1$3$4', $pee );
     531
    509532        // Remove more than two contiguous line breaks.
    510533        $pee = preg_replace("/\n\n+/", "\n\n", $pee);
    511534
     
    520543                $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n";
    521544        }
    522545
     546        // Replace newline placeholders with newlines.
     547        $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
     548
    523549        // Under certain strange conditions it could create a P of entirely whitespace.
    524550        $pee = preg_replace('|<p>\s*</p>|', '', $pee);
    525551
  • tests/phpunit/tests/formatting/Autop.php

     
    534534
    535535                $this->assertEquals( $expected, trim( wpautop( $content ) ) );
    536536        }
     537
     538        /**
     539         * @ticket 38656
     540         */
     541        function test_that_paragraphs_inside_blocks_without_starting_newlines_are_peed() {
     542                $content = "<div>a\n\nb</div>";
     543                $expected = "<div>\n<p>a</p>\n<p>b</p>\n</div>";
     544
     545                $this->assertEquals( $expected, trim( wpautop( $content ) ) );
     546        }
     547
     548        /**
     549         * @ticket 38656
     550         */
     551        function test_that_paragraphs_inside_non_pee_blocks_without_starting_newlines_are_not_peed() {
     552                $content = "<h1>a\n\nb</h1>";
     553                $expected = "<h1>a<br />\nb</h1>";
     554
     555                $this->assertEquals( $expected, trim( wpautop( $content ) ) );
     556        }
    537557}