sfc-parser.spec.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { parseComponent } from 'sfc/parser'
  2. describe('Single File Component parser', () => {
  3. it('should parse', () => {
  4. const res = parseComponent(`
  5. <template>
  6. <div>hi</div>
  7. </template>
  8. <style src="./test.css"></style>
  9. <style lang="stylus" scoped>
  10. h1
  11. color red
  12. h2
  13. color green
  14. </style>
  15. <style module>
  16. h1 { font-weight: bold }
  17. </style>
  18. <style bool-attr val-attr="test"></style>
  19. <script>
  20. export default {}
  21. </script>
  22. <div>
  23. <style>nested should be ignored</style>
  24. </div>
  25. `)
  26. expect(res.template.content.trim()).toBe('<div>hi</div>')
  27. expect(res.styles.length).toBe(4)
  28. expect(res.styles[0].src).toBe('./test.css')
  29. expect(res.styles[1].lang).toBe('stylus')
  30. expect(res.styles[1].scoped).toBe(true)
  31. expect(res.styles[1].content.trim()).toBe('h1\n color red\nh2\n color green')
  32. expect(res.styles[2].module).toBe(true)
  33. expect(res.styles[3].attrs['bool-attr']).toBe(true)
  34. expect(res.styles[3].attrs['val-attr']).toBe('test')
  35. expect(res.script.content.trim()).toBe('export default {}')
  36. })
  37. it('should parse template with closed input', () => {
  38. const res = parseComponent(`
  39. <template>
  40. <input type="text"/>
  41. </template>
  42. `)
  43. expect(res.template.content.trim()).toBe('<input type="text"/>')
  44. })
  45. it('should handle nested template', () => {
  46. const res = parseComponent(`
  47. <template>
  48. <div><template v-if="ok">hi</template></div>
  49. </template>
  50. `)
  51. expect(res.template.content.trim()).toBe('<div><template v-if="ok">hi</template></div>')
  52. })
  53. it('pad content', () => {
  54. const content = `
  55. <template>
  56. <div></div>
  57. </template>
  58. <script>
  59. export default {}
  60. </script>
  61. <style>
  62. h1 { color: red }
  63. </style>
  64. `
  65. const padDefault = parseComponent(content.trim(), { pad: true })
  66. const padLine = parseComponent(content.trim(), { pad: 'line' })
  67. const padSpace = parseComponent(content.trim(), { pad: 'space' })
  68. expect(padDefault.template.content).toBe(Array(1).join('\n') + `
  69. <div></div>
  70. `)
  71. expect(padDefault.script.content).toBe(Array(3 + 1).join('//\n') + `
  72. export default {}
  73. `)
  74. expect(padDefault.styles[0].content).toBe(Array(6 + 1).join('\n') + `
  75. h1 { color: red }
  76. `)
  77. expect(padLine.template.content).toBe(Array(1).join('\n') + `
  78. <div></div>
  79. `)
  80. expect(padLine.script.content).toBe(Array(3 + 1).join('//\n') + `
  81. export default {}
  82. `)
  83. expect(padLine.styles[0].content).toBe(Array(6 + 1).join('\n') + `
  84. h1 { color: red }
  85. `)
  86. expect(padSpace.template.content).toBe(`<template>`.replace(/./g, ' ') + `
  87. <div></div>
  88. `)
  89. expect(padSpace.script.content).toBe(`<template>
  90. <div></div>
  91. </template>
  92. <script>`.replace(/./g, ' ') + `
  93. export default {}
  94. `)
  95. expect(padSpace.styles[0].content).toBe(`<template>
  96. <div></div>
  97. </template>
  98. <script>
  99. export default {}
  100. </script>
  101. <style>`.replace(/./g, ' ') + `
  102. h1 { color: red }
  103. `)
  104. })
  105. it('should handle template blocks with lang as special text', () => {
  106. const res = parseComponent(`
  107. <template lang="pug">
  108. div
  109. h1(v-if='1 < 2') hello
  110. </template>
  111. `)
  112. expect(res.template.content.trim()).toBe(`div\n h1(v-if='1 < 2') hello`)
  113. })
  114. it('should handle component contains "<" only', () => {
  115. const res = parseComponent(`
  116. <template>
  117. <span><</span>
  118. </template>
  119. `)
  120. expect(res.template.content.trim()).toBe(`<span><</span>`)
  121. })
  122. it('should handle custom blocks without parsing them', () => {
  123. const res = parseComponent(`
  124. <template>
  125. <div></div>
  126. </template>
  127. <example name="simple">
  128. <my-button ref="button">Hello</my-button>
  129. </example>
  130. <example name="with props">
  131. <my-button color="red">Hello</my-button>
  132. </example>
  133. <test name="simple" foo="bar">
  134. export default function simple (vm) {
  135. describe('Hello', () => {
  136. it('should display Hello', () => {
  137. this.vm.$refs.button.$el.innerText.should.equal('Hello')
  138. }))
  139. }))
  140. }
  141. </test>
  142. `)
  143. expect(res.customBlocks.length).toBe(3)
  144. const simpleExample = res.customBlocks[0]
  145. expect(simpleExample.type).toBe('example')
  146. expect(simpleExample.content.trim()).toBe('<my-button ref="button">Hello</my-button>')
  147. expect(simpleExample.attrs.name).toBe('simple')
  148. const withProps = res.customBlocks[1]
  149. expect(withProps.type).toBe('example')
  150. expect(withProps.content.trim()).toBe('<my-button color="red">Hello</my-button>')
  151. expect(withProps.attrs.name).toBe('with props')
  152. const simpleTest = res.customBlocks[2]
  153. expect(simpleTest.type).toBe('test')
  154. expect(simpleTest.content.trim()).toBe(`export default function simple (vm) {
  155. describe('Hello', () => {
  156. it('should display Hello', () => {
  157. this.vm.$refs.button.$el.innerText.should.equal('Hello')
  158. }))
  159. }))
  160. }`)
  161. expect(simpleTest.attrs.name).toBe('simple')
  162. expect(simpleTest.attrs.foo).toBe('bar')
  163. })
  164. // Regression #4289
  165. it('accepts nested template tag', () => {
  166. const raw = `<div>
  167. <template v-if="true === true">
  168. <section class="section">
  169. <div class="container">
  170. Should be shown
  171. </div>
  172. </section>
  173. </template>
  174. <template v-else>
  175. <p>Should not be shown</p>
  176. </template>
  177. </div>`
  178. const res = parseComponent(`<template>${raw}</template>`)
  179. expect(res.template.content.trim()).toBe(raw)
  180. })
  181. it('should not hang on trailing text', () => {
  182. const res = parseComponent(`<template>hi</`)
  183. expect(res.template.content).toBe('hi')
  184. })
  185. })