text_spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var textParser = require('src/parsers/text')
  2. var dirParser = require('src/parsers/directive')
  3. var config = require('src/config')
  4. var testCases = [
  5. {
  6. // no tags
  7. text: 'foo',
  8. expected: null
  9. },
  10. {
  11. // basic
  12. text: 'a {{ a }} c',
  13. expected: [
  14. { value: 'a ' },
  15. { tag: true, value: 'a', html: false, oneTime: false },
  16. { value: ' c' }
  17. ]
  18. },
  19. {
  20. // html
  21. text: '{{ text }} and {{{ html }}}',
  22. expected: [
  23. { tag: true, value: 'text', html: false, oneTime: false },
  24. { value: ' and ' },
  25. { tag: true, value: 'html', html: true, oneTime: false }
  26. ]
  27. },
  28. {
  29. // one time
  30. text: '{{* text }} and {{{* html }}}',
  31. expected: [
  32. { tag: true, value: 'text', html: false, oneTime: true },
  33. { value: ' and ' },
  34. { tag: true, value: 'html', html: true, oneTime: true }
  35. ]
  36. },
  37. {
  38. text: '[{{abc}}]',
  39. expected: [
  40. { value: '[' },
  41. { tag: true, value: 'abc', html: false, oneTime: false },
  42. { value: ']' }
  43. ]
  44. },
  45. // multiline
  46. {
  47. text: '{{\n value \n}}',
  48. expected: [
  49. { tag: true, value: 'value', html: false, oneTime: false }
  50. ]
  51. },
  52. // multiline HTML
  53. {
  54. text: '{{{\n code \n}}}',
  55. expected: [
  56. { tag: true, value: 'code', html: true, oneTime: false }
  57. ]
  58. },
  59. // new lines preserved outside of tags
  60. {
  61. text: 'hello\n{{value}}\nworld',
  62. expected: [
  63. { value: 'hello\n' },
  64. { tag: true, value: 'value', html: false, oneTime: false },
  65. { value: '\nworld' }
  66. ]
  67. }
  68. ]
  69. function assertParse (test) {
  70. var res = textParser.parseText(test.text)
  71. var exp = test.expected
  72. if (!Array.isArray(exp)) {
  73. expect(res).toBe(exp)
  74. } else {
  75. expect(res.length).toBe(exp.length)
  76. res.forEach(function (r, i) {
  77. var e = exp[i]
  78. for (var key in e) {
  79. expect(e[key]).toEqual(r[key])
  80. }
  81. })
  82. }
  83. }
  84. describe('Text Parser', function () {
  85. it('parse', function () {
  86. testCases.forEach(assertParse)
  87. })
  88. it('cache', function () {
  89. var res1 = textParser.parseText('{{a}}')
  90. var res2 = textParser.parseText('{{a}}')
  91. expect(res1).toBe(res2)
  92. })
  93. it('custom delimiters', function () {
  94. config.delimiters = ['[%', '%]']
  95. config.unsafeDelimiters = ['{!!', '!!}']
  96. assertParse({
  97. text: '[%* text %] and {!! html !!}',
  98. expected: [
  99. { tag: true, value: 'text', html: false, oneTime: true },
  100. { value: ' and ' },
  101. { tag: true, value: 'html', html: true, oneTime: false }
  102. ]
  103. })
  104. config.delimiters = ['{{', '}}']
  105. config.unsafeDelimiters = ['{{{', '}}}']
  106. })
  107. it('tokens to expression', function () {
  108. var tokens = textParser.parseText('view-{{test + 1}}-test-{{ok + "|"}}')
  109. var exp = textParser.tokensToExp(tokens)
  110. expect(exp).toBe('"view-"+(test + 1)+"-test-"+(ok + "|")')
  111. })
  112. it('tokens to expression, single expression', function () {
  113. var tokens = textParser.parseText('{{test}}')
  114. var exp = textParser.tokensToExp(tokens)
  115. // should not have parens so it can be treated as a
  116. // simple path by the expression parser
  117. expect(exp).toBe('test')
  118. })
  119. it('tokens to expression with filters, multiple expressions', function () {
  120. var tokens = textParser.parseText('a {{b | c d | f}} e')
  121. var exp = textParser.tokensToExp(tokens)
  122. var filters = dirParser.parseDirective('b | c d | f').filters
  123. expect(exp).toBe(
  124. '"a "+this._applyFilters(b,null,' +
  125. JSON.stringify(filters) +
  126. ',false)+" e"')
  127. })
  128. })