Make WordPress Core

Ticket #48261: 48261-2.diff

File 48261-2.diff, 2.5 KB (added by birgire, 4 years ago)
  • src/wp-includes/formatting.php

    diff --git src/wp-includes/formatting.php src/wp-includes/formatting.php
    index 4fb4135..47f8e5f 100644
    function wp_targeted_link_rel_callback( $matches ) { 
    31683168                preg_match( $attr_regex, $link_html, $rel_match );
    31693169        }
    31703170
    3171         if ( ! empty( $rel_match[0] ) ) {
     3171        if ( ! empty( $rel_match[0] ) && preg_match( '#(^\s*|\s+)rel\s*=#', $link_html ) ) {
    31723172                $parts     = preg_split( '|\s+|', strtolower( $rel_match[2] ) );
    31733173                $parts     = array_map( 'esc_attr', $parts );
    31743174                $needed    = explode( ' ', $rel );
  • tests/phpunit/tests/formatting/WPTargetedLinkRel.php

    diff --git tests/phpunit/tests/formatting/WPTargetedLinkRel.php tests/phpunit/tests/formatting/WPTargetedLinkRel.php
    index 8be254a..9a12df8 100644
    class Tests_Targeted_Link_Rel extends WP_UnitTestCase { 
    139139                $this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
    140140        }
    141141
     142        /**
     143         * Ensure rel parameter in href is ignored when no rel attribute.
     144         *
     145         * @ticket 48261
     146         */
     147        public function test_ignore_rel_param_in_href_when_no_rel_attribute() {
     148                $content  = '<a href="https://www.somesite.com/index.php?v=yes&rel=0" target="_blank">Anchor Text</a>';
     149                $expected = '<a href="https://www.somesite.com/index.php?v=yes&rel=0" target="_blank" rel="noopener noreferrer">Anchor Text</a>';
     150                $this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
     151        }
     152
     153        /**
     154         * Ensure rel parameter in href is ignored when rel attribute exists.
     155         *
     156         * @ticket 48261
     157         */
     158        public function test_ignore_rel_param_in_href_when_rel_attribute() {
     159                $content  = '<a href="https://www.somesite.com/index.php?v=yes&rel=0" rel="noopener noreferrer" target="_blank">Anchor Text</a>';
     160                $expected = '<a href="https://www.somesite.com/index.php?v=yes&rel=0" rel="noopener noreferrer" target="_blank">Anchor Text</a>';
     161                $this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
     162        }
     163
     164        /**
     165         * Ensure rel parameter in href is ignored when rel is the first attribute.
     166         *
     167         * @ticket 48261
     168         */
     169        public function test_ignore_rel_param_in_href_when_rel_first_attribute() {
     170                $content  = '<a rel="test" href="https://www.somesite.com/index.php?v=yes&rel=0" target="_blank">Anchor Text</a>';
     171                $expected = '<a rel="test noopener noreferrer" href="https://www.somesite.com/index.php?v=yes&rel=0" target="_blank">Anchor Text</a>';
     172                $this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
     173        }
     174
    142175}