parser.spec.js 14 KB

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