text_spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var textParser = require('../../../../src/parsers/text')
  2. var config = require('../../../../src/config')
  3. var Vue = require('../../../../src/vue')
  4. var testCases = [
  5. {
  6. // no tags
  7. text: 'haha',
  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. ]
  46. function assertParse (test) {
  47. var res = textParser.parse(test.text)
  48. var exp = test.expected
  49. if (!Array.isArray(exp)) {
  50. expect(res).toBe(exp)
  51. } else {
  52. expect(res.length).toBe(exp.length)
  53. res.forEach(function (r, i) {
  54. var e = exp[i]
  55. for (var key in e) {
  56. expect(e[key]).toEqual(r[key])
  57. }
  58. })
  59. }
  60. }
  61. describe('Text Parser', function () {
  62. it('parse', function () {
  63. testCases.forEach(assertParse)
  64. })
  65. it('cache', function () {
  66. var res1 = textParser.parse('{{a}}')
  67. var res2 = textParser.parse('{{a}}')
  68. expect(res1).toBe(res2)
  69. })
  70. it('custom delimiters', function () {
  71. config.delimiters = ['[%', '%]']
  72. assertParse({
  73. text: '[%* text %] and [[% html %]]',
  74. expected: [
  75. { tag: true, value: 'text', html: false, oneTime: true },
  76. { value: ' and ' },
  77. { tag: true, value: 'html', html: true, oneTime: false },
  78. ]
  79. })
  80. config.delimiters = ['{{', '}}']
  81. })
  82. it('tokens to expression', function () {
  83. var tokens = textParser.parse('view-{{test + 1}}-test-{{ok + "|"}}')
  84. var exp = textParser.tokensToExp(tokens)
  85. expect(exp).toBe('"view-"+(test + 1)+"-test-"+(ok + "|")')
  86. })
  87. it('tokens to expression, single expression', function () {
  88. var tokens = textParser.parse('{{test}}')
  89. var exp = textParser.tokensToExp(tokens)
  90. // should not have parens so it can be treated as a
  91. // simple path by the expression parser
  92. expect(exp).toBe('test')
  93. })
  94. it('tokens to expression with oneTime tags & vm', function () {
  95. var vm = new Vue({
  96. data: { test: 'a', ok: 'b' }
  97. })
  98. var tokens = textParser.parse('view-{{*test}}-test-{{ok}}')
  99. var exp = textParser.tokensToExp(tokens, vm)
  100. expect(exp).toBe('"view-"+"a"+"-test-"+(ok)')
  101. })
  102. it('tokens to expression with filters, multiple expressions', function () {
  103. var tokens = textParser.parse('a {{b | c d | f}} e')
  104. var exp = textParser.tokensToExp(tokens)
  105. expect(exp).toBe(
  106. '"a "+this._applyFilter("f",[this._applyFilter("c",[b,' +
  107. JSON.stringify({value:'d',dynamic:true}) +
  108. '])])+" e"')
  109. })
  110. })