optimizer.spec.js 7.0 KB

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