text-parser.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. describe('Text Parser', function () {
  2. var TextParser = require('vue/src/text-parser')
  3. describe('.parse()', function () {
  4. var tokens
  5. before(function () {
  6. tokens = TextParser.parse('hello {{a}}! {{ {bcd} }}{{d.e.f}} {{a + (b || c) ? d : e}} {{>test}}{{{ a + "<em>" }}}')
  7. })
  8. it('should return null if no interpolate tags are present', function () {
  9. var result = TextParser.parse('hello no tags')
  10. assert.strictEqual(result, null)
  11. })
  12. it('should ignore escaped tags', function () {
  13. var result = TextParser.parse('test {{key}} &#123;&#123;hello&#125;&#125;')
  14. assert.strictEqual(result.length, 3)
  15. assert.strictEqual(result[2], ' &#123;&#123;hello&#125;&#125;')
  16. })
  17. it('should extract correct amount of tokens', function () {
  18. assert.strictEqual(tokens.length, 10)
  19. })
  20. it('should extract plain strings', function () {
  21. assert.strictEqual(typeof tokens[0], 'string')
  22. assert.strictEqual(typeof tokens[2], 'string')
  23. assert.strictEqual(typeof tokens[5], 'string')
  24. assert.strictEqual(typeof tokens[7], 'string')
  25. })
  26. it('should extract basic keys', function () {
  27. assert.strictEqual(tokens[1].key, 'a')
  28. })
  29. it('should trim extracted keys', function () {
  30. assert.strictEqual(tokens[3].key, '{bcd}')
  31. })
  32. it('should extract nested keys', function () {
  33. assert.strictEqual(tokens[4].key, 'd.e.f')
  34. })
  35. it('should extract expressions', function () {
  36. assert.strictEqual(tokens[6].key, 'a + (b || c) ? d : e')
  37. })
  38. it('should extract partials', function () {
  39. assert.strictEqual(tokens[8].key, '>test')
  40. })
  41. it('should extract triple mustache (html instead of text)', function () {
  42. assert.strictEqual(tokens[9].key, 'a + "<em>"')
  43. assert.ok(tokens[9].html)
  44. })
  45. })
  46. describe('.parseAttr()', function () {
  47. it('should return Directive.build friendly expression', function () {
  48. assert.strictEqual(TextParser.parseAttr('{{msg}}'), 'msg')
  49. assert.strictEqual(TextParser.parseAttr('{{msg + "123"}}'), 'msg + "123"')
  50. assert.strictEqual(TextParser.parseAttr('ha {{msg + "123"}} ho'), '"ha "+(msg + "123")+" ho"')
  51. })
  52. it('should extract and inline any filters', function () {
  53. var res = TextParser.parseAttr('a {{msg | test}} b')
  54. assert.strictEqual(res, '"a "+(this.$compiler.getOption("filters", "test").call(this,msg))+" b"')
  55. })
  56. })
  57. })