Formatting: Prevent `wp_targeted_link_rel()` adding an empty attribute.

Bypass adding a `rel` attribute when the `wp_targeted_link_rel` filter returns an empty string or other falsy result.

Props mcmwebsol, spartank, meatman89fs.
Fixes #45352.


git-svn-id: https://develop.svn.wordpress.org/trunk@44691 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2019-01-22 21:54:26 +00:00
parent fbe0605156
commit bb463b5265
2 changed files with 17 additions and 0 deletions

View File

@ -3065,6 +3065,11 @@ function wp_targeted_link_rel_callback( $matches ) {
*/
$rel = apply_filters( 'wp_targeted_link_rel', 'noopener noreferrer', $link_html );
// Avoid additional regex if the filter removes rel values.
if ( ! $rel ) {
return "<a $link_html>";
}
// Value with delimiters, spaces around are optional.
$attr_regex = '|rel\s*=\s*?(\\\\{0,1}["\'])(.*?)\\1|i';
preg_match( $attr_regex, $link_html, $rel_match );

View File

@ -71,4 +71,16 @@ class Tests_Targeted_Link_Rel extends WP_UnitTestCase {
$expected = '<p>Links: <a href="/" target="_blank" rel="noopener noreferrer">Change me</a> <a href="/">Do not change me</a></p>';
$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
}
/**
* Ensure empty rel attributes are not added.
*
* @ticket 45352.
*/
public function test_ignore_if_wp_targeted_link_rel_nulled() {
add_filter( 'wp_targeted_link_rel', '__return_empty_string' );
$content = '<p>Links: <a href="/" target="_blank">Do not change me</a></p>';
$expected = '<p>Links: <a href="/" target="_blank">Do not change me</a></p>';
$this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
}
}