From d5e5ca8e93e33cdb11845cc7a5e4ebd2b43fb8a1 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 18 Sep 2015 04:35:37 +0000 Subject: [PATCH] Comments: in `wp_rel_nofollow_callback()`, account for the fact that a link might already have a `rel` attribute. Currently, if a link already has a `rel`, it will result it duplicate attributes on the element with conflicting values. Adds unit tests. Props junsuijin, wonderboymusic. Fixes #9959. git-svn-id: https://develop.svn.wordpress.org/trunk@34277 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/formatting.php | 19 +++++++++++++++++-- .../tests/formatting/WPRelNoFollow.php | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/formatting/WPRelNoFollow.php diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 690b95a84d..240a21a928 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -2259,8 +2259,23 @@ function wp_rel_nofollow( $text ) { */ function wp_rel_nofollow_callback( $matches ) { $text = $matches[1]; - $text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text); - return ""; + $atts = shortcode_parse_atts( $matches[1] ); + $rel = 'nofollow'; + if ( ! empty( $atts['rel'] ) ) { + $parts = array_map( 'trim', explode( ' ', $atts['rel'] ) ); + if ( false === array_search( 'nofollow', $parts ) ) { + $parts[] = 'nofollow'; + } + $rel = implode( ' ', $parts ); + unset( $atts['rel'] ); + + $html = ''; + foreach ( $atts as $name => $value ) { + $html .= "{$name}=\"$value\" "; + } + $text = trim( $html ); + } + return ""; } /** diff --git a/tests/phpunit/tests/formatting/WPRelNoFollow.php b/tests/phpunit/tests/formatting/WPRelNoFollow.php new file mode 100644 index 0000000000..8d6a514e8e --- /dev/null +++ b/tests/phpunit/tests/formatting/WPRelNoFollow.php @@ -0,0 +1,18 @@ +This is some cool Code

'; + $expected = '

This is some cool Code

'; + $this->assertEquals( $expected, wp_rel_nofollow( $content ) ); + } + + public function test_convert_no_follow() { + $content = '

This is some cool Code

'; + $expected = '

This is some cool Code

'; + $this->assertEquals( $expected, wp_rel_nofollow( $content ) ); + } +} \ No newline at end of file