parser.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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('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 stateful 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.staticAttrs[0].name).toBe('id')
  87. expect(ast.staticAttrs[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="(item, index) 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.iterator1).toBe('index')
  102. expect(liAst.iterator2).toBeUndefined()
  103. })
  104. it('v-for directive iteration syntax (multiple)', () => {
  105. const ast = parse('<ul><li v-for="(item, key, index) in items"></li><ul>', baseOptions)
  106. const liAst = ast.children[0]
  107. expect(liAst.for).toBe('items')
  108. expect(liAst.alias).toBe('item')
  109. expect(liAst.iterator1).toBe('key')
  110. expect(liAst.iterator2).toBe('index')
  111. })
  112. it('v-for directive key', () => {
  113. const ast = parse('<ul><li v-for="item in items" :key="item.uid"></li><ul>', baseOptions)
  114. const liAst = ast.children[0]
  115. expect(liAst.for).toBe('items')
  116. expect(liAst.alias).toBe('item')
  117. expect(liAst.key).toBe('item.uid')
  118. })
  119. it('v-for directive invalid syntax', () => {
  120. parse('<ul><li v-for="item into items"></li><ul>', baseOptions)
  121. expect('Invalid v-for expression').toHaveBeenWarned()
  122. })
  123. it('v-if directive syntax', () => {
  124. const ast = parse('<p v-if="show">hello world</p>', baseOptions)
  125. expect(ast.if).toBe('show')
  126. })
  127. it('v-else directive syntax', () => {
  128. const ast = parse('<div><p v-if="show">hello</p><p v-else>world</p></div>', baseOptions)
  129. const ifAst = ast.children[0]
  130. const elseAst = ifAst.elseBlock
  131. expect(elseAst.else).toBe(true)
  132. expect(elseAst.children[0].text).toBe('world')
  133. expect(elseAst.parent).toBe(ast)
  134. })
  135. it('v-else directive invalid syntax', () => {
  136. parse('<div><p v-else>world</p></div>', baseOptions)
  137. expect('v-else used on element').toHaveBeenWarned()
  138. })
  139. it('v-once directive syntax', () => {
  140. const ast = parse('<p v-once>world</p>', baseOptions)
  141. expect(ast.once).toBe(true)
  142. })
  143. it('slot tag single syntax', () => {
  144. const ast = parse('<slot></slot>', baseOptions)
  145. expect(ast.tag).toBe('slot')
  146. expect(ast.slotName).toBeUndefined()
  147. })
  148. it('slot tag namped syntax', () => {
  149. const ast = parse('<slot name="one">hello world</slot>', baseOptions)
  150. expect(ast.tag).toBe('slot')
  151. expect(ast.slotName).toBe('"one"')
  152. })
  153. it('slot target', () => {
  154. const ast = parse('<p slot="one">hello world</p>', baseOptions)
  155. expect(ast.slotTarget).toBe('"one"')
  156. })
  157. it('component properties', () => {
  158. const ast = parse('<my-component :msg="hello"></my-component>', baseOptions)
  159. expect(ast.attrs[0].name).toBe('msg')
  160. expect(ast.attrs[0].value).toBe('hello')
  161. })
  162. it('component "is" attribute', () => {
  163. const ast = parse('<my-component is="component1"></my-component>', baseOptions)
  164. expect(ast.component).toBe('"component1"')
  165. })
  166. it('component "inline-template" attribute', () => {
  167. const ast = parse('<my-component inline-template>hello world</my-component>', baseOptions)
  168. expect(ast.inlineTemplate).toBe(true)
  169. })
  170. it('class binding', () => {
  171. // static
  172. const ast1 = parse('<p class="class1">hello world</p>', baseOptions)
  173. expect(ast1.staticClass).toBe('"class1"')
  174. // dynamic
  175. const ast2 = parse('<p :class="class1">hello world</p>', baseOptions)
  176. expect(ast2.classBinding).toBe('class1')
  177. // interpolation warning
  178. parse('<p class="{{error}}">hello world</p>', baseOptions)
  179. expect('Interpolation inside attributes has been deprecated').toHaveBeenWarned()
  180. })
  181. it('style binding', () => {
  182. const ast = parse('<p :style="error">hello world</p>', baseOptions)
  183. expect(ast.styleBinding).toBe('error')
  184. })
  185. it('transition', () => {
  186. const ast = parse('<p v-if="show" transition="expand">hello world</p>', baseOptions)
  187. expect(ast.transition).toBe('"expand"')
  188. })
  189. it('transition with empty', () => {
  190. const ast = parse('<p v-if="show" transition="">hello world</p>', baseOptions)
  191. expect(ast.transition).toBe(true)
  192. })
  193. it('attribute with v-bind', () => {
  194. const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
  195. expect(ast.attrsList[0].name).toBe('type')
  196. expect(ast.attrsList[0].value).toBe('text')
  197. expect(ast.attrsList[1].name).toBe('name')
  198. expect(ast.attrsList[1].value).toBe('field1')
  199. expect(ast.attrsMap['type']).toBe('text')
  200. expect(ast.attrsMap['name']).toBe('field1')
  201. expect(ast.staticAttrs[0].name).toBe('type')
  202. expect(ast.staticAttrs[0].value).toBe('"text"')
  203. expect(ast.staticAttrs[1].name).toBe('name')
  204. expect(ast.staticAttrs[1].value).toBe('"field1"')
  205. expect(ast.props[0].name).toBe('value')
  206. expect(ast.props[0].value).toBe('msg')
  207. })
  208. it('attribute with v-on', () => {
  209. const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
  210. expect(ast.events.input.value).toBe('onInput')
  211. })
  212. it('attribute with directive', () => {
  213. const ast = parse('<input type="text" name="field1" :value="msg" v-validate:field1="required">', baseOptions)
  214. expect(ast.directives[0].name).toBe('validate')
  215. expect(ast.directives[0].value).toBe('required')
  216. expect(ast.directives[0].arg).toBe('field1')
  217. })
  218. it('attribute with modifiered directive', () => {
  219. const ast = parse('<input type="text" name="field1" :value="msg" v-validate.on.off>', baseOptions)
  220. expect(ast.directives[0].modifiers.on).toBe(true)
  221. expect(ast.directives[0].modifiers.off).toBe(true)
  222. })
  223. it('literal attribute', () => {
  224. // basic
  225. const ast1 = parse('<input type="text" name="field1" value="hello world">', baseOptions)
  226. expect(ast1.attrsList[0].name).toBe('type')
  227. expect(ast1.attrsList[0].value).toBe('text')
  228. expect(ast1.attrsList[1].name).toBe('name')
  229. expect(ast1.attrsList[1].value).toBe('field1')
  230. expect(ast1.attrsList[2].name).toBe('value')
  231. expect(ast1.attrsList[2].value).toBe('hello world')
  232. expect(ast1.attrsMap['type']).toBe('text')
  233. expect(ast1.attrsMap['name']).toBe('field1')
  234. expect(ast1.attrsMap['value']).toBe('hello world')
  235. expect(ast1.staticAttrs[0].name).toBe('type')
  236. expect(ast1.staticAttrs[0].value).toBe('"text"')
  237. expect(ast1.staticAttrs[1].name).toBe('name')
  238. expect(ast1.staticAttrs[1].value).toBe('"field1"')
  239. expect(ast1.staticAttrs[2].name).toBe('value')
  240. expect(ast1.staticAttrs[2].value).toBe('"hello world"')
  241. // interpolation warning
  242. parse('<input type="text" name="field1" value="{{msg}}">', baseOptions)
  243. expect('Interpolation inside attributes has been deprecated').toHaveBeenWarned()
  244. })
  245. it('duplicate attribute', () => {
  246. parse('<p class="class1" class="class1">hello world</p>', baseOptions)
  247. expect('duplicate attribute').toHaveBeenWarned()
  248. })
  249. it('custom delimiter', () => {
  250. const ast = parse('<p>{msg}</p>', extend({ delimiters: ['{', '}'] }, baseOptions))
  251. expect(ast.children[0].expression).toBe('_s(msg)')
  252. })
  253. it('not specified getTagNamespace option', () => {
  254. const options = extend({}, baseOptions)
  255. delete options.getTagNamespace
  256. const ast = parse('<svg><text>hello world</text></svg>', options)
  257. expect(ast.tag).toBe('svg')
  258. expect(ast.ns).toBeUndefined()
  259. })
  260. it('not specified mustUseProp', () => {
  261. const options = extend({}, baseOptions)
  262. delete options.mustUseProp
  263. const ast = parse('<input type="text" name="field1" :value="msg">', options)
  264. expect(ast.props).toBeUndefined()
  265. })
  266. it('pre/post transforms', () => {
  267. const options = extend({}, baseOptions)
  268. const spy1 = jasmine.createSpy('preTransform')
  269. const spy2 = jasmine.createSpy('postTransform')
  270. options.modules = options.modules.concat([{
  271. preTransformNode (el) {
  272. spy1(el.tag)
  273. },
  274. postTransformNode (el) {
  275. expect(el.staticAttrs.length).toBe(1)
  276. spy2(el.tag)
  277. }
  278. }])
  279. parse('<img v-pre src="hi">', options)
  280. expect(spy1).toHaveBeenCalledWith('img')
  281. expect(spy2).toHaveBeenCalledWith('img')
  282. })
  283. })