parser.spec.js 12 KB

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