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;
|
global $_links_add_target;
|
||||||
$_links_add_target = $target;
|
$_links_add_target = $target;
|
||||||
$tags = implode('|', (array)$tags);
|
$tags = implode('|', (array)$tags);
|
||||||
return preg_replace_callback( "!<($tags)(.+?)>!i", '_links_add_target', $content );
|
return preg_replace_callback( "!<($tags)([^>]*)>!i", '_links_add_target', $content );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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…
Reference in New Issue