Improve the consistency of punctuation matching in `wptexturize()`.

Adds unit tests.

Props miqrogroove.
Fixes #31886.


git-svn-id: https://develop.svn.wordpress.org/trunk@32789 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-06-16 00:49:45 +00:00
parent 40bb9aa3d9
commit 1e8fb99ebd
2 changed files with 53 additions and 4 deletions

View File

@ -127,10 +127,10 @@ function wptexturize( $text, $reset = false ) {
// '99' and '99" are ambiguous among other patterns; assume it's an abbreviated year at the end of a quotation.
if ( "'" !== $apos || "'" !== $closing_single_quote ) {
$dynamic[ '/\'(\d\d)\'(?=\Z|[.,)}\-\]]|>|' . $spaces . ')/' ] = $apos . '$1' . $closing_single_quote;
$dynamic[ '/\'(\d\d)\'(?=\Z|[.,:;!?)}\-\]]|>|' . $spaces . ')/' ] = $apos . '$1' . $closing_single_quote;
}
if ( "'" !== $apos || '"' !== $closing_quote ) {
$dynamic[ '/\'(\d\d)"(?=\Z|[.,)}\-\]]|>|' . $spaces . ')/' ] = $apos . '$1' . $closing_quote;
$dynamic[ '/\'(\d\d)"(?=\Z|[.,:;!?)}\-\]]|>|' . $spaces . ')/' ] = $apos . '$1' . $closing_quote;
}
// '99 '99s '99's (apostrophe) But never '9 or '99% or '999 or '99.0.
@ -150,7 +150,7 @@ function wptexturize( $text, $reset = false ) {
// Apostrophe in a word. No spaces, double apostrophes, or other punctuation.
if ( "'" !== $apos ) {
$dynamic[ '/(?<!' . $spaces . ')\'(?!\Z|[.,:;"\'(){}[\]\-]|&[lg]t;|' . $spaces . ')/' ] = $apos;
$dynamic[ '/(?<!' . $spaces . ')\'(?!\Z|[.,:;!?"\'(){}[\]\-]|&[lg]t;|' . $spaces . ')/' ] = $apos;
}
// 9' (prime)
@ -160,7 +160,7 @@ function wptexturize( $text, $reset = false ) {
// Single quotes followed by spaces or ending punctuation.
if ( "'" !== $closing_single_quote ) {
$dynamic[ '/\'(?=\Z|[.,)}\-\]]|&gt;|' . $spaces . ')/' ] = $closing_single_quote;
$dynamic[ '/\'(?=\Z|[.,:;!?)}\-\]]|&gt;|' . $spaces . ')/' ] = $closing_single_quote;
}
$dynamic_characters['apos'] = array_keys( $dynamic );

View File

@ -112,12 +112,25 @@ class Tests_Formatting_WPTexturize extends WP_UnitTestCase {
$this->assertEquals('Class of &#8217;99', wptexturize("Class of '99"));
$this->assertEquals('Class of &#8217;99&#8217;s', wptexturize("Class of '99's"));
$this->assertEquals('&#8216;Class of &#8217;99&#8217;', wptexturize("'Class of '99'"));
$this->assertEquals('&#8216;Class of &#8217;99&#8217; ', wptexturize("'Class of '99' "));
$this->assertEquals('&#8216;Class of &#8217;99&#8217;.', wptexturize("'Class of '99'."));
$this->assertEquals('&#8216;Class of &#8217;99&#8217;, she said', wptexturize("'Class of '99', she said"));
$this->assertEquals('&#8216;Class of &#8217;99&#8217;:', wptexturize("'Class of '99':"));
$this->assertEquals('&#8216;Class of &#8217;99&#8217;;', wptexturize("'Class of '99';"));
$this->assertEquals('&#8216;Class of &#8217;99&#8217;!', wptexturize("'Class of '99'!"));
$this->assertEquals('&#8216;Class of &#8217;99&#8217;?', wptexturize("'Class of '99'?"));
$this->assertEquals('&#8216;Class of &#8217;99&#8217;s&#8217;', wptexturize("'Class of '99's'"));
$this->assertEquals('&#8216;Class of &#8217;99&#8217;s&#8217;', wptexturize("'Class of '99&#8217;s'"));
//$this->assertEquals('&#8220;Class of 99&#8221;', wptexturize("\"Class of 99\""));
$this->assertEquals('&#8220;Class of &#8217;99&#8221;', wptexturize("\"Class of '99\""));
$this->assertEquals('{&#8220;Class of &#8217;99&#8221;}', wptexturize("{\"Class of '99\"}"));
$this->assertEquals(' &#8220;Class of &#8217;99&#8221; ', wptexturize(" \"Class of '99\" "));
$this->assertEquals(' &#8220;Class of &#8217;99&#8221;.', wptexturize(" \"Class of '99\"."));
$this->assertEquals(' &#8220;Class of &#8217;99&#8221;, she said', wptexturize(" \"Class of '99\", she said"));
$this->assertEquals(' &#8220;Class of &#8217;99&#8221;:', wptexturize(" \"Class of '99\":"));
$this->assertEquals(' &#8220;Class of &#8217;99&#8221;;', wptexturize(" \"Class of '99\";"));
$this->assertEquals(' &#8220;Class of &#8217;99&#8221;!', wptexturize(" \"Class of '99\"!"));
$this->assertEquals(' &#8220;Class of &#8217;99&#8221;?', wptexturize(" \"Class of '99\"?"));
$this->assertEquals('}&#8221;Class of &#8217;99&#8243;{', wptexturize("}\"Class of '99\"{")); // Not a quotation, may be between two other quotations.
}
@ -701,6 +714,26 @@ class Tests_Formatting_WPTexturize extends WP_UnitTestCase {
'test sentence".',
'test sentence&#8221;.',
),
array(
'test sentence",',
'test sentence&#8221;,',
),
array(
'test sentence":',
'test sentence&#8221;:',
),
array(
'test sentence";',
'test sentence&#8221;;',
),
array(
'test sentence"!',
'test sentence&#8221;!',
),
array(
'test sentence"?',
'test sentence&#8221;?',
),
array(
'test sentence."',
'test sentence.&#8221;',
@ -746,6 +779,22 @@ class Tests_Formatting_WPTexturize extends WP_UnitTestCase {
"word word', she said",
"word word&#8217;, she said",
),
array(
"word word': word",
"word word&#8217;: word",
),
array(
"word word'; word",
"word word&#8217;; word",
),
array(
"word word'! word",
"word word&#8217;! word",
),
array(
"word word'? word",
"word word&#8217;? word",
),
array(
"word word'- word",
"word word&#8217;- word",