Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,18 @@ function wptexturize( $text, $reset = false ) {
} else {
// This is an HTML element delimiter.

// Replace each & with & unless it already looks like an entity.
$curl = preg_replace( '/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', '&', $curl );
/*
* Replace each & with & unless it already looks like an entity,
* but only when outside no-texturize tags like <script> and <pre>.
*
* When a no-texturize tag contains a raw '<' character (e.g. JavaScript
* like `if(a<b)`), preg_split() with _get_wptexturize_split_regex()
* misidentifies that content as an HTML element delimiter. Encoding &
* inside such a token would corrupt the script or preformatted content.
*/
if ( empty( $no_texturize_tags_stack ) ) {
$curl = preg_replace( '/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', '&#038;', $curl );
}

_wptexturize_pushpop_element( $curl, $no_texturize_tags_stack, $no_texturize_tags );
}
Expand Down
28 changes: 28 additions & 0 deletions tests/phpunit/tests/formatting/wpTexturize.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ public function test_disable() {
$this->assertSame( $invalid_nest, wptexturize( $invalid_nest ) );
}

/**
* @ticket 43785
* @covers ::wptexturize
*/
public function test_no_texturize_ampersand_inside_script_with_less_than() {
$this->assertSame(
'<script>if(a<b)window&&document</script>',
wptexturize( '<script>if(a<b)window&&document</script>' )
);
$this->assertSame(
'<script type="text/javascript">if(a<b)window&&document</script>',
wptexturize( '<script type="text/javascript">if(a<b)window&&document</script>' )
);
$this->assertSame(
'<pre>if(a<b)c&&d</pre>',
wptexturize( '<pre>if(a<b)c&&d</pre>' )
);
$this->assertSame(
'<code>if(a<b)c&&d</code>',
wptexturize( '<code>if(a<b)c&&d</code>' )
);
// Ensure & in real HTML attributes is still encoded outside no-texturize tags.
$this->assertSame(
'<a href="foo&#038;bar">link</a>',
wptexturize( '<a href="foo&bar">link</a>' )
);
}

/**
* @ticket 1418
*/
Expand Down
Loading