parse.spec.ts 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142
  1. import { ParserOptions } from '../src/options'
  2. import { ErrorCodes } from '../src/errors'
  3. import {
  4. CommentNode,
  5. ElementNode,
  6. ElementTypes,
  7. Namespaces,
  8. NodeTypes,
  9. Position,
  10. TextNode,
  11. InterpolationNode,
  12. ConstantTypes,
  13. DirectiveNode
  14. } from '../src/ast'
  15. import { baseParse } from '../src/parser'
  16. import { Program } from '@babel/types'
  17. /* eslint jest/no-disabled-tests: "off" */
  18. describe('compiler: parse', () => {
  19. describe('Text', () => {
  20. test('simple text', () => {
  21. const ast = baseParse('some text')
  22. const text = ast.children[0] as TextNode
  23. expect(text).toStrictEqual({
  24. type: NodeTypes.TEXT,
  25. content: 'some text',
  26. loc: {
  27. start: { offset: 0, line: 1, column: 1 },
  28. end: { offset: 9, line: 1, column: 10 },
  29. source: 'some text'
  30. }
  31. })
  32. })
  33. test('simple text with invalid end tag', () => {
  34. const onError = vi.fn()
  35. const ast = baseParse('some text</div>', { onError })
  36. const text = ast.children[0] as TextNode
  37. expect(onError.mock.calls).toMatchObject([
  38. [
  39. {
  40. code: ErrorCodes.X_INVALID_END_TAG,
  41. loc: {
  42. start: { column: 10, line: 1, offset: 9 },
  43. end: { column: 10, line: 1, offset: 9 }
  44. }
  45. }
  46. ]
  47. ])
  48. expect(text).toStrictEqual({
  49. type: NodeTypes.TEXT,
  50. content: 'some text',
  51. loc: {
  52. start: { offset: 0, line: 1, column: 1 },
  53. end: { offset: 9, line: 1, column: 10 },
  54. source: 'some text'
  55. }
  56. })
  57. })
  58. test('text with interpolation', () => {
  59. const ast = baseParse('some {{ foo + bar }} text')
  60. const text1 = ast.children[0] as TextNode
  61. const text2 = ast.children[2] as TextNode
  62. expect(text1).toStrictEqual({
  63. type: NodeTypes.TEXT,
  64. content: 'some ',
  65. loc: {
  66. start: { offset: 0, line: 1, column: 1 },
  67. end: { offset: 5, line: 1, column: 6 },
  68. source: 'some '
  69. }
  70. })
  71. expect(text2).toStrictEqual({
  72. type: NodeTypes.TEXT,
  73. content: ' text',
  74. loc: {
  75. start: { offset: 20, line: 1, column: 21 },
  76. end: { offset: 25, line: 1, column: 26 },
  77. source: ' text'
  78. }
  79. })
  80. })
  81. test('text with interpolation which has `<`', () => {
  82. const ast = baseParse('some {{ a<b && c>d }} text')
  83. const text1 = ast.children[0] as TextNode
  84. const text2 = ast.children[2] as TextNode
  85. expect(text1).toStrictEqual({
  86. type: NodeTypes.TEXT,
  87. content: 'some ',
  88. loc: {
  89. start: { offset: 0, line: 1, column: 1 },
  90. end: { offset: 5, line: 1, column: 6 },
  91. source: 'some '
  92. }
  93. })
  94. expect(text2).toStrictEqual({
  95. type: NodeTypes.TEXT,
  96. content: ' text',
  97. loc: {
  98. start: { offset: 21, line: 1, column: 22 },
  99. end: { offset: 26, line: 1, column: 27 },
  100. source: ' text'
  101. }
  102. })
  103. })
  104. test('text with mix of tags and interpolations', () => {
  105. const ast = baseParse('some <span>{{ foo < bar + foo }} text</span>')
  106. const text1 = ast.children[0] as TextNode
  107. const text2 = (ast.children[1] as ElementNode).children![1] as TextNode
  108. expect(text1).toStrictEqual({
  109. type: NodeTypes.TEXT,
  110. content: 'some ',
  111. loc: {
  112. start: { offset: 0, line: 1, column: 1 },
  113. end: { offset: 5, line: 1, column: 6 },
  114. source: 'some '
  115. }
  116. })
  117. expect(text2).toStrictEqual({
  118. type: NodeTypes.TEXT,
  119. content: ' text',
  120. loc: {
  121. start: { offset: 32, line: 1, column: 33 },
  122. end: { offset: 37, line: 1, column: 38 },
  123. source: ' text'
  124. }
  125. })
  126. })
  127. test('lonely "<" doesn\'t separate nodes', () => {
  128. const ast = baseParse('a < b', {
  129. onError: err => {
  130. if (err.code !== ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME) {
  131. throw err
  132. }
  133. }
  134. })
  135. const text = ast.children[0] as TextNode
  136. expect(text).toStrictEqual({
  137. type: NodeTypes.TEXT,
  138. content: 'a < b',
  139. loc: {
  140. start: { offset: 0, line: 1, column: 1 },
  141. end: { offset: 5, line: 1, column: 6 },
  142. source: 'a < b'
  143. }
  144. })
  145. })
  146. test('lonely "{{" doesn\'t separate nodes', () => {
  147. const ast = baseParse('a {{ b', {
  148. onError: error => {
  149. if (error.code !== ErrorCodes.X_MISSING_INTERPOLATION_END) {
  150. throw error
  151. }
  152. }
  153. })
  154. const text = ast.children[0] as TextNode
  155. expect(text).toStrictEqual({
  156. type: NodeTypes.TEXT,
  157. content: 'a {{ b',
  158. loc: {
  159. start: { offset: 0, line: 1, column: 1 },
  160. end: { offset: 6, line: 1, column: 7 },
  161. source: 'a {{ b'
  162. }
  163. })
  164. })
  165. })
  166. describe('Interpolation', () => {
  167. test('simple interpolation', () => {
  168. const ast = baseParse('{{message}}')
  169. const interpolation = ast.children[0] as InterpolationNode
  170. expect(interpolation).toStrictEqual({
  171. type: NodeTypes.INTERPOLATION,
  172. content: {
  173. type: NodeTypes.SIMPLE_EXPRESSION,
  174. content: `message`,
  175. isStatic: false,
  176. constType: ConstantTypes.NOT_CONSTANT,
  177. loc: {
  178. start: { offset: 2, line: 1, column: 3 },
  179. end: { offset: 9, line: 1, column: 10 },
  180. source: 'message'
  181. }
  182. },
  183. loc: {
  184. start: { offset: 0, line: 1, column: 1 },
  185. end: { offset: 11, line: 1, column: 12 },
  186. source: '{{message}}'
  187. }
  188. })
  189. })
  190. test('it can have tag-like notation', () => {
  191. const ast = baseParse('{{ a<b }}')
  192. const interpolation = ast.children[0] as InterpolationNode
  193. expect(interpolation).toStrictEqual({
  194. type: NodeTypes.INTERPOLATION,
  195. content: {
  196. type: NodeTypes.SIMPLE_EXPRESSION,
  197. content: `a<b`,
  198. isStatic: false,
  199. constType: ConstantTypes.NOT_CONSTANT,
  200. loc: {
  201. start: { offset: 3, line: 1, column: 4 },
  202. end: { offset: 6, line: 1, column: 7 },
  203. source: 'a<b'
  204. }
  205. },
  206. loc: {
  207. start: { offset: 0, line: 1, column: 1 },
  208. end: { offset: 9, line: 1, column: 10 },
  209. source: '{{ a<b }}'
  210. }
  211. })
  212. })
  213. test('it can have tag-like notation (2)', () => {
  214. const ast = baseParse('{{ a<b }}{{ c>d }}')
  215. const interpolation1 = ast.children[0] as InterpolationNode
  216. const interpolation2 = ast.children[1] as InterpolationNode
  217. expect(interpolation1).toStrictEqual({
  218. type: NodeTypes.INTERPOLATION,
  219. content: {
  220. type: NodeTypes.SIMPLE_EXPRESSION,
  221. content: `a<b`,
  222. isStatic: false,
  223. constType: ConstantTypes.NOT_CONSTANT,
  224. loc: {
  225. start: { offset: 3, line: 1, column: 4 },
  226. end: { offset: 6, line: 1, column: 7 },
  227. source: 'a<b'
  228. }
  229. },
  230. loc: {
  231. start: { offset: 0, line: 1, column: 1 },
  232. end: { offset: 9, line: 1, column: 10 },
  233. source: '{{ a<b }}'
  234. }
  235. })
  236. expect(interpolation2).toStrictEqual({
  237. type: NodeTypes.INTERPOLATION,
  238. content: {
  239. type: NodeTypes.SIMPLE_EXPRESSION,
  240. isStatic: false,
  241. constType: ConstantTypes.NOT_CONSTANT,
  242. content: 'c>d',
  243. loc: {
  244. start: { offset: 12, line: 1, column: 13 },
  245. end: { offset: 15, line: 1, column: 16 },
  246. source: 'c>d'
  247. }
  248. },
  249. loc: {
  250. start: { offset: 9, line: 1, column: 10 },
  251. end: { offset: 18, line: 1, column: 19 },
  252. source: '{{ c>d }}'
  253. }
  254. })
  255. })
  256. test('it can have tag-like notation (3)', () => {
  257. const ast = baseParse('<div>{{ "</div>" }}</div>')
  258. const element = ast.children[0] as ElementNode
  259. const interpolation = element.children[0] as InterpolationNode
  260. expect(interpolation).toStrictEqual({
  261. type: NodeTypes.INTERPOLATION,
  262. content: {
  263. type: NodeTypes.SIMPLE_EXPRESSION,
  264. isStatic: false,
  265. // The `constType` is the default value and will be determined in `transformExpression`.
  266. constType: ConstantTypes.NOT_CONSTANT,
  267. content: '"</div>"',
  268. loc: {
  269. start: { offset: 8, line: 1, column: 9 },
  270. end: { offset: 16, line: 1, column: 17 },
  271. source: '"</div>"'
  272. }
  273. },
  274. loc: {
  275. start: { offset: 5, line: 1, column: 6 },
  276. end: { offset: 19, line: 1, column: 20 },
  277. source: '{{ "</div>" }}'
  278. }
  279. })
  280. })
  281. test('custom delimiters', () => {
  282. const ast = baseParse('<p>{msg}</p>', {
  283. delimiters: ['{', '}']
  284. })
  285. const element = ast.children[0] as ElementNode
  286. const interpolation = element.children[0] as InterpolationNode
  287. expect(interpolation).toStrictEqual({
  288. type: NodeTypes.INTERPOLATION,
  289. content: {
  290. type: NodeTypes.SIMPLE_EXPRESSION,
  291. content: `msg`,
  292. isStatic: false,
  293. constType: ConstantTypes.NOT_CONSTANT,
  294. loc: {
  295. start: { offset: 4, line: 1, column: 5 },
  296. end: { offset: 7, line: 1, column: 8 },
  297. source: 'msg'
  298. }
  299. },
  300. loc: {
  301. start: { offset: 3, line: 1, column: 4 },
  302. end: { offset: 8, line: 1, column: 9 },
  303. source: '{msg}'
  304. }
  305. })
  306. })
  307. })
  308. describe('Comment', () => {
  309. test('empty comment', () => {
  310. const ast = baseParse('<!---->')
  311. const comment = ast.children[0] as CommentNode
  312. expect(comment).toStrictEqual({
  313. type: NodeTypes.COMMENT,
  314. content: '',
  315. loc: {
  316. start: { offset: 0, line: 1, column: 1 },
  317. end: { offset: 7, line: 1, column: 8 },
  318. source: '<!---->'
  319. }
  320. })
  321. })
  322. test('simple comment', () => {
  323. const ast = baseParse('<!--abc-->')
  324. const comment = ast.children[0] as CommentNode
  325. expect(comment).toStrictEqual({
  326. type: NodeTypes.COMMENT,
  327. content: 'abc',
  328. loc: {
  329. start: { offset: 0, line: 1, column: 1 },
  330. end: { offset: 10, line: 1, column: 11 },
  331. source: '<!--abc-->'
  332. }
  333. })
  334. })
  335. test('two comments', () => {
  336. const ast = baseParse('<!--abc--><!--def-->')
  337. const comment1 = ast.children[0] as CommentNode
  338. const comment2 = ast.children[1] as CommentNode
  339. expect(comment1).toStrictEqual({
  340. type: NodeTypes.COMMENT,
  341. content: 'abc',
  342. loc: {
  343. start: { offset: 0, line: 1, column: 1 },
  344. end: { offset: 10, line: 1, column: 11 },
  345. source: '<!--abc-->'
  346. }
  347. })
  348. expect(comment2).toStrictEqual({
  349. type: NodeTypes.COMMENT,
  350. content: 'def',
  351. loc: {
  352. start: { offset: 10, line: 1, column: 11 },
  353. end: { offset: 20, line: 1, column: 21 },
  354. source: '<!--def-->'
  355. }
  356. })
  357. })
  358. test('comments option', () => {
  359. const astOptionNoComment = baseParse('<!--abc-->', { comments: false })
  360. const astOptionWithComments = baseParse('<!--abc-->', { comments: true })
  361. expect(astOptionNoComment.children).toHaveLength(0)
  362. expect(astOptionWithComments.children).toHaveLength(1)
  363. })
  364. // #2217
  365. test('comments in the <pre> tag should be removed when comments option requires it', () => {
  366. const rawText = `<p/><!-- foo --><p/>`
  367. const astWithComments = baseParse(`<pre>${rawText}</pre>`, {
  368. comments: true
  369. })
  370. expect(
  371. (astWithComments.children[0] as ElementNode).children
  372. ).toMatchObject([
  373. {
  374. type: NodeTypes.ELEMENT,
  375. tag: 'p'
  376. },
  377. {
  378. type: NodeTypes.COMMENT
  379. },
  380. {
  381. type: NodeTypes.ELEMENT,
  382. tag: 'p'
  383. }
  384. ])
  385. const astWithoutComments = baseParse(`<pre>${rawText}</pre>`, {
  386. comments: false
  387. })
  388. expect(
  389. (astWithoutComments.children[0] as ElementNode).children
  390. ).toMatchObject([
  391. {
  392. type: NodeTypes.ELEMENT,
  393. tag: 'p'
  394. },
  395. {
  396. type: NodeTypes.ELEMENT,
  397. tag: 'p'
  398. }
  399. ])
  400. })
  401. })
  402. describe('Element', () => {
  403. test('simple div', () => {
  404. const ast = baseParse('<div>hello</div>')
  405. const element = ast.children[0] as ElementNode
  406. expect(element).toStrictEqual({
  407. type: NodeTypes.ELEMENT,
  408. ns: Namespaces.HTML,
  409. tag: 'div',
  410. tagType: ElementTypes.ELEMENT,
  411. codegenNode: undefined,
  412. props: [],
  413. children: [
  414. {
  415. type: NodeTypes.TEXT,
  416. content: 'hello',
  417. loc: {
  418. start: { offset: 5, line: 1, column: 6 },
  419. end: { offset: 10, line: 1, column: 11 },
  420. source: 'hello'
  421. }
  422. }
  423. ],
  424. loc: {
  425. start: { offset: 0, line: 1, column: 1 },
  426. end: { offset: 16, line: 1, column: 17 },
  427. source: '<div>hello</div>'
  428. }
  429. })
  430. })
  431. test('empty', () => {
  432. const ast = baseParse('<div></div>')
  433. const element = ast.children[0] as ElementNode
  434. expect(element).toStrictEqual({
  435. type: NodeTypes.ELEMENT,
  436. ns: Namespaces.HTML,
  437. tag: 'div',
  438. tagType: ElementTypes.ELEMENT,
  439. codegenNode: undefined,
  440. props: [],
  441. children: [],
  442. loc: {
  443. start: { offset: 0, line: 1, column: 1 },
  444. end: { offset: 11, line: 1, column: 12 },
  445. source: '<div></div>'
  446. }
  447. })
  448. })
  449. test('self closing', () => {
  450. const ast = baseParse('<div/>after')
  451. const element = ast.children[0] as ElementNode
  452. expect(element).toStrictEqual({
  453. type: NodeTypes.ELEMENT,
  454. ns: Namespaces.HTML,
  455. tag: 'div',
  456. tagType: ElementTypes.ELEMENT,
  457. codegenNode: undefined,
  458. props: [],
  459. children: [],
  460. isSelfClosing: true,
  461. loc: {
  462. start: { offset: 0, line: 1, column: 1 },
  463. end: { offset: 6, line: 1, column: 7 },
  464. source: '<div/>'
  465. }
  466. })
  467. })
  468. test('void element', () => {
  469. const ast = baseParse('<img>after', {
  470. isVoidTag: tag => tag === 'img'
  471. })
  472. const element = ast.children[0] as ElementNode
  473. expect(element).toStrictEqual({
  474. type: NodeTypes.ELEMENT,
  475. ns: Namespaces.HTML,
  476. tag: 'img',
  477. tagType: ElementTypes.ELEMENT,
  478. codegenNode: undefined,
  479. props: [],
  480. children: [],
  481. loc: {
  482. start: { offset: 0, line: 1, column: 1 },
  483. end: { offset: 5, line: 1, column: 6 },
  484. source: '<img>'
  485. }
  486. })
  487. })
  488. test('self-closing void element', () => {
  489. const ast = baseParse('<img/>after', {
  490. isVoidTag: tag => tag === 'img'
  491. })
  492. const element = ast.children[0] as ElementNode
  493. expect(element).toStrictEqual({
  494. type: NodeTypes.ELEMENT,
  495. ns: Namespaces.HTML,
  496. tag: 'img',
  497. tagType: ElementTypes.ELEMENT,
  498. codegenNode: undefined,
  499. props: [],
  500. children: [],
  501. isSelfClosing: true,
  502. loc: {
  503. start: { offset: 0, line: 1, column: 1 },
  504. end: { offset: 6, line: 1, column: 7 },
  505. source: '<img/>'
  506. }
  507. })
  508. })
  509. test('template element with directives', () => {
  510. const ast = baseParse('<template v-if="ok"></template>')
  511. const element = ast.children[0]
  512. expect(element).toMatchObject({
  513. type: NodeTypes.ELEMENT,
  514. tagType: ElementTypes.TEMPLATE
  515. })
  516. })
  517. test('template element without directives', () => {
  518. const ast = baseParse('<template></template>')
  519. const element = ast.children[0]
  520. expect(element).toMatchObject({
  521. type: NodeTypes.ELEMENT,
  522. tagType: ElementTypes.ELEMENT
  523. })
  524. })
  525. test('native element with `isNativeTag`', () => {
  526. const ast = baseParse('<div></div><comp></comp><Comp></Comp>', {
  527. isNativeTag: tag => tag === 'div'
  528. })
  529. expect(ast.children[0]).toMatchObject({
  530. type: NodeTypes.ELEMENT,
  531. tag: 'div',
  532. tagType: ElementTypes.ELEMENT
  533. })
  534. expect(ast.children[1]).toMatchObject({
  535. type: NodeTypes.ELEMENT,
  536. tag: 'comp',
  537. tagType: ElementTypes.COMPONENT
  538. })
  539. expect(ast.children[2]).toMatchObject({
  540. type: NodeTypes.ELEMENT,
  541. tag: 'Comp',
  542. tagType: ElementTypes.COMPONENT
  543. })
  544. })
  545. test('native element without `isNativeTag`', () => {
  546. const ast = baseParse('<div></div><comp></comp><Comp></Comp>')
  547. expect(ast.children[0]).toMatchObject({
  548. type: NodeTypes.ELEMENT,
  549. tag: 'div',
  550. tagType: ElementTypes.ELEMENT
  551. })
  552. expect(ast.children[1]).toMatchObject({
  553. type: NodeTypes.ELEMENT,
  554. tag: 'comp',
  555. tagType: ElementTypes.ELEMENT
  556. })
  557. expect(ast.children[2]).toMatchObject({
  558. type: NodeTypes.ELEMENT,
  559. tag: 'Comp',
  560. tagType: ElementTypes.COMPONENT
  561. })
  562. })
  563. test('is casting with `isNativeTag`', () => {
  564. const ast = baseParse(
  565. `<div></div><div is="vue:foo"></div><Comp></Comp>`,
  566. {
  567. isNativeTag: tag => tag === 'div'
  568. }
  569. )
  570. expect(ast.children[0]).toMatchObject({
  571. type: NodeTypes.ELEMENT,
  572. tag: 'div',
  573. tagType: ElementTypes.ELEMENT
  574. })
  575. expect(ast.children[1]).toMatchObject({
  576. type: NodeTypes.ELEMENT,
  577. tag: 'div',
  578. tagType: ElementTypes.COMPONENT
  579. })
  580. expect(ast.children[2]).toMatchObject({
  581. type: NodeTypes.ELEMENT,
  582. tag: 'Comp',
  583. tagType: ElementTypes.COMPONENT
  584. })
  585. })
  586. test('is casting without `isNativeTag`', () => {
  587. const ast = baseParse(`<div></div><div is="vue:foo"></div><Comp></Comp>`)
  588. expect(ast.children[0]).toMatchObject({
  589. type: NodeTypes.ELEMENT,
  590. tag: 'div',
  591. tagType: ElementTypes.ELEMENT
  592. })
  593. expect(ast.children[1]).toMatchObject({
  594. type: NodeTypes.ELEMENT,
  595. tag: 'div',
  596. tagType: ElementTypes.COMPONENT
  597. })
  598. expect(ast.children[2]).toMatchObject({
  599. type: NodeTypes.ELEMENT,
  600. tag: 'Comp',
  601. tagType: ElementTypes.COMPONENT
  602. })
  603. })
  604. test('custom element', () => {
  605. const ast = baseParse('<div></div><comp></comp>', {
  606. isNativeTag: tag => tag === 'div',
  607. isCustomElement: tag => tag === 'comp'
  608. })
  609. expect(ast.children[0]).toMatchObject({
  610. type: NodeTypes.ELEMENT,
  611. tag: 'div',
  612. tagType: ElementTypes.ELEMENT
  613. })
  614. expect(ast.children[1]).toMatchObject({
  615. type: NodeTypes.ELEMENT,
  616. tag: 'comp',
  617. tagType: ElementTypes.ELEMENT
  618. })
  619. })
  620. test('built-in component', () => {
  621. const ast = baseParse('<div></div><comp></comp>', {
  622. isBuiltInComponent: tag => (tag === 'comp' ? Symbol() : void 0)
  623. })
  624. expect(ast.children[0]).toMatchObject({
  625. type: NodeTypes.ELEMENT,
  626. tag: 'div',
  627. tagType: ElementTypes.ELEMENT
  628. })
  629. expect(ast.children[1]).toMatchObject({
  630. type: NodeTypes.ELEMENT,
  631. tag: 'comp',
  632. tagType: ElementTypes.COMPONENT
  633. })
  634. })
  635. test('slot element', () => {
  636. const ast = baseParse('<slot></slot><Comp></Comp>')
  637. expect(ast.children[0]).toMatchObject({
  638. type: NodeTypes.ELEMENT,
  639. tag: 'slot',
  640. tagType: ElementTypes.SLOT
  641. })
  642. expect(ast.children[1]).toMatchObject({
  643. type: NodeTypes.ELEMENT,
  644. tag: 'Comp',
  645. tagType: ElementTypes.COMPONENT
  646. })
  647. })
  648. test('attribute with no value', () => {
  649. const ast = baseParse('<div id></div>')
  650. const element = ast.children[0] as ElementNode
  651. expect(element).toStrictEqual({
  652. type: NodeTypes.ELEMENT,
  653. ns: Namespaces.HTML,
  654. tag: 'div',
  655. tagType: ElementTypes.ELEMENT,
  656. codegenNode: undefined,
  657. props: [
  658. {
  659. type: NodeTypes.ATTRIBUTE,
  660. name: 'id',
  661. nameLoc: {
  662. start: { offset: 5, line: 1, column: 6 },
  663. end: { offset: 7, line: 1, column: 8 },
  664. source: 'id'
  665. },
  666. value: undefined,
  667. loc: {
  668. start: { offset: 5, line: 1, column: 6 },
  669. end: { offset: 7, line: 1, column: 8 },
  670. source: 'id'
  671. }
  672. }
  673. ],
  674. children: [],
  675. loc: {
  676. start: { offset: 0, line: 1, column: 1 },
  677. end: { offset: 14, line: 1, column: 15 },
  678. source: '<div id></div>'
  679. }
  680. })
  681. })
  682. test('attribute with empty value, double quote', () => {
  683. const ast = baseParse('<div id=""></div>')
  684. const element = ast.children[0] as ElementNode
  685. expect(element).toStrictEqual({
  686. type: NodeTypes.ELEMENT,
  687. ns: Namespaces.HTML,
  688. tag: 'div',
  689. tagType: ElementTypes.ELEMENT,
  690. codegenNode: undefined,
  691. props: [
  692. {
  693. type: NodeTypes.ATTRIBUTE,
  694. name: 'id',
  695. nameLoc: {
  696. start: { offset: 5, line: 1, column: 6 },
  697. end: { offset: 7, line: 1, column: 8 },
  698. source: 'id'
  699. },
  700. value: {
  701. type: NodeTypes.TEXT,
  702. content: '',
  703. loc: {
  704. start: { offset: 8, line: 1, column: 9 },
  705. end: { offset: 10, line: 1, column: 11 },
  706. source: '""'
  707. }
  708. },
  709. loc: {
  710. start: { offset: 5, line: 1, column: 6 },
  711. end: { offset: 10, line: 1, column: 11 },
  712. source: 'id=""'
  713. }
  714. }
  715. ],
  716. children: [],
  717. loc: {
  718. start: { offset: 0, line: 1, column: 1 },
  719. end: { offset: 17, line: 1, column: 18 },
  720. source: '<div id=""></div>'
  721. }
  722. })
  723. })
  724. test('attribute with empty value, single quote', () => {
  725. const ast = baseParse("<div id=''></div>")
  726. const element = ast.children[0] as ElementNode
  727. expect(element).toStrictEqual({
  728. type: NodeTypes.ELEMENT,
  729. ns: Namespaces.HTML,
  730. tag: 'div',
  731. tagType: ElementTypes.ELEMENT,
  732. codegenNode: undefined,
  733. props: [
  734. {
  735. type: NodeTypes.ATTRIBUTE,
  736. name: 'id',
  737. nameLoc: {
  738. start: { offset: 5, line: 1, column: 6 },
  739. end: { offset: 7, line: 1, column: 8 },
  740. source: 'id'
  741. },
  742. value: {
  743. type: NodeTypes.TEXT,
  744. content: '',
  745. loc: {
  746. start: { offset: 8, line: 1, column: 9 },
  747. end: { offset: 10, line: 1, column: 11 },
  748. source: "''"
  749. }
  750. },
  751. loc: {
  752. start: { offset: 5, line: 1, column: 6 },
  753. end: { offset: 10, line: 1, column: 11 },
  754. source: "id=''"
  755. }
  756. }
  757. ],
  758. children: [],
  759. loc: {
  760. start: { offset: 0, line: 1, column: 1 },
  761. end: { offset: 17, line: 1, column: 18 },
  762. source: "<div id=''></div>"
  763. }
  764. })
  765. })
  766. test('attribute with value, double quote', () => {
  767. const ast = baseParse('<div id=">\'"></div>')
  768. const element = ast.children[0] as ElementNode
  769. expect(element).toStrictEqual({
  770. type: NodeTypes.ELEMENT,
  771. ns: Namespaces.HTML,
  772. tag: 'div',
  773. tagType: ElementTypes.ELEMENT,
  774. codegenNode: undefined,
  775. props: [
  776. {
  777. type: NodeTypes.ATTRIBUTE,
  778. name: 'id',
  779. nameLoc: {
  780. start: { offset: 5, line: 1, column: 6 },
  781. end: { offset: 7, line: 1, column: 8 },
  782. source: 'id'
  783. },
  784. value: {
  785. type: NodeTypes.TEXT,
  786. content: ">'",
  787. loc: {
  788. start: { offset: 8, line: 1, column: 9 },
  789. end: { offset: 12, line: 1, column: 13 },
  790. source: '">\'"'
  791. }
  792. },
  793. loc: {
  794. start: { offset: 5, line: 1, column: 6 },
  795. end: { offset: 12, line: 1, column: 13 },
  796. source: 'id=">\'"'
  797. }
  798. }
  799. ],
  800. children: [],
  801. loc: {
  802. start: { offset: 0, line: 1, column: 1 },
  803. end: { offset: 19, line: 1, column: 20 },
  804. source: '<div id=">\'"></div>'
  805. }
  806. })
  807. })
  808. test('attribute with value, single quote', () => {
  809. const ast = baseParse("<div id='>\"'></div>")
  810. const element = ast.children[0] as ElementNode
  811. expect(element).toStrictEqual({
  812. type: NodeTypes.ELEMENT,
  813. ns: Namespaces.HTML,
  814. tag: 'div',
  815. tagType: ElementTypes.ELEMENT,
  816. codegenNode: undefined,
  817. props: [
  818. {
  819. type: NodeTypes.ATTRIBUTE,
  820. name: 'id',
  821. nameLoc: {
  822. start: { offset: 5, line: 1, column: 6 },
  823. end: { offset: 7, line: 1, column: 8 },
  824. source: 'id'
  825. },
  826. value: {
  827. type: NodeTypes.TEXT,
  828. content: '>"',
  829. loc: {
  830. start: { offset: 8, line: 1, column: 9 },
  831. end: { offset: 12, line: 1, column: 13 },
  832. source: "'>\"'"
  833. }
  834. },
  835. loc: {
  836. start: { offset: 5, line: 1, column: 6 },
  837. end: { offset: 12, line: 1, column: 13 },
  838. source: "id='>\"'"
  839. }
  840. }
  841. ],
  842. children: [],
  843. loc: {
  844. start: { offset: 0, line: 1, column: 1 },
  845. end: { offset: 19, line: 1, column: 20 },
  846. source: "<div id='>\"'></div>"
  847. }
  848. })
  849. })
  850. test('attribute with value, unquoted', () => {
  851. const ast = baseParse('<div id=a/></div>')
  852. const element = ast.children[0] as ElementNode
  853. expect(element).toStrictEqual({
  854. type: NodeTypes.ELEMENT,
  855. ns: Namespaces.HTML,
  856. tag: 'div',
  857. tagType: ElementTypes.ELEMENT,
  858. codegenNode: undefined,
  859. props: [
  860. {
  861. type: NodeTypes.ATTRIBUTE,
  862. name: 'id',
  863. nameLoc: {
  864. start: { offset: 5, line: 1, column: 6 },
  865. end: { offset: 7, line: 1, column: 8 },
  866. source: 'id'
  867. },
  868. value: {
  869. type: NodeTypes.TEXT,
  870. content: 'a/',
  871. loc: {
  872. start: { offset: 8, line: 1, column: 9 },
  873. end: { offset: 10, line: 1, column: 11 },
  874. source: 'a/'
  875. }
  876. },
  877. loc: {
  878. start: { offset: 5, line: 1, column: 6 },
  879. end: { offset: 10, line: 1, column: 11 },
  880. source: 'id=a/'
  881. }
  882. }
  883. ],
  884. children: [],
  885. loc: {
  886. start: { offset: 0, line: 1, column: 1 },
  887. end: { offset: 17, line: 1, column: 18 },
  888. source: '<div id=a/></div>'
  889. }
  890. })
  891. })
  892. test('multiple attributes', () => {
  893. const ast = baseParse('<div id=a class="c" inert style=\'\'></div>')
  894. const element = ast.children[0] as ElementNode
  895. expect(element).toStrictEqual({
  896. type: NodeTypes.ELEMENT,
  897. ns: Namespaces.HTML,
  898. tag: 'div',
  899. tagType: ElementTypes.ELEMENT,
  900. codegenNode: undefined,
  901. props: [
  902. {
  903. type: NodeTypes.ATTRIBUTE,
  904. name: 'id',
  905. nameLoc: {
  906. start: { offset: 5, line: 1, column: 6 },
  907. end: { offset: 7, line: 1, column: 8 },
  908. source: 'id'
  909. },
  910. value: {
  911. type: NodeTypes.TEXT,
  912. content: 'a',
  913. loc: {
  914. start: { offset: 8, line: 1, column: 9 },
  915. end: { offset: 9, line: 1, column: 10 },
  916. source: 'a'
  917. }
  918. },
  919. loc: {
  920. start: { offset: 5, line: 1, column: 6 },
  921. end: { offset: 9, line: 1, column: 10 },
  922. source: 'id=a'
  923. }
  924. },
  925. {
  926. type: NodeTypes.ATTRIBUTE,
  927. name: 'class',
  928. nameLoc: {
  929. start: { offset: 10, line: 1, column: 11 },
  930. end: { offset: 15, line: 1, column: 16 },
  931. source: 'class'
  932. },
  933. value: {
  934. type: NodeTypes.TEXT,
  935. content: 'c',
  936. loc: {
  937. start: { offset: 16, line: 1, column: 17 },
  938. end: { offset: 19, line: 1, column: 20 },
  939. source: '"c"'
  940. }
  941. },
  942. loc: {
  943. start: { offset: 10, line: 1, column: 11 },
  944. end: { offset: 19, line: 1, column: 20 },
  945. source: 'class="c"'
  946. }
  947. },
  948. {
  949. type: NodeTypes.ATTRIBUTE,
  950. name: 'inert',
  951. nameLoc: {
  952. start: { offset: 20, line: 1, column: 21 },
  953. end: { offset: 25, line: 1, column: 26 },
  954. source: 'inert'
  955. },
  956. value: undefined,
  957. loc: {
  958. start: { offset: 20, line: 1, column: 21 },
  959. end: { offset: 25, line: 1, column: 26 },
  960. source: 'inert'
  961. }
  962. },
  963. {
  964. type: NodeTypes.ATTRIBUTE,
  965. name: 'style',
  966. nameLoc: {
  967. start: { offset: 26, line: 1, column: 27 },
  968. end: { offset: 31, line: 1, column: 32 },
  969. source: 'style'
  970. },
  971. value: {
  972. type: NodeTypes.TEXT,
  973. content: '',
  974. loc: {
  975. start: { offset: 32, line: 1, column: 33 },
  976. end: { offset: 34, line: 1, column: 35 },
  977. source: "''"
  978. }
  979. },
  980. loc: {
  981. start: { offset: 26, line: 1, column: 27 },
  982. end: { offset: 34, line: 1, column: 35 },
  983. source: "style=''"
  984. }
  985. }
  986. ],
  987. children: [],
  988. loc: {
  989. start: { offset: 0, line: 1, column: 1 },
  990. end: { offset: 41, line: 1, column: 42 },
  991. source: '<div id=a class="c" inert style=\'\'></div>'
  992. }
  993. })
  994. })
  995. // https://github.com/vuejs/core/issues/4251
  996. test('class attribute should ignore whitespace when parsed', () => {
  997. const ast = baseParse('<div class=" \n\t c \t\n "></div>')
  998. const element = ast.children[0] as ElementNode
  999. expect(element).toStrictEqual({
  1000. children: [],
  1001. codegenNode: undefined,
  1002. loc: {
  1003. start: { column: 1, line: 1, offset: 0 },
  1004. end: { column: 10, line: 3, offset: 29 },
  1005. source: '<div class=" \n\t c \t\n "></div>'
  1006. },
  1007. ns: Namespaces.HTML,
  1008. props: [
  1009. {
  1010. name: 'class',
  1011. nameLoc: {
  1012. start: { column: 6, line: 1, offset: 5 },
  1013. end: { column: 11, line: 1, offset: 10 },
  1014. source: 'class'
  1015. },
  1016. type: NodeTypes.ATTRIBUTE,
  1017. value: {
  1018. content: 'c',
  1019. loc: {
  1020. start: { column: 12, line: 1, offset: 11 },
  1021. end: { column: 3, line: 3, offset: 22 },
  1022. source: '" \n\t c \t\n "'
  1023. },
  1024. type: NodeTypes.TEXT
  1025. },
  1026. loc: {
  1027. start: { column: 6, line: 1, offset: 5 },
  1028. end: { column: 3, line: 3, offset: 22 },
  1029. source: 'class=" \n\t c \t\n "'
  1030. }
  1031. }
  1032. ],
  1033. tag: 'div',
  1034. tagType: ElementTypes.ELEMENT,
  1035. type: NodeTypes.ELEMENT
  1036. })
  1037. })
  1038. test('directive with no value', () => {
  1039. const ast = baseParse('<div v-if/>')
  1040. const directive = (ast.children[0] as ElementNode).props[0]
  1041. expect(directive).toStrictEqual({
  1042. type: NodeTypes.DIRECTIVE,
  1043. name: 'if',
  1044. rawName: 'v-if',
  1045. arg: undefined,
  1046. modifiers: [],
  1047. exp: undefined,
  1048. loc: {
  1049. start: { offset: 5, line: 1, column: 6 },
  1050. end: { offset: 9, line: 1, column: 10 },
  1051. source: 'v-if'
  1052. }
  1053. })
  1054. })
  1055. test('directive with value', () => {
  1056. const ast = baseParse('<div v-if="a"/>')
  1057. const directive = (ast.children[0] as ElementNode).props[0]
  1058. expect(directive).toStrictEqual({
  1059. type: NodeTypes.DIRECTIVE,
  1060. name: 'if',
  1061. rawName: 'v-if',
  1062. arg: undefined,
  1063. modifiers: [],
  1064. exp: {
  1065. type: NodeTypes.SIMPLE_EXPRESSION,
  1066. content: 'a',
  1067. isStatic: false,
  1068. constType: ConstantTypes.NOT_CONSTANT,
  1069. loc: {
  1070. start: { offset: 11, line: 1, column: 12 },
  1071. end: { offset: 12, line: 1, column: 13 },
  1072. source: 'a'
  1073. }
  1074. },
  1075. loc: {
  1076. start: { offset: 5, line: 1, column: 6 },
  1077. end: { offset: 13, line: 1, column: 14 },
  1078. source: 'v-if="a"'
  1079. }
  1080. })
  1081. })
  1082. test('directive with argument', () => {
  1083. const ast = baseParse('<div v-on:click/>')
  1084. const directive = (ast.children[0] as ElementNode).props[0]
  1085. expect(directive).toStrictEqual({
  1086. type: NodeTypes.DIRECTIVE,
  1087. name: 'on',
  1088. rawName: 'v-on:click',
  1089. arg: {
  1090. type: NodeTypes.SIMPLE_EXPRESSION,
  1091. content: 'click',
  1092. isStatic: true,
  1093. constType: ConstantTypes.CAN_STRINGIFY,
  1094. loc: {
  1095. start: { column: 11, line: 1, offset: 10 },
  1096. end: { column: 16, line: 1, offset: 15 },
  1097. source: 'click'
  1098. }
  1099. },
  1100. modifiers: [],
  1101. exp: undefined,
  1102. loc: {
  1103. start: { offset: 5, line: 1, column: 6 },
  1104. end: { offset: 15, line: 1, column: 16 },
  1105. source: 'v-on:click'
  1106. }
  1107. })
  1108. })
  1109. // #3494
  1110. test('directive argument edge case', () => {
  1111. const ast = baseParse('<div v-slot:slot />')
  1112. const directive = (ast.children[0] as ElementNode)
  1113. .props[0] as DirectiveNode
  1114. expect(directive.arg).toMatchObject({
  1115. loc: {
  1116. start: { offset: 12, line: 1, column: 13 },
  1117. end: { offset: 16, line: 1, column: 17 }
  1118. }
  1119. })
  1120. })
  1121. // https://github.com/vuejs/language-tools/issues/2710
  1122. test('directive argument edge case (2)', () => {
  1123. const ast = baseParse('<div #item.item />')
  1124. const directive = (ast.children[0] as ElementNode)
  1125. .props[0] as DirectiveNode
  1126. expect(directive.arg).toMatchObject({
  1127. content: 'item.item',
  1128. loc: {
  1129. start: { offset: 6, line: 1, column: 7 },
  1130. end: { offset: 15, line: 1, column: 16 }
  1131. }
  1132. })
  1133. })
  1134. test('directive with dynamic argument', () => {
  1135. const ast = baseParse('<div v-on:[event]/>')
  1136. const directive = (ast.children[0] as ElementNode).props[0]
  1137. expect(directive).toStrictEqual({
  1138. type: NodeTypes.DIRECTIVE,
  1139. name: 'on',
  1140. rawName: 'v-on:[event]',
  1141. arg: {
  1142. type: NodeTypes.SIMPLE_EXPRESSION,
  1143. content: 'event',
  1144. isStatic: false,
  1145. constType: ConstantTypes.NOT_CONSTANT,
  1146. loc: {
  1147. start: { column: 11, line: 1, offset: 10 },
  1148. end: { column: 18, line: 1, offset: 17 },
  1149. source: '[event]'
  1150. }
  1151. },
  1152. modifiers: [],
  1153. exp: undefined,
  1154. loc: {
  1155. start: { offset: 5, line: 1, column: 6 },
  1156. end: { offset: 17, line: 1, column: 18 },
  1157. source: 'v-on:[event]'
  1158. }
  1159. })
  1160. })
  1161. test('directive with a modifier', () => {
  1162. const ast = baseParse('<div v-on.enter/>')
  1163. const directive = (ast.children[0] as ElementNode).props[0]
  1164. expect(directive).toStrictEqual({
  1165. type: NodeTypes.DIRECTIVE,
  1166. name: 'on',
  1167. rawName: 'v-on.enter',
  1168. arg: undefined,
  1169. modifiers: ['enter'],
  1170. exp: undefined,
  1171. loc: {
  1172. start: { offset: 5, line: 1, column: 6 },
  1173. end: { offset: 15, line: 1, column: 16 },
  1174. source: 'v-on.enter'
  1175. }
  1176. })
  1177. })
  1178. test('directive with two modifiers', () => {
  1179. const ast = baseParse('<div v-on.enter.exact/>')
  1180. const directive = (ast.children[0] as ElementNode).props[0]
  1181. expect(directive).toStrictEqual({
  1182. type: NodeTypes.DIRECTIVE,
  1183. name: 'on',
  1184. rawName: 'v-on.enter.exact',
  1185. arg: undefined,
  1186. modifiers: ['enter', 'exact'],
  1187. exp: undefined,
  1188. loc: {
  1189. start: { offset: 5, line: 1, column: 6 },
  1190. end: { offset: 21, line: 1, column: 22 },
  1191. source: 'v-on.enter.exact'
  1192. }
  1193. })
  1194. })
  1195. test('directive with argument and modifiers', () => {
  1196. const ast = baseParse('<div v-on:click.enter.exact/>')
  1197. const directive = (ast.children[0] as ElementNode).props[0]
  1198. expect(directive).toStrictEqual({
  1199. type: NodeTypes.DIRECTIVE,
  1200. name: 'on',
  1201. rawName: 'v-on:click.enter.exact',
  1202. arg: {
  1203. type: NodeTypes.SIMPLE_EXPRESSION,
  1204. content: 'click',
  1205. isStatic: true,
  1206. constType: ConstantTypes.CAN_STRINGIFY,
  1207. loc: {
  1208. start: { column: 11, line: 1, offset: 10 },
  1209. end: { column: 16, line: 1, offset: 15 },
  1210. source: 'click'
  1211. }
  1212. },
  1213. modifiers: ['enter', 'exact'],
  1214. exp: undefined,
  1215. loc: {
  1216. start: { offset: 5, line: 1, column: 6 },
  1217. end: { offset: 27, line: 1, column: 28 },
  1218. source: 'v-on:click.enter.exact'
  1219. }
  1220. })
  1221. })
  1222. test('directive with dynamic argument and modifiers', () => {
  1223. const ast = baseParse('<div v-on:[a.b].camel/>')
  1224. const directive = (ast.children[0] as ElementNode).props[0]
  1225. expect(directive).toStrictEqual({
  1226. type: NodeTypes.DIRECTIVE,
  1227. name: 'on',
  1228. rawName: 'v-on:[a.b].camel',
  1229. arg: {
  1230. type: NodeTypes.SIMPLE_EXPRESSION,
  1231. content: 'a.b',
  1232. isStatic: false,
  1233. constType: ConstantTypes.NOT_CONSTANT,
  1234. loc: {
  1235. start: { column: 11, line: 1, offset: 10 },
  1236. end: { column: 16, line: 1, offset: 15 },
  1237. source: '[a.b]'
  1238. }
  1239. },
  1240. modifiers: ['camel'],
  1241. exp: undefined,
  1242. loc: {
  1243. start: { offset: 5, line: 1, column: 6 },
  1244. end: { offset: 21, line: 1, column: 22 },
  1245. source: 'v-on:[a.b].camel'
  1246. }
  1247. })
  1248. })
  1249. test('directive with no name', () => {
  1250. let errorCode = -1
  1251. const ast = baseParse('<div v-/>', {
  1252. onError: err => {
  1253. errorCode = err.code as number
  1254. }
  1255. })
  1256. const directive = (ast.children[0] as ElementNode).props[0]
  1257. expect(errorCode).toBe(ErrorCodes.X_MISSING_DIRECTIVE_NAME)
  1258. expect(directive).toStrictEqual({
  1259. type: NodeTypes.ATTRIBUTE,
  1260. name: 'v-',
  1261. value: undefined,
  1262. loc: {
  1263. start: { offset: 5, line: 1, column: 6 },
  1264. end: { offset: 7, line: 1, column: 8 },
  1265. source: 'v-'
  1266. },
  1267. nameLoc: {
  1268. start: { offset: 5, line: 1, column: 6 },
  1269. end: { offset: 7, line: 1, column: 8 },
  1270. source: 'v-'
  1271. }
  1272. })
  1273. })
  1274. test('v-bind shorthand', () => {
  1275. const ast = baseParse('<div :a=b />')
  1276. const directive = (ast.children[0] as ElementNode).props[0]
  1277. expect(directive).toStrictEqual({
  1278. type: NodeTypes.DIRECTIVE,
  1279. name: 'bind',
  1280. rawName: ':a',
  1281. arg: {
  1282. type: NodeTypes.SIMPLE_EXPRESSION,
  1283. content: 'a',
  1284. isStatic: true,
  1285. constType: ConstantTypes.CAN_STRINGIFY,
  1286. loc: {
  1287. start: { column: 7, line: 1, offset: 6 },
  1288. end: { column: 8, line: 1, offset: 7 },
  1289. source: 'a'
  1290. }
  1291. },
  1292. modifiers: [],
  1293. exp: {
  1294. type: NodeTypes.SIMPLE_EXPRESSION,
  1295. content: 'b',
  1296. isStatic: false,
  1297. constType: ConstantTypes.NOT_CONSTANT,
  1298. loc: {
  1299. start: { offset: 8, line: 1, column: 9 },
  1300. end: { offset: 9, line: 1, column: 10 },
  1301. source: 'b'
  1302. }
  1303. },
  1304. loc: {
  1305. start: { offset: 5, line: 1, column: 6 },
  1306. end: { offset: 9, line: 1, column: 10 },
  1307. source: ':a=b'
  1308. }
  1309. })
  1310. })
  1311. test('v-bind .prop shorthand', () => {
  1312. const ast = baseParse('<div .a=b />')
  1313. const directive = (ast.children[0] as ElementNode).props[0]
  1314. expect(directive).toStrictEqual({
  1315. type: NodeTypes.DIRECTIVE,
  1316. name: 'bind',
  1317. rawName: '.a',
  1318. arg: {
  1319. type: NodeTypes.SIMPLE_EXPRESSION,
  1320. content: 'a',
  1321. isStatic: true,
  1322. constType: ConstantTypes.CAN_STRINGIFY,
  1323. loc: {
  1324. start: { column: 7, line: 1, offset: 6 },
  1325. end: { column: 8, line: 1, offset: 7 },
  1326. source: 'a'
  1327. }
  1328. },
  1329. modifiers: ['prop'],
  1330. exp: {
  1331. type: NodeTypes.SIMPLE_EXPRESSION,
  1332. content: 'b',
  1333. isStatic: false,
  1334. constType: ConstantTypes.NOT_CONSTANT,
  1335. loc: {
  1336. start: { offset: 8, line: 1, column: 9 },
  1337. end: { offset: 9, line: 1, column: 10 },
  1338. source: 'b'
  1339. }
  1340. },
  1341. loc: {
  1342. start: { offset: 5, line: 1, column: 6 },
  1343. end: { offset: 9, line: 1, column: 10 },
  1344. source: '.a=b'
  1345. }
  1346. })
  1347. })
  1348. test('v-bind shorthand with modifier', () => {
  1349. const ast = baseParse('<div :a.sync=b />')
  1350. const directive = (ast.children[0] as ElementNode).props[0]
  1351. expect(directive).toStrictEqual({
  1352. type: NodeTypes.DIRECTIVE,
  1353. name: 'bind',
  1354. rawName: ':a.sync',
  1355. arg: {
  1356. type: NodeTypes.SIMPLE_EXPRESSION,
  1357. content: 'a',
  1358. isStatic: true,
  1359. constType: ConstantTypes.CAN_STRINGIFY,
  1360. loc: {
  1361. start: { column: 7, line: 1, offset: 6 },
  1362. end: { column: 8, line: 1, offset: 7 },
  1363. source: 'a'
  1364. }
  1365. },
  1366. modifiers: ['sync'],
  1367. exp: {
  1368. type: NodeTypes.SIMPLE_EXPRESSION,
  1369. content: 'b',
  1370. isStatic: false,
  1371. constType: ConstantTypes.NOT_CONSTANT,
  1372. loc: {
  1373. start: { offset: 13, line: 1, column: 14 },
  1374. end: { offset: 14, line: 1, column: 15 },
  1375. source: 'b'
  1376. }
  1377. },
  1378. loc: {
  1379. start: { offset: 5, line: 1, column: 6 },
  1380. end: { offset: 14, line: 1, column: 15 },
  1381. source: ':a.sync=b'
  1382. }
  1383. })
  1384. })
  1385. test('v-on shorthand', () => {
  1386. const ast = baseParse('<div @a=b />')
  1387. const directive = (ast.children[0] as ElementNode).props[0]
  1388. expect(directive).toStrictEqual({
  1389. type: NodeTypes.DIRECTIVE,
  1390. name: 'on',
  1391. rawName: '@a',
  1392. arg: {
  1393. type: NodeTypes.SIMPLE_EXPRESSION,
  1394. content: 'a',
  1395. isStatic: true,
  1396. constType: ConstantTypes.CAN_STRINGIFY,
  1397. loc: {
  1398. start: { column: 7, line: 1, offset: 6 },
  1399. end: { column: 8, line: 1, offset: 7 },
  1400. source: 'a'
  1401. }
  1402. },
  1403. modifiers: [],
  1404. exp: {
  1405. type: NodeTypes.SIMPLE_EXPRESSION,
  1406. content: 'b',
  1407. isStatic: false,
  1408. constType: ConstantTypes.NOT_CONSTANT,
  1409. loc: {
  1410. start: { offset: 8, line: 1, column: 9 },
  1411. end: { offset: 9, line: 1, column: 10 },
  1412. source: 'b'
  1413. }
  1414. },
  1415. loc: {
  1416. start: { offset: 5, line: 1, column: 6 },
  1417. end: { offset: 9, line: 1, column: 10 },
  1418. source: '@a=b'
  1419. }
  1420. })
  1421. })
  1422. test('v-on shorthand with modifier', () => {
  1423. const ast = baseParse('<div @a.enter=b />')
  1424. const directive = (ast.children[0] as ElementNode).props[0]
  1425. expect(directive).toStrictEqual({
  1426. type: NodeTypes.DIRECTIVE,
  1427. name: 'on',
  1428. rawName: '@a.enter',
  1429. arg: {
  1430. type: NodeTypes.SIMPLE_EXPRESSION,
  1431. content: 'a',
  1432. isStatic: true,
  1433. constType: ConstantTypes.CAN_STRINGIFY,
  1434. loc: {
  1435. start: { column: 7, line: 1, offset: 6 },
  1436. end: { column: 8, line: 1, offset: 7 },
  1437. source: 'a'
  1438. }
  1439. },
  1440. modifiers: ['enter'],
  1441. exp: {
  1442. type: NodeTypes.SIMPLE_EXPRESSION,
  1443. content: 'b',
  1444. isStatic: false,
  1445. constType: ConstantTypes.NOT_CONSTANT,
  1446. loc: {
  1447. start: { offset: 14, line: 1, column: 15 },
  1448. end: { offset: 15, line: 1, column: 16 },
  1449. source: 'b'
  1450. }
  1451. },
  1452. loc: {
  1453. start: { offset: 5, line: 1, column: 6 },
  1454. end: { offset: 15, line: 1, column: 16 },
  1455. source: '@a.enter=b'
  1456. }
  1457. })
  1458. })
  1459. test('v-slot shorthand', () => {
  1460. const ast = baseParse('<Comp #a="{ b }" />')
  1461. const directive = (ast.children[0] as ElementNode).props[0]
  1462. expect(directive).toStrictEqual({
  1463. type: NodeTypes.DIRECTIVE,
  1464. name: 'slot',
  1465. rawName: '#a',
  1466. arg: {
  1467. type: NodeTypes.SIMPLE_EXPRESSION,
  1468. content: 'a',
  1469. isStatic: true,
  1470. constType: ConstantTypes.CAN_STRINGIFY,
  1471. loc: {
  1472. start: { column: 8, line: 1, offset: 7 },
  1473. end: { column: 9, line: 1, offset: 8 },
  1474. source: 'a'
  1475. }
  1476. },
  1477. modifiers: [],
  1478. exp: {
  1479. type: NodeTypes.SIMPLE_EXPRESSION,
  1480. content: '{ b }',
  1481. isStatic: false,
  1482. // The `constType` is the default value and will be determined in transformExpression
  1483. constType: ConstantTypes.NOT_CONSTANT,
  1484. loc: {
  1485. start: { offset: 10, line: 1, column: 11 },
  1486. end: { offset: 15, line: 1, column: 16 },
  1487. source: '{ b }'
  1488. }
  1489. },
  1490. loc: {
  1491. start: { offset: 6, line: 1, column: 7 },
  1492. end: { offset: 16, line: 1, column: 17 },
  1493. source: '#a="{ b }"'
  1494. }
  1495. })
  1496. })
  1497. // #1241 special case for 2.x compat
  1498. test('v-slot arg containing dots', () => {
  1499. const ast = baseParse('<Comp v-slot:foo.bar="{ a }" />')
  1500. const directive = (ast.children[0] as ElementNode).props[0]
  1501. expect(directive).toMatchObject({
  1502. type: NodeTypes.DIRECTIVE,
  1503. name: 'slot',
  1504. rawName: 'v-slot:foo.bar',
  1505. arg: {
  1506. type: NodeTypes.SIMPLE_EXPRESSION,
  1507. content: 'foo.bar',
  1508. isStatic: true,
  1509. constType: ConstantTypes.CAN_STRINGIFY,
  1510. loc: {
  1511. start: {
  1512. column: 14,
  1513. line: 1,
  1514. offset: 13
  1515. },
  1516. end: {
  1517. column: 21,
  1518. line: 1,
  1519. offset: 20
  1520. }
  1521. }
  1522. }
  1523. })
  1524. })
  1525. test('v-pre', () => {
  1526. const ast = baseParse(
  1527. `<div v-pre :id="foo"><Comp/>{{ bar }}</div>\n` +
  1528. `<div :id="foo"><Comp/>{{ bar }}</div>`
  1529. )
  1530. const divWithPre = ast.children[0] as ElementNode
  1531. expect(divWithPre.props).toMatchObject([
  1532. {
  1533. type: NodeTypes.ATTRIBUTE,
  1534. name: `:id`,
  1535. value: {
  1536. type: NodeTypes.TEXT,
  1537. content: `foo`
  1538. },
  1539. loc: {
  1540. start: { line: 1, column: 12 },
  1541. end: { line: 1, column: 21 }
  1542. }
  1543. }
  1544. ])
  1545. expect(divWithPre.children[0]).toMatchObject({
  1546. type: NodeTypes.ELEMENT,
  1547. tagType: ElementTypes.ELEMENT,
  1548. tag: `Comp`
  1549. })
  1550. expect(divWithPre.children[1]).toMatchObject({
  1551. type: NodeTypes.TEXT,
  1552. content: `{{ bar }}`
  1553. })
  1554. // should not affect siblings after it
  1555. const divWithoutPre = ast.children[1] as ElementNode
  1556. expect(divWithoutPre.props).toMatchObject([
  1557. {
  1558. type: NodeTypes.DIRECTIVE,
  1559. name: `bind`,
  1560. arg: {
  1561. type: NodeTypes.SIMPLE_EXPRESSION,
  1562. isStatic: true,
  1563. content: `id`
  1564. },
  1565. exp: {
  1566. type: NodeTypes.SIMPLE_EXPRESSION,
  1567. isStatic: false,
  1568. content: `foo`
  1569. },
  1570. loc: {
  1571. start: {
  1572. line: 2,
  1573. column: 6
  1574. },
  1575. end: {
  1576. line: 2,
  1577. column: 15
  1578. }
  1579. }
  1580. }
  1581. ])
  1582. expect(divWithoutPre.children[0]).toMatchObject({
  1583. type: NodeTypes.ELEMENT,
  1584. tagType: ElementTypes.COMPONENT,
  1585. tag: `Comp`
  1586. })
  1587. expect(divWithoutPre.children[1]).toMatchObject({
  1588. type: NodeTypes.INTERPOLATION,
  1589. content: {
  1590. type: NodeTypes.SIMPLE_EXPRESSION,
  1591. content: `bar`,
  1592. isStatic: false
  1593. }
  1594. })
  1595. })
  1596. test('self-closing v-pre', () => {
  1597. const ast = baseParse(
  1598. `<div v-pre/>\n<div :id="foo"><Comp/>{{ bar }}</div>`
  1599. )
  1600. // should not affect siblings after it
  1601. const divWithoutPre = ast.children[1] as ElementNode
  1602. expect(divWithoutPre.props).toMatchObject([
  1603. {
  1604. type: NodeTypes.DIRECTIVE,
  1605. name: `bind`,
  1606. arg: {
  1607. type: NodeTypes.SIMPLE_EXPRESSION,
  1608. isStatic: true,
  1609. content: `id`
  1610. },
  1611. exp: {
  1612. type: NodeTypes.SIMPLE_EXPRESSION,
  1613. isStatic: false,
  1614. content: `foo`
  1615. },
  1616. loc: {
  1617. start: {
  1618. line: 2,
  1619. column: 6
  1620. },
  1621. end: {
  1622. line: 2,
  1623. column: 15
  1624. }
  1625. }
  1626. }
  1627. ])
  1628. expect(divWithoutPre.children[0]).toMatchObject({
  1629. type: NodeTypes.ELEMENT,
  1630. tagType: ElementTypes.COMPONENT,
  1631. tag: `Comp`
  1632. })
  1633. expect(divWithoutPre.children[1]).toMatchObject({
  1634. type: NodeTypes.INTERPOLATION,
  1635. content: {
  1636. type: NodeTypes.SIMPLE_EXPRESSION,
  1637. content: `bar`,
  1638. isStatic: false
  1639. }
  1640. })
  1641. })
  1642. test('end tags are case-insensitive.', () => {
  1643. const ast = baseParse('<div>hello</DIV>after')
  1644. const element = ast.children[0] as ElementNode
  1645. const text = element.children[0] as TextNode
  1646. expect(text).toStrictEqual({
  1647. type: NodeTypes.TEXT,
  1648. content: 'hello',
  1649. loc: {
  1650. start: { offset: 5, line: 1, column: 6 },
  1651. end: { offset: 10, line: 1, column: 11 },
  1652. source: 'hello'
  1653. }
  1654. })
  1655. })
  1656. })
  1657. describe('Edge Cases', () => {
  1658. test('self closing single tag', () => {
  1659. const ast = baseParse('<div :class="{ some: condition }" />')
  1660. expect(ast.children).toHaveLength(1)
  1661. expect(ast.children[0]).toMatchObject({ tag: 'div' })
  1662. })
  1663. test('self closing multiple tag', () => {
  1664. const ast = baseParse(
  1665. `<div :class="{ some: condition }" />\n` +
  1666. `<p v-bind:style="{ color: 'red' }"/>`
  1667. )
  1668. expect(ast).toMatchSnapshot()
  1669. expect(ast.children).toHaveLength(2)
  1670. expect(ast.children[0]).toMatchObject({ tag: 'div' })
  1671. expect(ast.children[1]).toMatchObject({ tag: 'p' })
  1672. })
  1673. test('valid html', () => {
  1674. const ast = baseParse(
  1675. `<div :class="{ some: condition }">\n` +
  1676. ` <p v-bind:style="{ color: 'red' }"/>\n` +
  1677. ` <!-- a comment with <html> inside it -->\n` +
  1678. `</div>`
  1679. )
  1680. expect(ast).toMatchSnapshot()
  1681. expect(ast.children).toHaveLength(1)
  1682. const el = ast.children[0] as any
  1683. expect(el).toMatchObject({
  1684. tag: 'div'
  1685. })
  1686. expect(el.children).toHaveLength(2)
  1687. expect(el.children[0]).toMatchObject({
  1688. tag: 'p'
  1689. })
  1690. expect(el.children[1]).toMatchObject({
  1691. type: NodeTypes.COMMENT
  1692. })
  1693. })
  1694. test('invalid html', () => {
  1695. expect(() => {
  1696. baseParse(`<div>\n<span>\n</div>\n</span>`)
  1697. }).toThrow('Element is missing end tag.')
  1698. const spy = vi.fn()
  1699. const ast = baseParse(`<div>\n<span>\n</div>\n</span>`, {
  1700. onError: spy
  1701. })
  1702. expect(spy.mock.calls).toMatchObject([
  1703. [
  1704. {
  1705. code: ErrorCodes.X_MISSING_END_TAG,
  1706. loc: {
  1707. start: {
  1708. offset: 6,
  1709. line: 2,
  1710. column: 1
  1711. }
  1712. }
  1713. }
  1714. ],
  1715. [
  1716. {
  1717. code: ErrorCodes.X_INVALID_END_TAG,
  1718. loc: {
  1719. start: {
  1720. offset: 20,
  1721. line: 4,
  1722. column: 1
  1723. }
  1724. }
  1725. }
  1726. ]
  1727. ])
  1728. expect(ast).toMatchSnapshot()
  1729. })
  1730. test('parse with correct location info', () => {
  1731. const fooSrc = `foo\n is `
  1732. const barSrc = `{{ bar }}`
  1733. const butSrc = ` but `
  1734. const bazSrc = `{{ baz }}`
  1735. const [foo, bar, but, baz] = baseParse(
  1736. fooSrc + barSrc + butSrc + bazSrc
  1737. ).children
  1738. let offset = 0
  1739. expect(foo.loc.start).toEqual({ line: 1, column: 1, offset })
  1740. offset += fooSrc.length
  1741. expect(foo.loc.end).toEqual({ line: 2, column: 5, offset })
  1742. expect(bar.loc.start).toEqual({ line: 2, column: 5, offset })
  1743. const barInner = (bar as InterpolationNode).content
  1744. offset += 3
  1745. expect(barInner.loc.start).toEqual({ line: 2, column: 8, offset })
  1746. offset += 3
  1747. expect(barInner.loc.end).toEqual({ line: 2, column: 11, offset })
  1748. offset += 3
  1749. expect(bar.loc.end).toEqual({ line: 2, column: 14, offset })
  1750. expect(but.loc.start).toEqual({ line: 2, column: 14, offset })
  1751. offset += butSrc.length
  1752. expect(but.loc.end).toEqual({ line: 2, column: 19, offset })
  1753. expect(baz.loc.start).toEqual({ line: 2, column: 19, offset })
  1754. const bazInner = (baz as InterpolationNode).content
  1755. offset += 3
  1756. expect(bazInner.loc.start).toEqual({ line: 2, column: 22, offset })
  1757. offset += 3
  1758. expect(bazInner.loc.end).toEqual({ line: 2, column: 25, offset })
  1759. offset += 3
  1760. expect(baz.loc.end).toEqual({ line: 2, column: 28, offset })
  1761. })
  1762. // With standard HTML parsing, the following input would ignore the slash
  1763. // and treat "<" and "template" as attributes on the open tag of "Hello",
  1764. // causing `<template>` to fail to close, and `<script>` being parsed as its
  1765. // child. This is would never be intended in actual templates, but is a common
  1766. // intermediate state from user input when parsing for IDE support. We want
  1767. // the `<script>` to be at root-level to keep the SFC structure stable for
  1768. // Volar to do incremental computations.
  1769. test('tag termination handling for IDE', () => {
  1770. const spy = vi.fn()
  1771. const ast = baseParse(
  1772. `<template><Hello\n</template><script>console.log(1)</script>`,
  1773. {
  1774. onError: spy
  1775. }
  1776. )
  1777. //
  1778. expect(ast.children.length).toBe(2)
  1779. expect(ast.children[1]).toMatchObject({
  1780. type: NodeTypes.ELEMENT,
  1781. tag: 'script'
  1782. })
  1783. })
  1784. test('arg should be undefined on shorthand dirs with no arg', () => {
  1785. const ast = baseParse(`<template #></template>`)
  1786. const el = ast.children[0] as ElementNode
  1787. expect(el.props[0]).toMatchObject({
  1788. type: NodeTypes.DIRECTIVE,
  1789. name: 'slot',
  1790. exp: undefined,
  1791. arg: undefined
  1792. })
  1793. })
  1794. // edge case found in vue-macros where the input is TS or JSX
  1795. test('should reset inRCDATA state', () => {
  1796. baseParse(`<Foo>`, { parseMode: 'sfc', onError() {} })
  1797. expect(() => baseParse(`{ foo }`)).not.toThrow()
  1798. })
  1799. })
  1800. describe('decodeEntities option', () => {
  1801. test('use decode by default', () => {
  1802. const ast: any = baseParse('&gt;&lt;&amp;&apos;&quot;&foo;')
  1803. expect(ast.children.length).toBe(1)
  1804. expect(ast.children[0].type).toBe(NodeTypes.TEXT)
  1805. expect(ast.children[0].content).toBe('><&\'"&foo;')
  1806. })
  1807. test('should warn in non-browser build', () => {
  1808. baseParse('&amp;&cups;', {
  1809. decodeEntities: text => text.replace('&cups;', '\u222A\uFE00'),
  1810. onError: () => {} // Ignore errors
  1811. })
  1812. expect(
  1813. `decodeEntities option is passed but will be ignored`
  1814. ).toHaveBeenWarned()
  1815. })
  1816. })
  1817. describe('whitespace management when adopting strategy condense', () => {
  1818. const parse = (content: string, options?: ParserOptions) =>
  1819. baseParse(content, {
  1820. whitespace: 'condense',
  1821. ...options
  1822. })
  1823. test('should remove whitespaces at start/end inside an element', () => {
  1824. const ast = parse(`<div> <span/> </div>`)
  1825. expect((ast.children[0] as ElementNode).children.length).toBe(1)
  1826. })
  1827. test('should remove whitespaces w/ newline between elements', () => {
  1828. const ast = parse(`<div/> \n <div/> \n <div/>`)
  1829. expect(ast.children.length).toBe(3)
  1830. expect(ast.children.every(c => c.type === NodeTypes.ELEMENT)).toBe(true)
  1831. })
  1832. test('should remove whitespaces adjacent to comments', () => {
  1833. const ast = parse(`<div/> \n <!--foo--> <div/>`)
  1834. expect(ast.children.length).toBe(3)
  1835. expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
  1836. expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
  1837. expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
  1838. })
  1839. test('should remove whitespaces w/ newline between comments and elements', () => {
  1840. const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
  1841. expect(ast.children.length).toBe(3)
  1842. expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
  1843. expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
  1844. expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
  1845. })
  1846. test('should NOT remove whitespaces w/ newline between interpolations', () => {
  1847. const ast = parse(`{{ foo }} \n {{ bar }}`)
  1848. expect(ast.children.length).toBe(3)
  1849. expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
  1850. expect(ast.children[1]).toMatchObject({
  1851. type: NodeTypes.TEXT,
  1852. content: ' '
  1853. })
  1854. expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
  1855. })
  1856. test('should NOT remove whitespaces w/ newline between interpolation and comment', () => {
  1857. const ast = parse(`<!-- foo --> \n {{msg}}`)
  1858. expect(ast.children.length).toBe(3)
  1859. expect(ast.children[0].type).toBe(NodeTypes.COMMENT)
  1860. expect(ast.children[1]).toMatchObject({
  1861. type: NodeTypes.TEXT,
  1862. content: ' '
  1863. })
  1864. expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
  1865. })
  1866. test('should NOT remove whitespaces w/o newline between elements', () => {
  1867. const ast = parse(`<div/> <div/> <div/>`)
  1868. expect(ast.children.length).toBe(5)
  1869. expect(ast.children.map(c => c.type)).toMatchObject([
  1870. NodeTypes.ELEMENT,
  1871. NodeTypes.TEXT,
  1872. NodeTypes.ELEMENT,
  1873. NodeTypes.TEXT,
  1874. NodeTypes.ELEMENT
  1875. ])
  1876. })
  1877. test('should condense consecutive whitespaces in text', () => {
  1878. const ast = parse(` foo \n bar baz `)
  1879. expect((ast.children[0] as TextNode).content).toBe(` foo bar baz `)
  1880. })
  1881. test('should remove leading newline character immediately following the pre element start tag', () => {
  1882. const ast = baseParse(`<pre>\n foo bar </pre>`, {
  1883. isPreTag: tag => tag === 'pre'
  1884. })
  1885. expect(ast.children).toHaveLength(1)
  1886. const preElement = ast.children[0] as ElementNode
  1887. expect(preElement.children).toHaveLength(1)
  1888. expect((preElement.children[0] as TextNode).content).toBe(` foo bar `)
  1889. })
  1890. test('should NOT remove leading newline character immediately following child-tag of pre element', () => {
  1891. const ast = baseParse(`<pre><span></span>\n foo bar </pre>`, {
  1892. isPreTag: tag => tag === 'pre'
  1893. })
  1894. const preElement = ast.children[0] as ElementNode
  1895. expect(preElement.children).toHaveLength(2)
  1896. expect((preElement.children[1] as TextNode).content).toBe(
  1897. `\n foo bar `
  1898. )
  1899. })
  1900. test('self-closing pre tag', () => {
  1901. const ast = baseParse(`<pre/><span>\n foo bar</span>`, {
  1902. isPreTag: tag => tag === 'pre'
  1903. })
  1904. const elementAfterPre = ast.children[1] as ElementNode
  1905. // should not affect the <span> and condense its whitespace inside
  1906. expect((elementAfterPre.children[0] as TextNode).content).toBe(` foo bar`)
  1907. })
  1908. test('should NOT condense whitespaces in RCDATA text mode', () => {
  1909. const ast = baseParse(`<textarea>Text:\n foo</textarea>`, {
  1910. parseMode: 'html'
  1911. })
  1912. const preElement = ast.children[0] as ElementNode
  1913. expect(preElement.children).toHaveLength(1)
  1914. expect((preElement.children[0] as TextNode).content).toBe(`Text:\n foo`)
  1915. })
  1916. })
  1917. describe('whitespace management when adopting strategy preserve', () => {
  1918. const parse = (content: string, options?: ParserOptions) =>
  1919. baseParse(content, {
  1920. whitespace: 'preserve',
  1921. ...options
  1922. })
  1923. test('should still remove whitespaces at start/end inside an element', () => {
  1924. const ast = parse(`<div> <span/> </div>`)
  1925. expect((ast.children[0] as ElementNode).children.length).toBe(1)
  1926. })
  1927. test('should preserve whitespaces w/ newline between elements', () => {
  1928. const ast = parse(`<div/> \n <div/> \n <div/>`)
  1929. expect(ast.children.length).toBe(5)
  1930. expect(ast.children.map(c => c.type)).toMatchObject([
  1931. NodeTypes.ELEMENT,
  1932. NodeTypes.TEXT,
  1933. NodeTypes.ELEMENT,
  1934. NodeTypes.TEXT,
  1935. NodeTypes.ELEMENT
  1936. ])
  1937. })
  1938. test('should preserve whitespaces adjacent to comments', () => {
  1939. const ast = parse(`<div/> \n <!--foo--> <div/>`)
  1940. expect(ast.children.length).toBe(5)
  1941. expect(ast.children.map(c => c.type)).toMatchObject([
  1942. NodeTypes.ELEMENT,
  1943. NodeTypes.TEXT,
  1944. NodeTypes.COMMENT,
  1945. NodeTypes.TEXT,
  1946. NodeTypes.ELEMENT
  1947. ])
  1948. })
  1949. test('should preserve whitespaces w/ newline between comments and elements', () => {
  1950. const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
  1951. expect(ast.children.length).toBe(5)
  1952. expect(ast.children.map(c => c.type)).toMatchObject([
  1953. NodeTypes.ELEMENT,
  1954. NodeTypes.TEXT,
  1955. NodeTypes.COMMENT,
  1956. NodeTypes.TEXT,
  1957. NodeTypes.ELEMENT
  1958. ])
  1959. })
  1960. test('should preserve whitespaces w/ newline between interpolations', () => {
  1961. const ast = parse(`{{ foo }} \n {{ bar }}`)
  1962. expect(ast.children.length).toBe(3)
  1963. expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
  1964. expect(ast.children[1]).toMatchObject({
  1965. type: NodeTypes.TEXT,
  1966. content: ' '
  1967. })
  1968. expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
  1969. })
  1970. test('should preserve whitespaces w/o newline between elements', () => {
  1971. const ast = parse(`<div/> <div/> <div/>`)
  1972. expect(ast.children.length).toBe(5)
  1973. expect(ast.children.map(c => c.type)).toMatchObject([
  1974. NodeTypes.ELEMENT,
  1975. NodeTypes.TEXT,
  1976. NodeTypes.ELEMENT,
  1977. NodeTypes.TEXT,
  1978. NodeTypes.ELEMENT
  1979. ])
  1980. })
  1981. test('should preserve consecutive whitespaces in text', () => {
  1982. const content = ` foo \n bar baz `
  1983. const ast = parse(content)
  1984. expect((ast.children[0] as TextNode).content).toBe(content)
  1985. })
  1986. })
  1987. describe('expression parsing', () => {
  1988. test('interpolation', () => {
  1989. const ast = baseParse(`{{ a + b }}`, { prefixIdentifiers: true })
  1990. // @ts-ignore
  1991. expect((ast.children[0] as InterpolationNode).content.ast?.type).toBe(
  1992. 'BinaryExpression'
  1993. )
  1994. })
  1995. test('v-bind', () => {
  1996. const ast = baseParse(`<div :[key+1]="foo()" />`, {
  1997. prefixIdentifiers: true
  1998. })
  1999. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2000. // @ts-ignore
  2001. expect(dir.arg?.ast?.type).toBe('BinaryExpression')
  2002. // @ts-ignore
  2003. expect(dir.exp?.ast?.type).toBe('CallExpression')
  2004. })
  2005. test('v-on multi statements', () => {
  2006. const ast = baseParse(`<div @click="a++;b++" />`, {
  2007. prefixIdentifiers: true
  2008. })
  2009. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2010. // @ts-ignore
  2011. expect(dir.exp?.ast?.type).toBe('Program')
  2012. expect((dir.exp?.ast as Program).body).toMatchObject([
  2013. { type: 'ExpressionStatement' },
  2014. { type: 'ExpressionStatement' }
  2015. ])
  2016. })
  2017. test('v-slot', () => {
  2018. const ast = baseParse(`<Comp #foo="{ a, b }" />`, {
  2019. prefixIdentifiers: true
  2020. })
  2021. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2022. // @ts-ignore
  2023. expect(dir.exp?.ast?.type).toBe('ArrowFunctionExpression')
  2024. })
  2025. test('v-for', () => {
  2026. const ast = baseParse(`<div v-for="({ a, b }, key, index) of a.b" />`, {
  2027. prefixIdentifiers: true
  2028. })
  2029. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2030. const { source, value, key, index } = dir.forParseResult!
  2031. // @ts-ignore
  2032. expect(source.ast?.type).toBe('MemberExpression')
  2033. // @ts-ignore
  2034. expect(value?.ast?.type).toBe('ArrowFunctionExpression')
  2035. expect(key?.ast).toBeNull() // simple ident
  2036. expect(index?.ast).toBeNull() // simple ident
  2037. })
  2038. })
  2039. describe('Errors', () => {
  2040. // HTML parsing errors as specified at
  2041. // https://html.spec.whatwg.org/multipage/parsing.html#parse-errors
  2042. // We ignore some errors that do NOT affect parse result in meaningful ways
  2043. // but have non-trivial implementation cost.
  2044. const patterns: {
  2045. [key: string]: Array<{
  2046. code: string
  2047. errors: Array<{ type: ErrorCodes; loc: Position }>
  2048. options?: Partial<ParserOptions>
  2049. }>
  2050. } = {
  2051. // ABRUPT_CLOSING_OF_EMPTY_COMMENT: [
  2052. // {
  2053. // code: '<template><!--></template>',
  2054. // errors: [
  2055. // {
  2056. // type: ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  2057. // loc: { offset: 10, line: 1, column: 11 }
  2058. // }
  2059. // ]
  2060. // },
  2061. // {
  2062. // code: '<template><!---></template>',
  2063. // errors: [
  2064. // {
  2065. // type: ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  2066. // loc: { offset: 10, line: 1, column: 11 }
  2067. // }
  2068. // ]
  2069. // },
  2070. // {
  2071. // code: '<template><!----></template>',
  2072. // errors: []
  2073. // }
  2074. // ],
  2075. CDATA_IN_HTML_CONTENT: [
  2076. {
  2077. code: '<template><![CDATA[cdata]]></template>',
  2078. errors: [
  2079. {
  2080. type: ErrorCodes.CDATA_IN_HTML_CONTENT,
  2081. loc: { offset: 10, line: 1, column: 11 }
  2082. }
  2083. ]
  2084. },
  2085. {
  2086. code: '<template><svg><![CDATA[cdata]]></svg></template>',
  2087. errors: []
  2088. }
  2089. ],
  2090. DUPLICATE_ATTRIBUTE: [
  2091. {
  2092. code: '<template><div id="" id=""></div></template>',
  2093. errors: [
  2094. {
  2095. type: ErrorCodes.DUPLICATE_ATTRIBUTE,
  2096. loc: { offset: 21, line: 1, column: 22 }
  2097. }
  2098. ]
  2099. }
  2100. ],
  2101. // END_TAG_WITH_ATTRIBUTES: [
  2102. // {
  2103. // code: '<template><div></div id=""></template>',
  2104. // errors: [
  2105. // {
  2106. // type: ErrorCodes.END_TAG_WITH_ATTRIBUTES,
  2107. // loc: { offset: 21, line: 1, column: 22 }
  2108. // }
  2109. // ]
  2110. // }
  2111. // ],
  2112. // END_TAG_WITH_TRAILING_SOLIDUS: [
  2113. // {
  2114. // code: '<template><div></div/></template>',
  2115. // errors: [
  2116. // {
  2117. // type: ErrorCodes.END_TAG_WITH_TRAILING_SOLIDUS,
  2118. // loc: { offset: 20, line: 1, column: 21 }
  2119. // }
  2120. // ]
  2121. // }
  2122. // ],
  2123. EOF_BEFORE_TAG_NAME: [
  2124. {
  2125. code: '<template><',
  2126. errors: [
  2127. {
  2128. type: ErrorCodes.EOF_BEFORE_TAG_NAME,
  2129. loc: { offset: 11, line: 1, column: 12 }
  2130. },
  2131. {
  2132. type: ErrorCodes.X_MISSING_END_TAG,
  2133. loc: { offset: 0, line: 1, column: 1 }
  2134. }
  2135. ]
  2136. },
  2137. {
  2138. code: '<template></',
  2139. errors: [
  2140. {
  2141. type: ErrorCodes.EOF_BEFORE_TAG_NAME,
  2142. loc: { offset: 12, line: 1, column: 13 }
  2143. },
  2144. {
  2145. type: ErrorCodes.X_MISSING_END_TAG,
  2146. loc: { offset: 0, line: 1, column: 1 }
  2147. }
  2148. ]
  2149. }
  2150. ],
  2151. EOF_IN_CDATA: [
  2152. {
  2153. code: '<template><svg><![CDATA[cdata',
  2154. errors: [
  2155. {
  2156. type: ErrorCodes.EOF_IN_CDATA,
  2157. loc: { offset: 29, line: 1, column: 30 }
  2158. },
  2159. {
  2160. type: ErrorCodes.X_MISSING_END_TAG,
  2161. loc: { offset: 10, line: 1, column: 11 }
  2162. },
  2163. {
  2164. type: ErrorCodes.X_MISSING_END_TAG,
  2165. loc: { offset: 0, line: 1, column: 1 }
  2166. }
  2167. ]
  2168. },
  2169. {
  2170. code: '<template><svg><![CDATA[',
  2171. errors: [
  2172. {
  2173. type: ErrorCodes.EOF_IN_CDATA,
  2174. loc: { offset: 24, line: 1, column: 25 }
  2175. },
  2176. {
  2177. type: ErrorCodes.X_MISSING_END_TAG,
  2178. loc: { offset: 10, line: 1, column: 11 }
  2179. },
  2180. {
  2181. type: ErrorCodes.X_MISSING_END_TAG,
  2182. loc: { offset: 0, line: 1, column: 1 }
  2183. }
  2184. ]
  2185. }
  2186. ],
  2187. EOF_IN_COMMENT: [
  2188. {
  2189. code: '<template><!--comment',
  2190. errors: [
  2191. {
  2192. type: ErrorCodes.EOF_IN_COMMENT,
  2193. loc: { offset: 21, line: 1, column: 22 }
  2194. },
  2195. {
  2196. type: ErrorCodes.X_MISSING_END_TAG,
  2197. loc: { offset: 0, line: 1, column: 1 }
  2198. }
  2199. ]
  2200. },
  2201. {
  2202. code: '<template><!--',
  2203. errors: [
  2204. {
  2205. type: ErrorCodes.EOF_IN_COMMENT,
  2206. loc: { offset: 14, line: 1, column: 15 }
  2207. },
  2208. {
  2209. type: ErrorCodes.X_MISSING_END_TAG,
  2210. loc: { offset: 0, line: 1, column: 1 }
  2211. }
  2212. ]
  2213. }
  2214. // // Bogus comments don't throw eof-in-comment error.
  2215. // // https://html.spec.whatwg.org/multipage/parsing.html#bogus-comment-state
  2216. // {
  2217. // code: '<template><!',
  2218. // errors: [
  2219. // {
  2220. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2221. // loc: { offset: 10, line: 1, column: 11 }
  2222. // },
  2223. // {
  2224. // type: ErrorCodes.X_MISSING_END_TAG,
  2225. // loc: { offset: 0, line: 1, column: 1 }
  2226. // }
  2227. // ]
  2228. // },
  2229. // {
  2230. // code: '<template><!-',
  2231. // errors: [
  2232. // {
  2233. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2234. // loc: { offset: 10, line: 1, column: 11 }
  2235. // },
  2236. // {
  2237. // type: ErrorCodes.X_MISSING_END_TAG,
  2238. // loc: { offset: 0, line: 1, column: 1 }
  2239. // }
  2240. // ]
  2241. // },
  2242. // {
  2243. // code: '<template><!abc',
  2244. // errors: [
  2245. // {
  2246. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2247. // loc: { offset: 10, line: 1, column: 11 }
  2248. // },
  2249. // {
  2250. // type: ErrorCodes.X_MISSING_END_TAG,
  2251. // loc: { offset: 0, line: 1, column: 1 }
  2252. // }
  2253. // ]
  2254. // }
  2255. ],
  2256. // EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT: [
  2257. // {
  2258. // code: "<script><!--console.log('hello')",
  2259. // errors: [
  2260. // {
  2261. // type: ErrorCodes.X_MISSING_END_TAG,
  2262. // loc: { offset: 0, line: 1, column: 1 }
  2263. // },
  2264. // {
  2265. // type: ErrorCodes.EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT,
  2266. // loc: { offset: 32, line: 1, column: 33 }
  2267. // }
  2268. // ]
  2269. // },
  2270. // {
  2271. // code: "<script>console.log('hello')",
  2272. // errors: [
  2273. // {
  2274. // type: ErrorCodes.X_MISSING_END_TAG,
  2275. // loc: { offset: 0, line: 1, column: 1 }
  2276. // }
  2277. // ]
  2278. // }
  2279. // ],
  2280. EOF_IN_TAG: [
  2281. {
  2282. code: '<template><div',
  2283. errors: [
  2284. {
  2285. type: ErrorCodes.EOF_IN_TAG,
  2286. loc: { offset: 14, line: 1, column: 15 }
  2287. },
  2288. {
  2289. type: ErrorCodes.X_MISSING_END_TAG,
  2290. loc: { offset: 0, line: 1, column: 1 }
  2291. }
  2292. ]
  2293. },
  2294. {
  2295. code: '<template><div ',
  2296. errors: [
  2297. {
  2298. type: ErrorCodes.EOF_IN_TAG,
  2299. loc: { offset: 15, line: 1, column: 16 }
  2300. },
  2301. {
  2302. type: ErrorCodes.X_MISSING_END_TAG,
  2303. loc: { offset: 0, line: 1, column: 1 }
  2304. }
  2305. ]
  2306. },
  2307. {
  2308. code: '<template><div id',
  2309. errors: [
  2310. {
  2311. type: ErrorCodes.EOF_IN_TAG,
  2312. loc: { offset: 17, line: 1, column: 18 }
  2313. },
  2314. {
  2315. type: ErrorCodes.X_MISSING_END_TAG,
  2316. loc: { offset: 0, line: 1, column: 1 }
  2317. }
  2318. ]
  2319. },
  2320. {
  2321. code: '<template><div id ',
  2322. errors: [
  2323. {
  2324. type: ErrorCodes.EOF_IN_TAG,
  2325. loc: { offset: 18, line: 1, column: 19 }
  2326. },
  2327. {
  2328. type: ErrorCodes.X_MISSING_END_TAG,
  2329. loc: { offset: 0, line: 1, column: 1 }
  2330. }
  2331. ]
  2332. },
  2333. {
  2334. code: '<template><div id =',
  2335. errors: [
  2336. // {
  2337. // type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2338. // loc: { offset: 19, line: 1, column: 20 }
  2339. // },
  2340. {
  2341. type: ErrorCodes.EOF_IN_TAG,
  2342. loc: { offset: 19, line: 1, column: 20 }
  2343. },
  2344. {
  2345. type: ErrorCodes.X_MISSING_END_TAG,
  2346. loc: { offset: 0, line: 1, column: 1 }
  2347. }
  2348. ]
  2349. },
  2350. {
  2351. code: "<template><div id='abc",
  2352. errors: [
  2353. {
  2354. type: ErrorCodes.EOF_IN_TAG,
  2355. loc: { offset: 22, line: 1, column: 23 }
  2356. },
  2357. {
  2358. type: ErrorCodes.X_MISSING_END_TAG,
  2359. loc: { offset: 0, line: 1, column: 1 }
  2360. }
  2361. ]
  2362. },
  2363. {
  2364. code: '<template><div id="abc',
  2365. errors: [
  2366. {
  2367. type: ErrorCodes.EOF_IN_TAG,
  2368. loc: { offset: 22, line: 1, column: 23 }
  2369. },
  2370. {
  2371. type: ErrorCodes.X_MISSING_END_TAG,
  2372. loc: { offset: 0, line: 1, column: 1 }
  2373. }
  2374. ]
  2375. },
  2376. {
  2377. code: "<template><div id='abc'",
  2378. errors: [
  2379. {
  2380. type: ErrorCodes.EOF_IN_TAG,
  2381. loc: { offset: 23, line: 1, column: 24 }
  2382. },
  2383. {
  2384. type: ErrorCodes.X_MISSING_END_TAG,
  2385. loc: { offset: 0, line: 1, column: 1 }
  2386. }
  2387. ]
  2388. },
  2389. {
  2390. code: '<template><div id="abc"',
  2391. errors: [
  2392. {
  2393. type: ErrorCodes.EOF_IN_TAG,
  2394. loc: { offset: 23, line: 1, column: 24 }
  2395. },
  2396. {
  2397. type: ErrorCodes.X_MISSING_END_TAG,
  2398. loc: { offset: 0, line: 1, column: 1 }
  2399. }
  2400. ]
  2401. },
  2402. {
  2403. code: '<template><div id=abc',
  2404. errors: [
  2405. {
  2406. type: ErrorCodes.EOF_IN_TAG,
  2407. loc: { offset: 21, line: 1, column: 22 }
  2408. },
  2409. {
  2410. type: ErrorCodes.X_MISSING_END_TAG,
  2411. loc: { offset: 0, line: 1, column: 1 }
  2412. }
  2413. ]
  2414. },
  2415. {
  2416. code: "<template><div id='abc'/",
  2417. errors: [
  2418. {
  2419. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2420. loc: { offset: 23, line: 1, column: 24 }
  2421. },
  2422. {
  2423. type: ErrorCodes.EOF_IN_TAG,
  2424. loc: { offset: 24, line: 1, column: 25 }
  2425. },
  2426. {
  2427. type: ErrorCodes.X_MISSING_END_TAG,
  2428. loc: { offset: 0, line: 1, column: 1 }
  2429. }
  2430. ]
  2431. },
  2432. {
  2433. code: '<template><div id="abc"/',
  2434. errors: [
  2435. {
  2436. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2437. loc: { offset: 23, line: 1, column: 24 }
  2438. },
  2439. {
  2440. type: ErrorCodes.EOF_IN_TAG,
  2441. loc: { offset: 24, line: 1, column: 25 }
  2442. },
  2443. {
  2444. type: ErrorCodes.X_MISSING_END_TAG,
  2445. loc: { offset: 0, line: 1, column: 1 }
  2446. }
  2447. ]
  2448. },
  2449. {
  2450. code: '<template><div id=abc /',
  2451. errors: [
  2452. {
  2453. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2454. loc: { offset: 22, line: 1, column: 23 }
  2455. },
  2456. {
  2457. type: ErrorCodes.EOF_IN_TAG,
  2458. loc: { offset: 23, line: 1, column: 24 }
  2459. },
  2460. {
  2461. type: ErrorCodes.X_MISSING_END_TAG,
  2462. loc: { offset: 0, line: 1, column: 1 }
  2463. }
  2464. ]
  2465. },
  2466. {
  2467. code: '<div></div',
  2468. errors: [
  2469. {
  2470. type: ErrorCodes.EOF_IN_TAG,
  2471. loc: { offset: 10, line: 1, column: 11 }
  2472. },
  2473. {
  2474. type: ErrorCodes.X_MISSING_END_TAG,
  2475. loc: { offset: 0, line: 1, column: 1 }
  2476. }
  2477. ]
  2478. }
  2479. ],
  2480. // INCORRECTLY_CLOSED_COMMENT: [
  2481. // {
  2482. // code: '<template><!--comment--!></template>',
  2483. // errors: [
  2484. // {
  2485. // type: ErrorCodes.INCORRECTLY_CLOSED_COMMENT,
  2486. // loc: { offset: 10, line: 1, column: 11 }
  2487. // }
  2488. // ]
  2489. // }
  2490. // ],
  2491. // INCORRECTLY_OPENED_COMMENT: [
  2492. // {
  2493. // code: '<template><!></template>',
  2494. // errors: [
  2495. // {
  2496. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2497. // loc: { offset: 10, line: 1, column: 11 }
  2498. // }
  2499. // ]
  2500. // },
  2501. // {
  2502. // code: '<template><!-></template>',
  2503. // errors: [
  2504. // {
  2505. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2506. // loc: { offset: 10, line: 1, column: 11 }
  2507. // }
  2508. // ]
  2509. // },
  2510. // {
  2511. // code: '<template><!ELEMENT br EMPTY></template>',
  2512. // errors: [
  2513. // {
  2514. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2515. // loc: { offset: 10, line: 1, column: 11 }
  2516. // }
  2517. // ]
  2518. // },
  2519. // // Just ignore doctype.
  2520. // {
  2521. // code: '<!DOCTYPE html>',
  2522. // errors: []
  2523. // }
  2524. // ],
  2525. // INVALID_FIRST_CHARACTER_OF_TAG_NAME: [
  2526. // {
  2527. // code: '<template>a < b</template>',
  2528. // errors: [
  2529. // {
  2530. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2531. // loc: { offset: 13, line: 1, column: 14 }
  2532. // }
  2533. // ]
  2534. // },
  2535. // {
  2536. // code: '<template><�></template>',
  2537. // errors: [
  2538. // {
  2539. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2540. // loc: { offset: 11, line: 1, column: 12 }
  2541. // }
  2542. // ]
  2543. // },
  2544. // {
  2545. // code: '<template>a </ b</template>',
  2546. // errors: [
  2547. // {
  2548. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2549. // loc: { offset: 14, line: 1, column: 15 }
  2550. // },
  2551. // {
  2552. // type: ErrorCodes.X_MISSING_END_TAG,
  2553. // loc: { offset: 0, line: 1, column: 1 }
  2554. // }
  2555. // ]
  2556. // },
  2557. // {
  2558. // code: '<template></�></template>',
  2559. // errors: [
  2560. // {
  2561. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2562. // loc: { offset: 12, line: 1, column: 13 }
  2563. // }
  2564. // ]
  2565. // },
  2566. // // Don't throw invalid-first-character-of-tag-name in interpolation
  2567. // {
  2568. // code: '<template>{{a < b}}</template>',
  2569. // errors: []
  2570. // }
  2571. // ],
  2572. MISSING_ATTRIBUTE_VALUE: [
  2573. {
  2574. code: '<template><div id=></div></template>',
  2575. errors: [
  2576. {
  2577. type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2578. loc: { offset: 18, line: 1, column: 19 }
  2579. }
  2580. ]
  2581. },
  2582. {
  2583. code: '<template><div id= ></div></template>',
  2584. errors: [
  2585. {
  2586. type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2587. loc: { offset: 19, line: 1, column: 20 }
  2588. }
  2589. ]
  2590. },
  2591. {
  2592. code: '<template><div id= /></div></template>',
  2593. errors: []
  2594. }
  2595. ],
  2596. MISSING_END_TAG_NAME: [
  2597. {
  2598. code: '<template></></template>',
  2599. errors: [
  2600. {
  2601. type: ErrorCodes.MISSING_END_TAG_NAME,
  2602. loc: { offset: 12, line: 1, column: 13 }
  2603. }
  2604. ]
  2605. }
  2606. ],
  2607. // MISSING_WHITESPACE_BETWEEN_ATTRIBUTES: [
  2608. // {
  2609. // code: '<template><div id="foo"class="bar"></div></template>',
  2610. // errors: [
  2611. // {
  2612. // type: ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES,
  2613. // loc: { offset: 23, line: 1, column: 24 }
  2614. // }
  2615. // ]
  2616. // },
  2617. // // CR doesn't appear in tokenization phase, but all CR are removed in preprocessing.
  2618. // // https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
  2619. // {
  2620. // code: '<template><div id="foo"\r\nclass="bar"></div></template>',
  2621. // errors: []
  2622. // }
  2623. // ],
  2624. // NESTED_COMMENT: [
  2625. // {
  2626. // code: '<template><!--a<!--b--></template>',
  2627. // errors: [
  2628. // {
  2629. // type: ErrorCodes.NESTED_COMMENT,
  2630. // loc: { offset: 15, line: 1, column: 16 }
  2631. // }
  2632. // ]
  2633. // },
  2634. // {
  2635. // code: '<template><!--a<!--b<!--c--></template>',
  2636. // errors: [
  2637. // {
  2638. // type: ErrorCodes.NESTED_COMMENT,
  2639. // loc: { offset: 15, line: 1, column: 16 }
  2640. // },
  2641. // {
  2642. // type: ErrorCodes.NESTED_COMMENT,
  2643. // loc: { offset: 20, line: 1, column: 21 }
  2644. // }
  2645. // ]
  2646. // },
  2647. // {
  2648. // code: '<template><!--a<!--b<!----></template>',
  2649. // errors: [
  2650. // {
  2651. // type: ErrorCodes.NESTED_COMMENT,
  2652. // loc: { offset: 15, line: 1, column: 16 }
  2653. // }
  2654. // ]
  2655. // },
  2656. // {
  2657. // code: '<template><!--a<!--></template>',
  2658. // errors: []
  2659. // },
  2660. // {
  2661. // code: '<template><!--a<!--',
  2662. // errors: [
  2663. // {
  2664. // type: ErrorCodes.EOF_IN_COMMENT,
  2665. // loc: { offset: 19, line: 1, column: 20 }
  2666. // },
  2667. // {
  2668. // type: ErrorCodes.X_MISSING_END_TAG,
  2669. // loc: { offset: 0, line: 1, column: 1 }
  2670. // }
  2671. // ]
  2672. // }
  2673. // ],
  2674. UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME: [
  2675. {
  2676. code: "<template><div a\"bc=''></div></template>",
  2677. errors: [
  2678. {
  2679. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  2680. loc: { offset: 16, line: 1, column: 17 }
  2681. }
  2682. ]
  2683. },
  2684. {
  2685. code: "<template><div a'bc=''></div></template>",
  2686. errors: [
  2687. {
  2688. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  2689. loc: { offset: 16, line: 1, column: 17 }
  2690. }
  2691. ]
  2692. },
  2693. {
  2694. code: "<template><div a<bc=''></div></template>",
  2695. errors: [
  2696. {
  2697. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  2698. loc: { offset: 16, line: 1, column: 17 }
  2699. }
  2700. ]
  2701. }
  2702. ],
  2703. UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE: [
  2704. {
  2705. code: '<template><div foo=bar"></div></template>',
  2706. errors: [
  2707. {
  2708. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  2709. loc: { offset: 22, line: 1, column: 23 }
  2710. }
  2711. ]
  2712. },
  2713. {
  2714. code: "<template><div foo=bar'></div></template>",
  2715. errors: [
  2716. {
  2717. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  2718. loc: { offset: 22, line: 1, column: 23 }
  2719. }
  2720. ]
  2721. },
  2722. {
  2723. code: '<template><div foo=bar<div></div></template>',
  2724. errors: [
  2725. {
  2726. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  2727. loc: { offset: 22, line: 1, column: 23 }
  2728. }
  2729. ]
  2730. },
  2731. {
  2732. code: '<template><div foo=bar=baz></div></template>',
  2733. errors: [
  2734. {
  2735. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  2736. loc: { offset: 22, line: 1, column: 23 }
  2737. }
  2738. ]
  2739. },
  2740. {
  2741. code: '<template><div foo=bar`></div></template>',
  2742. errors: [
  2743. {
  2744. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  2745. loc: { offset: 22, line: 1, column: 23 }
  2746. }
  2747. ]
  2748. }
  2749. ],
  2750. UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME: [
  2751. {
  2752. code: '<template><div =foo=bar></div></template>',
  2753. errors: [
  2754. {
  2755. type: ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  2756. loc: { offset: 15, line: 1, column: 16 }
  2757. }
  2758. ]
  2759. },
  2760. {
  2761. code: '<template><div =></div></template>',
  2762. errors: [
  2763. {
  2764. type: ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  2765. loc: { offset: 15, line: 1, column: 16 }
  2766. }
  2767. ]
  2768. }
  2769. ],
  2770. UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME: [
  2771. {
  2772. code: '<template><?xml?></template>',
  2773. errors: [
  2774. {
  2775. type: ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME,
  2776. loc: { offset: 11, line: 1, column: 12 }
  2777. }
  2778. ]
  2779. }
  2780. ],
  2781. UNEXPECTED_SOLIDUS_IN_TAG: [
  2782. {
  2783. code: '<template><div a/b></div></template>',
  2784. errors: [
  2785. {
  2786. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2787. loc: { offset: 16, line: 1, column: 17 }
  2788. }
  2789. ]
  2790. }
  2791. ],
  2792. X_INVALID_END_TAG: [
  2793. {
  2794. code: '<template></div></template>',
  2795. errors: [
  2796. {
  2797. type: ErrorCodes.X_INVALID_END_TAG,
  2798. loc: { offset: 10, line: 1, column: 11 }
  2799. }
  2800. ]
  2801. },
  2802. {
  2803. code: '<template></div></div></template>',
  2804. errors: [
  2805. {
  2806. type: ErrorCodes.X_INVALID_END_TAG,
  2807. loc: { offset: 10, line: 1, column: 11 }
  2808. },
  2809. {
  2810. type: ErrorCodes.X_INVALID_END_TAG,
  2811. loc: { offset: 16, line: 1, column: 17 }
  2812. }
  2813. ]
  2814. },
  2815. {
  2816. code: '<template>a </ b</template>',
  2817. errors: [
  2818. {
  2819. type: ErrorCodes.X_INVALID_END_TAG,
  2820. loc: { offset: 12, line: 1, column: 13 }
  2821. },
  2822. {
  2823. type: ErrorCodes.X_MISSING_END_TAG,
  2824. loc: { offset: 0, line: 1, column: 1 }
  2825. }
  2826. ]
  2827. },
  2828. {
  2829. code: "<template>{{'</div>'}}</template>",
  2830. errors: []
  2831. },
  2832. {
  2833. code: '<textarea></div></textarea>',
  2834. errors: []
  2835. },
  2836. {
  2837. code: '<svg><![CDATA[</div>]]></svg>',
  2838. errors: []
  2839. },
  2840. {
  2841. code: '<svg><!--</div>--></svg>',
  2842. errors: []
  2843. }
  2844. ],
  2845. X_MISSING_END_TAG: [
  2846. {
  2847. code: '<template><div></template>',
  2848. errors: [
  2849. {
  2850. type: ErrorCodes.X_MISSING_END_TAG,
  2851. loc: { offset: 10, line: 1, column: 11 }
  2852. }
  2853. ]
  2854. },
  2855. {
  2856. code: '<template><div>',
  2857. errors: [
  2858. {
  2859. type: ErrorCodes.X_MISSING_END_TAG,
  2860. loc: { offset: 10, line: 1, column: 11 }
  2861. },
  2862. {
  2863. type: ErrorCodes.X_MISSING_END_TAG,
  2864. loc: { offset: 0, line: 1, column: 1 }
  2865. }
  2866. ]
  2867. }
  2868. ],
  2869. X_MISSING_INTERPOLATION_END: [
  2870. {
  2871. code: '{{ foo',
  2872. errors: [
  2873. {
  2874. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  2875. loc: { offset: 0, line: 1, column: 1 }
  2876. }
  2877. ]
  2878. },
  2879. {
  2880. code: '{{',
  2881. errors: [
  2882. {
  2883. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  2884. loc: { offset: 0, line: 1, column: 1 }
  2885. }
  2886. ]
  2887. },
  2888. {
  2889. code: '<div>{{ foo</div>',
  2890. errors: [
  2891. {
  2892. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  2893. loc: { offset: 5, line: 1, column: 6 }
  2894. },
  2895. {
  2896. type: ErrorCodes.X_MISSING_END_TAG,
  2897. loc: { offset: 0, line: 1, column: 1 }
  2898. }
  2899. ]
  2900. },
  2901. {
  2902. code: '{{}}',
  2903. errors: []
  2904. }
  2905. ],
  2906. X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END: [
  2907. {
  2908. code: `<div v-foo:[sef fsef] />`,
  2909. errors: [
  2910. {
  2911. type: ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
  2912. loc: { offset: 15, line: 1, column: 16 }
  2913. }
  2914. ]
  2915. }
  2916. ]
  2917. }
  2918. for (const key of Object.keys(patterns)) {
  2919. describe(key, () => {
  2920. for (const { code, errors, options } of patterns[key]) {
  2921. test(
  2922. code.replace(
  2923. /[\r\n]/g,
  2924. c => `\\x0${c.codePointAt(0)!.toString(16)};`
  2925. ),
  2926. () => {
  2927. const spy = vi.fn()
  2928. const ast = baseParse(code, {
  2929. parseMode: 'html',
  2930. getNamespace: tag =>
  2931. tag === 'svg' ? Namespaces.SVG : Namespaces.HTML,
  2932. ...options,
  2933. onError: spy
  2934. })
  2935. expect(
  2936. spy.mock.calls.map(([err]) => ({
  2937. type: err.code,
  2938. loc: err.loc.start
  2939. }))
  2940. ).toMatchObject(errors)
  2941. expect(ast).toMatchSnapshot()
  2942. }
  2943. )
  2944. }
  2945. })
  2946. }
  2947. })
  2948. })