WordPress.org

Make WordPress Core

Opened 3 days ago

Closed 29 hours ago

#51313 closed defect (bug) (fixed)

Incorrect regular expression in links_add_target function

Reported by: antonlukin Owned by: SergeyBiryukov
Milestone: 5.6 Priority: normal
Severity: normal Version: 5.5.1
Component: Formatting Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

links_add_target function relies on a regular expression "!<($tags)([^>]*)>!i". It ignores cases where a tag is part of another tag, for example aside.

$content = 'test <aside>test</aside> test';
$content = links_add_target( $content, '_blank', array( 'a' ) );

This code return test <aside target="_blank">test</aside> test but it shouldn't.

Possible solution is to replace regexp with !<($tags)(\s[^>]*)?>!i

https://developer.wordpress.org/reference/functions/links_add_target/

Attachments (1)

formatting.diff (522 bytes) - added by antonlukin 3 days ago.
Patch to links_add_target function

Download all attachments as: .zip

Change History (6)

#1 @SergeyBiryukov
3 days ago

  • Keywords needs-patch needs-unit-tests added; has-patch removed

@antonlukin
3 days ago

Patch to links_add_target function

#2 @antonlukin
3 days ago

  • Keywords has-patch added; needs-patch removed

#3 @antonlukin
3 days ago

My patch contains a bug. I will fix it shortly and send pull request to GitHub mirror.

#4 @SergeyBiryukov
32 hours ago

  • Keywords has-unit-tests added; needs-unit-tests removed
  • Milestone changed from Awaiting Review to 5.6
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#5 @SergeyBiryukov
29 hours ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 48983:

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.

Note: See TracTickets for help on using tickets.