parser.spec.js 20 KB

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