Make WordPress Core

Ticket #27907: 27907.3.patch

File 27907.3.patch, 3.5 KB (added by Clorith, 12 years ago)
  • src/wp-includes/js/shortcode.js

    <+>UTF-8
     
    3434                                shortcode: wp.shortcode.fromMatch( match )
    3535                        };
    3636
    37                         // If we matched a leading `[`, strip it from the match
    38                         // and increment the index accordingly.
    39                         if ( match[1] ) {
    40                                 result.match = result.match.slice( 1 );
    41                                 result.index++;
    42                         }
     37                        // Check that we have a valid result match before processing it
     38                        if ( typeof result.match !== 'undefined' ) {
     39                                // If we matched a leading `[`, strip it from the match
     40                                // and increment the index accordingly.
     41                                if ( match[1] ) {
     42                                        result.match = result.match.slice( 1 );
     43                                        result.index++;
     44                                }
    4345
    44                         // If we matched a trailing `]`, strip it from the match.
    45                         if ( match[7] ) {
    46                                 result.match = result.match.slice( 0, -1 );
     46                                // If we matched a trailing `]`, strip it from the match.
     47                                if ( match[7] ) {
     48                                        result.match = result.match.slice( 0, -1 );
     49                                }
    4750                        }
    4851
    4952                        return result;
  • tests/qunit/wp-includes/js/shortcode.js

    <+>UTF-8
     
    5757                equal( result, undefined, 'foo shortcode not found when escaped with params' );
    5858        });
    5959
     60        test( 'next() should find shortcodes that are incorrectly escaped by newlines', function() {
     61                var result;
     62
     63                result = wp.shortcode.next( 'foo', 'this has the [\n[foo]] shortcode' );
     64                equal( result.index, 15, 'shortcode found when incorrectly escaping the start of it' );
     65
     66                result = wp.shortcode.next( 'foo', 'this has the [[foo]\n] shortcode' );
     67                equal( result.index, 13, 'shortcode found when incorrectly escaping the end of it' );
     68        });
     69
     70        test( 'next() should still work when there are not equal ammounts of square brackets', function() {
     71                var result;
     72
     73                result = wp.shortcode.next( 'foo', 'this has the [[foo] shortcode' );
     74                equal( result.index, 13, 'shortcode found when there are offset square brackets' );
     75
     76                result = wp.shortcode.next( 'foo', 'this has the [foo]] shortcode' );
     77                equal( result.index, 13, 'shortcode found when there are offset square brackets' );
     78        });
     79
    6080        test( 'next() should find the second instances of the shortcode when the first one is escaped', function() {
    6181                var result;
    6282
     
    121141
    122142                result = wp.shortcode.replace( 'foo', 'this [foo] has the [[foo param="bar"]] shortcode escaped', shortcodeReplaceCallback );
    123143                equal( result, 'this bar has the [[foo param="bar"]] shortcode escaped', 'escaped foo with params not replaced but unescaped foo replaced' );
     144        });
     145
     146        test( 'replace() should replace improperly escaped shortcodes that include newlines', function() {
     147                var result;
     148
     149                result = wp.shortcode.replace( 'foo', 'this [foo] has the [[foo param="bar"]\n] shortcode ', shortcodeReplaceCallback );
     150                equal( result, 'this bar has the [bar\n] shortcode ', 'escaping with newlines should not actually escape the content' );
     151
     152                result = wp.shortcode.replace( 'foo', 'this [foo] has the [\n[foo param="bar"]] shortcode ', shortcodeReplaceCallback );
     153                equal( result, 'this bar has the [\nbar] shortcode ', 'escaping with newlines should not actually escape the content' );
    124154        });
    125155
    126156        test( 'replace() should not replace the shortcode when it is an incomplete match', function() {