optimizer.spec.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { parse } from 'compiler/parser/index'
  2. import { optimize } from 'compiler/optimizer'
  3. import directives from 'web/compiler/directives/index'
  4. import { isReservedTag, isUnaryTag, mustUseProp, getTagNamespace } from 'web/util/index'
  5. describe('optimizer', () => {
  6. const baseOptions = {
  7. expectHTML: true,
  8. preserveWhitespace: true,
  9. directives,
  10. isReservedTag,
  11. isUnaryTag,
  12. mustUseProp,
  13. getTagNamespace
  14. }
  15. it('simple', () => {
  16. const ast = parse('<h1 id="section1">hello world</h1>', baseOptions)
  17. optimize(ast, baseOptions)
  18. expect(ast.static).toBe(true) // h1
  19. expect(ast.staticRoot).toBe(true)
  20. expect(ast.children[0].static).toBe(true) // text node
  21. })
  22. it('interpolation', () => {
  23. const ast = parse('<h1>{{msg}}</h1>', baseOptions)
  24. optimize(ast, baseOptions)
  25. expect(ast.static).toBe(false) // h1
  26. expect(ast.children[0].static).toBe(false) // text node with interpolation
  27. })
  28. it('nested elements', () => {
  29. const ast = parse('<ul><li>hello</li><li>world</li></ul>', baseOptions)
  30. optimize(ast, baseOptions)
  31. // ul
  32. expect(ast.static).toBe(true)
  33. expect(ast.staticRoot).toBe(true)
  34. // li
  35. expect(ast.children[0].static).toBe(true) // first
  36. expect(ast.children[1].static).toBe(true) // second
  37. // text node inside li
  38. expect(ast.children[0].children[0].static).toBe(true) // first
  39. expect(ast.children[1].children[0].static).toBe(true) // second
  40. })
  41. it('nested complex elements', () => {
  42. const ast = parse('<ul><li>{{msg1}}</li><li>---</li><li>{{msg2}}</li></ul>', baseOptions)
  43. optimize(ast, baseOptions)
  44. // ul
  45. expect(ast.static).toBe(false) // ul
  46. // li
  47. expect(ast.children[0].static).toBe(false) // firts
  48. expect(ast.children[1].static).toBe(true) // second
  49. expect(ast.children[2].static).toBe(false) // third
  50. // text node inside li
  51. expect(ast.children[0].children[0].static).toBe(false) // first
  52. expect(ast.children[1].children[0].static).toBe(true) // second
  53. expect(ast.children[2].children[0].static).toBe(false) // third
  54. })
  55. it('v-if directive', () => {
  56. const ast = parse('<h1 id="section1" v-if="show">hello world</h1>', baseOptions)
  57. optimize(ast, baseOptions)
  58. expect(ast.static).toBe(false)
  59. expect(ast.children[0].static).toBe(true)
  60. })
  61. it('v-else directive', () => {
  62. const ast = parse('<div><p v-if="show">hello world</p><p v-else>foo bar</p></div>', baseOptions)
  63. optimize(ast, baseOptions)
  64. expect(ast.static).toBe(false)
  65. expect(ast.children[0].static).toBe(false)
  66. expect(ast.children[0].elseBlock.static).toBeUndefined()
  67. })
  68. it('v-pre directive', () => {
  69. const ast = parse('<ul v-pre><li>{{msg}}</li><li>world</li></ul>', baseOptions)
  70. optimize(ast, baseOptions)
  71. expect(ast.static).toBe(true)
  72. expect(ast.staticRoot).toBe(true)
  73. expect(ast.children[0].static).toBe(true)
  74. expect(ast.children[1].static).toBe(true)
  75. expect(ast.children[0].children[0].static).toBe(true)
  76. expect(ast.children[1].children[0].static).toBe(true)
  77. })
  78. it('v-for directive', () => {
  79. const ast = parse('<ul><li v-for="item in items">hello world {{$index}}</li></ul>', baseOptions)
  80. optimize(ast, baseOptions)
  81. // ul
  82. expect(ast.static).toBe(false)
  83. // li with v-for
  84. expect(ast.children[0].static).toBe(false)
  85. expect(ast.children[0].children[0].static).toBe(false)
  86. })
  87. it('v-once directive', () => {
  88. const ast = parse('<p v-once>{{msg}}</p>', baseOptions)
  89. optimize(ast, baseOptions)
  90. expect(ast.static).toBe(false) // p
  91. expect(ast.children[0].static).toBe(false) // text node
  92. })
  93. it('render tag', () => {
  94. const ast = parse('<render :method="onRender"><p>hello</p></render>', baseOptions)
  95. optimize(ast, baseOptions)
  96. expect(ast.static).toBe(false)
  97. expect(ast.children[0].static).toBe(true)
  98. expect(ast.children[0].children[0].static).toBe(true)
  99. })
  100. it('single slot', () => {
  101. const ast = parse('<slot>hello</slot>', baseOptions)
  102. optimize(ast, baseOptions)
  103. expect(ast.static).toBe(false) // slot
  104. expect(ast.children[0].static).toBe(true) // text node
  105. })
  106. it('named slot', () => {
  107. const ast = parse('<slot name="one">hello world</slot>', baseOptions)
  108. optimize(ast, baseOptions)
  109. expect(ast.static).toBe(false) // slot
  110. expect(ast.children[0].static).toBe(true) // text node
  111. })
  112. it('slot target', () => {
  113. const ast = parse('<p slot="one">hello world</p>', baseOptions)
  114. optimize(ast, baseOptions)
  115. expect(ast.static).toBe(false) // slot
  116. expect(ast.children[0].static).toBe(true) // text node
  117. })
  118. it('component', () => {
  119. const ast = parse('<my-component></my-component>', baseOptions)
  120. optimize(ast, baseOptions)
  121. expect(ast.static).toBe(false) // component
  122. })
  123. it('component for inline-template', () => {
  124. const ast = parse('<my-component inline-template><p>hello world</p><p>{{msg}}</p></my-component>', baseOptions)
  125. optimize(ast, baseOptions)
  126. // component
  127. expect(ast.static).toBe(false) // component
  128. // p
  129. expect(ast.children[0].static).toBe(true) // first
  130. expect(ast.children[1].static).toBe(false) // second
  131. // text node inside p
  132. expect(ast.children[0].children[0].static).toBe(true) // first
  133. expect(ast.children[1].children[0].static).toBe(false) // second
  134. })
  135. it('class binding', () => {
  136. const ast = parse('<p :class="class1">hello world</p>', baseOptions)
  137. optimize(ast, baseOptions)
  138. expect(ast.static).toBe(false)
  139. expect(ast.children[0].static).toBe(true)
  140. })
  141. it('style binding', () => {
  142. const ast = parse('<p :style="error">{{msg}}</p>', baseOptions)
  143. optimize(ast, baseOptions)
  144. expect(ast.static).toBe(false)
  145. expect(ast.children[0].static).toBe(false)
  146. })
  147. it('transition', () => {
  148. const ast = parse('<p v-if="show" transition="expand">hello world</p>', baseOptions)
  149. optimize(ast, baseOptions)
  150. expect(ast.static).toBe(false)
  151. expect(ast.children[0].static).toBe(true)
  152. })
  153. it('v-bind directive', () => {
  154. const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
  155. optimize(ast, baseOptions)
  156. expect(ast.static).toBe(false)
  157. })
  158. it('v-ref directive', () => {
  159. const ast = parse('<p v-ref:foo>hello world</p>', baseOptions)
  160. optimize(ast, baseOptions)
  161. expect(ast.static).toBe(false)
  162. expect(ast.children[0].static).toBe(true)
  163. })
  164. it('v-on directive', () => {
  165. const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
  166. optimize(ast, baseOptions)
  167. expect(ast.static).toBe(false)
  168. })
  169. it('custom directive', () => {
  170. const ast = parse('<fom><input type="text" name="field1" :value="msg" v-validate:field1="required"></form>', baseOptions)
  171. optimize(ast, baseOptions)
  172. expect(ast.static).toBe(false)
  173. expect(ast.children[0].static).toBe(false)
  174. })
  175. it('not root ast', () => {
  176. const ast = null
  177. optimize(ast, baseOptions)
  178. expect(ast).toBe(null)
  179. })
  180. it('not specified isReservedTag option', () => {
  181. const ast = parse('<h1 id="section1">hello world</h1>', baseOptions)
  182. optimize(ast, {})
  183. expect(ast.static).toBe(false)
  184. })
  185. })