2
0

parser.spec.js 12 KB

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