parser.spec.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. import { parse } from 'compiler/parser/index'
  2. import { extend } from 'shared/util'
  3. import { baseOptions } from 'web/compiler/index'
  4. import { isIE } from 'core/util/env'
  5. describe('parser', () => {
  6. it('simple element', () => {
  7. const ast = parse('<h1>hello world</h1>', baseOptions)
  8. expect(ast.tag).toBe('h1')
  9. expect(ast.plain).toBe(true)
  10. expect(ast.children[0].text).toBe('hello world')
  11. })
  12. it('interpolation in element', () => {
  13. const ast = parse('<h1>{{msg}}</h1>', baseOptions)
  14. expect(ast.tag).toBe('h1')
  15. expect(ast.plain).toBe(true)
  16. expect(ast.children[0].expression).toBe('_s(msg)')
  17. })
  18. it('child elements', () => {
  19. const ast = parse('<ul><li>hello world</li></ul>', baseOptions)
  20. expect(ast.tag).toBe('ul')
  21. expect(ast.plain).toBe(true)
  22. expect(ast.children[0].tag).toBe('li')
  23. expect(ast.children[0].plain).toBe(true)
  24. expect(ast.children[0].children[0].text).toBe('hello world')
  25. expect(ast.children[0].parent).toBe(ast)
  26. })
  27. it('unary element', () => {
  28. const ast = parse('<hr>', baseOptions)
  29. expect(ast.tag).toBe('hr')
  30. expect(ast.plain).toBe(true)
  31. expect(ast.children.length).toBe(0)
  32. })
  33. it('svg element', () => {
  34. const ast = parse('<svg><text>hello world</text></svg>', baseOptions)
  35. expect(ast.tag).toBe('svg')
  36. expect(ast.ns).toBe('svg')
  37. expect(ast.plain).toBe(true)
  38. expect(ast.children[0].tag).toBe('text')
  39. expect(ast.children[0].children[0].text).toBe('hello world')
  40. expect(ast.children[0].parent).toBe(ast)
  41. })
  42. it('camelCase element', () => {
  43. const ast = parse('<MyComponent><p>hello world</p></MyComponent>', baseOptions)
  44. expect(ast.tag).toBe('MyComponent')
  45. expect(ast.plain).toBe(true)
  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 responsible 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 responsible for mapping the state').toHaveBeenWarned()
  66. })
  67. it('not contain root element', () => {
  68. parse('hello world', baseOptions)
  69. expect('Component template requires a root element, rather than just text').toHaveBeenWarned()
  70. })
  71. it('warn multiple root elements', () => {
  72. parse('<div></div><div></div>', baseOptions)
  73. expect('Component template should contain exactly one root element:\n\n<div></div><div></div>').toHaveBeenWarned()
  74. })
  75. it('not warn 2 root elements with v-if and v-else', () => {
  76. parse('<div v-if="1"></div><div v-else></div>', baseOptions)
  77. expect('Component template should contain exactly one root element')
  78. .not.toHaveBeenWarned()
  79. })
  80. it('not warn 3 root elements with v-if, v-else-if and v-else', () => {
  81. parse('<div v-if="1"></div><div v-else-if="2"></div><div v-else></div>', baseOptions)
  82. expect('Component template should contain exactly one root element')
  83. .not.toHaveBeenWarned()
  84. })
  85. it('not warn 2 root elements with v-if and v-else on separate lines', () => {
  86. parse(`
  87. <div v-if="1"></div>
  88. <div v-else></div>
  89. `, baseOptions)
  90. expect('Component template should contain exactly one root element')
  91. .not.toHaveBeenWarned()
  92. })
  93. it('not warn 3 or more root elements with v-if, v-else-if and v-else on separate lines', () => {
  94. parse(`
  95. <div v-if="1"></div>
  96. <div v-else-if="2"></div>
  97. <div v-else></div>
  98. `, baseOptions)
  99. expect('Component template should contain exactly one root element')
  100. .not.toHaveBeenWarned()
  101. parse(`
  102. <div v-if="1"></div>
  103. <div v-else-if="2"></div>
  104. <div v-else-if="3"></div>
  105. <div v-else-if="4"></div>
  106. <div v-else></div>
  107. `, baseOptions)
  108. expect('Component template should contain exactly one root element')
  109. .not.toHaveBeenWarned()
  110. })
  111. it('generate correct ast for 2 root elements with v-if and v-else on separate lines', () => {
  112. const ast = parse(`
  113. <div v-if="1"></div>
  114. <p v-else></p>
  115. `, baseOptions)
  116. expect(ast.tag).toBe('div')
  117. expect(ast.conditions[1].block.tag).toBe('p')
  118. })
  119. it('generate correct ast for 3 or more root elements with v-if and v-else on separate lines', () => {
  120. const ast = parse(`
  121. <div v-if="1"></div>
  122. <span v-else-if="2"></span>
  123. <p v-else></p>
  124. `, baseOptions)
  125. expect(ast.tag).toBe('div')
  126. expect(ast.conditions[0].block.tag).toBe('div')
  127. expect(ast.conditions[1].block.tag).toBe('span')
  128. expect(ast.conditions[2].block.tag).toBe('p')
  129. const astMore = parse(`
  130. <div v-if="1"></div>
  131. <span v-else-if="2"></span>
  132. <div v-else-if="3"></div>
  133. <span v-else-if="4"></span>
  134. <p v-else></p>
  135. `, baseOptions)
  136. expect(astMore.tag).toBe('div')
  137. expect(astMore.conditions[0].block.tag).toBe('div')
  138. expect(astMore.conditions[1].block.tag).toBe('span')
  139. expect(astMore.conditions[2].block.tag).toBe('div')
  140. expect(astMore.conditions[3].block.tag).toBe('span')
  141. expect(astMore.conditions[4].block.tag).toBe('p')
  142. })
  143. it('warn 2 root elements with v-if', () => {
  144. parse('<div v-if="1"></div><div v-if="2"></div>', baseOptions)
  145. expect('Component template should contain exactly one root element:\n\n<div v-if="1"></div><div v-if="2"></div>')
  146. .toHaveBeenWarned()
  147. })
  148. it('warn 3 root elements with v-if and v-else on first 2', () => {
  149. parse('<div v-if="1"></div><div v-else></div><div></div>', baseOptions)
  150. expect('Component template should contain exactly one root element:\n\n<div v-if="1"></div><div v-else></div><div></div>')
  151. .toHaveBeenWarned()
  152. })
  153. it('warn 3 root elements with v-if and v-else-if on first 2', () => {
  154. parse('<div v-if="1"></div><div v-else-if></div><div></div>', baseOptions)
  155. expect('Component template should contain exactly one root element:\n\n' +
  156. '<div v-if="1"></div><div v-else-if></div><div></div>')
  157. .toHaveBeenWarned()
  158. })
  159. it('warn 4 root elements with v-if, v-else-if and v-else on first 2', () => {
  160. parse('<div v-if="1"></div><div v-else-if></div><div v-else></div><div></div>', baseOptions)
  161. expect('Component template should contain exactly one root element:\n\n' +
  162. '<div v-if="1"></div><div v-else-if></div><div v-else></div><div></div>')
  163. .toHaveBeenWarned()
  164. })
  165. it('warn 2 root elements with v-if and v-else with v-for on 2nd', () => {
  166. parse('<div v-if="1"></div><div v-else v-for="i in [1]"></div>', baseOptions)
  167. expect('Cannot use v-for on stateful component root element because it renders multiple elements:\n' +
  168. '<div v-if="1"></div><div v-else v-for="i in [1]"></div>')
  169. .toHaveBeenWarned()
  170. })
  171. it('warn 2 root elements with v-if and v-else-if with v-for on 2nd', () => {
  172. parse('<div v-if="1"></div><div v-else-if="2" v-for="i in [1]"></div>', baseOptions)
  173. expect('Cannot use v-for on stateful component root element because it renders multiple elements:\n' +
  174. '<div v-if="1"></div><div v-else-if="2" v-for="i in [1]"></div>')
  175. .toHaveBeenWarned()
  176. })
  177. it('warn <template> as root element', () => {
  178. parse('<template></template>', baseOptions)
  179. expect('Cannot use <template> as component root element').toHaveBeenWarned()
  180. })
  181. it('warn <slot> as root element', () => {
  182. parse('<slot></slot>', baseOptions)
  183. expect('Cannot use <slot> as component root element').toHaveBeenWarned()
  184. })
  185. it('warn v-for on root element', () => {
  186. parse('<div v-for="item in items"></div>', baseOptions)
  187. expect('Cannot use v-for on stateful component root element').toHaveBeenWarned()
  188. })
  189. it('warn <template> key', () => {
  190. parse('<div><template v-for="i in 10" :key="i"></template></div>', baseOptions)
  191. expect('<template> cannot be keyed').toHaveBeenWarned()
  192. })
  193. it('v-pre directive', () => {
  194. const ast = parse('<div v-pre id="message1"><p>{{msg}}</p></div>', baseOptions)
  195. expect(ast.pre).toBe(true)
  196. expect(ast.attrs[0].name).toBe('id')
  197. expect(ast.attrs[0].value).toBe('"message1"')
  198. expect(ast.children[0].children[0].text).toBe('{{msg}}')
  199. })
  200. it('v-for directive basic syntax', () => {
  201. const ast = parse('<ul><li v-for="item in items"></li><ul>', baseOptions)
  202. const liAst = ast.children[0]
  203. expect(liAst.for).toBe('items')
  204. expect(liAst.alias).toBe('item')
  205. })
  206. it('v-for directive iteration syntax', () => {
  207. const ast = parse('<ul><li v-for="(item, index) in items"></li><ul>', baseOptions)
  208. const liAst = ast.children[0]
  209. expect(liAst.for).toBe('items')
  210. expect(liAst.alias).toBe('item')
  211. expect(liAst.iterator1).toBe('index')
  212. expect(liAst.iterator2).toBeUndefined()
  213. })
  214. it('v-for directive iteration syntax (multiple)', () => {
  215. const ast = parse('<ul><li v-for="(item, key, index) in items"></li><ul>', baseOptions)
  216. const liAst = ast.children[0]
  217. expect(liAst.for).toBe('items')
  218. expect(liAst.alias).toBe('item')
  219. expect(liAst.iterator1).toBe('key')
  220. expect(liAst.iterator2).toBe('index')
  221. })
  222. it('v-for directive key', () => {
  223. const ast = parse('<ul><li v-for="item in items" :key="item.uid"></li><ul>', baseOptions)
  224. const liAst = ast.children[0]
  225. expect(liAst.for).toBe('items')
  226. expect(liAst.alias).toBe('item')
  227. expect(liAst.key).toBe('item.uid')
  228. })
  229. it('v-for directive invalid syntax', () => {
  230. parse('<ul><li v-for="item into items"></li><ul>', baseOptions)
  231. expect('Invalid v-for expression').toHaveBeenWarned()
  232. })
  233. it('v-if directive syntax', () => {
  234. const ast = parse('<p v-if="show">hello world</p>', baseOptions)
  235. expect(ast.if).toBe('show')
  236. expect(ast.conditions[0].exp).toBe('show')
  237. })
  238. it('v-else-if directive syntax', () => {
  239. const ast = parse('<div><p v-if="show">hello</p><span v-else-if="2">elseif</span><p v-else>world</p></div>', baseOptions)
  240. const ifAst = ast.children[0]
  241. const conditionsAst = ifAst.conditions
  242. expect(conditionsAst.length).toBe(3)
  243. expect(conditionsAst[1].block.children[0].text).toBe('elseif')
  244. expect(conditionsAst[1].block.parent).toBe(ast)
  245. expect(conditionsAst[2].block.children[0].text).toBe('world')
  246. expect(conditionsAst[2].block.parent).toBe(ast)
  247. })
  248. it('v-else directive syntax', () => {
  249. const ast = parse('<div><p v-if="show">hello</p><p v-else>world</p></div>', baseOptions)
  250. const ifAst = ast.children[0]
  251. const conditionsAst = ifAst.conditions
  252. expect(conditionsAst.length).toBe(2)
  253. expect(conditionsAst[1].block.children[0].text).toBe('world')
  254. expect(conditionsAst[1].block.parent).toBe(ast)
  255. })
  256. it('v-else-if directive invalid syntax', () => {
  257. parse('<div><p v-else-if="1">world</p></div>', baseOptions)
  258. expect('v-else-if="1" used on element').toHaveBeenWarned()
  259. })
  260. it('v-else directive invalid syntax', () => {
  261. parse('<div><p v-else>world</p></div>', baseOptions)
  262. expect('v-else used on element').toHaveBeenWarned()
  263. })
  264. it('v-once directive syntax', () => {
  265. const ast = parse('<p v-once>world</p>', baseOptions)
  266. expect(ast.once).toBe(true)
  267. })
  268. it('slot tag single syntax', () => {
  269. const ast = parse('<slot></slot>', baseOptions)
  270. expect(ast.tag).toBe('slot')
  271. expect(ast.slotName).toBeUndefined()
  272. })
  273. it('slot tag namped syntax', () => {
  274. const ast = parse('<slot name="one">hello world</slot>', baseOptions)
  275. expect(ast.tag).toBe('slot')
  276. expect(ast.slotName).toBe('"one"')
  277. })
  278. it('slot target', () => {
  279. const ast = parse('<p slot="one">hello world</p>', baseOptions)
  280. expect(ast.slotTarget).toBe('"one"')
  281. })
  282. it('component properties', () => {
  283. const ast = parse('<my-component :msg="hello"></my-component>', baseOptions)
  284. expect(ast.attrs[0].name).toBe('msg')
  285. expect(ast.attrs[0].value).toBe('hello')
  286. })
  287. it('component "is" attribute', () => {
  288. const ast = parse('<my-component is="component1"></my-component>', baseOptions)
  289. expect(ast.component).toBe('"component1"')
  290. })
  291. it('component "inline-template" attribute', () => {
  292. const ast = parse('<my-component inline-template>hello world</my-component>', baseOptions)
  293. expect(ast.inlineTemplate).toBe(true)
  294. })
  295. it('class binding', () => {
  296. // static
  297. const ast1 = parse('<p class="class1">hello world</p>', baseOptions)
  298. expect(ast1.staticClass).toBe('"class1"')
  299. // dynamic
  300. const ast2 = parse('<p :class="class1">hello world</p>', baseOptions)
  301. expect(ast2.classBinding).toBe('class1')
  302. // interpolation warning
  303. parse('<p class="{{error}}">hello world</p>', baseOptions)
  304. expect('Interpolation inside attributes has been removed').toHaveBeenWarned()
  305. })
  306. it('style binding', () => {
  307. const ast = parse('<p :style="error">hello world</p>', baseOptions)
  308. expect(ast.styleBinding).toBe('error')
  309. })
  310. it('attribute with v-bind', () => {
  311. const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
  312. expect(ast.attrsList[0].name).toBe('type')
  313. expect(ast.attrsList[0].value).toBe('text')
  314. expect(ast.attrsList[1].name).toBe('name')
  315. expect(ast.attrsList[1].value).toBe('field1')
  316. expect(ast.attrsMap['type']).toBe('text')
  317. expect(ast.attrsMap['name']).toBe('field1')
  318. expect(ast.attrs[0].name).toBe('type')
  319. expect(ast.attrs[0].value).toBe('"text"')
  320. expect(ast.attrs[1].name).toBe('name')
  321. expect(ast.attrs[1].value).toBe('"field1"')
  322. expect(ast.props[0].name).toBe('value')
  323. expect(ast.props[0].value).toBe('msg')
  324. })
  325. it('attribute with v-on', () => {
  326. const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
  327. expect(ast.events.input.value).toBe('onInput')
  328. })
  329. it('attribute with directive', () => {
  330. const ast = parse('<input type="text" name="field1" :value="msg" v-validate:field1="required">', baseOptions)
  331. expect(ast.directives[0].name).toBe('validate')
  332. expect(ast.directives[0].value).toBe('required')
  333. expect(ast.directives[0].arg).toBe('field1')
  334. })
  335. it('attribute with modifiered directive', () => {
  336. const ast = parse('<input type="text" name="field1" :value="msg" v-validate.on.off>', baseOptions)
  337. expect(ast.directives[0].modifiers.on).toBe(true)
  338. expect(ast.directives[0].modifiers.off).toBe(true)
  339. })
  340. it('literal attribute', () => {
  341. // basic
  342. const ast1 = parse('<input type="text" name="field1" value="hello world">', baseOptions)
  343. expect(ast1.attrsList[0].name).toBe('type')
  344. expect(ast1.attrsList[0].value).toBe('text')
  345. expect(ast1.attrsList[1].name).toBe('name')
  346. expect(ast1.attrsList[1].value).toBe('field1')
  347. expect(ast1.attrsList[2].name).toBe('value')
  348. expect(ast1.attrsList[2].value).toBe('hello world')
  349. expect(ast1.attrsMap['type']).toBe('text')
  350. expect(ast1.attrsMap['name']).toBe('field1')
  351. expect(ast1.attrsMap['value']).toBe('hello world')
  352. expect(ast1.attrs[0].name).toBe('type')
  353. expect(ast1.attrs[0].value).toBe('"text"')
  354. expect(ast1.attrs[1].name).toBe('name')
  355. expect(ast1.attrs[1].value).toBe('"field1"')
  356. expect(ast1.attrs[2].name).toBe('value')
  357. expect(ast1.attrs[2].value).toBe('"hello world"')
  358. // interpolation warning
  359. parse('<input type="text" name="field1" value="{{msg}}">', baseOptions)
  360. expect('Interpolation inside attributes has been removed').toHaveBeenWarned()
  361. })
  362. if (!isIE) {
  363. it('duplicate attribute', () => {
  364. parse('<p class="class1" class="class1">hello world</p>', baseOptions)
  365. expect('duplicate attribute').toHaveBeenWarned()
  366. })
  367. }
  368. it('custom delimiter', () => {
  369. const ast = parse('<p>{msg}</p>', extend({ delimiters: ['{', '}'] }, baseOptions))
  370. expect(ast.children[0].expression).toBe('_s(msg)')
  371. })
  372. it('not specified getTagNamespace option', () => {
  373. const options = extend({}, baseOptions)
  374. delete options.getTagNamespace
  375. const ast = parse('<svg><text>hello world</text></svg>', options)
  376. expect(ast.tag).toBe('svg')
  377. expect(ast.ns).toBeUndefined()
  378. })
  379. it('not specified mustUseProp', () => {
  380. const options = extend({}, baseOptions)
  381. delete options.mustUseProp
  382. const ast = parse('<input type="text" name="field1" :value="msg">', options)
  383. expect(ast.props).toBeUndefined()
  384. })
  385. it('pre/post transforms', () => {
  386. const options = extend({}, baseOptions)
  387. const spy1 = jasmine.createSpy('preTransform')
  388. const spy2 = jasmine.createSpy('postTransform')
  389. options.modules = options.modules.concat([{
  390. preTransformNode (el) {
  391. spy1(el.tag)
  392. },
  393. postTransformNode (el) {
  394. expect(el.attrs.length).toBe(1)
  395. spy2(el.tag)
  396. }
  397. }])
  398. parse('<img v-pre src="hi">', options)
  399. expect(spy1).toHaveBeenCalledWith('img')
  400. expect(spy2).toHaveBeenCalledWith('img')
  401. })
  402. it('preserve whitespace in <pre> tag', function () {
  403. const options = extend({}, baseOptions)
  404. const ast = parse('<pre><code> \n<span>hi</span>\n </code></pre>', options)
  405. const code = ast.children[0]
  406. expect(code.children[0].type).toBe(3)
  407. expect(code.children[0].text).toBe(' \n')
  408. expect(code.children[2].type).toBe(3)
  409. expect(code.children[2].text).toBe('\n ')
  410. })
  411. it('forgivingly handle < in plain text', () => {
  412. const options = extend({}, baseOptions)
  413. const ast = parse('<p>1 < 2 < 3</p>', options)
  414. expect(ast.tag).toBe('p')
  415. expect(ast.children.length).toBe(1)
  416. expect(ast.children[0].type).toBe(3)
  417. expect(ast.children[0].text).toBe('1 < 2 < 3')
  418. })
  419. it('IE conditional comments', () => {
  420. const options = extend({}, baseOptions)
  421. const ast = parse(`
  422. <div>
  423. <!--[if lte IE 8]>
  424. <p>Test 1</p>
  425. <![endif]-->
  426. </div>
  427. `, options)
  428. expect(ast.tag).toBe('div')
  429. expect(ast.chilldren).toBeUndefined()
  430. })
  431. })