WordPress.org

Make WordPress Core

Ticket #4539: dev-snapshot-2010-11-27.php.diff

File dev-snapshot-2010-11-27.php.diff, 19.0 KB (added by norbertm, 8 years ago)
  • wordpress/wp-includes/formatting.php

     
    3131        static $static_setup = false, $opening_quote, $closing_quote, $default_no_texturize_tags, $default_no_texturize_shortcodes, $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements;
    3232        $output = '';
    3333        $curl = '';
    34         $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
     34        $textarr = preg_split('/(<!--.*-->|<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
    3535        $stop = count($textarr);
    3636
    3737        // No need to set up these variables more than once
     
    5656                $static_characters = array_merge(array('---', ' -- ', '--', ' - ', 'xn&#8211;', '...', '``', '\'\'', ' (tm)'), $cockney);
    5757                $static_replacements = array_merge(array('&#8212;', ' &#8212; ', '&#8211;', ' &#8211; ', 'xn--', '&#8230;', $opening_quote, $closing_quote, ' &#8482;'), $cockneyreplace);
    5858
    59                 $dynamic_characters = array('/\'(\d\d(?:&#8217;|\')?s)/', '/\'(\d)/', '/(\s|\A|[([{<]|")\'/', '/(\d)"/', '/(\d)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A|[([{<])"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/\b(\d+)x(\d+)\b/');
    60                 $dynamic_replacements = array('&#8217;$1','&#8217;$1', '$1&#8216;', '$1&#8243;', '$1&#8242;', '$1&#8217;$2', '$1' . $opening_quote . '$2', $closing_quote . '$1', '&#8217;$1', '$1&#215;$2');
     59                $dynamic_map = array(
     60                        '/([\A\s])\'(\d{2})([^\d]|\'\w|\Z)/' => '$1&#8217;$2$3', // '99, '99ers
    6161
     62                        '/([\w\]})])\'([\w])/' => '$1&#8217;$2', // test's
     63
     64                        '/\'([^\']*)\'/' => '&#8216;$1&#8217;', // 'asd'
     65                        '/"([^"]*)"/' => $opening_quote . '$1' . $closing_quote, // "qwe"
     66
     67                        '/(\A|\s)(\d)\'/' => '$1$2&#8242;', // 9'
     68                        '/(\A|\s)(\d)"/' => '$1$2&#8243;', // 9"
     69
     70                        '/\b(\d+)x(\d+)\b/' => '$1&#215;$2' // 97x34
     71                );
     72
     73                $dynamic_characters = array_keys( $dynamic_map );
     74                $dynamic_replacements = array_values( $dynamic_map );
     75
    6276                $static_setup = true;
    6377        }
    6478
     
    7084        $no_texturize_tags_stack = array();
    7185        $no_texturize_shortcodes_stack = array();
    7286
     87        $single_quote_state = '&#8216;';
     88        $double_quote_state = $opening_quote;
     89
     90        $text_node_count = 0;
     91        $whitespace_before_last_tag = false;
     92
    7393        for ( $i = 0; $i < $stop; $i++ ) {
    7494                $curl = $textarr[$i];
    7595
     
    7898                        // This is not a tag, nor is the texturization disabled
    7999                        // static strings
    80100                        $curl = str_replace($static_characters, $static_replacements, $curl);
     101                        // quotes after tags, e.g. <b>somebody</b>'s
     102                        if ( ( $text_node_count > 0 ) && ( ! $whitespace_before_last_tag ) ) {
     103                                $curl = preg_replace( '/^(\')/', '&#8217;', $curl );
     104                        }
     105
    81106                        // regular expressions
    82107                        $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl);
     108
     109                        // Quotes that span across multiple tags & shortcodes
     110                        // Dynamic regexps above ensure that there is at most 1 quote left in the string.
     111                        // Otherwise, the less efficient preg_replace() within a while loop would be required.
     112                        if ( strpos( $curl, '\'' ) !== false ) {
     113                                $curl = str_replace( '\'', $single_quote_state, $curl);
     114                                $single_quote_state = ( ( '&#8216;' == $single_quote_state ) ? '&#8217;' : '&#8216;' );
     115                        }
     116                        if ( strpos($curl, '"' ) !== false ) {
     117                                $curl = str_replace( '"', $double_quote_state, $curl);
     118                                $double_quote_state = ( $opening_quote == $double_quote_state ) ? $closing_quote : $opening_quote;
     119                        }
     120                        // stats for quotes after tags above
     121                        $text_node_count++;
     122                        $whitespace_before_last_tag = ( preg_match('/\s$/', $curl) > 0 );
    83123                } elseif (!empty($curl)) {
    84124                        /*
    85125                         * Only call _wptexturize_pushpop_element if first char is correct
  • wp-testcase/test_includes_formatting.php

     
    371371               
    372372                $invalid_nest = '<pre></code>"baba"</pre>';
    373373                $this->assertEquals($invalid_nest, wptexturize($invalid_nest));
     374        }
    374375
     376        //WP Ticket #8912, #10033
     377        function test_skip_html_comments() {
     378                $this->assertEquals('<ul><li>Hello.</li><!--<li>Goodbye.</li>--></ul>', wptexturize('<ul><li>Hello.</li><!--<li>Goodbye.</li>--></ul>'));
     379
     380                $html = '<!--[if !IE]>-->' . "\n";
     381                $html .= '<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/_nkZ3eHeXlc" width="320" height="260">' . "\n";
     382                $html .= '<!--<![endif]-->' . "\n";
     383                $this->assertEquals($html, wptexturize($html));
     384
     385                $html = '<!--[if !IE]>-->' . "\n";
     386                $html .= '</object>' . "\n";
     387                $html .= '<!--<![endif]-->' . "\n";
     388                $this->assertEquals($html, wptexturize($html));
    375389        }
    376390       
    377391        //WP Ticket #1418
     
    388402       
    389403        //WP Ticket #4539
    390404        function test_basic_quotes() {
     405                $this->assertNotEquals('&#8216;&#8217;', wptexturize("''")); // this does not work as expected due to a static replacement
     406                $this->assertEquals('&#8220;&#8221;', wptexturize('""'));
     407                $this->assertEquals('&#8220;&#8221;', wptexturize("``''")); // this is what causes '' to fail
     408                $this->assertEquals('&#8216; &#8217;', wptexturize("' '"));
     409                $this->assertEquals('&#8220; &#8221;', wptexturize('" "'));
     410                $this->assertEquals('&#8216;<img src="">&#8217;', wptexturize('\'<img src="">\''));
     411                $this->assertEquals('&#8220;<img src="">&#8221;', wptexturize('"<img src="">"'));
     412                $this->assertEquals('Peter&#8217;s photo: &#8216;<img src="">&#8217;', wptexturize('Peter\'s photo: \'<img src="">\''));
     413                $this->assertEquals('Peter&#8217;s photo: &#8220;<img src="">&#8221;', wptexturize('Peter\'s photo: "<img src="">"'));
     414
    391415                $this->assertEquals('test&#8217;s', wptexturize('test\'s'));
    392416                $this->assertEquals('test&#8217;s', wptexturize('test\'s'));
    393417
    394418                $this->assertEquals('&#8216;quoted&#8217;', wptexturize('\'quoted\''));
    395419                $this->assertEquals('&#8220;quoted&#8221;', wptexturize('"quoted"'));
     420                $this->assertEquals('&#8216;quoted&#8217;s&#8217;', wptexturize('\'quoted\'s\''));
     421                $this->assertEquals('&#8220;quoted&#8217;s&#8221;', wptexturize('"quoted\'s"'));
    396422
    397423                $this->assertEquals('(&#8216;quoted&#8217;)', wptexturize('(\'quoted\')'));
    398424                $this->assertEquals('{&#8220;quoted&#8221;}', wptexturize('{"quoted"}'));
     425                $this->assertEquals('["quoted"]', wptexturize('["quoted"]')); // shortcode
     426                $this->assertEquals('<"quoted">', wptexturize('<"quoted">')); // tag
     427                $this->assertEquals('(&#8216;quoted&#8217;s&#8217;)', wptexturize('(\'quoted\'s\')'));
     428                $this->assertEquals('{&#8220;quoted&#8217;s&#8221;}', wptexturize('{"quoted\'s"}'));
     429                $this->assertEquals('["quoted\'s"]', wptexturize('["quoted\'s"]')); // shortcode
     430                $this->assertEquals('<"quoted\'s">', wptexturize('<"quoted\'s">')); // tag
    399431
    400432                $this->assertEquals('&#8216;qu(ot)ed&#8217;', wptexturize('\'qu(ot)ed\''));
    401433                $this->assertEquals('&#8220;qu{ot}ed&#8221;', wptexturize('"qu{ot}ed"'));
     434                $this->assertEquals('&#8220;qu[ot]ed&#8221;', wptexturize('"qu[ot]ed"'));
     435                $this->assertEquals('&#8220;qu<ot>ed&#8221;', wptexturize('"qu<ot>ed"'));
     436                $this->assertEquals('&#8216;qu(ot)ed&#8217;s&#8217;', wptexturize('\'qu(ot)ed\'s\''));
     437                $this->assertEquals('&#8220;qu{ot}ed&#8217;s&#8221;', wptexturize('"qu{ot}ed\'s"'));
     438                $this->assertEquals('&#8220;qu[ot]ed&#8217;s&#8221;', wptexturize('"qu[ot]ed\'s"'));
     439                $this->assertEquals('&#8220;qu<ot>ed&#8217;s&#8221;', wptexturize('"qu<ot>ed\'s"'));
     440                $this->assertEquals('&#8216;qu(ot)&#8217;s&#8217;', wptexturize('\'qu(ot)\'s\''));
     441                $this->assertEquals('&#8220;qu{ot}&#8217;s&#8221;', wptexturize('"qu{ot}\'s"'));
     442                $this->assertEquals('&#8220;qu[ot]&#8217;s&#8221;', wptexturize('"qu[ot]\'s"'));
     443                $this->assertEquals('&#8220;qu<ot>&#8217;s&#8221;', wptexturize('"qu<ot>\'s"'));
    402444
    403445                $this->assertEquals('&#8216;test&#8217;s quoted&#8217;', wptexturize('\'test\'s quoted\''));
    404446                $this->assertEquals('&#8220;test&#8217;s quoted&#8221;', wptexturize('"test\'s quoted"'));
    405447        }
    406448
     449        //WP Ticket #14491
     450        function test_quoted_numbers() {
     451                $this->knownWPBug(14491);
     452
     453                // 4 digits
     454                $this->assertEquals('&#8216;2010&#8217;', wptexturize("'2010'"));
     455                $this->assertEquals('&#8220;2011&#8221;', wptexturize('"2011"'));
     456                $this->assertEquals('&#8216; 2010 &#8217;', wptexturize("' 2010 '"));
     457                $this->assertEquals('&#8220; 2011 &#8221;', wptexturize('" 2011 "'));
     458                $this->assertEquals(' &#8216;2010&#8217; ', wptexturize(" '2010' "));
     459                $this->assertEquals(' &#8220;2011&#8221; ', wptexturize(' "2011" '));
     460                $this->assertEquals(' &#8216; 2010 &#8217; ', wptexturize(" ' 2010 ' "));
     461                $this->assertEquals(' &#8220; 2011 &#8221; ', wptexturize(' " 2011 " '));
     462
     463                // 2 digits to test against '99' vs '99'ers
     464                $this->assertEquals('&#8216;10&#8217;', wptexturize("'10'"));
     465                $this->assertEquals('&#8220;11&#8221;', wptexturize('"11"'));
     466                $this->assertEquals('&#8216; 10 &#8217;', wptexturize("' 10 '"));
     467                $this->assertEquals('&#8220; 11 &#8221;', wptexturize('" 11 "'));
     468                //$this->assertEquals(' &#8216;10&#8217; ', wptexturize(" '10' "));
     469                $this->assertEquals(' &#8220;11&#8221; ', wptexturize(' "11" '));
     470                $this->assertEquals(' &#8216; 10 &#8217; ', wptexturize(" ' 10 ' "));
     471                $this->assertEquals(' &#8220; 11 &#8221; ', wptexturize(' " 11 " '));
     472        }
     473
     474        //WP Ticket #4539
     475        function test_nested_quotes() {
     476                $this->assertEquals('&#8220;This is a &#8216;nested quote&#8217;.&#8221;', wptexturize('"This is a \'nested quote\'."'));
     477                $this->assertEquals('&#8216;This is a &#8220;nested quote&#8221;.&#8217;', wptexturize('\'This is a "nested quote".\''));
     478                $this->assertEquals('&#8220;These are some &#8216;nested&#8217; &#8216;quotes&#8217;.&#8221;', wptexturize('"These are some \'nested\' \'quotes\'."'));
     479                $this->assertEquals('&#8216;These are some &#8220;nested&#8221; &#8220;quotes&#8221;.&#8217;', wptexturize('\'These are some "nested" "quotes".\''));
     480        }
     481
    407482        //WP Tickets #4539, #15241
    408483        function test_full_sentences_with_unmatched_single_quotes() {
    409484                $this->assertEquals(
    410485                        'That means every moment you&#8217;re working on something without it being in the public it&#8217;s actually dying.',
    411486                        wptexturize("That means every moment you're working on something without it being in the public it's actually dying.")
    412487                );
     488                $this->assertEquals(
     489                        '&#8216;That means every moment you&#8217;re working on something without it being in the public it&#8217;s actually dying.&#8217;',
     490                        wptexturize("'That means every moment you're working on something without it being in the public it's actually dying.'")
     491                );
     492                $this->assertEquals(
     493                        '&#8220;That means every moment you&#8217;re working on something without it being in the public it&#8217;s actually dying.&#8221;',
     494                        wptexturize("\"That means every moment you're working on something without it being in the public it's actually dying.\"")
     495                );
     496                $this->assertEquals(
     497                        'That means every moment you&#8217;re working on &#8216;something&#8217; without it being in the public it&#8217;s actually dying.',
     498                        wptexturize("That means every moment you're working on 'something' without it being in the public it's actually dying.")
     499                );
     500                $this->assertEquals(
     501                        'That means every moment you&#8217;re working on &#8220;something&#8221; without it being in the public it&#8217;s actually dying.',
     502                        wptexturize("That means every moment you're working on \"something\" without it being in the public it's actually dying.")
     503                );
    413504        }
    414505
    415         //WP Ticket #4539
     506        //WP Tickets #4539, #10606
    416507        function test_quotes() {
    417508                $this->knownWPBug(4539);
    418509                $this->assertEquals('&#8220;Quoted String&#8221;', wptexturize('"Quoted String"'));
    419510                $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"'));
     511                $this->assertEquals('Here is &#8220; <a href="http://example.com">a test with a link</a> &#8221;', wptexturize('Here is " <a href="http://example.com">a test with a link</a> "'));
     512                $this->assertEquals('Here is &#8220;<a href="http://example.com"> a test with a link </a>&#8221;', wptexturize('Here is "<a href="http://example.com"> a test with a link </a>"'));
    420513                $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link and a period</a>&#8221;.', wptexturize('Here is "<a href="http://example.com">a test with a link and a period</a>".'));
    421514                $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221; and a space.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>" and a space.'));
    422515                $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a> and some text quoted&#8221;', wptexturize('Here is "<a href="http://example.com">a test with a link</a> and some text quoted"'));
     
    424517                $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;; and a semi-colon.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"; and a semi-colon.'));
    425518                $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;- and a dash.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"- and a dash.'));
    426519                $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;&#8230; and ellipses.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"... and ellipses.'));
     520                $this->assertEquals('Here is &#8220;<a href="http://example.com">a test</a> with a link&#8221;.', wptexturize('Here is "<a href="http://example.com">a test</a> with a link".'));
    427521                $this->assertEquals('Here is &#8220;a test <a href="http://example.com">with a link</a>&#8221;.', wptexturize('Here is "a test <a href="http://example.com">with a link</a>".'));
    428                 $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;and a work stuck to the end.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"and a work stuck to the end.'));
     522                $this->assertEquals('Here is &#8220;a test <a href="http://example.com">with a link</a>&#8221; .', wptexturize('Here is "a test <a href="http://example.com">with a link</a>" .'));
     523                $this->assertEquals('Here is &#8220;<a href="http://example.com">a test with a link</a>&#8221;and a word stuck to the end.', wptexturize('Here is "<a href="http://example.com">a test with a link</a>"and a word stuck to the end.'));
    429524                $this->assertEquals('A test with a finishing number, &#8220;like 23&#8221;.', wptexturize('A test with a finishing number, "like 23".'));
    430525                $this->assertEquals('A test with a number, &#8220;like 62&#8221;, is nice to have.', wptexturize('A test with a number, "like 62", is nice to have.'));
    431526        }
     
    451546                $this->assertEquals('&#8220;Class of 99&#8221;', wptexturize("\"Class of 99\""));
    452547                $this->assertEquals('&#8220;Class of &#8217;99&#8221;', wptexturize("\"Class of '99\""));
    453548        }
    454        
    455         function test_quotes_after_numbers() {
    456                 $this->assertEquals('Class of &#8217;99', wptexturize("Class of '99"));
    457         }
    458        
     549
    459550        //WP Ticket #15241
    460551        function test_other_html() {
    461552                $this->knownWPBug(15241);
    462553                $this->assertEquals('&#8216;<strong>', wptexturize("'<strong>"));
     554                $this->assertEquals('&#8220;<strong>', wptexturize('"<strong>'));
     555                $this->assertEquals('&#8216;<strong></strong>&#8217;', wptexturize("'<strong></strong>'"));
     556                $this->assertEquals('&#8220;<strong></strong>&#8221;', wptexturize('"<strong></strong>"'));
    463557                $this->assertEquals('&#8216;<strong>Quoted Text</strong>&#8217;,', wptexturize("'<strong>Quoted Text</strong>',"));
    464558                $this->assertEquals('&#8220;<strong>Quoted Text</strong>&#8221;,', wptexturize('"<strong>Quoted Text</strong>",'));
    465559        }
    466        
     560
     561        //WP Ticket #15241
     562        function test_many_single_quotes() {
     563                $this->assertEquals('This isn&#8217;t inherently bad, but I don&#8217;t think it&#8217;s normal.', wptexturize("This isn't inherently bad, but I don't think it's normal."));
     564        }
     565
     566        //WP Ticket #1258
     567        function test_enumeration() {
     568                $this->assertEquals("&#8216;products&#8217;, &#8216;services&#8217;", wptexturize("'products', 'services'"));
     569                $this->assertEquals("&#8216;hello&#8217;, &#8216;world&#8217;, &#8217;tis", wptexturize("'hello', 'world', 'tis"));
     570                $this->assertEquals("&#8216;hello&#8217;, &#8216;world&#8217;, &#8217;tis ", wptexturize("'hello', 'world', 'tis "));
     571        }
     572
     573        //WP Ticket #11275
     574        function test_quoting() {
     575                $this->assertEquals('She said—&#8220;No!&#8221;', wptexturize('She said—"No!"'));
     576                $this->assertEquals('She said — &#8220;No!&#8221;', wptexturize('She said — "No!"'));
     577                $this->assertEquals('She said—&#8220;<a href="#">No!</a>&#8221;', wptexturize('She said—"<a href="#">No!</a>"'));
     578                $this->assertEquals('She said—&#8216;<a href="#">It&#8217;s Peter&#8217;s!</a>&#8217;', wptexturize('She said—\'<a href="#">It\'s Peter\'s!</a>\''));
     579                $this->assertEquals('She said—&#8220;<a href="#">It&#8217;s Peter&#8217;s!</a>&#8221;', wptexturize('She said—"<a href="#">It\'s Peter\'s!</a>"'));
     580        }
     581
     582        //WP Ticket #15444
     583        function test_tag_followed_by_quote() {
     584                $this->knownWPBug(15444);
     585                $this->assertEquals('<a href="#">Jim</a>&#8217;s red bike.', wptexturize('<a href="#">Jim</a>\'s red bike.'));
     586                $this->assertEquals('&#8216;<a href="#">Jim</a>&#8217;s red bike.&#8217;', wptexturize('\'<a href="#">Jim</a>\'s red bike.\''));
     587                $this->assertEquals('&#8220;<a href="#">Jim</a>&#8217;s red bike.&#8221;', wptexturize('"<a href="#">Jim</a>\'s red bike."'));
     588                $this->assertEquals('<a href="#">Jim</a>&#8217;s &#8216;red bike.&#8217;', wptexturize('<a href="#">Jim</a>\'s \'red bike.\''));
     589                $this->assertEquals('<a href="#">Jim</a>&#8217;s &#8220;red bike.&#8221;', wptexturize('<a href="#">Jim</a>\'s "red bike."'));
     590                $this->assertEquals('<a href="#">Jim</a>&#8217;s &#8216;<a href="#">red bike</a>.&#8217;', wptexturize('<a href="#">Jim</a>\'s \'<a href="#">red bike</a>.\''));
     591                $this->assertEquals('<a href="#">Jim</a>&#8217;s &#8220;<a href="#">red bike</a>.&#8221;', wptexturize('<a href="#">Jim</a>\'s "<a href="#">red bike</a>."'));
     592        }
     593
    467594        function test_x() {
    468595                $this->assertEquals('14&#215;24', wptexturize("14x24"));
     596                $this->assertEquals('&#8216;14&#215;24&#8217;', wptexturize("'14x24'"));
     597                $this->assertEquals('&#8220;14&#215;24&#8221;', wptexturize('"14x24"'));
     598                $this->assertEquals('&#8216;<a href="#">14&#215;24</a>&#8217;', wptexturize('\'<a href="#">14x24</a>\''));
     599                $this->assertEquals('&#8220;<a href="#">14&#215;24</a>&#8221;', wptexturize('"<a href="#">14x24</a>"'));
    469600        }
    470        
     601
     602        //WP Ticket #4116
     603        function test_x_4116() {
     604                $this->knownWPBug(4116);
     605                $this->assertEquals('www.a4x3b.com', wptexturize("www.a4x3b.com"));
     606                $this->assertEquals('http://www.youtube.com/watch?v=irWR7F0x2uU', wptexturize("http://www.youtube.com/watch?v=irWR7F0x2uU"));
     607        }
     608
    471609        function test_minutes_seconds() {
    472610                $this->assertEquals('9&#8242;', wptexturize('9\''));
    473611                $this->assertEquals('9&#8243;', wptexturize("9\""));
     
    484622                $this->assertEquals('&nbsp;&#8220;Testing&#8221;', wptexturize('&nbsp;"Testing"'));
    485623                $this->assertEquals('&#38;&#8220;Testing&#8221;', wptexturize('&#38;"Testing"'));
    486624        }
     625
     626        //WP Ticket #6969
     627        function test_shortcode_skip() {
     628                $this->assertEquals('[code lang="php"]$foo = \'bar\';[/code]', wptexturize('[code lang="php"]$foo = \'bar\';[/code]'));
     629        }
     630
    487631}
    488632
    489633class TestCleanUrl extends WPTestCase {