shortcode.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* global wp, jQuery */
  2. jQuery( function() {
  3. module( 'shortcode' );
  4. test( 'next() should find the shortcode', function() {
  5. var result;
  6. // Basic
  7. result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode' );
  8. equal( result.index, 13, 'foo shortcode found at index 13' );
  9. result = wp.shortcode.next( 'foo', 'this has the [foo param="foo"] shortcode' );
  10. equal( result.index, 13, 'foo shortcode with params found at index 13' );
  11. });
  12. test( 'next() should not shortcodes that are not there', function() {
  13. var result;
  14. // Not found
  15. result = wp.shortcode.next( 'bar', 'this has the [foo] shortcode' );
  16. equal( result, undefined, 'bar shortcode not found' );
  17. result = wp.shortcode.next( 'bar', 'this has the [foo param="bar"] shortcode' );
  18. equal( result, undefined, 'bar shortcode not found with params' );
  19. });
  20. test( 'next() should find the shortcode when told to start looking beyond the start of the string', function() {
  21. var result;
  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. result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 13 );
  26. equal( result.index, 13, 'foo shortcode found at index 13' );
  27. result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 14 );
  28. equal( result, undefined, 'foo shortcode not found after index 13' );
  29. });
  30. test( 'next() should find the second instances of the shortcode when the starting indice is after the start of the first one', function() {
  31. var result;
  32. result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode [foo] twice', 14 );
  33. equal( result.index, 29, 'foo shortcode found the second foo at index 29' );
  34. });
  35. test( 'next() should not find escaped shortcodes', function() {
  36. var result;
  37. // Escaped
  38. result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode' );
  39. equal( result, undefined, 'foo shortcode not found when escaped' );
  40. result = wp.shortcode.next( 'foo', 'this has the [[foo param="foo"]] shortcode' );
  41. equal( result, undefined, 'foo shortcode not found when escaped with params' );
  42. });
  43. test( 'next() should find the second instances of the shortcode when the first one is escaped', function() {
  44. var result;
  45. result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode [foo] twice' );
  46. equal( result.index, 31, 'foo shortcode found the non-escaped foo at index 31' );
  47. });
  48. test( 'next() should not find shortcodes that are not full matches', function() {
  49. var result;
  50. // Stubs
  51. result = wp.shortcode.next( 'foo', 'this has the [foobar] shortcode' );
  52. equal( result, undefined, 'stub does not trigger match' );
  53. result = wp.shortcode.next( 'foobar', 'this has the [foo] shortcode' );
  54. equal( result, undefined, 'stub does not trigger match' );
  55. });
  56. test( 'replace() should replace the shortcode', function() {
  57. var result;
  58. // Basic
  59. result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode', shortcodeReplaceCallback );
  60. equal( result, 'this has the bar shortcode', 'foo replaced with bar' );
  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. test( 'replace() should not replace the shortcode when it does not match', function() {
  65. var result;
  66. // Not found
  67. result = wp.shortcode.replace( 'bar', 'this has the [foo] shortcode', shortcodeReplaceCallback );
  68. equal( result, 'this has the [foo] shortcode', 'bar not found' );
  69. result = wp.shortcode.replace( 'bar', 'this has the [foo param="bar"] shortcode', shortcodeReplaceCallback );
  70. equal( result, 'this has the [foo param="bar"] shortcode', 'bar not found with params' );
  71. });
  72. test( 'replace() should replace the shortcode in all instances of its use', function() {
  73. var result;
  74. // Multiple instances
  75. result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode [foo] twice', shortcodeReplaceCallback );
  76. equal( result, 'this has the bar shortcode bar twice', 'foo replaced with bar twice' );
  77. result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode [foo] twice', shortcodeReplaceCallback );
  78. equal( result, 'this has the bar shortcode bar twice', 'foo and params replaced with bar twice' );
  79. });
  80. test( 'replace() should not replace the escaped shortcodes', function() {
  81. var result;
  82. // Escaped
  83. result = wp.shortcode.replace( 'foo', 'this has the [[foo]] shortcode', shortcodeReplaceCallback );
  84. equal( result, 'this has the [[foo]] shortcode', 'escaped foo not replaced' );
  85. result = wp.shortcode.replace( 'foo', 'this has the [[foo param="bar"]] shortcode', shortcodeReplaceCallback );
  86. equal( result, 'this has the [[foo param="bar"]] shortcode', 'escaped foo with params not replaced' );
  87. result = wp.shortcode.replace( 'foo', 'this [foo] has the [[foo param="bar"]] shortcode escaped', shortcodeReplaceCallback );
  88. equal( result, 'this bar has the [[foo param="bar"]] shortcode escaped', 'escaped foo with params not replaced but unescaped foo replaced' );
  89. });
  90. test( 'replace() should not replace the shortcode when it is an incomplete match', function() {
  91. var result;
  92. // Stubs
  93. result = wp.shortcode.replace( 'foo', 'this has the [foobar] shortcode', shortcodeReplaceCallback );
  94. equal( result, 'this has the [foobar] shortcode', 'stub not replaced' );
  95. result = wp.shortcode.replace( 'foobar', 'this has the [foo] shortcode', shortcodeReplaceCallback );
  96. equal( result, 'this has the [foo] shortcode', 'stub not replaced' );
  97. });
  98. // A callback function for the replace tests
  99. function shortcodeReplaceCallback( ) {
  100. return 'bar';
  101. }
  102. test( 'attrs() should return named attributes created with single, double, and no quotes', function() {
  103. var expected = {
  104. 'named': {
  105. 'param': 'foo',
  106. 'another': 'bar',
  107. 'andagain': 'baz'
  108. }, 'numeric' : []
  109. };
  110. deepEqual( wp.shortcode.attrs('param="foo" another=\'bar\' andagain=baz'), expected, 'attr parsed all three named types');
  111. });
  112. test( 'attrs() should return numeric attributes in the order they are used', function() {
  113. var expected = {
  114. 'named': {}, 'numeric' : ['foo', 'bar', 'baz']
  115. };
  116. deepEqual( wp.shortcode.attrs('foo bar baz'), expected, 'attr parsed numeric attributes');
  117. });
  118. test( 'attrs() should return numeric attributes in the order they are used when they have named attributes in between', function() {
  119. var expected = {
  120. 'named': { 'not': 'a blocker' }, 'numeric' : ['foo', 'bar', 'baz']
  121. };
  122. deepEqual( wp.shortcode.attrs('foo not="a blocker" bar baz'), expected, 'attr parsed numeric attributes');
  123. });
  124. });