parser.spec.js 13 KB

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