parser.spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 responsible 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 responsible for mapping the state').toHaveBeenWarned()
  65. })
  66. it('not contain root element', () => {
  67. parse('hello world', baseOptions)
  68. expect('Component template requires a root element, rather than just text').toHaveBeenWarned()
  69. })
  70. it('warn multiple root elements', () => {
  71. parse('<div></div><div></div>', baseOptions)
  72. expect('Component template should contain exactly one root element:\n\n<div></div><div></div>').toHaveBeenWarned()
  73. })
  74. it('not warn 2 root elements with v-if and v-else', () => {
  75. parse('<div v-if="1"></div><div v-else></div>', baseOptions)
  76. expect('Component template should contain exactly one root element')
  77. .not.toHaveBeenWarned()
  78. })
  79. it('not warn 2 root elements with v-if and v-else on separate lines', () => {
  80. parse(`
  81. <div v-if="1"></div>
  82. <div v-else></div>
  83. `, baseOptions)
  84. expect('Component template should contain exactly one root element')
  85. .not.toHaveBeenWarned()
  86. })
  87. it('generate correct ast for 2 root elements with v-if and v-else on separate lines', () => {
  88. const ast = parse(`
  89. <div v-if="1"></div>
  90. <p v-else></p>
  91. `, baseOptions)
  92. expect(ast.tag).toBe('div')
  93. expect(ast.elseBlock.tag).toBe('p')
  94. })
  95. it('warn 2 root elements with v-if', () => {
  96. parse('<div v-if="1"></div><div v-if="2"></div>', baseOptions)
  97. expect('Component template should contain exactly one root element:\n\n<div v-if="1"></div><div v-if="2"></div>')
  98. .toHaveBeenWarned()
  99. })
  100. it('warn 3 root elements with v-if and v-else on first 2', () => {
  101. parse('<div v-if="1"></div><div v-else></div><div></div>', baseOptions)
  102. expect('Component template should contain exactly one root element:\n\n<div v-if="1"></div><div v-else></div><div></div>')
  103. .toHaveBeenWarned()
  104. })
  105. it('warn 2 root elements with v-if and v-else with v-for on 2nd', () => {
  106. parse('<div v-if="1"></div><div v-else v-for="i in [1]"></div>', baseOptions)
  107. expect('Cannot use v-for on stateful component root element because it renders multiple elements:\n<div v-if="1"></div><div v-else v-for="i in [1]"></div>')
  108. .toHaveBeenWarned()
  109. })
  110. it('warn <template> as root element', () => {
  111. parse('<template></template>', baseOptions)
  112. expect('Cannot use <template> as component root element').toHaveBeenWarned()
  113. })
  114. it('warn <slot> as root element', () => {
  115. parse('<slot></slot>', baseOptions)
  116. expect('Cannot use <slot> as component root element').toHaveBeenWarned()
  117. })
  118. it('warn v-for on root element', () => {
  119. parse('<div v-for="item in items"></div>', baseOptions)
  120. expect('Cannot use v-for on stateful component root element').toHaveBeenWarned()
  121. })
  122. it('warn <template> key', () => {
  123. parse('<div><template v-for="i in 10" :key="i"></template></div>', baseOptions)
  124. expect('<template> cannot be keyed').toHaveBeenWarned()
  125. })
  126. it('v-pre directive', () => {
  127. const ast = parse('<div v-pre id="message1"><p>{{msg}}</p></div>', baseOptions)
  128. expect(ast.pre).toBe(true)
  129. expect(ast.attrs[0].name).toBe('id')
  130. expect(ast.attrs[0].value).toBe('"message1"')
  131. expect(ast.children[0].children[0].text).toBe('{{msg}}')
  132. })
  133. it('v-for directive basic syntax', () => {
  134. const ast = parse('<ul><li v-for="item in items"></li><ul>', baseOptions)
  135. const liAst = ast.children[0]
  136. expect(liAst.for).toBe('items')
  137. expect(liAst.alias).toBe('item')
  138. })
  139. it('v-for directive iteration syntax', () => {
  140. const ast = parse('<ul><li v-for="(item, index) in items"></li><ul>', baseOptions)
  141. const liAst = ast.children[0]
  142. expect(liAst.for).toBe('items')
  143. expect(liAst.alias).toBe('item')
  144. expect(liAst.iterator1).toBe('index')
  145. expect(liAst.iterator2).toBeUndefined()
  146. })
  147. it('v-for directive iteration syntax (multiple)', () => {
  148. const ast = parse('<ul><li v-for="(item, key, index) in items"></li><ul>', baseOptions)
  149. const liAst = ast.children[0]
  150. expect(liAst.for).toBe('items')
  151. expect(liAst.alias).toBe('item')
  152. expect(liAst.iterator1).toBe('key')
  153. expect(liAst.iterator2).toBe('index')
  154. })
  155. it('v-for directive key', () => {
  156. const ast = parse('<ul><li v-for="item in items" :key="item.uid"></li><ul>', baseOptions)
  157. const liAst = ast.children[0]
  158. expect(liAst.for).toBe('items')
  159. expect(liAst.alias).toBe('item')
  160. expect(liAst.key).toBe('item.uid')
  161. })
  162. it('v-for directive invalid syntax', () => {
  163. parse('<ul><li v-for="item into items"></li><ul>', baseOptions)
  164. expect('Invalid v-for expression').toHaveBeenWarned()
  165. })
  166. it('v-if directive syntax', () => {
  167. const ast = parse('<p v-if="show">hello world</p>', baseOptions)
  168. expect(ast.if).toBe('show')
  169. })
  170. it('v-else directive syntax', () => {
  171. const ast = parse('<div><p v-if="show">hello</p><p v-else>world</p></div>', baseOptions)
  172. const ifAst = ast.children[0]
  173. const elseAst = ifAst.elseBlock
  174. expect(elseAst.else).toBe(true)
  175. expect(elseAst.children[0].text).toBe('world')
  176. expect(elseAst.parent).toBe(ast)
  177. })
  178. it('v-else directive invalid syntax', () => {
  179. parse('<div><p v-else>world</p></div>', baseOptions)
  180. expect('v-else used on element').toHaveBeenWarned()
  181. })
  182. it('v-once directive syntax', () => {
  183. const ast = parse('<p v-once>world</p>', baseOptions)
  184. expect(ast.once).toBe(true)
  185. })
  186. it('slot tag single syntax', () => {
  187. const ast = parse('<slot></slot>', baseOptions)
  188. expect(ast.tag).toBe('slot')
  189. expect(ast.slotName).toBeUndefined()
  190. })
  191. it('slot tag namped syntax', () => {
  192. const ast = parse('<slot name="one">hello world</slot>', baseOptions)
  193. expect(ast.tag).toBe('slot')
  194. expect(ast.slotName).toBe('"one"')
  195. })
  196. it('slot target', () => {
  197. const ast = parse('<p slot="one">hello world</p>', baseOptions)
  198. expect(ast.slotTarget).toBe('"one"')
  199. })
  200. it('component properties', () => {
  201. const ast = parse('<my-component :msg="hello"></my-component>', baseOptions)
  202. expect(ast.attrs[0].name).toBe('msg')
  203. expect(ast.attrs[0].value).toBe('hello')
  204. })
  205. it('component "is" attribute', () => {
  206. const ast = parse('<my-component is="component1"></my-component>', baseOptions)
  207. expect(ast.component).toBe('"component1"')
  208. })
  209. it('component "inline-template" attribute', () => {
  210. const ast = parse('<my-component inline-template>hello world</my-component>', baseOptions)
  211. expect(ast.inlineTemplate).toBe(true)
  212. })
  213. it('class binding', () => {
  214. // static
  215. const ast1 = parse('<p class="class1">hello world</p>', baseOptions)
  216. expect(ast1.staticClass).toBe('"class1"')
  217. // dynamic
  218. const ast2 = parse('<p :class="class1">hello world</p>', baseOptions)
  219. expect(ast2.classBinding).toBe('class1')
  220. // interpolation warning
  221. parse('<p class="{{error}}">hello world</p>', baseOptions)
  222. expect('Interpolation inside attributes has been deprecated').toHaveBeenWarned()
  223. })
  224. it('style binding', () => {
  225. const ast = parse('<p :style="error">hello world</p>', baseOptions)
  226. expect(ast.styleBinding).toBe('error')
  227. })
  228. it('attribute with v-bind', () => {
  229. const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
  230. expect(ast.attrsList[0].name).toBe('type')
  231. expect(ast.attrsList[0].value).toBe('text')
  232. expect(ast.attrsList[1].name).toBe('name')
  233. expect(ast.attrsList[1].value).toBe('field1')
  234. expect(ast.attrsMap['type']).toBe('text')
  235. expect(ast.attrsMap['name']).toBe('field1')
  236. expect(ast.attrs[0].name).toBe('type')
  237. expect(ast.attrs[0].value).toBe('"text"')
  238. expect(ast.attrs[1].name).toBe('name')
  239. expect(ast.attrs[1].value).toBe('"field1"')
  240. expect(ast.props[0].name).toBe('value')
  241. expect(ast.props[0].value).toBe('msg')
  242. })
  243. it('attribute with v-on', () => {
  244. const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
  245. expect(ast.events.input.value).toBe('onInput')
  246. })
  247. it('attribute with directive', () => {
  248. const ast = parse('<input type="text" name="field1" :value="msg" v-validate:field1="required">', baseOptions)
  249. expect(ast.directives[0].name).toBe('validate')
  250. expect(ast.directives[0].value).toBe('required')
  251. expect(ast.directives[0].arg).toBe('field1')
  252. })
  253. it('attribute with modifiered directive', () => {
  254. const ast = parse('<input type="text" name="field1" :value="msg" v-validate.on.off>', baseOptions)
  255. expect(ast.directives[0].modifiers.on).toBe(true)
  256. expect(ast.directives[0].modifiers.off).toBe(true)
  257. })
  258. it('literal attribute', () => {
  259. // basic
  260. const ast1 = parse('<input type="text" name="field1" value="hello world">', baseOptions)
  261. expect(ast1.attrsList[0].name).toBe('type')
  262. expect(ast1.attrsList[0].value).toBe('text')
  263. expect(ast1.attrsList[1].name).toBe('name')
  264. expect(ast1.attrsList[1].value).toBe('field1')
  265. expect(ast1.attrsList[2].name).toBe('value')
  266. expect(ast1.attrsList[2].value).toBe('hello world')
  267. expect(ast1.attrsMap['type']).toBe('text')
  268. expect(ast1.attrsMap['name']).toBe('field1')
  269. expect(ast1.attrsMap['value']).toBe('hello world')
  270. expect(ast1.attrs[0].name).toBe('type')
  271. expect(ast1.attrs[0].value).toBe('"text"')
  272. expect(ast1.attrs[1].name).toBe('name')
  273. expect(ast1.attrs[1].value).toBe('"field1"')
  274. expect(ast1.attrs[2].name).toBe('value')
  275. expect(ast1.attrs[2].value).toBe('"hello world"')
  276. // interpolation warning
  277. parse('<input type="text" name="field1" value="{{msg}}">', baseOptions)
  278. expect('Interpolation inside attributes has been deprecated').toHaveBeenWarned()
  279. })
  280. it('duplicate attribute', () => {
  281. parse('<p class="class1" class="class1">hello world</p>', baseOptions)
  282. expect('duplicate attribute').toHaveBeenWarned()
  283. })
  284. it('custom delimiter', () => {
  285. const ast = parse('<p>{msg}</p>', extend({ delimiters: ['{', '}'] }, baseOptions))
  286. expect(ast.children[0].expression).toBe('_s(msg)')
  287. })
  288. it('not specified getTagNamespace option', () => {
  289. const options = extend({}, baseOptions)
  290. delete options.getTagNamespace
  291. const ast = parse('<svg><text>hello world</text></svg>', options)
  292. expect(ast.tag).toBe('svg')
  293. expect(ast.ns).toBeUndefined()
  294. })
  295. it('not specified mustUseProp', () => {
  296. const options = extend({}, baseOptions)
  297. delete options.mustUseProp
  298. const ast = parse('<input type="text" name="field1" :value="msg">', options)
  299. expect(ast.props).toBeUndefined()
  300. })
  301. it('pre/post transforms', () => {
  302. const options = extend({}, baseOptions)
  303. const spy1 = jasmine.createSpy('preTransform')
  304. const spy2 = jasmine.createSpy('postTransform')
  305. options.modules = options.modules.concat([{
  306. preTransformNode (el) {
  307. spy1(el.tag)
  308. },
  309. postTransformNode (el) {
  310. expect(el.attrs.length).toBe(1)
  311. spy2(el.tag)
  312. }
  313. }])
  314. parse('<img v-pre src="hi">', options)
  315. expect(spy1).toHaveBeenCalledWith('img')
  316. expect(spy2).toHaveBeenCalledWith('img')
  317. })
  318. it('preserve whitespace in <pre> tag', function () {
  319. const options = extend({}, baseOptions)
  320. const ast = parse('<pre><code> \n<span>hi</span>\n </code></pre>', options)
  321. const code = ast.children[0]
  322. expect(code.children[0].type).toBe(3)
  323. expect(code.children[0].text).toBe(' \n')
  324. expect(code.children[2].type).toBe(3)
  325. expect(code.children[2].text).toBe('\n ')
  326. })
  327. })