Skip to content
Permalink
Browse files

Formatting: Adjust `wp_targeted_link_rel()` to ensure JSON format is …

…preserved and correct quotes are used when adding the missing `rel` attribute.

Props birgire, TobiasBg, fierevere, audrasjb, SergeyBiryukov.
Merges [45348] to the 5.2 branch.
Fixes #46316, #47244.

git-svn-id: https://develop.svn.wordpress.org/branches/5.2@45349 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
SergeyBiryukov committed May 17, 2019
1 parent 14c25eb commit c70201ac74bd07be3eeb072ace44c880a08ae18a
Showing with 30 additions and 0 deletions.
  1. +4 −0 src/wp-includes/formatting.php
  2. +26 −0 tests/phpunit/tests/formatting/WPTargetedLinkRel.php
@@ -3094,6 +3094,10 @@ function wp_targeted_link_rel_callback( $matches ) {
$delimiter = trim( $rel_match[1] ) ? $rel_match[1] : '"';
$rel = 'rel=' . $delimiter . trim( implode( ' ', $parts ) ) . $delimiter;
$link_html = str_replace( $rel_match[0], $rel, $link_html );
} elseif ( preg_match( '|target\s*=\s*?\\\\"|', $link_html ) ) {
$link_html .= " rel=\\\"$rel\\\"";
} elseif ( preg_match( '#(target|href)\s*=\s*?\'#', $link_html ) ) {
$link_html .= " rel='$rel'";
} else {
$link_html .= " rel=\"$rel\"";
}
@@ -101,4 +101,30 @@ public function test_wp_targeted_link_rel_filters_run() {
$this->assertEquals( $expected, $post->post_content );
}
/**
* Ensure JSON format is preserved when relation attribute (rel) is missing.
*
* @ticket 46316
*/
public function test_wp_targeted_link_rel_should_preserve_json() {
$content = '<p>Links: <a href=\"\/\" target=\"_blank\">No rel<\/a><\/p>';
$expected = '<p>Links: <a href=\"\/\" target=\"_blank\" rel=\"noopener noreferrer\">No rel<\/a><\/p>';
$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
}
/**
* Ensure correct quotes are used when relation attribute (rel) is missing.
*
* @ticket 47244
*/
public function test_wp_targeted_link_rel_should_use_correct_quotes() {
$content = '<p>Links: <a href=\'\/\' target=\'_blank\'>No rel<\/a><\/p>';
$expected = '<p>Links: <a href=\'\/\' target=\'_blank\' rel=\'noopener noreferrer\'>No rel<\/a><\/p>';
$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
$content = '<p>Links: <a href=\'\/\' target=_blank>No rel<\/a><\/p>';
$expected = '<p>Links: <a href=\'\/\' target=_blank rel=\'noopener noreferrer\'>No rel<\/a><\/p>';
$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
}
}

0 comments on commit c70201a

Please sign in to comment.
You can’t perform that action at this time.