parser.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import { parse } from 'compiler/parser/index'
  2. import directives from 'web/compiler/directives/index'
  3. import { extend } from 'shared/util'
  4. import { isReservedTag, isUnaryTag, mustUseProp, getTagNamespace } from 'web/util/index'
  5. describe('parser', () => {
  6. const baseOptions = {
  7. expectHTML: true,
  8. preserveWhitespace: true,
  9. directives,
  10. isReservedTag,
  11. isUnaryTag,
  12. mustUseProp,
  13. getTagNamespace
  14. }
  15. it('simple element', () => {
  16. const ast = parse('<h1>hello world</h1>', baseOptions)
  17. expect(ast.tag).toBe('h1')
  18. expect(ast.plain).toBe(true)
  19. expect(ast.children[0].text).toBe('hello world')
  20. })
  21. it('interpolation in element', () => {
  22. const ast = parse('<h1>{{msg}}</h1>', baseOptions)
  23. expect(ast.tag).toBe('h1')
  24. expect(ast.plain).toBe(true)
  25. expect(ast.children[0].expression).toBe('__toString__(msg)')
  26. })
  27. it('child elements', () => {
  28. const ast = parse('<ul><li>hello world</li></ul>', baseOptions)
  29. expect(ast.tag).toBe('ul')
  30. expect(ast.plain).toBe(true)
  31. expect(ast.children[0].tag).toBe('li')
  32. expect(ast.children[0].plain).toBe(true)
  33. expect(ast.children[0].children[0].text).toBe('hello world')
  34. expect(ast.children[0].parent).toBe(ast)
  35. })
  36. it('unary element', () => {
  37. const ast = parse('<hr>', baseOptions)
  38. expect(ast.tag).toBe('hr')
  39. expect(ast.plain).toBe(true)
  40. expect(ast.children.length).toBe(0)
  41. })
  42. it('svg element', () => {
  43. const ast = parse('<svg><text>hello world</text></svg>', baseOptions)
  44. expect(ast.tag).toBe('svg')
  45. expect(ast.ns).toBe('svg')
  46. expect(ast.plain).toBe(true)
  47. expect(ast.children[0].tag).toBe('text')
  48. expect(ast.children[0].children[0].text).toBe('hello world')
  49. expect(ast.children[0].parent).toBe(ast)
  50. })
  51. it('camelCase element', () => {
  52. const ast = parse('<MyComponent><p>hello world</p></MyComponent>', baseOptions)
  53. expect(ast.tag).toBe('my-component')
  54. expect(ast.plain).toBe(true)
  55. expect('Found camelCase tag in template').toHaveBeenWarned()
  56. expect(ast.children[0].tag).toBe('p')
  57. expect(ast.children[0].plain).toBe(true)
  58. expect(ast.children[0].children[0].text).toBe('hello world')
  59. expect(ast.children[0].parent).toBe(ast)
  60. })
  61. it('forbidden element', () => {
  62. // style
  63. const styleAst = parse('<style>error { color: red; }</style>', baseOptions)
  64. expect(styleAst.tag).toBe('style')
  65. expect(styleAst.plain).toBe(true)
  66. expect(styleAst.forbidden).toBe(true)
  67. expect(styleAst.children[0].text).toBe('error { color: red; }')
  68. expect('Templates should only be responsbile for mapping the state').toHaveBeenWarned()
  69. // script
  70. const scriptAst = parse('<script type="text/javascript">alert("hello world!")</script>', baseOptions)
  71. expect(scriptAst.tag).toBe('script')
  72. expect(scriptAst.plain).toBe(false)
  73. expect(scriptAst.forbidden).toBe(true)
  74. expect(scriptAst.children[0].text).toBe('alert("hello world!")')
  75. expect('Templates should only be responsbile for mapping the state').toHaveBeenWarned()
  76. })
  77. it('not contain root element', () => {
  78. parse('hello world', baseOptions)
  79. expect('Component template should contain exactly one root element').toHaveBeenWarned()
  80. })
  81. it('v-pre directive', () => {
  82. const ast = parse('<div v-pre id="message1"><p>{{msg}}</p></div>', baseOptions)
  83. expect(ast.pre).toBe(true)
  84. expect(ast.attrs[0].name).toBe('id')
  85. expect(ast.attrs[0].value).toBe('"message1"')
  86. expect(ast.children[0].children[0].text).toBe('{{msg}}')
  87. })
  88. it('v-for directive basic syntax', () => {
  89. const ast = parse('<ul><li v-for="item in items"></li><ul>', baseOptions)
  90. const liAst = ast.children[0]
  91. expect(liAst.for).toBe('items')
  92. expect(liAst.alias).toBe('item')
  93. })
  94. it('v-for directive iteration syntax', () => {
  95. const ast = parse('<ul><li v-for="(index, item) in items"></li><ul>', baseOptions)
  96. const liAst = ast.children[0]
  97. expect(liAst.for).toBe('items')
  98. expect(liAst.alias).toBe('item')
  99. expect(liAst.iterator).toBe('index')
  100. })
  101. it('v-for directive track-by', () => {
  102. const ast = parse('<ul><li v-for="item in items" track-by="item.uid"></li><ul>', baseOptions)
  103. const liAst = ast.children[0]
  104. expect(liAst.for).toBe('items')
  105. expect(liAst.alias).toBe('item')
  106. expect(liAst.key).toBe('item.uid')
  107. })
  108. it('v-for directive invalid syntax', () => {
  109. parse('<ul><li v-for="item into items"></li><ul>', baseOptions)
  110. expect('Invalid v-for expression').toHaveBeenWarned()
  111. })
  112. it('v-if directive syntax', () => {
  113. const ast = parse('<p v-if="show">hello world</p>', baseOptions)
  114. expect(ast.if).toBe('show')
  115. })
  116. it('v-else directive syntax', () => {
  117. const ast = parse('<div><p v-if="show">hello</p><p v-else>world</p></div>', baseOptions)
  118. const ifAst = ast.children[0]
  119. const elseAst = ifAst.elseBlock
  120. expect(elseAst.else).toBe(true)
  121. expect(elseAst.children[0].text).toBe('world')
  122. expect(elseAst.parent).toBe(ast)
  123. })
  124. it('v-else directive invalid syntax', () => {
  125. parse('<div><p v-else>world</p></div>', baseOptions)
  126. expect('v-else used on element').toHaveBeenWarned()
  127. })
  128. it('v-once directive syntax', () => {
  129. const ast = parse('<p v-once>world</p>', baseOptions)
  130. expect(ast.once).toBe(true)
  131. })
  132. it('render tag syntax', () => {
  133. const ast = parse('<render :method="onRender", :args="params"></render>', baseOptions)
  134. expect(ast.render).toBe(true)
  135. expect(ast.renderMethod).toBe('onRender')
  136. expect(ast.renderArgs).toBe('params')
  137. })
  138. it('render tag invalid syntax', () => {
  139. // method nothing
  140. const invalidAst1 = parse('<render></render>', baseOptions)
  141. expect('method attribute is required on <render>.').toHaveBeenWarned()
  142. expect(invalidAst1.render).toBe(true)
  143. expect(invalidAst1.renderMethod).toBeUndefined()
  144. expect(invalidAst1.renderArgs).toBeUndefined()
  145. // method no dynamic binding
  146. parse('<render method="onRender"></render>', baseOptions)
  147. expect('<render> method should use a dynamic binding').toHaveBeenWarned()
  148. // args no dynamic binding
  149. parse('<render :method="onRender" args="params"></render>', baseOptions)
  150. expect('<render> args should use a dynamic binding').toHaveBeenWarned()
  151. })
  152. it('slot tag single syntax', () => {
  153. const ast = parse('<slot></slot>', baseOptions)
  154. expect(ast.tag).toBe('slot')
  155. expect(ast.slotName).toBeUndefined()
  156. })
  157. it('slot tag namped syntax', () => {
  158. const ast = parse('<slot name="one">hello world</slot>', baseOptions)
  159. expect(ast.tag).toBe('slot')
  160. expect(ast.slotName).toBe('"one"')
  161. })
  162. it('slot target', () => {
  163. const ast = parse('<p slot="one">hello world</p>', baseOptions)
  164. expect(ast.slotTarget).toBe('"one"')
  165. })
  166. it('component properties', () => {
  167. const ast = parse('<my-component :msg="hello"></my-component>', baseOptions)
  168. expect(ast.attrs[0].name).toBe('msg')
  169. expect(ast.attrs[0].value).toBe('hello')
  170. })
  171. it('component "is" attribute', () => {
  172. const ast = parse('<my-component is="component1"></my-component>', baseOptions)
  173. expect(ast.component).toBe('"component1"')
  174. })
  175. it('component "inline-template" attribute', () => {
  176. const ast = parse('<my-component inline-template>hello world</my-component>', baseOptions)
  177. expect(ast.inlineTemplate).toBe(true)
  178. })
  179. it('class binding', () => {
  180. // static
  181. const ast1 = parse('<p class="class1">hello world</p>', baseOptions)
  182. expect(ast1.staticClass).toBe('"class1"')
  183. // dynamic
  184. const ast2 = parse('<p :class="class1">hello world</p>', baseOptions)
  185. expect(ast2.classBinding).toBe('class1')
  186. // interpolation warning
  187. parse('<p class="{{error}}">hello world</p>', baseOptions)
  188. expect('Interpolation inside attributes has been deprecated').toHaveBeenWarned()
  189. })
  190. it('style binding', () => {
  191. const ast = parse('<p :style="error">hello world</p>', baseOptions)
  192. expect(ast.styleBinding).toBe('error')
  193. })
  194. it('transition', () => {
  195. const ast = parse('<p v-if="show" transition="expand">hello world</p>', baseOptions)
  196. expect(ast.transition).toBe('"expand"')
  197. expect(ast.transitionOnAppear).toBe(false)
  198. })
  199. it('transition with empty', () => {
  200. const ast = parse('<p v-if="show" transition="">hello world</p>', baseOptions)
  201. expect(ast.transition).toBe(true)
  202. expect(ast.transitionOnAppear).toBe(false)
  203. })
  204. it('attribute with v-bind', () => {
  205. const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
  206. expect(ast.attrsList[0].name).toBe('type')
  207. expect(ast.attrsList[0].value).toBe('text')
  208. expect(ast.attrsList[1].name).toBe('name')
  209. expect(ast.attrsList[1].value).toBe('field1')
  210. expect(ast.attrsMap['type']).toBe('text')
  211. expect(ast.attrsMap['name']).toBe('field1')
  212. expect(ast.staticAttrs[0].name).toBe('type')
  213. expect(ast.staticAttrs[0].value).toBe('"text"')
  214. expect(ast.staticAttrs[1].name).toBe('name')
  215. expect(ast.staticAttrs[1].value).toBe('"field1"')
  216. expect(ast.props[0].name).toBe('value')
  217. expect(ast.props[0].value).toBe('msg')
  218. })
  219. it('attribute with v-on', () => {
  220. const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
  221. expect(ast.events.input.value).toBe('onInput')
  222. })
  223. it('attribute with directive', () => {
  224. const ast = parse('<input type="text" name="field1" :value="msg" v-validate:field1="required">', baseOptions)
  225. expect(ast.directives[0].name).toBe('validate')
  226. expect(ast.directives[0].value).toBe('required')
  227. expect(ast.directives[0].arg).toBe('field1')
  228. })
  229. it('attribute with modifiered directive', () => {
  230. const ast = parse('<input type="text" name="field1" :value="msg" v-validate.on.off>', baseOptions)
  231. expect(ast.directives[0].modifiers.on).toBe(true)
  232. expect(ast.directives[0].modifiers.off).toBe(true)
  233. })
  234. it('literal attribute', () => {
  235. // basic
  236. const ast1 = parse('<input type="text" name="field1" value="hello world">', baseOptions)
  237. expect(ast1.attrsList[0].name).toBe('type')
  238. expect(ast1.attrsList[0].value).toBe('text')
  239. expect(ast1.attrsList[1].name).toBe('name')
  240. expect(ast1.attrsList[1].value).toBe('field1')
  241. expect(ast1.attrsList[2].name).toBe('value')
  242. expect(ast1.attrsList[2].value).toBe('hello world')
  243. expect(ast1.attrsMap['type']).toBe('text')
  244. expect(ast1.attrsMap['name']).toBe('field1')
  245. expect(ast1.attrsMap['value']).toBe('hello world')
  246. expect(ast1.staticAttrs[0].name).toBe('type')
  247. expect(ast1.staticAttrs[0].value).toBe('"text"')
  248. expect(ast1.staticAttrs[1].name).toBe('name')
  249. expect(ast1.staticAttrs[1].value).toBe('"field1"')
  250. expect(ast1.staticAttrs[2].name).toBe('value')
  251. expect(ast1.staticAttrs[2].value).toBe('"hello world"')
  252. // interpolation warning
  253. parse('<input type="text" name="field1" value="{{msg}}">', baseOptions)
  254. expect('Interpolation inside attributes has been deprecated').toHaveBeenWarned()
  255. })
  256. it('duplicate attribute', () => {
  257. parse('<p class="class1" class="class1">hello world</p>', baseOptions)
  258. expect('duplicate attribute').toHaveBeenWarned()
  259. })
  260. it('custom delimiter', () => {
  261. const ast = parse('<p>{msg}</p>', extend({ delimiters: ['{', '}'] }, baseOptions))
  262. expect(ast.children[0].expression).toBe('__toString__(msg)')
  263. })
  264. it('not specified getTagNamespace option', () => {
  265. const ast = parse('<svg><text>hello world</text></svg>', {
  266. expectHTML: true,
  267. preserveWhitespace: true,
  268. directives,
  269. isReservedTag,
  270. isUnaryTag,
  271. mustUseProp
  272. })
  273. expect(ast.tag).toBe('svg')
  274. expect(ast.ns).toBeUndefined()
  275. })
  276. it('not specified mustUseProp', () => {
  277. const ast = parse('<input type="text" name="field1" :value="msg">', {
  278. expectHTML: true,
  279. preserveWhitespace: true,
  280. directives,
  281. isReservedTag,
  282. isUnaryTag,
  283. getTagNamespace
  284. })
  285. expect(ast.props).toBeUndefined()
  286. })
  287. })