template.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var templateParser = require('../../src/parse/template')
  2. var parse = templateParser.parse
  3. var testString = '<div>hello</div><p class="test">world</p>'
  4. describe('Template Parser', function () {
  5. it('should return same if argument is already a fragment', function () {
  6. var frag = document.createDocumentFragment()
  7. var res = parse(frag)
  8. expect(res).toBe(frag)
  9. })
  10. // only test template node if it works in the browser being tested.
  11. var templateNode = document.createElement('template')
  12. if (templateNode.content) {
  13. it('should return content if argument is a valid template node', function () {
  14. var res = parse(templateNode)
  15. expect(res).toBe(templateNode.content)
  16. })
  17. }
  18. it('should parse if argument is a template string', function () {
  19. var res = parse(testString)
  20. expect(res instanceof DocumentFragment).toBeTruthy()
  21. expect(res.childNodes.length).toBe(2)
  22. expect(res.querySelector('.test').textContent).toBe('world')
  23. })
  24. it('should parse textContent if argument is a script node', function () {
  25. var node = document.createElement('script')
  26. node.textContent = testString
  27. var res = parse(node)
  28. expect(res instanceof DocumentFragment).toBeTruthy()
  29. expect(res.childNodes.length).toBe(2)
  30. expect(res.querySelector('.test').textContent).toBe('world')
  31. })
  32. it('should parse innerHTML if argument is a normal node', function () {
  33. var node = document.createElement('div')
  34. node.innerHTML = testString
  35. var res = parse(node)
  36. expect(res instanceof DocumentFragment).toBeTruthy()
  37. expect(res.childNodes.length).toBe(2)
  38. expect(res.querySelector('.test').textContent).toBe('world')
  39. })
  40. it('should retrieve and parse if argument is an id selector', function () {
  41. var node = document.createElement('script')
  42. node.setAttribute('id', 'template-test')
  43. node.setAttribute('type', 'x/template')
  44. node.textContent = testString
  45. document.head.appendChild(node)
  46. var res = parse('#template-test')
  47. expect(res instanceof DocumentFragment).toBeTruthy()
  48. expect(res.childNodes.length).toBe(2)
  49. expect(res.querySelector('.test').textContent).toBe('world')
  50. document.head.removeChild(node)
  51. })
  52. it('should work for table elements', function () {
  53. var res = parse('<td>hello</td>')
  54. expect(res instanceof DocumentFragment).toBeTruthy()
  55. expect(res.childNodes.length).toBe(1)
  56. expect(res.firstChild.tagName).toBe('TD')
  57. expect(res.firstChild.textContent).toBe('hello')
  58. })
  59. it('should work for option elements', function () {
  60. var res = parse('<option>hello</option>')
  61. expect(res instanceof DocumentFragment).toBeTruthy()
  62. expect(res.childNodes.length).toBe(1)
  63. expect(res.firstChild.tagName).toBe('OPTION')
  64. expect(res.firstChild.textContent).toBe('hello')
  65. })
  66. it('should work for svg elements', function () {
  67. var res = parse('<circle></circle>')
  68. expect(res instanceof DocumentFragment).toBeTruthy()
  69. expect(res.childNodes.length).toBe(1)
  70. // SVG tagNames should be lowercase because they are XML nodes not HTML
  71. expect(res.firstChild.tagName).toBe('circle')
  72. expect(res.firstChild.namespaceURI).toBe('http://www.w3.org/2000/svg')
  73. })
  74. it('should cache template strings', function () {
  75. var res1 = parse(testString)
  76. var res2 = parse(testString)
  77. expect(res1).toBe(res2)
  78. })
  79. it('should cache id selectors', function () {
  80. var node = document.createElement('script')
  81. node.setAttribute('id', 'template-test')
  82. node.setAttribute('type', 'x/template')
  83. node.textContent = '<div>never seen before content</div>'
  84. document.head.appendChild(node)
  85. var res1 = parse('#template-test')
  86. var res2 = parse('#template-test')
  87. expect(res1).toBe(res2)
  88. document.head.removeChild(node)
  89. })
  90. })