template_spec.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. var _ = require('../../../../src/util')
  2. var templateParser = require('../../../../src/parsers/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 handle string that contains html entities', function () {
  34. var res = parse('hi&lt;hi')
  35. expect(res instanceof DocumentFragment).toBeTruthy()
  36. expect(res.childNodes.length).toBe(1)
  37. expect(res.firstChild.nodeValue).toBe('hi<hi')
  38. })
  39. it('should parse textContent if argument is a script node', function () {
  40. var node = document.createElement('script')
  41. node.textContent = testString
  42. var res = parse(node)
  43. expect(res instanceof DocumentFragment).toBeTruthy()
  44. expect(res.childNodes.length).toBe(2)
  45. expect(res.querySelector('.test').textContent).toBe('world')
  46. })
  47. it('should parse innerHTML if argument is a normal node', function () {
  48. var node = document.createElement('div')
  49. node.innerHTML = testString
  50. var res = parse(node)
  51. expect(res instanceof DocumentFragment).toBeTruthy()
  52. expect(res.childNodes.length).toBe(2)
  53. expect(res.querySelector('.test').textContent).toBe('world')
  54. })
  55. it('should retrieve and parse if argument is an id selector', function () {
  56. var node = document.createElement('script')
  57. node.setAttribute('id', 'template-test')
  58. node.setAttribute('type', 'x/template')
  59. node.textContent = testString
  60. document.head.appendChild(node)
  61. var res = parse('#template-test')
  62. expect(res instanceof DocumentFragment).toBeTruthy()
  63. expect(res.childNodes.length).toBe(2)
  64. expect(res.querySelector('.test').textContent).toBe('world')
  65. document.head.removeChild(node)
  66. })
  67. it('should work for table elements', function () {
  68. var res = parse('<td>hello</td>')
  69. expect(res instanceof DocumentFragment).toBeTruthy()
  70. expect(res.childNodes.length).toBe(1)
  71. expect(res.firstChild.tagName).toBe('TD')
  72. expect(res.firstChild.textContent).toBe('hello')
  73. })
  74. it('should work for option elements', function () {
  75. var res = parse('<option>hello</option>')
  76. expect(res instanceof DocumentFragment).toBeTruthy()
  77. expect(res.childNodes.length).toBe(1)
  78. expect(res.firstChild.tagName).toBe('OPTION')
  79. expect(res.firstChild.textContent).toBe('hello')
  80. })
  81. it('should work for svg elements', function () {
  82. var res = parse('<circle></circle>')
  83. expect(res instanceof DocumentFragment).toBeTruthy()
  84. expect(res.childNodes.length).toBe(1)
  85. // SVG tagNames should be lowercase because they are XML nodes not HTML
  86. expect(res.firstChild.tagName).toBe('circle')
  87. expect(res.firstChild.namespaceURI).toBe('http://www.w3.org/2000/svg')
  88. })
  89. it('should cache template strings', function () {
  90. var res1 = parse(testString)
  91. var res2 = parse(testString)
  92. expect(res1).toBe(res2)
  93. })
  94. it('should clone', function () {
  95. var res1 = parse(testString, true)
  96. var res2 = parse(testString, true)
  97. expect(res1).not.toBe(res2)
  98. })
  99. it('should cache id selectors', function () {
  100. var node = document.createElement('script')
  101. node.setAttribute('id', 'template-test')
  102. node.setAttribute('type', 'x/template')
  103. node.textContent = '<div>never seen before content</div>'
  104. document.head.appendChild(node)
  105. var res1 = parse('#template-test')
  106. var res2 = parse('#template-test')
  107. expect(res1).toBe(res2)
  108. document.head.removeChild(node)
  109. })
  110. it('should be able to not use id selectors', function () {
  111. var res = parse('#hi', false, true)
  112. expect(res instanceof DocumentFragment).toBeTruthy()
  113. expect(res.firstChild.nodeValue).toBe('#hi')
  114. })
  115. it('should deal with Safari template clone bug', function () {
  116. var a = document.createElement('div')
  117. a.innerHTML = '<template>1</template>'
  118. var c = templateParser.clone(a)
  119. expect(a.firstChild.innerHTML).toBe('1')
  120. })
  121. it('should deal with IE textarea clone bug', function () {
  122. var t = document.createElement('textarea')
  123. t.placeholder = 't'
  124. var c = templateParser.clone(t)
  125. expect(c.value).toBe('')
  126. })
  127. })
  128. }