mirror of
git://develop.git.wordpress.org/
synced 2025-01-19 05:38:07 +01:00
Add unit tests for links_add_target()
. Alter the patterns in links_add_target()
and _links_add_target()
slightly based on learnings after running tests.
Props mdbitz. Fixes #26164. git-svn-id: https://develop.svn.wordpress.org/trunk@26328 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
b209e63234
commit
1dc930ba13
@ -3243,7 +3243,7 @@ function links_add_target( $content, $target = '_blank', $tags = array('a') ) {
|
||||
global $_links_add_target;
|
||||
$_links_add_target = $target;
|
||||
$tags = implode('|', (array)$tags);
|
||||
return preg_replace_callback( "!<($tags)(.+?)>!i", '_links_add_target', $content );
|
||||
return preg_replace_callback( "!<($tags)([^>]*)>!i", '_links_add_target', $content );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -3258,7 +3258,7 @@ function links_add_target( $content, $target = '_blank', $tags = array('a') ) {
|
||||
function _links_add_target( $m ) {
|
||||
global $_links_add_target;
|
||||
$tag = $m[1];
|
||||
$link = preg_replace('|(target=([\'"])(.*?)\2)|i', '', $m[2]);
|
||||
$link = preg_replace('|( target=([\'"])(.*?)\2)|i', '', $m[2]);
|
||||
return '<' . $tag . $link . ' target="' . esc_attr( $_links_add_target ) . '">';
|
||||
}
|
||||
|
||||
|
84
tests/phpunit/tests/formatting/LinksAddTarget.php
Normal file
84
tests/phpunit/tests/formatting/LinksAddTarget.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* @group formatting
|
||||
*/
|
||||
class Tests_Formatting_LinksAddTarget extends WP_UnitTestCase {
|
||||
/**
|
||||
* Test Content DataProvider
|
||||
*
|
||||
* array ( input_txt, converted_output_txt)
|
||||
*/
|
||||
public function get_input_output() {
|
||||
return array (
|
||||
array (
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> END TEXT',
|
||||
null,
|
||||
null,
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC" target="_blank">LINK</a> HERE </div> END TEXT'
|
||||
),
|
||||
array (
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <A href="XYZ" src="ABC">LINK</A> HERE </div> END TEXT',
|
||||
null,
|
||||
null,
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <A href="XYZ" src="ABC" target="_blank">LINK</A> HERE </div> END TEXT'
|
||||
),
|
||||
array (
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <a href="XYZ" >LINK</a>END TEXT',
|
||||
null,
|
||||
null,
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC" target="_blank">LINK</a> HERE </div> <a href="XYZ" target="_blank">LINK</a>END TEXT'
|
||||
),
|
||||
array (
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span>END TEXT</span>',
|
||||
"_top",
|
||||
null,
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC" target="_top">LINK</a> HERE </div> <span>END TEXT</span>'
|
||||
),
|
||||
array (
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span>END TEXT</span>',
|
||||
"_top",
|
||||
array( 'span'),
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span target="_top">END TEXT</span>'
|
||||
),
|
||||
array (
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span>END TEXT</span>',
|
||||
"_top",
|
||||
array( 'SPAN'),
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span target="_top">END TEXT</span>'
|
||||
),
|
||||
array (
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span target="_top">END TEXT</span>',
|
||||
"_top",
|
||||
array( 'span', 'div'),
|
||||
'MY CONTENT <div target="_top"> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span target="_top">END TEXT</span>'
|
||||
),
|
||||
array (
|
||||
'MY CONTENT <div target=\'ABC\'> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span target="xyz">END TEXT</span>',
|
||||
"_top",
|
||||
array( 'span', 'div'),
|
||||
'MY CONTENT <div target="_top"> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span target="_top">END TEXT</span>'
|
||||
),
|
||||
array (
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span target="xyz" src="ABC">END TEXT</span>',
|
||||
"_top",
|
||||
array( 'span'),
|
||||
'MY CONTENT <div> SOME ADDITIONAL TEXT <a href="XYZ" src="ABC">LINK</a> HERE </div> <span src="ABC" target="_top">END TEXT</span>'
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the normalize_whitespace function
|
||||
*
|
||||
* @dataProvider get_input_output
|
||||
*/
|
||||
function test_normalize_whitespace( $content, $target, $tags, $exp_str ) {
|
||||
if ( true === is_null( $target ) ) {
|
||||
$this->assertEquals( $exp_str, links_add_target( $content ) );
|
||||
} elseif ( true === is_null( $tags ) ) {
|
||||
$this->assertEquals( $exp_str, links_add_target( $content, $target ) );
|
||||
} else {
|
||||
$this->assertEquals( $exp_str, links_add_target( $content, $target, $tags ) );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user