Make WordPress Core

Changeset 45349


Ignore:
Timestamp:
05/17/2019 12:11:06 PM (5 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
branches/5.2
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.2

  • branches/5.2/src/wp-includes/formatting.php

    r45240 r45349  
    30953095        $rel       = 'rel=' . $delimiter . trim( implode( ' ', $parts ) ) . $delimiter;
    30963096        $link_html = str_replace( $rel_match[0], $rel, $link_html );
     3097    } elseif ( preg_match( '|target\s*=\s*?\\\\"|', $link_html ) ) {
     3098        $link_html .= " rel=\\\"$rel\\\"";
     3099    } elseif ( preg_match( '#(target|href)\s*=\s*?\'#', $link_html ) ) {
     3100        $link_html .= " rel='$rel'";
    30973101    } else {
    30983102        $link_html .= " rel=\"$rel\"";
  • branches/5.2/tests/phpunit/tests/formatting/WPTargetedLinkRel.php

    r44714 r45349  
    102102        $this->assertEquals( $expected, $post->post_content );
    103103    }
     104
     105    /**
     106     * Ensure JSON format is preserved when relation attribute (rel) is missing.
     107     *
     108     * @ticket 46316
     109     */
     110    public function test_wp_targeted_link_rel_should_preserve_json() {
     111        $content  = '<p>Links: <a href=\"\/\" target=\"_blank\">No rel<\/a><\/p>';
     112        $expected = '<p>Links: <a href=\"\/\" target=\"_blank\" rel=\"noopener noreferrer\">No rel<\/a><\/p>';
     113        $this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
     114    }
     115
     116    /**
     117     * Ensure correct quotes are used when relation attribute (rel) is missing.
     118     *
     119     * @ticket 47244
     120     */
     121    public function test_wp_targeted_link_rel_should_use_correct_quotes() {
     122        $content  = '<p>Links: <a href=\'\/\' target=\'_blank\'>No rel<\/a><\/p>';
     123        $expected = '<p>Links: <a href=\'\/\' target=\'_blank\' rel=\'noopener noreferrer\'>No rel<\/a><\/p>';
     124        $this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
     125
     126        $content  = '<p>Links: <a href=\'\/\' target=_blank>No rel<\/a><\/p>';
     127        $expected = '<p>Links: <a href=\'\/\' target=_blank rel=\'noopener noreferrer\'>No rel<\/a><\/p>';
     128        $this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
     129    }
    104130}
Note: See TracChangeset for help on using the changeset viewer.