Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#51313 closed defect (bug) (fixed)

Incorrect regular expression in links_add_target function

Reported by: antonlukin's profile antonlukin Owned by: sergeybiryukov's profile 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 4 years ago.
Patch to links_add_target function

Download all attachments as: .zip

Change History (7)

#1 @SergeyBiryukov
4 years ago

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

@antonlukin
4 years ago

Patch to links_add_target function

#2 @antonlukin
4 years ago

  • Keywords has-patch added; needs-patch removed

#3 @antonlukin
4 years ago

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

#4 @SergeyBiryukov
4 years 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
4 years 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.