parse.spec.ts 95 KB

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