template_spec.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. var _ = require('../../../../src/util')
  2. var templateParser = require('../../../../src/parse/template')
  3. var parse = templateParser.parse
  4. var testString = '<div>hello</div><p class="test">world</p>'
  5. if (_.inBrowser) {
  6. describe('Template Parser', function () {
  7. it('should return same if argument is already a fragment', function () {
  8. var frag = document.createDocumentFragment()
  9. var res = parse(frag)
  10. expect(res).toBe(frag)
  11. })
  12. it('should return content if argument is a valid template node', function () {
  13. var templateNode = document.createElement('template')
  14. if (!templateNode.content) {
  15. // mock the content
  16. templateNode.content = document.createDocumentFragment()
  17. }
  18. var res = parse(templateNode)
  19. expect(res).toBe(templateNode.content)
  20. })
  21. it('should parse if argument is a template string', function () {
  22. var res = parse(testString)
  23. expect(res instanceof DocumentFragment).toBeTruthy()
  24. expect(res.childNodes.length).toBe(2)
  25. expect(res.querySelector('.test').textContent).toBe('world')
  26. })
  27. it('should work if the template string doesn\'t contain tags', function () {
  28. var res = parse('hello!')
  29. expect(res instanceof DocumentFragment).toBeTruthy()
  30. expect(res.childNodes.length).toBe(1)
  31. expect(res.firstChild.nodeType).toBe(3) // Text node
  32. })
  33. it('should parse textContent if argument is a script node', function () {
  34. var node = document.createElement('script')
  35. node.textContent = testString
  36. var res = parse(node)
  37. expect(res instanceof DocumentFragment).toBeTruthy()
  38. expect(res.childNodes.length).toBe(2)
  39. expect(res.querySelector('.test').textContent).toBe('world')
  40. })
  41. it('should parse innerHTML if argument is a normal node', function () {
  42. var node = document.createElement('div')
  43. node.innerHTML = testString
  44. var res = parse(node)
  45. expect(res instanceof DocumentFragment).toBeTruthy()
  46. expect(res.childNodes.length).toBe(2)
  47. expect(res.querySelector('.test').textContent).toBe('world')
  48. })
  49. it('should retrieve and parse if argument is an id selector', function () {
  50. var node = document.createElement('script')
  51. node.setAttribute('id', 'template-test')
  52. node.setAttribute('type', 'x/template')
  53. node.textContent = testString
  54. document.head.appendChild(node)
  55. var res = parse('#template-test')
  56. expect(res instanceof DocumentFragment).toBeTruthy()
  57. expect(res.childNodes.length).toBe(2)
  58. expect(res.querySelector('.test').textContent).toBe('world')
  59. document.head.removeChild(node)
  60. })
  61. it('should work for table elements', function () {
  62. var res = parse('<td>hello</td>')
  63. expect(res instanceof DocumentFragment).toBeTruthy()
  64. expect(res.childNodes.length).toBe(1)
  65. expect(res.firstChild.tagName).toBe('TD')
  66. expect(res.firstChild.textContent).toBe('hello')
  67. })
  68. it('should work for option elements', function () {
  69. var res = parse('<option>hello</option>')
  70. expect(res instanceof DocumentFragment).toBeTruthy()
  71. expect(res.childNodes.length).toBe(1)
  72. expect(res.firstChild.tagName).toBe('OPTION')
  73. expect(res.firstChild.textContent).toBe('hello')
  74. })
  75. it('should work for svg elements', function () {
  76. var res = parse('<circle></circle>')
  77. expect(res instanceof DocumentFragment).toBeTruthy()
  78. expect(res.childNodes.length).toBe(1)
  79. // SVG tagNames should be lowercase because they are XML nodes not HTML
  80. expect(res.firstChild.tagName).toBe('circle')
  81. expect(res.firstChild.namespaceURI).toBe('http://www.w3.org/2000/svg')
  82. })
  83. it('should cache template strings', function () {
  84. var res1 = parse(testString)
  85. var res2 = parse(testString)
  86. expect(res1).toBe(res2)
  87. })
  88. it('should clone', function () {
  89. var res1 = parse(testString, true)
  90. var res2 = parse(testString, true)
  91. expect(res1).not.toBe(res2)
  92. })
  93. it('should cache id selectors', function () {
  94. var node = document.createElement('script')
  95. node.setAttribute('id', 'template-test')
  96. node.setAttribute('type', 'x/template')
  97. node.textContent = '<div>never seen before content</div>'
  98. document.head.appendChild(node)
  99. var res1 = parse('#template-test')
  100. var res2 = parse('#template-test')
  101. expect(res1).toBe(res2)
  102. document.head.removeChild(node)
  103. })
  104. it('should deal with Safari template clone bug', function () {
  105. var a = document.createElement('div')
  106. a.innerHTML = '<template>1</template>'
  107. var c = templateParser.clone(a)
  108. expect(a.firstChild.innerHTML).toBe('1')
  109. })
  110. it('should deal with IE textarea clone bug', function () {
  111. var t = document.createElement('textarea')
  112. t.placeholder = 't'
  113. var c = templateParser.clone(t)
  114. expect(c.value).toBe('')
  115. })
  116. })
  117. }