Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ function _wptexturize_pushpop_element( $text, &$stack, $disabled_elements ) {
* @return string Text which has been converted into correct paragraph tags.
*/
function wpautop( $text, $br = true ) {
$pre_tags = array();
$pre_tags = array();
$has_block_anchor = false;
$block_anchor_placeholder = '';

if ( '' === trim( $text ) ) {
return '';
Expand Down Expand Up @@ -486,6 +488,29 @@ function wpautop( $text, $br = true ) {

$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';

/*
* Anchors can contain block-level elements. Replace their tags with temporary
* block-level placeholders so the paragraph cleanup does not split the anchor.
*/
if ( str_contains( $text, '<a' ) ) {
$block_anchor_placeholder = 'wp-autop-block-anchor';

while ( str_contains( $text, '<' . $block_anchor_placeholder ) ) {
$block_anchor_placeholder .= '-';
}

$text = preg_replace(
'~<a(\b[^>]*)>(?=\s*<' . $allblocks . '[\s/>])(.*?)(</a>)~s',
'<' . $block_anchor_placeholder . '$1>$2</' . $block_anchor_placeholder . '>',
$text
);
$has_block_anchor = str_contains( $text, '<' . $block_anchor_placeholder );

if ( $has_block_anchor ) {
$allblocks = substr( $allblocks, 0, -1 ) . '|' . $block_anchor_placeholder . ')';
}
}

// Add a double line break above block-level opening tags.
$text = preg_replace( '!(<' . $allblocks . '[\s/>])!', "\n\n$1", $text );

Expand Down Expand Up @@ -563,6 +588,13 @@ function wpautop( $text, $br = true ) {
$text = preg_replace( '|<p><blockquote([^>]*)>|i', '<blockquote$1><p>', $text );
$text = str_replace( '</blockquote></p>', '</p></blockquote>', $text );

// Move paragraphs inside anchors that contain block-level elements.
if ( $has_block_anchor ) {
$text = preg_replace( '|<p>(<' . $block_anchor_placeholder . '[^>]*>)|', '$1<p>', $text );
$text = preg_replace( '|(</' . $block_anchor_placeholder . '>)</p>|', '</p>$1', $text );
$text = preg_replace( '|<p>\s*</p>|', '', $text );
}

// If an opening or closing block element tag is preceded by an opening <p> tag, remove it.
$text = preg_replace( '!<p>\s*(</?' . $allblocks . '[^>]*>)!', '$1', $text );

Expand Down Expand Up @@ -601,6 +633,11 @@ function wpautop( $text, $br = true ) {
$text = str_replace( array( ' <!-- wpnl --> ', '<!-- wpnl -->' ), "\n", $text );
}

// Restore anchors that contain block-level elements.
if ( $has_block_anchor ) {
$text = str_replace( array( '<' . $block_anchor_placeholder, '</' . $block_anchor_placeholder . '>' ), array( '<a', '</a>' ), $text );
}

return $text;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/phpunit/tests/formatting/wpAutop.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,21 @@ public function test_that_wpautop_treats_inline_elements_as_inline() {
$this->assertSame( $expected, trim( wpautop( $content ) ) );
}

/**
* @ticket 40202
*/
public function test_that_wpautop_does_not_split_anchors_containing_block_elements() {
$content = '<a href="document.htm"><div>Text</div></a>';
$expected = "<a href=\"document.htm\">\n<div>Text</div>\n</a>";

$this->assertSame( $expected, trim( wpautop( $content ) ) );

$content = '<a href="document.htm"><div>First</div><section>Second</section></a>';
$expected = "<a href=\"document.htm\">\n<div>First</div>\n<section>Second</section>\n</a>";

$this->assertSame( $expected, trim( wpautop( $content ) ) );
}

/**
* Do not allow newlines within HTML elements to become mangled.
*
Expand Down
Loading