Make WordPress Core

Ticket #27907: 27907.2.diff

File 27907.2.diff, 3.6 KB (added by jorbin, 12 years ago)
  • src/wp-includes/js/shortcode.js

     
    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] ) {
     37                        // If we matched a leading `[`, and it has a matching counterpart,
     38                        // strip it from the match and increment the index accordingly.
     39                        if ( match[1] && typeof result.match !== 'undefined' ) {
    4040                                result.match = result.match.slice( 1 );
    4141                                result.index++;
    4242                        }
    4343
    44                         // If we matched a trailing `]`, strip it from the match.
    45                         if ( match[7] ) {
     44                        // If we matched a trailing `]`, , and it has a matching counterpart,
     45                        // strip it from the match.
     46                        if ( match[7] && typeof result.match !== 'undefined' ) {
    4647                                result.match = result.match.slice( 0, -1 );
    4748                        }
    4849
     
    353354                        return text + '</' + options.tag + '>';
    354355                }
    355356        });
    356 }());
    357  No newline at end of file
     357}());
  • tests/qunit/wp-includes/js/shortcode.js

     
    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
     71        test( 'next() should still work when there are not equal ammounts of square brackets', function() {
     72                var result;
     73
     74                result = wp.shortcode.next( 'foo', 'this has the [[foo] shortcode' );
     75                equal( result.index, 13, 'shortcode found when there are offset square brackets' );
     76
     77                result = wp.shortcode.next( 'foo', 'this has the [foo]] shortcode' );
     78                equal( result.index, 13, 'shortcode found when there are offset square brackets' );
     79        });
     80
    6081        test( 'next() should find the second instances of the shortcode when the first one is escaped', function() {
    6182                var result;
    6283
    63 
    6484                result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode [foo] twice' );
    6585                equal( result.index, 31, 'foo shortcode found the non-escaped foo at index 31' );
    6686        });
     
    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
    124145        });
    125146
     147        test( 'replace() should replace improperly escaped shortcodes that include newlines', function() {
     148                var result;
     149
     150                result = wp.shortcode.replace( 'foo', 'this [foo] has the [[foo param="bar"]\n] shortcode ', shortcodeReplaceCallback );
     151                equal( result, 'this bar has the [bar\n] shortcode ', 'escaping with newlines should not actually escape the content' );
     152
     153
     154                result = wp.shortcode.replace( 'foo', 'this [foo] has the [\n[foo param="bar"]] shortcode ', shortcodeReplaceCallback );
     155                equal( result, 'this bar has the [\nbar] shortcode ', 'escaping with newlines should not actually escape the content' );
     156        });
     157
    126158        test( 'replace() should not replace the shortcode when it is an incomplete match', function() {
    127159                var result;
    128160