parser.spec.js 19 KB

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