parser.spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { parse } from 'compiler/parser/index'
  2. import modules from 'web/compiler/modules/index'
  3. import directives from 'web/compiler/directives/index'
  4. import { extend } from 'shared/util'
  5. import { baseOptions } from 'entries/web-compiler'
  6. describe('parser', () => {
  7. it('simple element', () => {
  8. const ast = parse('<h1>hello world</h1>', baseOptions)
  9. expect(ast.tag).toBe('h1')
  10. expect(ast.plain).toBe(true)
  11. expect(ast.children[0].text).toBe('hello world')
  12. })
  13. it('interpolation in element', () => {
  14. const ast = parse('<h1>{{msg}}</h1>', baseOptions)
  15. expect(ast.tag).toBe('h1')
  16. expect(ast.plain).toBe(true)
  17. expect(ast.children[0].expression).toBe('_s(msg)')
  18. })
  19. it('child elements', () => {
  20. const ast = parse('<ul><li>hello world</li></ul>', baseOptions)
  21. expect(ast.tag).toBe('ul')
  22. expect(ast.plain).toBe(true)
  23. expect(ast.children[0].tag).toBe('li')
  24. expect(ast.children[0].plain).toBe(true)
  25. expect(ast.children[0].children[0].text).toBe('hello world')
  26. expect(ast.children[0].parent).toBe(ast)
  27. })
  28. it('unary element', () => {
  29. const ast = parse('<hr>', baseOptions)
  30. expect(ast.tag).toBe('hr')
  31. expect(ast.plain).toBe(true)
  32. expect(ast.children.length).toBe(0)
  33. })
  34. it('svg element', () => {
  35. const ast = parse('<svg><text>hello world</text></svg>', baseOptions)
  36. expect(ast.tag).toBe('svg')
  37. expect(ast.ns).toBe('svg')
  38. expect(ast.plain).toBe(true)
  39. expect(ast.children[0].tag).toBe('text')
  40. expect(ast.children[0].children[0].text).toBe('hello world')
  41. expect(ast.children[0].parent).toBe(ast)
  42. })
  43. it('camelCase element', () => {
  44. const ast = parse('<MyComponent><p>hello world</p></MyComponent>', baseOptions)
  45. expect(ast.tag).toBe('my-component')
  46. expect(ast.plain).toBe(true)
  47. expect('Found camelCase tag in template').toHaveBeenWarned()
  48. expect(ast.children[0].tag).toBe('p')
  49. expect(ast.children[0].plain).toBe(true)
  50. expect(ast.children[0].children[0].text).toBe('hello world')
  51. expect(ast.children[0].parent).toBe(ast)
  52. })
  53. it('forbidden element', () => {
  54. // style
  55. const styleAst = parse('<style>error { color: red; }</style>', baseOptions)
  56. expect(styleAst.tag).toBe('style')
  57. expect(styleAst.plain).toBe(true)
  58. expect(styleAst.forbidden).toBe(true)
  59. expect(styleAst.children[0].text).toBe('error { color: red; }')
  60. expect('Templates should only be responsbile for mapping the state').toHaveBeenWarned()
  61. // script
  62. const scriptAst = parse('<script type="text/javascript">alert("hello world!")</script>', baseOptions)
  63. expect(scriptAst.tag).toBe('script')
  64. expect(scriptAst.plain).toBe(false)
  65. expect(scriptAst.forbidden).toBe(true)
  66. expect(scriptAst.children[0].text).toBe('alert("hello world!")')
  67. expect('Templates should only be responsbile for mapping the state').toHaveBeenWarned()
  68. })
  69. it('not contain root element', () => {
  70. parse('hello world', baseOptions)
  71. expect('Component template should contain exactly one root element').toHaveBeenWarned()
  72. })
  73. it('v-pre directive', () => {
  74. const ast = parse('<div v-pre id="message1"><p>{{msg}}</p></div>', baseOptions)
  75. expect(ast.pre).toBe(true)
  76. expect(ast.attrs[0].name).toBe('id')
  77. expect(ast.attrs[0].value).toBe('"message1"')
  78. expect(ast.children[0].children[0].text).toBe('{{msg}}')
  79. })
  80. it('v-for directive basic syntax', () => {
  81. const ast = parse('<ul><li v-for="item in items"></li><ul>', baseOptions)
  82. const liAst = ast.children[0]
  83. expect(liAst.for).toBe('items')
  84. expect(liAst.alias).toBe('item')
  85. })
  86. it('v-for directive iteration syntax', () => {
  87. const ast = parse('<ul><li v-for="(index, item) in items"></li><ul>', baseOptions)
  88. const liAst = ast.children[0]
  89. expect(liAst.for).toBe('items')
  90. expect(liAst.alias).toBe('item')
  91. expect(liAst.iterator).toBe('index')
  92. })
  93. it('v-for directive track-by', () => {
  94. const ast = parse('<ul><li v-for="item in items" track-by="item.uid"></li><ul>', baseOptions)
  95. const liAst = ast.children[0]
  96. expect(liAst.for).toBe('items')
  97. expect(liAst.alias).toBe('item')
  98. expect(liAst.key).toBe('item.uid')
  99. })
  100. it('v-for directive invalid syntax', () => {
  101. parse('<ul><li v-for="item into items"></li><ul>', baseOptions)
  102. expect('Invalid v-for expression').toHaveBeenWarned()
  103. })
  104. it('v-if directive syntax', () => {
  105. const ast = parse('<p v-if="show">hello world</p>', baseOptions)
  106. expect(ast.if).toBe('show')
  107. })
  108. it('v-else directive syntax', () => {
  109. const ast = parse('<div><p v-if="show">hello</p><p v-else>world</p></div>', baseOptions)
  110. const ifAst = ast.children[0]
  111. const elseAst = ifAst.elseBlock
  112. expect(elseAst.else).toBe(true)
  113. expect(elseAst.children[0].text).toBe('world')
  114. expect(elseAst.parent).toBe(ast)
  115. })
  116. it('v-else directive invalid syntax', () => {
  117. parse('<div><p v-else>world</p></div>', baseOptions)
  118. expect('v-else used on element').toHaveBeenWarned()
  119. })
  120. it('v-once directive syntax', () => {
  121. const ast = parse('<p v-once>world</p>', baseOptions)
  122. expect(ast.once).toBe(true)
  123. })
  124. it('render tag syntax', () => {
  125. const ast = parse('<render :method="onRender", :args="params"></render>', baseOptions)
  126. expect(ast.render).toBe(true)
  127. expect(ast.renderMethod).toBe('onRender')
  128. expect(ast.renderArgs).toBe('params')
  129. })
  130. it('render tag invalid syntax', () => {
  131. // method nothing
  132. const invalidAst1 = parse('<render></render>', baseOptions)
  133. expect('method attribute is required on <render>.').toHaveBeenWarned()
  134. expect(invalidAst1.render).toBe(true)
  135. expect(invalidAst1.renderMethod).toBeUndefined()
  136. expect(invalidAst1.renderArgs).toBeUndefined()
  137. // method no dynamic binding
  138. parse('<render method="onRender"></render>', baseOptions)
  139. expect('<render> method should use a dynamic binding').toHaveBeenWarned()
  140. // args no dynamic binding
  141. parse('<render :method="onRender" args="params"></render>', baseOptions)
  142. expect('<render> args should use a dynamic binding').toHaveBeenWarned()
  143. })
  144. it('slot tag single syntax', () => {
  145. const ast = parse('<slot></slot>', baseOptions)
  146. expect(ast.tag).toBe('slot')
  147. expect(ast.slotName).toBeUndefined()
  148. })
  149. it('slot tag namped syntax', () => {
  150. const ast = parse('<slot name="one">hello world</slot>', baseOptions)
  151. expect(ast.tag).toBe('slot')
  152. expect(ast.slotName).toBe('"one"')
  153. })
  154. it('slot target', () => {
  155. const ast = parse('<p slot="one">hello world</p>', baseOptions)
  156. expect(ast.slotTarget).toBe('"one"')
  157. })
  158. it('component properties', () => {
  159. const ast = parse('<my-component :msg="hello"></my-component>', baseOptions)
  160. expect(ast.attrs[0].name).toBe('msg')
  161. expect(ast.attrs[0].value).toBe('hello')
  162. })
  163. it('component "is" attribute', () => {
  164. const ast = parse('<my-component is="component1"></my-component>', baseOptions)
  165. expect(ast.component).toBe('"component1"')
  166. })
  167. it('component "inline-template" attribute', () => {
  168. const ast = parse('<my-component inline-template>hello world</my-component>', baseOptions)
  169. expect(ast.inlineTemplate).toBe(true)
  170. })
  171. it('class binding', () => {
  172. // static
  173. const ast1 = parse('<p class="class1">hello world</p>', baseOptions)
  174. expect(ast1.staticClass).toBe('"class1"')
  175. // dynamic
  176. const ast2 = parse('<p :class="class1">hello world</p>', baseOptions)
  177. expect(ast2.classBinding).toBe('class1')
  178. // interpolation warning
  179. parse('<p class="{{error}}">hello world</p>', baseOptions)
  180. expect('Interpolation inside attributes has been deprecated').toHaveBeenWarned()
  181. })
  182. it('style binding', () => {
  183. const ast = parse('<p :style="error">hello world</p>', baseOptions)
  184. expect(ast.styleBinding).toBe('error')
  185. })
  186. it('transition', () => {
  187. const ast = parse('<p v-if="show" transition="expand">hello world</p>', baseOptions)
  188. expect(ast.transition).toBe('"expand"')
  189. expect(ast.transitionOnAppear).toBe(false)
  190. })
  191. it('transition with empty', () => {
  192. const ast = parse('<p v-if="show" transition="">hello world</p>', baseOptions)
  193. expect(ast.transition).toBe(true)
  194. expect(ast.transitionOnAppear).toBe(false)
  195. })
  196. it('attribute with v-bind', () => {
  197. const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
  198. expect(ast.attrsList[0].name).toBe('type')
  199. expect(ast.attrsList[0].value).toBe('text')
  200. expect(ast.attrsList[1].name).toBe('name')
  201. expect(ast.attrsList[1].value).toBe('field1')
  202. expect(ast.attrsMap['type']).toBe('text')
  203. expect(ast.attrsMap['name']).toBe('field1')
  204. expect(ast.staticAttrs[0].name).toBe('type')
  205. expect(ast.staticAttrs[0].value).toBe('"text"')
  206. expect(ast.staticAttrs[1].name).toBe('name')
  207. expect(ast.staticAttrs[1].value).toBe('"field1"')
  208. expect(ast.props[0].name).toBe('value')
  209. expect(ast.props[0].value).toBe('msg')
  210. })
  211. it('attribute with v-on', () => {
  212. const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
  213. expect(ast.events.input.value).toBe('onInput')
  214. })
  215. it('attribute with directive', () => {
  216. const ast = parse('<input type="text" name="field1" :value="msg" v-validate:field1="required">', baseOptions)
  217. expect(ast.directives[0].name).toBe('validate')
  218. expect(ast.directives[0].value).toBe('required')
  219. expect(ast.directives[0].arg).toBe('field1')
  220. })
  221. it('attribute with modifiered directive', () => {
  222. const ast = parse('<input type="text" name="field1" :value="msg" v-validate.on.off>', baseOptions)
  223. expect(ast.directives[0].modifiers.on).toBe(true)
  224. expect(ast.directives[0].modifiers.off).toBe(true)
  225. })
  226. it('literal attribute', () => {
  227. // basic
  228. const ast1 = parse('<input type="text" name="field1" value="hello world">', baseOptions)
  229. expect(ast1.attrsList[0].name).toBe('type')
  230. expect(ast1.attrsList[0].value).toBe('text')
  231. expect(ast1.attrsList[1].name).toBe('name')
  232. expect(ast1.attrsList[1].value).toBe('field1')
  233. expect(ast1.attrsList[2].name).toBe('value')
  234. expect(ast1.attrsList[2].value).toBe('hello world')
  235. expect(ast1.attrsMap['type']).toBe('text')
  236. expect(ast1.attrsMap['name']).toBe('field1')
  237. expect(ast1.attrsMap['value']).toBe('hello world')
  238. expect(ast1.staticAttrs[0].name).toBe('type')
  239. expect(ast1.staticAttrs[0].value).toBe('"text"')
  240. expect(ast1.staticAttrs[1].name).toBe('name')
  241. expect(ast1.staticAttrs[1].value).toBe('"field1"')
  242. expect(ast1.staticAttrs[2].name).toBe('value')
  243. expect(ast1.staticAttrs[2].value).toBe('"hello world"')
  244. // interpolation warning
  245. parse('<input type="text" name="field1" value="{{msg}}">', baseOptions)
  246. expect('Interpolation inside attributes has been deprecated').toHaveBeenWarned()
  247. })
  248. it('duplicate attribute', () => {
  249. parse('<p class="class1" class="class1">hello world</p>', baseOptions)
  250. expect('duplicate attribute').toHaveBeenWarned()
  251. })
  252. it('custom delimiter', () => {
  253. const ast = parse('<p>{msg}</p>', extend({ delimiters: ['{', '}'] }, baseOptions))
  254. expect(ast.children[0].expression).toBe('_s(msg)')
  255. })
  256. it('not specified getTagNamespace option', () => {
  257. const options = Object.assign({}, baseOptions)
  258. delete options.getTagNamespace
  259. const ast = parse('<svg><text>hello world</text></svg>', options)
  260. expect(ast.tag).toBe('svg')
  261. expect(ast.ns).toBeUndefined()
  262. })
  263. it('not specified mustUseProp', () => {
  264. const options = Object.assign({}, baseOptions)
  265. delete options.mustUseProp
  266. const ast = parse('<input type="text" name="field1" :value="msg">', options)
  267. expect(ast.props).toBeUndefined()
  268. })
  269. })