| | 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 | |
| | 16 | test( 'next() should not shortcodes that are not there', function() { |
| | 17 | var result; |
| | 18 | |
| | 19 | // Not found |
| | 20 | result = wp.shortcode.next( 'bar', 'this has the [foo] shortcode' ); |
| | 21 | equal( result, undefined, 'bar shortcode not found' ); |
| | 22 | |
| | 23 | result = wp.shortcode.next( 'bar', 'this has the [foo param="bar"] shortcode' ); |
| | 24 | equal( result, undefined, 'bar shortcode not found with params' ); |
| | 25 | }); |
| | 26 | |
| | 27 | test( 'next() should find the shortcode when told to start looking beyond the start of the string', function() { |
| | 28 | var result; |
| | 29 | |
| | 30 | // Starting at indices |
| | 31 | result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 12 ); |
| | 32 | equal( result.index, 13, 'foo shortcode found before index 13' ); |
| | 33 | |
| | 34 | result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 13 ); |
| | 35 | equal( result.index, 13, 'foo shortcode found at index 13' ); |
| | 36 | |
| | 37 | result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 14 ); |
| | 38 | equal( result, undefined, 'foo shortcode not found after index 13' ); |
| | 39 | }); |
| | 40 | |
| | 41 | test( 'next() should find the second instances of the shortcode when the starting indice is after the start of the first one', function() { |
| | 42 | var result; |
| | 43 | |
| | 44 | result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode [foo] twice', 14 ); |
| | 45 | equal( result.index, 29, 'foo shortcode found the second foo at index 29' ); |
| | 46 | }); |
| | 47 | |
| | 48 | |
| | 49 | test( 'next() should not find escaped shortcodes', function() { |
| | 50 | var result; |
| | 51 | |
| | 52 | // Escaped |
| | 53 | result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode' ); |
| | 54 | equal( result, undefined, 'foo shortcode not found when escaped' ); |
| | 55 | |
| | 56 | result = wp.shortcode.next( 'foo', 'this has the [[foo param="foo"]] shortcode' ); |
| | 57 | equal( result, undefined, 'foo shortcode not found when escaped with params' ); |
| | 58 | }); |
| | 59 | |
| | 60 | test( 'next() should find the second instances of the shortcode when the first one is escaped', function() { |
| | 61 | var result; |
| | 62 | |
| | 63 | |
| | 64 | result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode [foo] twice' ); |
| | 65 | equal( result.index, 31, 'foo shortcode found the non-escaped foo at index 31' ); |
| | 66 | }); |
| | 67 | |
| | 68 | test( 'next() should not find shortcodes that are not full matches', function() { |
| | 69 | var result; |
| | 70 | |
| | 71 | // Stubs |
| | 72 | result = wp.shortcode.next( 'foo', 'this has the [foobar] shortcode' ); |
| | 73 | equal( result, undefined, 'stub does not trigger match' ); |
| | 74 | |
| | 75 | result = wp.shortcode.next( 'foobar', 'this has the [foo] shortcode' ); |
| | 76 | equal( result, undefined, 'stub does not trigger match' ); |
| | 77 | }); |
| | 78 | |
| | 79 | test( 'replace() should replace the shortcode', function() { |
| | 80 | var result; |
| | 81 | |
| | 82 | // Basic |
| | 83 | result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode', shortcodeReplaceCallback ); |
| | 84 | equal( result, 'this has the bar shortcode', 'foo replaced with bar' ); |
| | 85 | |
| | 86 | result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode', shortcodeReplaceCallback ); |
| | 87 | equal( result, 'this has the bar shortcode', 'foo and params replaced with bar' ); |
| | 88 | }); |
| | 89 | |
| | 90 | test( 'replace() should not replace the shortcode when it does not match', function() { |
| | 91 | var result; |
| | 92 | |
| | 93 | // Not found |
| | 94 | result = wp.shortcode.replace( 'bar', 'this has the [foo] shortcode', shortcodeReplaceCallback ); |
| | 95 | equal( result, 'this has the [foo] shortcode', 'bar not found' ); |
| | 96 | |
| | 97 | result = wp.shortcode.replace( 'bar', 'this has the [foo param="bar"] shortcode', shortcodeReplaceCallback ); |
| | 98 | equal( result, 'this has the [foo param="bar"] shortcode', 'bar not found with params' ); |
| | 99 | }); |
| | 100 | |
| | 101 | test( 'replace() should replace the shortcode in all instances of its use', function() { |
| | 102 | var result; |
| | 103 | |
| | 104 | // Multiple instances |
| | 105 | result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode [foo] twice', shortcodeReplaceCallback ); |
| | 106 | equal( result, 'this has the bar shortcode bar twice', 'foo replaced with bar twice' ); |
| | 107 | |
| | 108 | result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode [foo] twice', shortcodeReplaceCallback ); |
| | 109 | equal( result, 'this has the bar shortcode bar twice', 'foo and params replaced with bar twice' ); |
| | 110 | }); |
| | 111 | |
| | 112 | test( 'replace() should not replace the escaped shortcodes', function() { |
| | 113 | var result; |
| | 114 | |
| | 115 | // Escaped |
| | 116 | result = wp.shortcode.replace( 'foo', 'this has the [[foo]] shortcode', shortcodeReplaceCallback ); |
| | 117 | equal( result, 'this has the [[foo]] shortcode', 'escaped foo not replaced' ); |
| | 118 | |
| | 119 | result = wp.shortcode.replace( 'foo', 'this has the [[foo param="bar"]] shortcode', shortcodeReplaceCallback ); |
| | 120 | equal( result, 'this has the [[foo param="bar"]] shortcode', 'escaped foo with params not replaced' ); |
| | 121 | |
| | 122 | result = wp.shortcode.replace( 'foo', 'this [foo] has the [[foo param="bar"]] shortcode escaped', shortcodeReplaceCallback ); |
| | 123 | equal( result, 'this bar has the [[foo param="bar"]] shortcode escaped', 'escaped foo with params not replaced but unescaped foo replaced' ); |
| | 124 | }); |
| | 125 | |
| | 126 | test( 'replace() should not replace the shortcode when it is an incomplete match', function() { |
| | 127 | var result; |
| | 128 | |
| | 129 | // Stubs |
| | 130 | result = wp.shortcode.replace( 'foo', 'this has the [foobar] shortcode', shortcodeReplaceCallback ); |
| | 131 | equal( result, 'this has the [foobar] shortcode', 'stub not replaced' ); |
| | 132 | |
| | 133 | result = wp.shortcode.replace( 'foobar', 'this has the [foo] shortcode', shortcodeReplaceCallback ); |
| | 134 | equal( result, 'this has the [foo] shortcode', 'stub not replaced' ); |
| | 135 | }); |
| | 136 | |
| | 137 | function shortcodeReplaceCallback( ) { |
| | 138 | return 'bar'; |
| | 139 | } |
| | 140 | }); |