shortcode.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* global wp, jQuery */
  2. jQuery( function() {
  3. QUnit.module( 'shortcode' );
  4. QUnit.test( 'next() should find the shortcode', function( assert ) {
  5. var result;
  6. // Basic.
  7. result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode' );
  8. assert.equal( result.index, 13, 'foo shortcode found at index 13' );
  9. result = wp.shortcode.next( 'foo', 'this has the [foo param="foo"] shortcode' );
  10. assert.equal( result.index, 13, 'foo shortcode with params found at index 13' );
  11. });
  12. QUnit.test( 'next() should not find shortcodes that are not there', function( assert ) {
  13. var result;
  14. // Not found.
  15. result = wp.shortcode.next( 'bar', 'this has the [foo] shortcode' );
  16. assert.equal( result, undefined, 'bar shortcode not found' );
  17. result = wp.shortcode.next( 'bar', 'this has the [foo param="bar"] shortcode' );
  18. assert.equal( result, undefined, 'bar shortcode not found with params' );
  19. });
  20. QUnit.test( 'next() should find the shortcode when told to start looking beyond the start of the string', function( assert ) {
  21. var result;
  22. // Starting at indices.
  23. result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 12 );
  24. assert.equal( result.index, 13, 'foo shortcode found before index 13' );
  25. result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 13 );
  26. assert.equal( result.index, 13, 'foo shortcode found at index 13' );
  27. result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 14 );
  28. assert.equal( result, undefined, 'foo shortcode not found after index 13' );
  29. });
  30. QUnit.test( 'next() should find the second instances of the shortcode when the starting indice is after the start of the first one', function( assert ) {
  31. var result;
  32. result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode [foo] twice', 14 );
  33. assert.equal( result.index, 29, 'foo shortcode found the second foo at index 29' );
  34. });
  35. QUnit.test( 'next() should not find escaped shortcodes', function( assert ) {
  36. var result;
  37. // Escaped.
  38. result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode' );
  39. assert.equal( result, undefined, 'foo shortcode not found when escaped' );
  40. result = wp.shortcode.next( 'foo', 'this has the [[foo param="foo"]] shortcode' );
  41. assert.equal( result, undefined, 'foo shortcode not found when escaped with params' );
  42. });
  43. QUnit.test( 'next() should find shortcodes that are incorrectly escaped by newlines', function( assert ) {
  44. var result;
  45. result = wp.shortcode.next( 'foo', 'this has the [\n[foo]] shortcode' );
  46. assert.equal( result.index, 15, 'shortcode found when incorrectly escaping the start of it' );
  47. result = wp.shortcode.next( 'foo', 'this has the [[foo]\n] shortcode' );
  48. assert.equal( result.index, 14, 'shortcode found when incorrectly escaping the end of it' );
  49. });
  50. QUnit.test( 'next() should still work when there are not equal ammounts of square brackets', function( assert ) {
  51. var result;
  52. result = wp.shortcode.next( 'foo', 'this has the [[foo] shortcode' );
  53. assert.equal( result.index, 14, 'shortcode found when there are offset square brackets' );
  54. result = wp.shortcode.next( 'foo', 'this has the [foo]] shortcode' );
  55. assert.equal( result.index, 13, 'shortcode found when there are offset square brackets' );
  56. });
  57. QUnit.test( 'next() should find the second instances of the shortcode when the first one is escaped', function( assert ) {
  58. var result;
  59. result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode [foo] twice' );
  60. assert.equal( result.index, 31, 'foo shortcode found the non-escaped foo at index 31' );
  61. });
  62. QUnit.test( 'next() should not find shortcodes that are not full matches', function( assert ) {
  63. var result;
  64. // Stubs.
  65. result = wp.shortcode.next( 'foo', 'this has the [foobar] shortcode' );
  66. assert.equal( result, undefined, 'stub does not trigger match' );
  67. result = wp.shortcode.next( 'foobar', 'this has the [foo] shortcode' );
  68. assert.equal( result, undefined, 'stub does not trigger match' );
  69. });
  70. QUnit.test( 'replace() should replace the shortcode', function( assert ) {
  71. var result;
  72. // Basic.
  73. result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode', shortcodeReplaceCallback );
  74. assert.equal( result, 'this has the bar shortcode', 'foo replaced with bar' );
  75. result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode', shortcodeReplaceCallback );
  76. assert.equal( result, 'this has the bar shortcode', 'foo and params replaced with bar' );
  77. });
  78. QUnit.test( 'replace() should not replace the shortcode when it does not match', function( assert ) {
  79. var result;
  80. // Not found.
  81. result = wp.shortcode.replace( 'bar', 'this has the [foo] shortcode', shortcodeReplaceCallback );
  82. assert.equal( result, 'this has the [foo] shortcode', 'bar not found' );
  83. result = wp.shortcode.replace( 'bar', 'this has the [foo param="bar"] shortcode', shortcodeReplaceCallback );
  84. assert.equal( result, 'this has the [foo param="bar"] shortcode', 'bar not found with params' );
  85. });
  86. QUnit.test( 'replace() should replace the shortcode in all instances of its use', function( assert ) {
  87. var result;
  88. // Multiple instances.
  89. result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode [foo] twice', shortcodeReplaceCallback );
  90. assert.equal( result, 'this has the bar shortcode bar twice', 'foo replaced with bar twice' );
  91. result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode [foo] twice', shortcodeReplaceCallback );
  92. assert.equal( result, 'this has the bar shortcode bar twice', 'foo and params replaced with bar twice' );
  93. });
  94. QUnit.test( 'replace() should not replace the escaped shortcodes', function( assert ) {
  95. var result;
  96. // Escaped.
  97. result = wp.shortcode.replace( 'foo', 'this has the [[foo]] shortcode', shortcodeReplaceCallback );
  98. assert.equal( result, 'this has the [[foo]] shortcode', 'escaped foo not replaced' );
  99. result = wp.shortcode.replace( 'foo', 'this has the [[foo param="bar"]] shortcode', shortcodeReplaceCallback );
  100. assert.equal( result, 'this has the [[foo param="bar"]] shortcode', 'escaped foo with params not replaced' );
  101. result = wp.shortcode.replace( 'foo', 'this [foo] has the [[foo param="bar"]] shortcode escaped', shortcodeReplaceCallback );
  102. assert.equal( result, 'this bar has the [[foo param="bar"]] shortcode escaped', 'escaped foo with params not replaced but unescaped foo replaced' );
  103. });
  104. QUnit.test( 'replace() should replace improperly escaped shortcodes that include newlines', function( assert ) {
  105. var result;
  106. result = wp.shortcode.replace( 'foo', 'this [foo] has the [[foo param="bar"]\n] shortcode ', shortcodeReplaceCallback );
  107. assert.equal( result, 'this bar has the [bar\n] shortcode ', 'escaping with newlines should not actually escape the content' );
  108. result = wp.shortcode.replace( 'foo', 'this [foo] has the [\n[foo param="bar"]] shortcode ', shortcodeReplaceCallback );
  109. assert.equal( result, 'this bar has the [\nbar] shortcode ', 'escaping with newlines should not actually escape the content' );
  110. });
  111. QUnit.test( 'replace() should not replace the shortcode when it is an incomplete match', function( assert ) {
  112. var result;
  113. // Stubs.
  114. result = wp.shortcode.replace( 'foo', 'this has the [foobar] shortcode', shortcodeReplaceCallback );
  115. assert.equal( result, 'this has the [foobar] shortcode', 'stub not replaced' );
  116. result = wp.shortcode.replace( 'foobar', 'this has the [foo] shortcode', shortcodeReplaceCallback );
  117. assert.equal( result, 'this has the [foo] shortcode', 'stub not replaced' );
  118. });
  119. /**
  120. * A callback function for the replace tests.
  121. */
  122. function shortcodeReplaceCallback( ) {
  123. return 'bar';
  124. }
  125. QUnit.test( 'attrs() should return named attributes created with single, double, and no quotes', function( assert ) {
  126. var expected = {
  127. 'named': {
  128. 'param': 'foo',
  129. 'another': 'bar',
  130. 'andagain': 'baz'
  131. }, 'numeric' : []
  132. };
  133. assert.deepEqual( wp.shortcode.attrs('param="foo" another=\'bar\' andagain=baz'), expected, 'attr parsed all three named types');
  134. });
  135. QUnit.test( 'attrs() should return numeric attributes in the order they are used', function( assert ) {
  136. var expected = {
  137. 'named': {}, 'numeric' : ['foo', 'bar', 'baz']
  138. };
  139. assert.deepEqual( wp.shortcode.attrs('foo bar baz'), expected, 'attr parsed numeric attributes');
  140. });
  141. QUnit.test( 'attrs() should return numeric attributes in the order they are used when they have named attributes in between', function( assert ) {
  142. var expected = {
  143. 'named': { 'not': 'a blocker' }, 'numeric' : ['foo', 'bar', 'baz']
  144. };
  145. assert.deepEqual( wp.shortcode.attrs('foo not="a blocker" bar baz'), expected, 'attr parsed numeric attributes');
  146. });
  147. QUnit.test( 'attrs() should return numeric attributes created with single, double, and no quotes', function( assert ) {
  148. var expected = {
  149. 'named': {}, 'numeric' : ['foo', 'bar', 'baz']
  150. };
  151. assert.deepEqual( wp.shortcode.attrs('foo "bar" \'baz\''), expected, 'attr parsed numeric attributes');
  152. });
  153. QUnit.test( 'attrs() should return mixed attributes created with single, double, and no quotes', function( assert ) {
  154. var expected = {
  155. 'named': { a: 'foo', b: 'bar', c: 'baz' }, 'numeric' : ['foo', 'bar', 'baz']
  156. };
  157. assert.deepEqual( wp.shortcode.attrs('a="foo" b=\'bar\' c=baz foo "bar" \'baz\''), expected, 'attr parsed numeric attributes');
  158. });
  159. QUnit.test( 'string() should accept attrs in any order', function( assert ) {
  160. var expected = '[short abc123 foo="bar"]';
  161. var result;
  162. result = wp.shortcode.string({
  163. tag : 'short',
  164. type : 'single',
  165. attrs : {
  166. named : { foo : 'bar' },
  167. numeric : [ 'abc123' ]
  168. }
  169. });
  170. assert.deepEqual( result, expected, 'attributes are accepted in any order' );
  171. result = wp.shortcode.string({
  172. tag : 'short',
  173. type : 'single',
  174. attrs : {
  175. numeric : [ 'abc123' ],
  176. named : { foo : 'bar' }
  177. }
  178. });
  179. assert.deepEqual( result, expected, 'attributes are accepted in any order' );
  180. });
  181. });