From e55da18ea2dfff6683ce59c70ae7f0c9cdf007f5 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 22 Nov 2018 00:24:01 +0000 Subject: [PATCH] Formatting: Revert pre-save filter adding `rel="noopener"`. Removes filters adding `rel="noopener"` to links targeting `_blank`. Previous implementation could introduce blank `rel` attributes and could corrupt JSON data when saving via the customizer. See #43187. Reverts [43732] and [43733] from the 5.0 branch. git-svn-id: https://develop.svn.wordpress.org/branches/5.0@43930 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/default-filters.php | 15 ---- src/wp-includes/formatting.php | 63 ---------------- .../tests/formatting/WPTargetedLinkRel.php | 74 ------------------- .../rest-api/rest-attachments-controller.php | 8 +- .../tests/rest-api/rest-posts-controller.php | 8 +- 5 files changed, 8 insertions(+), 160 deletions(-) delete mode 100644 tests/phpunit/tests/formatting/WPTargetedLinkRel.php diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index dfc0508f05..c1393cb066 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -118,21 +118,6 @@ foreach ( array( 'content_save_pre', 'excerpt_save_pre', 'comment_save_pre', 'pr add_filter( $filter, 'balanceTags', 50 ); } -// Add proper rel values for links with target. -foreach ( array( - 'title_save_pre', - 'content_save_pre', - 'excerpt_save_pre', - 'content_filtered_save_pre', - 'pre_comment_content', - 'pre_term_description', - 'pre_link_description', - 'pre_link_notes', - 'pre_user_description', -) as $filter ) { - add_filter( $filter, 'wp_targeted_link_rel' ); -}; - // Format strings for display. foreach ( array( 'comment_author', 'term_name', 'link_name', 'link_description', 'link_notes', 'bloginfo', 'wp_title', 'widget_title' ) as $filter ) { add_filter( $filter, 'wptexturize' ); diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 6bd58b5328..04235bbcd6 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -2773,69 +2773,6 @@ function wp_rel_nofollow_callback( $matches ) { return ""; } -/** - * Adds rel noreferrer and noopener to all HTML A elements that have a target. - * - * @param string $text Content that may contain HTML A elements. - * @return string Converted content. - */ -function wp_targeted_link_rel( $text ) { - // Don't run (more expensive) regex if no links with targets. - if ( stripos( $text, 'target' ) !== false && stripos( $text, ']*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $text ); - } - - return $text; -} - -/** - * Callback to add rel="noreferrer noopener" string to HTML A element. - * - * Will not duplicate existing noreferrer and noopener values - * to prevent from invalidating the HTML. - * - * @param array $matches Single Match - * @return string HTML A Element with rel noreferrer noopener in addition to any existing values - */ -function wp_targeted_link_rel_callback( $matches ) { - $link_html = $matches[1]; - $rel_match = array(); - - /** - * Filters the rel values that are added to links with `target` attribute. - * - * @since 5.0.0 - * - * @param string The rel values. - * @param string $link_html The matched content of the link tag including all HTML attributes. - */ - $rel = apply_filters( 'wp_targeted_link_rel', 'noopener noreferrer', $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 ); - - if ( empty( $rel_match[0] ) ) { - // No delimiters, try with a single value and spaces, because `rel = va"lue` is totally fine... - $attr_regex = '|rel\s*=(\s*)([^\s]*)|i'; - preg_match( $attr_regex, $link_html, $rel_match ); - } - - if ( ! empty( $rel_match[0] ) ) { - $parts = preg_split( '|\s+|', strtolower( $rel_match[2] ) ); - $parts = array_map( 'esc_attr', $parts ); - $needed = explode( ' ', $rel ); - $parts = array_unique( array_merge( $parts, $needed ) ); - $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 ); - } else { - $link_html .= " rel=\"$rel\""; - } - - return ""; -} - /** * Convert one smiley code to the icon graphic file equivalent. * diff --git a/tests/phpunit/tests/formatting/WPTargetedLinkRel.php b/tests/phpunit/tests/formatting/WPTargetedLinkRel.php deleted file mode 100644 index 49c843ec2c..0000000000 --- a/tests/phpunit/tests/formatting/WPTargetedLinkRel.php +++ /dev/null @@ -1,74 +0,0 @@ -Links: No rel

'; - $expected = '

Links: No rel

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } - - public function test_add_to_links_with_target_foo() { - $content = '

Links: No rel

'; - $expected = '

Links: No rel

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } - - public function test_target_as_first_attribute() { - $content = '

Links: No rel

'; - $expected = '

Links: No rel

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } - - public function test_add_to_existing_rel() { - $content = '

Links: Existing rel

'; - $expected = '

Links: Existing rel

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } - - public function test_no_duplicate_values_added() { - $content = '

Links: Existing rel

'; - $expected = '

Links: Existing rel

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } - - public function test_rel_with_single_quote_delimiter() { - $content = '

Links: Existing rel

'; - $expected = '

Links: Existing rel

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } - - public function test_rel_with_no_delimiter() { - $content = '

Links: Existing rel

'; - $expected = '

Links: Existing rel

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } - - public function test_rel_value_spaced_and_no_delimiter() { - $content = '

Links: Existing rel

'; - $expected = '

Links: Existing rel

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } - - public function test_rel_value_spaced_and_no_delimiter_and_values_to_escape() { - $content = '

Links: Existing rel

'; - $expected = '

Links: Existing rel

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } - - public function test_escaped_quotes() { - $content = '

Links: Existing rel

'; - $expected = '

Links: Existing rel

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } - - public function test_ignore_links_with_no_target() { - $content = '

Links: Change me Do not change me

'; - $expected = '

Links: Change me Do not change me

'; - $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); - } -} diff --git a/tests/phpunit/tests/rest-api/rest-attachments-controller.php b/tests/phpunit/tests/rest-api/rest-attachments-controller.php index c5f4cfa011..f003764099 100644 --- a/tests/phpunit/tests/rest-api/rest-attachments-controller.php +++ b/tests/phpunit/tests/rest-api/rest-attachments-controller.php @@ -941,12 +941,12 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control 'rendered' => 'link', ), 'description' => array( - 'raw' => 'link', - 'rendered' => '

link

', + 'raw' => 'link', + 'rendered' => '

link

', ), 'caption' => array( - 'raw' => 'link', - 'rendered' => '

link

', + 'raw' => 'link', + 'rendered' => '

link

', ), ) ), diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index ff97d2648d..27a1fa354b 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -2983,12 +2983,12 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 'rendered' => 'link', ), 'content' => array( - 'raw' => 'link', - 'rendered' => '

link

', + 'raw' => 'link', + 'rendered' => '

link

', ), 'excerpt' => array( - 'raw' => 'link', - 'rendered' => '

link

', + 'raw' => 'link', + 'rendered' => '

link

', ), ) ),