Don't convert URLs inside <pre>
and <code>
tags when parsing string using make_clickable()
.
Adds Unit Tests. Props johnjamesjacoby, helen, nacin, adamsilverstein, sirbrillig. Fixes #23756. git-svn-id: https://develop.svn.wordpress.org/trunk@26052 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
9f9bc20bde
commit
0670cf7cc4
@ -1606,8 +1606,15 @@ function _make_email_clickable_cb($matches) {
|
||||
function make_clickable( $text ) {
|
||||
$r = '';
|
||||
$textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
|
||||
$nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code>
|
||||
foreach ( $textarr as $piece ) {
|
||||
if ( empty( $piece ) || ( $piece[0] == '<' && ! preg_match('|^<\s*[\w]{1,20}+://|', $piece) ) ) {
|
||||
|
||||
if ( preg_match( '|^<code[\s>]|', $piece ) || preg_match( '|^<pre[\s>]|', $piece ) )
|
||||
$nested_code_pre++;
|
||||
elseif ( ( '</code>' === $piece || '</pre>' === $piece ) && $nested_code_pre )
|
||||
$nested_code_pre--;
|
||||
|
||||
if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) {
|
||||
$r .= $piece;
|
||||
continue;
|
||||
}
|
||||
|
@ -288,6 +288,46 @@ class Tests_Formatting_MakeClickable extends WP_UnitTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 23756
|
||||
*/
|
||||
function test_no_links_inside_pre_or_code() {
|
||||
$before = array(
|
||||
'<pre>http://wordpress.org</pre>',
|
||||
'<code>http://wordpress.org</code>',
|
||||
'<pre class="foobar" id="foo">http://wordpress.org</pre>',
|
||||
'<code class="foobar" id="foo">http://wordpress.org</code>',
|
||||
'<precustomtag>http://wordpress.org</precustomtag>',
|
||||
'<codecustomtag>http://wordpress.org</codecustomtag>',
|
||||
'URL before pre http://wordpress.org<pre>http://wordpress.org</pre>',
|
||||
'URL before code http://wordpress.org<code>http://wordpress.org</code>',
|
||||
'URL after pre <pre>http://wordpress.org</pre>http://wordpress.org',
|
||||
'URL after code <code>http://wordpress.org</code>http://wordpress.org',
|
||||
'URL before and after pre http://wordpress.org<pre>http://wordpress.org</pre>http://wordpress.org',
|
||||
'URL before and after code http://wordpress.org<code>http://wordpress.org</code>http://wordpress.org',
|
||||
'code inside pre <pre>http://wordpress.org <code>http://wordpress.org</code> http://wordpress.org</pre>',
|
||||
);
|
||||
|
||||
$expected = array(
|
||||
'<pre>http://wordpress.org</pre>',
|
||||
'<code>http://wordpress.org</code>',
|
||||
'<pre class="foobar" id="foo">http://wordpress.org</pre>',
|
||||
'<code class="foobar" id="foo">http://wordpress.org</code>',
|
||||
'<precustomtag><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a></precustomtag>',
|
||||
'<codecustomtag><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a></codecustomtag>',
|
||||
'URL before pre <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a><pre>http://wordpress.org</pre>',
|
||||
'URL before code <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a><code>http://wordpress.org</code>',
|
||||
'URL after pre <pre>http://wordpress.org</pre><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>',
|
||||
'URL after code <code>http://wordpress.org</code><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>',
|
||||
'URL before and after pre <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a><pre>http://wordpress.org</pre><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>',
|
||||
'URL before and after code <a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a><code>http://wordpress.org</code><a href="http://wordpress.org" rel="nofollow">http://wordpress.org</a>',
|
||||
'code inside pre <pre>http://wordpress.org <code>http://wordpress.org</code> http://wordpress.org</pre>',
|
||||
);
|
||||
|
||||
foreach ( $before as $key => $url )
|
||||
$this->assertEquals( $expected[ $key ], make_clickable( $url ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 16892
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user