Make WordPress Core

Changeset 48983


Ignore:
Timestamp:
09/17/2020 10:12:32 AM (4 years ago)
Author:
SergeyBiryukov
Message:

Formatting: Make sure links_add_target() adds the target attribute to the correct tag.

Previously, the attribute could unintentionally be added to a tag that starts with the same characters, e.g. <aside> instead of <a>.

Props antonlukin.
Fixes #51313.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r48817 r48983  
    52445244    $_links_add_target = $target;
    52455245    $tags              = implode( '|', (array) $tags );
    5246     return preg_replace_callback( "!<($tags)([^>]*)>!i", '_links_add_target', $content );
     5246    return preg_replace_callback( "!<($tags)((\s[^>]*)?)>!i", '_links_add_target', $content );
    52475247}
    52485248
  • trunk/tests/phpunit/tests/formatting/LinksAddTarget.php

    r48937 r48983  
    6565                'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span src="ABC" target="_top">END TEXT</span>',
    6666            ),
     67            array(
     68                'MY CONTENT <aside> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </aside> END TEXT',
     69                null,
     70                null,
     71                'MY CONTENT <aside> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC" target="_blank">LINK</a> HERE </aside> END TEXT',
     72            ),
     73            array(
     74                'MY CONTENT <aside class="_blank"> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </aside> END TEXT',
     75                null,
     76                null,
     77                'MY CONTENT <aside class="_blank"> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC" target="_blank">LINK</a> HERE </aside> END TEXT',
     78            ),
     79            array(
     80                'MY CONTENT <blockquote>SOME</blockquote> ADDITIONAL TEXT <b>LINK</b> HERE END TEXT',
     81                '_blank',
     82                array( 'b' ),
     83                'MY CONTENT <blockquote>SOME</blockquote> ADDITIONAL TEXT <b target="_blank">LINK</b> HERE END TEXT',
     84            ),
     85            array(
     86                'MY CONTENT <blockquote target="_self">SOME</blockquote> ADDITIONAL TEXT <b>LINK</b> HERE END TEXT',
     87                '_blank',
     88                array( 'b' ),
     89                'MY CONTENT <blockquote target="_self">SOME</blockquote> ADDITIONAL TEXT <b target="_blank">LINK</b> HERE END TEXT',
     90            ),
    6791        );
    6892    }
Note: See TracChangeset for help on using the changeset viewer.