| 1 | /* global wp, jQuery */ |
| 2 | jQuery( function() { |
| 3 | module( 'shortcode' ); |
| 4 | |
| 5 | test( 'next() should find the shortcode', function() { |
| 6 | var result; |
| 7 | |
| 8 | // Basic |
| 9 | result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode' ); |
| 10 | equal( result.index, 13, 'foo shortcode found at index 13' ); |
| 11 | |
| 12 | result = wp.shortcode.next( 'foo', 'this has the [foo param="foo"] shortcode' ); |
| 13 | equal( result.index, 13, 'foo shortcode with params found at index 13' ); |
| 14 | |
| 15 | // Not found |
| 16 | result = wp.shortcode.next( 'bar', 'this has the [foo] shortcode' ); |
| 17 | equal( result, undefined, 'bar shortcode not found' ); |
| 18 | |
| 19 | result = wp.shortcode.next( 'bar', 'this has the [foo param="bar"] shortcode' ); |
| 20 | equal( result, undefined, 'bar shortcode not found with params' ); |
| 21 | |
| 22 | // Starting at indices |
| 23 | result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 12 ); |
| 24 | equal( result.index, 13, 'foo shortcode found before index 13' ); |
| 25 | |
| 26 | result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 13 ); |
| 27 | equal( result.index, 13, 'foo shortcode found at index 13' ); |
| 28 | |
| 29 | result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 14 ); |
| 30 | equal( result, undefined, 'foo shortcode not found after index 13' ); |
| 31 | |
| 32 | // Multiple instances |
| 33 | result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode [foo] twice', 14 ); |
| 34 | equal( result.index, 29, 'foo shortcode found the second foo at index 29' ); |
| 35 | |
| 36 | result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode [foo] twice' ); |
| 37 | equal( result.index, 31, 'foo shortcode found the non-escaped foo at index 31' ); |
| 38 | |
| 39 | // Escaped |
| 40 | result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode' ); |
| 41 | equal( result, undefined, 'foo shortcode not found when escaped' ); |
| 42 | |
| 43 | result = wp.shortcode.next( 'foo', 'this has the [[foo param="foo"]] shortcode' ); |
| 44 | equal( result, undefined, 'foo shortcode not found when escaped with params' ); |
| 45 | |
| 46 | // Stubs |
| 47 | result = wp.shortcode.next( 'foo', 'this has the [foobar] shortcode' ); |
| 48 | equal( result, undefined, "stub doesn't trigger match" ); |
| 49 | |
| 50 | result = wp.shortcode.next( 'foobar', 'this has the [foo] shortcode' ); |
| 51 | equal( result, undefined, "stub doesn't trigger match" ); |
| 52 | }); |
| 53 | |
| 54 | test( 'replace() should replace the shortcode', function() { |
| 55 | var result; |
| 56 | |
| 57 | // Basic |
| 58 | result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode', shortcodeReplaceCallback ); |
| 59 | equal( result, 'this has the bar shortcode', 'foo replaced with bar' ); |
| 60 | |
| 61 | result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode', shortcodeReplaceCallback ); |
| 62 | equal( result, 'this has the bar shortcode', 'foo and params replaced with bar' ); |
| 63 | |
| 64 | // Not found |
| 65 | result = wp.shortcode.replace( 'bar', 'this has the [foo] shortcode', shortcodeReplaceCallback ); |
| 66 | equal( result, 'this has the [foo] shortcode', 'bar not found' ); |
| 67 | |
| 68 | result = wp.shortcode.replace( 'bar', 'this has the [foo param="bar"] shortcode', shortcodeReplaceCallback ); |
| 69 | equal( result, 'this has the [foo param="bar"] shortcode', 'bar not found with params' ); |
| 70 | |
| 71 | // Multiple instances |
| 72 | result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode [foo] twice', shortcodeReplaceCallback ); |
| 73 | equal( result, 'this has the bar shortcode bar twice', 'foo replaced with bar twice' ); |
| 74 | |
| 75 | result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode [foo] twice', shortcodeReplaceCallback ); |
| 76 | equal( result, 'this has the bar shortcode bar twice', 'foo and params replaced with bar twice' ); |
| 77 | |
| 78 | // Escaped |
| 79 | result = wp.shortcode.replace( 'foo', 'this has the [[foo]] shortcode', shortcodeReplaceCallback ); |
| 80 | equal( result, 'this has the [[foo]] shortcode', 'escaped foo not replaced' ); |
| 81 | |
| 82 | result = wp.shortcode.replace( 'foo', 'this has the [[foo param="bar"]] shortcode', shortcodeReplaceCallback ); |
| 83 | equal( result, 'this has the [[foo param="bar"]] shortcode', 'escaped foo with params not replaced' ); |
| 84 | |
| 85 | // Stubs |
| 86 | result = wp.shortcode.replace( 'foo', 'this has the [foobar] shortcode', shortcodeReplaceCallback ) |
| 87 | equal( result, 'this has the [foobar] shortcode', 'stub not replaced' ); |
| 88 | |
| 89 | result = wp.shortcode.replace( 'foobar', 'this has the [foo] shortcode', shortcodeReplaceCallback ); |
| 90 | equal( result, 'this has the [foo] shortcode', 'stub not replaced' ); |
| 91 | }); |
| 92 | |
| 93 | function shortcodeReplaceCallback( shortcode ) { |
| 94 | return 'bar'; |
| 95 | } |
| 96 | }); |