parse.spec.ts 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471
  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: [
  1251. {
  1252. constType: 3,
  1253. content: 'enter',
  1254. isStatic: true,
  1255. loc: {
  1256. end: {
  1257. column: 16,
  1258. line: 1,
  1259. offset: 15,
  1260. },
  1261. source: 'enter',
  1262. start: {
  1263. column: 11,
  1264. line: 1,
  1265. offset: 10,
  1266. },
  1267. },
  1268. type: 4,
  1269. },
  1270. ],
  1271. exp: undefined,
  1272. loc: {
  1273. start: { offset: 5, line: 1, column: 6 },
  1274. end: { offset: 15, line: 1, column: 16 },
  1275. source: 'v-on.enter',
  1276. },
  1277. })
  1278. })
  1279. test('directive with two modifiers', () => {
  1280. const ast = baseParse('<div v-on.enter.exact/>')
  1281. const directive = (ast.children[0] as ElementNode).props[0]
  1282. expect(directive).toStrictEqual({
  1283. type: NodeTypes.DIRECTIVE,
  1284. name: 'on',
  1285. rawName: 'v-on.enter.exact',
  1286. arg: undefined,
  1287. modifiers: [
  1288. {
  1289. constType: 3,
  1290. content: 'enter',
  1291. isStatic: true,
  1292. loc: {
  1293. end: {
  1294. column: 16,
  1295. line: 1,
  1296. offset: 15,
  1297. },
  1298. source: 'enter',
  1299. start: {
  1300. column: 11,
  1301. line: 1,
  1302. offset: 10,
  1303. },
  1304. },
  1305. type: 4,
  1306. },
  1307. {
  1308. constType: 3,
  1309. content: 'exact',
  1310. isStatic: true,
  1311. loc: {
  1312. end: {
  1313. column: 22,
  1314. line: 1,
  1315. offset: 21,
  1316. },
  1317. source: 'exact',
  1318. start: {
  1319. column: 17,
  1320. line: 1,
  1321. offset: 16,
  1322. },
  1323. },
  1324. type: 4,
  1325. },
  1326. ],
  1327. exp: undefined,
  1328. loc: {
  1329. start: { offset: 5, line: 1, column: 6 },
  1330. end: { offset: 21, line: 1, column: 22 },
  1331. source: 'v-on.enter.exact',
  1332. },
  1333. })
  1334. })
  1335. test('directive with argument and modifiers', () => {
  1336. const ast = baseParse('<div v-on:click.enter.exact/>')
  1337. const directive = (ast.children[0] as ElementNode).props[0]
  1338. expect(directive).toStrictEqual({
  1339. type: NodeTypes.DIRECTIVE,
  1340. name: 'on',
  1341. rawName: 'v-on:click.enter.exact',
  1342. arg: {
  1343. type: NodeTypes.SIMPLE_EXPRESSION,
  1344. content: 'click',
  1345. isStatic: true,
  1346. constType: ConstantTypes.CAN_STRINGIFY,
  1347. loc: {
  1348. start: { column: 11, line: 1, offset: 10 },
  1349. end: { column: 16, line: 1, offset: 15 },
  1350. source: 'click',
  1351. },
  1352. },
  1353. modifiers: [
  1354. {
  1355. constType: 3,
  1356. content: 'enter',
  1357. isStatic: true,
  1358. loc: {
  1359. end: {
  1360. column: 22,
  1361. line: 1,
  1362. offset: 21,
  1363. },
  1364. source: 'enter',
  1365. start: {
  1366. column: 17,
  1367. line: 1,
  1368. offset: 16,
  1369. },
  1370. },
  1371. type: 4,
  1372. },
  1373. {
  1374. constType: 3,
  1375. content: 'exact',
  1376. isStatic: true,
  1377. loc: {
  1378. end: {
  1379. column: 28,
  1380. line: 1,
  1381. offset: 27,
  1382. },
  1383. source: 'exact',
  1384. start: {
  1385. column: 23,
  1386. line: 1,
  1387. offset: 22,
  1388. },
  1389. },
  1390. type: 4,
  1391. },
  1392. ],
  1393. exp: undefined,
  1394. loc: {
  1395. start: { offset: 5, line: 1, column: 6 },
  1396. end: { offset: 27, line: 1, column: 28 },
  1397. source: 'v-on:click.enter.exact',
  1398. },
  1399. })
  1400. })
  1401. test('directive with dynamic argument and modifiers', () => {
  1402. const ast = baseParse('<div v-on:[a.b].camel/>')
  1403. const directive = (ast.children[0] as ElementNode).props[0]
  1404. expect(directive).toStrictEqual({
  1405. type: NodeTypes.DIRECTIVE,
  1406. name: 'on',
  1407. rawName: 'v-on:[a.b].camel',
  1408. arg: {
  1409. type: NodeTypes.SIMPLE_EXPRESSION,
  1410. content: 'a.b',
  1411. isStatic: false,
  1412. constType: ConstantTypes.NOT_CONSTANT,
  1413. loc: {
  1414. start: { column: 11, line: 1, offset: 10 },
  1415. end: { column: 16, line: 1, offset: 15 },
  1416. source: '[a.b]',
  1417. },
  1418. },
  1419. modifiers: [
  1420. {
  1421. constType: 3,
  1422. content: 'camel',
  1423. isStatic: true,
  1424. loc: {
  1425. end: {
  1426. column: 22,
  1427. line: 1,
  1428. offset: 21,
  1429. },
  1430. source: 'camel',
  1431. start: {
  1432. column: 17,
  1433. line: 1,
  1434. offset: 16,
  1435. },
  1436. },
  1437. type: 4,
  1438. },
  1439. ],
  1440. exp: undefined,
  1441. loc: {
  1442. start: { offset: 5, line: 1, column: 6 },
  1443. end: { offset: 21, line: 1, column: 22 },
  1444. source: 'v-on:[a.b].camel',
  1445. },
  1446. })
  1447. })
  1448. test('directive with no name', () => {
  1449. let errorCode = -1
  1450. const ast = baseParse('<div v-/>', {
  1451. onError: err => {
  1452. errorCode = err.code as number
  1453. },
  1454. })
  1455. const directive = (ast.children[0] as ElementNode).props[0]
  1456. expect(errorCode).toBe(ErrorCodes.X_MISSING_DIRECTIVE_NAME)
  1457. expect(directive).toStrictEqual({
  1458. type: NodeTypes.ATTRIBUTE,
  1459. name: 'v-',
  1460. value: undefined,
  1461. loc: {
  1462. start: { offset: 5, line: 1, column: 6 },
  1463. end: { offset: 7, line: 1, column: 8 },
  1464. source: 'v-',
  1465. },
  1466. nameLoc: {
  1467. start: { offset: 5, line: 1, column: 6 },
  1468. end: { offset: 7, line: 1, column: 8 },
  1469. source: 'v-',
  1470. },
  1471. })
  1472. })
  1473. test('v-bind shorthand', () => {
  1474. const ast = baseParse('<div :a=b />')
  1475. const directive = (ast.children[0] as ElementNode).props[0]
  1476. expect(directive).toStrictEqual({
  1477. type: NodeTypes.DIRECTIVE,
  1478. name: 'bind',
  1479. rawName: ':a',
  1480. arg: {
  1481. type: NodeTypes.SIMPLE_EXPRESSION,
  1482. content: 'a',
  1483. isStatic: true,
  1484. constType: ConstantTypes.CAN_STRINGIFY,
  1485. loc: {
  1486. start: { column: 7, line: 1, offset: 6 },
  1487. end: { column: 8, line: 1, offset: 7 },
  1488. source: 'a',
  1489. },
  1490. },
  1491. modifiers: [],
  1492. exp: {
  1493. type: NodeTypes.SIMPLE_EXPRESSION,
  1494. content: 'b',
  1495. isStatic: false,
  1496. constType: ConstantTypes.NOT_CONSTANT,
  1497. loc: {
  1498. start: { offset: 8, line: 1, column: 9 },
  1499. end: { offset: 9, line: 1, column: 10 },
  1500. source: 'b',
  1501. },
  1502. },
  1503. loc: {
  1504. start: { offset: 5, line: 1, column: 6 },
  1505. end: { offset: 9, line: 1, column: 10 },
  1506. source: ':a=b',
  1507. },
  1508. })
  1509. })
  1510. test('v-bind .prop shorthand', () => {
  1511. const ast = baseParse('<div .a=b />')
  1512. const directive = (ast.children[0] as ElementNode).props[0]
  1513. expect(directive).toStrictEqual({
  1514. type: NodeTypes.DIRECTIVE,
  1515. name: 'bind',
  1516. rawName: '.a',
  1517. arg: {
  1518. type: NodeTypes.SIMPLE_EXPRESSION,
  1519. content: 'a',
  1520. isStatic: true,
  1521. constType: ConstantTypes.CAN_STRINGIFY,
  1522. loc: {
  1523. start: { column: 7, line: 1, offset: 6 },
  1524. end: { column: 8, line: 1, offset: 7 },
  1525. source: 'a',
  1526. },
  1527. },
  1528. modifiers: [
  1529. {
  1530. constType: 0,
  1531. content: 'prop',
  1532. isStatic: false,
  1533. loc: {
  1534. end: {
  1535. column: 1,
  1536. line: 1,
  1537. offset: 0,
  1538. },
  1539. source: '',
  1540. start: {
  1541. column: 1,
  1542. line: 1,
  1543. offset: 0,
  1544. },
  1545. },
  1546. type: 4,
  1547. },
  1548. ],
  1549. exp: {
  1550. type: NodeTypes.SIMPLE_EXPRESSION,
  1551. content: 'b',
  1552. isStatic: false,
  1553. constType: ConstantTypes.NOT_CONSTANT,
  1554. loc: {
  1555. start: { offset: 8, line: 1, column: 9 },
  1556. end: { offset: 9, line: 1, column: 10 },
  1557. source: 'b',
  1558. },
  1559. },
  1560. loc: {
  1561. start: { offset: 5, line: 1, column: 6 },
  1562. end: { offset: 9, line: 1, column: 10 },
  1563. source: '.a=b',
  1564. },
  1565. })
  1566. })
  1567. test('v-bind shorthand with modifier', () => {
  1568. const ast = baseParse('<div :a.sync=b />')
  1569. const directive = (ast.children[0] as ElementNode).props[0]
  1570. expect(directive).toStrictEqual({
  1571. type: NodeTypes.DIRECTIVE,
  1572. name: 'bind',
  1573. rawName: ':a.sync',
  1574. arg: {
  1575. type: NodeTypes.SIMPLE_EXPRESSION,
  1576. content: 'a',
  1577. isStatic: true,
  1578. constType: ConstantTypes.CAN_STRINGIFY,
  1579. loc: {
  1580. start: { column: 7, line: 1, offset: 6 },
  1581. end: { column: 8, line: 1, offset: 7 },
  1582. source: 'a',
  1583. },
  1584. },
  1585. modifiers: [
  1586. {
  1587. constType: 3,
  1588. content: 'sync',
  1589. isStatic: true,
  1590. loc: {
  1591. end: {
  1592. column: 13,
  1593. line: 1,
  1594. offset: 12,
  1595. },
  1596. source: 'sync',
  1597. start: {
  1598. column: 9,
  1599. line: 1,
  1600. offset: 8,
  1601. },
  1602. },
  1603. type: 4,
  1604. },
  1605. ],
  1606. exp: {
  1607. type: NodeTypes.SIMPLE_EXPRESSION,
  1608. content: 'b',
  1609. isStatic: false,
  1610. constType: ConstantTypes.NOT_CONSTANT,
  1611. loc: {
  1612. start: { offset: 13, line: 1, column: 14 },
  1613. end: { offset: 14, line: 1, column: 15 },
  1614. source: 'b',
  1615. },
  1616. },
  1617. loc: {
  1618. start: { offset: 5, line: 1, column: 6 },
  1619. end: { offset: 14, line: 1, column: 15 },
  1620. source: ':a.sync=b',
  1621. },
  1622. })
  1623. })
  1624. test('v-on shorthand', () => {
  1625. const ast = baseParse('<div @a=b />')
  1626. const directive = (ast.children[0] as ElementNode).props[0]
  1627. expect(directive).toStrictEqual({
  1628. type: NodeTypes.DIRECTIVE,
  1629. name: 'on',
  1630. rawName: '@a',
  1631. arg: {
  1632. type: NodeTypes.SIMPLE_EXPRESSION,
  1633. content: 'a',
  1634. isStatic: true,
  1635. constType: ConstantTypes.CAN_STRINGIFY,
  1636. loc: {
  1637. start: { column: 7, line: 1, offset: 6 },
  1638. end: { column: 8, line: 1, offset: 7 },
  1639. source: 'a',
  1640. },
  1641. },
  1642. modifiers: [],
  1643. exp: {
  1644. type: NodeTypes.SIMPLE_EXPRESSION,
  1645. content: 'b',
  1646. isStatic: false,
  1647. constType: ConstantTypes.NOT_CONSTANT,
  1648. loc: {
  1649. start: { offset: 8, line: 1, column: 9 },
  1650. end: { offset: 9, line: 1, column: 10 },
  1651. source: 'b',
  1652. },
  1653. },
  1654. loc: {
  1655. start: { offset: 5, line: 1, column: 6 },
  1656. end: { offset: 9, line: 1, column: 10 },
  1657. source: '@a=b',
  1658. },
  1659. })
  1660. })
  1661. test('v-on shorthand with modifier', () => {
  1662. const ast = baseParse('<div @a.enter=b />')
  1663. const directive = (ast.children[0] as ElementNode).props[0]
  1664. expect(directive).toStrictEqual({
  1665. type: NodeTypes.DIRECTIVE,
  1666. name: 'on',
  1667. rawName: '@a.enter',
  1668. arg: {
  1669. type: NodeTypes.SIMPLE_EXPRESSION,
  1670. content: 'a',
  1671. isStatic: true,
  1672. constType: ConstantTypes.CAN_STRINGIFY,
  1673. loc: {
  1674. start: { column: 7, line: 1, offset: 6 },
  1675. end: { column: 8, line: 1, offset: 7 },
  1676. source: 'a',
  1677. },
  1678. },
  1679. modifiers: [
  1680. {
  1681. constType: 3,
  1682. content: 'enter',
  1683. isStatic: true,
  1684. loc: {
  1685. end: {
  1686. column: 14,
  1687. line: 1,
  1688. offset: 13,
  1689. },
  1690. source: 'enter',
  1691. start: {
  1692. column: 9,
  1693. line: 1,
  1694. offset: 8,
  1695. },
  1696. },
  1697. type: 4,
  1698. },
  1699. ],
  1700. exp: {
  1701. type: NodeTypes.SIMPLE_EXPRESSION,
  1702. content: 'b',
  1703. isStatic: false,
  1704. constType: ConstantTypes.NOT_CONSTANT,
  1705. loc: {
  1706. start: { offset: 14, line: 1, column: 15 },
  1707. end: { offset: 15, line: 1, column: 16 },
  1708. source: 'b',
  1709. },
  1710. },
  1711. loc: {
  1712. start: { offset: 5, line: 1, column: 6 },
  1713. end: { offset: 15, line: 1, column: 16 },
  1714. source: '@a.enter=b',
  1715. },
  1716. })
  1717. })
  1718. test('v-slot shorthand', () => {
  1719. const ast = baseParse('<Comp #a="{ b }" />')
  1720. const directive = (ast.children[0] as ElementNode).props[0]
  1721. expect(directive).toStrictEqual({
  1722. type: NodeTypes.DIRECTIVE,
  1723. name: 'slot',
  1724. rawName: '#a',
  1725. arg: {
  1726. type: NodeTypes.SIMPLE_EXPRESSION,
  1727. content: 'a',
  1728. isStatic: true,
  1729. constType: ConstantTypes.CAN_STRINGIFY,
  1730. loc: {
  1731. start: { column: 8, line: 1, offset: 7 },
  1732. end: { column: 9, line: 1, offset: 8 },
  1733. source: 'a',
  1734. },
  1735. },
  1736. modifiers: [],
  1737. exp: {
  1738. type: NodeTypes.SIMPLE_EXPRESSION,
  1739. content: '{ b }',
  1740. isStatic: false,
  1741. // The `constType` is the default value and will be determined in transformExpression
  1742. constType: ConstantTypes.NOT_CONSTANT,
  1743. loc: {
  1744. start: { offset: 10, line: 1, column: 11 },
  1745. end: { offset: 15, line: 1, column: 16 },
  1746. source: '{ b }',
  1747. },
  1748. },
  1749. loc: {
  1750. start: { offset: 6, line: 1, column: 7 },
  1751. end: { offset: 16, line: 1, column: 17 },
  1752. source: '#a="{ b }"',
  1753. },
  1754. })
  1755. })
  1756. // #1241 special case for 2.x compat
  1757. test('v-slot arg containing dots', () => {
  1758. const ast = baseParse('<Comp v-slot:foo.bar="{ a }" />')
  1759. const directive = (ast.children[0] as ElementNode).props[0]
  1760. expect(directive).toMatchObject({
  1761. type: NodeTypes.DIRECTIVE,
  1762. name: 'slot',
  1763. rawName: 'v-slot:foo.bar',
  1764. arg: {
  1765. type: NodeTypes.SIMPLE_EXPRESSION,
  1766. content: 'foo.bar',
  1767. isStatic: true,
  1768. constType: ConstantTypes.CAN_STRINGIFY,
  1769. loc: {
  1770. start: {
  1771. column: 14,
  1772. line: 1,
  1773. offset: 13,
  1774. },
  1775. end: {
  1776. column: 21,
  1777. line: 1,
  1778. offset: 20,
  1779. },
  1780. },
  1781. },
  1782. })
  1783. })
  1784. test('v-pre', () => {
  1785. const ast = baseParse(
  1786. `<div v-pre :id="foo"><Comp/>{{ bar }}</div>\n` +
  1787. `<div :id="foo"><Comp/>{{ bar }}</div>`,
  1788. )
  1789. const divWithPre = ast.children[0] as ElementNode
  1790. expect(divWithPre.props).toMatchObject([
  1791. {
  1792. type: NodeTypes.ATTRIBUTE,
  1793. name: `:id`,
  1794. value: {
  1795. type: NodeTypes.TEXT,
  1796. content: `foo`,
  1797. },
  1798. loc: {
  1799. start: { line: 1, column: 12 },
  1800. end: { line: 1, column: 21 },
  1801. },
  1802. },
  1803. ])
  1804. expect(divWithPre.children[0]).toMatchObject({
  1805. type: NodeTypes.ELEMENT,
  1806. tagType: ElementTypes.ELEMENT,
  1807. tag: `Comp`,
  1808. })
  1809. expect(divWithPre.children[1]).toMatchObject({
  1810. type: NodeTypes.TEXT,
  1811. content: `{{ bar }}`,
  1812. })
  1813. // should not affect siblings after it
  1814. const divWithoutPre = ast.children[1] as ElementNode
  1815. expect(divWithoutPre.props).toMatchObject([
  1816. {
  1817. type: NodeTypes.DIRECTIVE,
  1818. name: `bind`,
  1819. arg: {
  1820. type: NodeTypes.SIMPLE_EXPRESSION,
  1821. isStatic: true,
  1822. content: `id`,
  1823. },
  1824. exp: {
  1825. type: NodeTypes.SIMPLE_EXPRESSION,
  1826. isStatic: false,
  1827. content: `foo`,
  1828. },
  1829. loc: {
  1830. start: {
  1831. line: 2,
  1832. column: 6,
  1833. },
  1834. end: {
  1835. line: 2,
  1836. column: 15,
  1837. },
  1838. },
  1839. },
  1840. ])
  1841. expect(divWithoutPre.children[0]).toMatchObject({
  1842. type: NodeTypes.ELEMENT,
  1843. tagType: ElementTypes.COMPONENT,
  1844. tag: `Comp`,
  1845. })
  1846. expect(divWithoutPre.children[1]).toMatchObject({
  1847. type: NodeTypes.INTERPOLATION,
  1848. content: {
  1849. type: NodeTypes.SIMPLE_EXPRESSION,
  1850. content: `bar`,
  1851. isStatic: false,
  1852. },
  1853. })
  1854. })
  1855. // https://github.com/vuejs/docs/issues/2586
  1856. test('v-pre with half-open interpolation', () => {
  1857. const ast = baseParse(
  1858. `<div v-pre>
  1859. <span>{{ number </span>
  1860. <span>}}</span>
  1861. </div>
  1862. `,
  1863. )
  1864. expect((ast.children[0] as ElementNode).children).toMatchObject([
  1865. {
  1866. type: NodeTypes.ELEMENT,
  1867. children: [{ type: NodeTypes.TEXT, content: `{{ number ` }],
  1868. },
  1869. {
  1870. type: NodeTypes.ELEMENT,
  1871. children: [{ type: NodeTypes.TEXT, content: `}}` }],
  1872. },
  1873. ])
  1874. const ast2 = baseParse(`<div v-pre><span>{{ number </span></div>`)
  1875. expect((ast2.children[0] as ElementNode).children).toMatchObject([
  1876. {
  1877. type: NodeTypes.ELEMENT,
  1878. children: [{ type: NodeTypes.TEXT, content: `{{ number ` }],
  1879. },
  1880. ])
  1881. const ast3 = baseParse(`<div v-pre><textarea>{{ foo </textarea></div>`, {
  1882. parseMode: 'html',
  1883. })
  1884. expect((ast3.children[0] as ElementNode).children).toMatchObject([
  1885. {
  1886. type: NodeTypes.ELEMENT,
  1887. children: [
  1888. {
  1889. type: NodeTypes.TEXT,
  1890. content: `{{ foo `,
  1891. },
  1892. ],
  1893. },
  1894. ])
  1895. })
  1896. test('self-closing v-pre', () => {
  1897. const ast = baseParse(
  1898. `<div v-pre/>\n<div :id="foo"><Comp/>{{ bar }}</div>`,
  1899. )
  1900. // should not affect siblings after it
  1901. const divWithoutPre = ast.children[1] as ElementNode
  1902. expect(divWithoutPre.props).toMatchObject([
  1903. {
  1904. type: NodeTypes.DIRECTIVE,
  1905. name: `bind`,
  1906. arg: {
  1907. type: NodeTypes.SIMPLE_EXPRESSION,
  1908. isStatic: true,
  1909. content: `id`,
  1910. },
  1911. exp: {
  1912. type: NodeTypes.SIMPLE_EXPRESSION,
  1913. isStatic: false,
  1914. content: `foo`,
  1915. },
  1916. loc: {
  1917. start: {
  1918. line: 2,
  1919. column: 6,
  1920. },
  1921. end: {
  1922. line: 2,
  1923. column: 15,
  1924. },
  1925. },
  1926. },
  1927. ])
  1928. expect(divWithoutPre.children[0]).toMatchObject({
  1929. type: NodeTypes.ELEMENT,
  1930. tagType: ElementTypes.COMPONENT,
  1931. tag: `Comp`,
  1932. })
  1933. expect(divWithoutPre.children[1]).toMatchObject({
  1934. type: NodeTypes.INTERPOLATION,
  1935. content: {
  1936. type: NodeTypes.SIMPLE_EXPRESSION,
  1937. content: `bar`,
  1938. isStatic: false,
  1939. },
  1940. })
  1941. })
  1942. test('end tags are case-insensitive.', () => {
  1943. const ast = baseParse('<div>hello</DIV>after')
  1944. const element = ast.children[0] as ElementNode
  1945. const text = element.children[0] as TextNode
  1946. expect(text).toStrictEqual({
  1947. type: NodeTypes.TEXT,
  1948. content: 'hello',
  1949. loc: {
  1950. start: { offset: 5, line: 1, column: 6 },
  1951. end: { offset: 10, line: 1, column: 11 },
  1952. source: 'hello',
  1953. },
  1954. })
  1955. })
  1956. })
  1957. describe('Edge Cases', () => {
  1958. test('self closing single tag', () => {
  1959. const ast = baseParse('<div :class="{ some: condition }" />')
  1960. expect(ast.children).toHaveLength(1)
  1961. expect(ast.children[0]).toMatchObject({ tag: 'div' })
  1962. })
  1963. test('self closing multiple tag', () => {
  1964. const ast = baseParse(
  1965. `<div :class="{ some: condition }" />\n` +
  1966. `<p v-bind:style="{ color: 'red' }"/>`,
  1967. )
  1968. expect(ast).toMatchSnapshot()
  1969. expect(ast.children).toHaveLength(2)
  1970. expect(ast.children[0]).toMatchObject({ tag: 'div' })
  1971. expect(ast.children[1]).toMatchObject({ tag: 'p' })
  1972. })
  1973. test('valid html', () => {
  1974. const ast = baseParse(
  1975. `<div :class="{ some: condition }">\n` +
  1976. ` <p v-bind:style="{ color: 'red' }"/>\n` +
  1977. ` <!-- a comment with <html> inside it -->\n` +
  1978. `</div>`,
  1979. )
  1980. expect(ast).toMatchSnapshot()
  1981. expect(ast.children).toHaveLength(1)
  1982. const el = ast.children[0] as any
  1983. expect(el).toMatchObject({
  1984. tag: 'div',
  1985. })
  1986. expect(el.children).toHaveLength(2)
  1987. expect(el.children[0]).toMatchObject({
  1988. tag: 'p',
  1989. })
  1990. expect(el.children[1]).toMatchObject({
  1991. type: NodeTypes.COMMENT,
  1992. })
  1993. })
  1994. test('invalid html', () => {
  1995. expect(() => {
  1996. baseParse(`<div>\n<span>\n</div>\n</span>`)
  1997. }).toThrow('Element is missing end tag.')
  1998. const spy = vi.fn()
  1999. const ast = baseParse(`<div>\n<span>\n</div>\n</span>`, {
  2000. onError: spy,
  2001. })
  2002. expect(spy.mock.calls).toMatchObject([
  2003. [
  2004. {
  2005. code: ErrorCodes.X_MISSING_END_TAG,
  2006. loc: {
  2007. start: {
  2008. offset: 6,
  2009. line: 2,
  2010. column: 1,
  2011. },
  2012. },
  2013. },
  2014. ],
  2015. [
  2016. {
  2017. code: ErrorCodes.X_INVALID_END_TAG,
  2018. loc: {
  2019. start: {
  2020. offset: 20,
  2021. line: 4,
  2022. column: 1,
  2023. },
  2024. },
  2025. },
  2026. ],
  2027. ])
  2028. expect(ast).toMatchSnapshot()
  2029. })
  2030. test('parse with correct location info', () => {
  2031. const fooSrc = `foo\n is `
  2032. const barSrc = `{{ bar }}`
  2033. const butSrc = ` but `
  2034. const bazSrc = `{{ baz }}`
  2035. const [foo, bar, but, baz] = baseParse(
  2036. fooSrc + barSrc + butSrc + bazSrc,
  2037. ).children
  2038. let offset = 0
  2039. expect(foo.loc.start).toEqual({ line: 1, column: 1, offset })
  2040. offset += fooSrc.length
  2041. expect(foo.loc.end).toEqual({ line: 2, column: 5, offset })
  2042. expect(bar.loc.start).toEqual({ line: 2, column: 5, offset })
  2043. const barInner = (bar as InterpolationNode).content
  2044. offset += 3
  2045. expect(barInner.loc.start).toEqual({ line: 2, column: 8, offset })
  2046. offset += 3
  2047. expect(barInner.loc.end).toEqual({ line: 2, column: 11, offset })
  2048. offset += 3
  2049. expect(bar.loc.end).toEqual({ line: 2, column: 14, offset })
  2050. expect(but.loc.start).toEqual({ line: 2, column: 14, offset })
  2051. offset += butSrc.length
  2052. expect(but.loc.end).toEqual({ line: 2, column: 19, offset })
  2053. expect(baz.loc.start).toEqual({ line: 2, column: 19, offset })
  2054. const bazInner = (baz as InterpolationNode).content
  2055. offset += 3
  2056. expect(bazInner.loc.start).toEqual({ line: 2, column: 22, offset })
  2057. offset += 3
  2058. expect(bazInner.loc.end).toEqual({ line: 2, column: 25, offset })
  2059. offset += 3
  2060. expect(baz.loc.end).toEqual({ line: 2, column: 28, offset })
  2061. })
  2062. // With standard HTML parsing, the following input would ignore the slash
  2063. // and treat "<" and "template" as attributes on the open tag of "Hello",
  2064. // causing `<template>` to fail to close, and `<script>` being parsed as its
  2065. // child. This is would never be intended in actual templates, but is a common
  2066. // intermediate state from user input when parsing for IDE support. We want
  2067. // the `<script>` to be at root-level to keep the SFC structure stable for
  2068. // Volar to do incremental computations.
  2069. test('tag termination handling for IDE', () => {
  2070. const spy = vi.fn()
  2071. const ast = baseParse(
  2072. `<template><Hello\n</template><script>console.log(1)</script>`,
  2073. {
  2074. onError: spy,
  2075. },
  2076. )
  2077. //
  2078. expect(ast.children.length).toBe(2)
  2079. expect(ast.children[1]).toMatchObject({
  2080. type: NodeTypes.ELEMENT,
  2081. tag: 'script',
  2082. })
  2083. })
  2084. test('arg should be undefined on shorthand dirs with no arg', () => {
  2085. const ast = baseParse(`<template #></template>`)
  2086. const el = ast.children[0] as ElementNode
  2087. expect(el.props[0]).toMatchObject({
  2088. type: NodeTypes.DIRECTIVE,
  2089. name: 'slot',
  2090. exp: undefined,
  2091. arg: undefined,
  2092. })
  2093. })
  2094. // edge case found in vue-macros where the input is TS or JSX
  2095. test('should reset inRCDATA state', () => {
  2096. baseParse(`<Foo>`, { parseMode: 'sfc', onError() {} })
  2097. expect(() => baseParse(`{ foo }`)).not.toThrow()
  2098. })
  2099. test('correct loc when the closing > is foarmatted', () => {
  2100. const [span] = baseParse(`<span></span
  2101. >`).children
  2102. expect(span.loc.source).toBe('<span></span\n \n >')
  2103. expect(span.loc.start.offset).toBe(0)
  2104. expect(span.loc.end.offset).toBe(27)
  2105. })
  2106. test('correct loc when a line in attribute value ends with &', () => {
  2107. const [span] = baseParse(`<span v-if="foo &&\nbar"></span>`).children
  2108. expect(span.loc.end.line).toBe(2)
  2109. })
  2110. })
  2111. describe('decodeEntities option', () => {
  2112. test('use decode by default', () => {
  2113. const ast: any = baseParse('&gt;&lt;&amp;&apos;&quot;&foo;')
  2114. expect(ast.children.length).toBe(1)
  2115. expect(ast.children[0].type).toBe(NodeTypes.TEXT)
  2116. expect(ast.children[0].content).toBe('><&\'"&foo;')
  2117. })
  2118. test('should warn in non-browser build', () => {
  2119. baseParse('&amp;&cups;', {
  2120. decodeEntities: text => text.replace('&cups;', '\u222A\uFE00'),
  2121. onError: () => {}, // Ignore errors
  2122. })
  2123. expect(
  2124. `decodeEntities option is passed but will be ignored`,
  2125. ).toHaveBeenWarned()
  2126. })
  2127. })
  2128. describe('whitespace management when adopting strategy condense', () => {
  2129. const parse = (content: string, options?: ParserOptions) =>
  2130. baseParse(content, {
  2131. whitespace: 'condense',
  2132. ...options,
  2133. })
  2134. test('should remove whitespaces at start/end inside an element', () => {
  2135. const ast = parse(`<div> <span/> </div>`)
  2136. expect((ast.children[0] as ElementNode).children.length).toBe(1)
  2137. })
  2138. test('should remove whitespaces w/ newline between elements', () => {
  2139. const ast = parse(`<div/> \n <div/> \n <div/>`)
  2140. expect(ast.children.length).toBe(3)
  2141. expect(ast.children.every(c => c.type === NodeTypes.ELEMENT)).toBe(true)
  2142. })
  2143. test('should remove whitespaces adjacent to comments', () => {
  2144. const ast = parse(`<div/> \n <!--foo--> <div/>`)
  2145. expect(ast.children.length).toBe(3)
  2146. expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
  2147. expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
  2148. expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
  2149. })
  2150. test('should remove whitespaces w/ newline between comments and elements', () => {
  2151. const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
  2152. expect(ast.children.length).toBe(3)
  2153. expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
  2154. expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
  2155. expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
  2156. })
  2157. test('should NOT remove whitespaces w/ newline between interpolations', () => {
  2158. const ast = parse(`{{ foo }} \n {{ bar }}`)
  2159. expect(ast.children.length).toBe(3)
  2160. expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
  2161. expect(ast.children[1]).toMatchObject({
  2162. type: NodeTypes.TEXT,
  2163. content: ' ',
  2164. })
  2165. expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
  2166. })
  2167. test('should NOT remove whitespaces w/ newline between interpolation and comment', () => {
  2168. const ast = parse(`<!-- foo --> \n {{msg}}`)
  2169. expect(ast.children.length).toBe(3)
  2170. expect(ast.children[0].type).toBe(NodeTypes.COMMENT)
  2171. expect(ast.children[1]).toMatchObject({
  2172. type: NodeTypes.TEXT,
  2173. content: ' ',
  2174. })
  2175. expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
  2176. })
  2177. test('should NOT remove whitespaces w/o newline between elements', () => {
  2178. const ast = parse(`<div/> <div/> <div/>`)
  2179. expect(ast.children.length).toBe(5)
  2180. expect(ast.children.map(c => c.type)).toMatchObject([
  2181. NodeTypes.ELEMENT,
  2182. NodeTypes.TEXT,
  2183. NodeTypes.ELEMENT,
  2184. NodeTypes.TEXT,
  2185. NodeTypes.ELEMENT,
  2186. ])
  2187. })
  2188. test('should condense consecutive whitespaces in text', () => {
  2189. const ast = parse(` foo \n bar baz `)
  2190. expect((ast.children[0] as TextNode).content).toBe(` foo bar baz `)
  2191. })
  2192. test('should remove leading newline character immediately following the pre element start tag', () => {
  2193. const ast = parse(`<pre>\n foo bar </pre>`, {
  2194. isPreTag: tag => tag === 'pre',
  2195. isIgnoreNewlineTag: tag => tag === 'pre',
  2196. })
  2197. expect(ast.children).toHaveLength(1)
  2198. const preElement = ast.children[0] as ElementNode
  2199. expect(preElement.children).toHaveLength(1)
  2200. expect((preElement.children[0] as TextNode).content).toBe(` foo bar `)
  2201. })
  2202. test('should NOT remove leading newline character immediately following child-tag of pre element', () => {
  2203. const ast = parse(`<pre><span></span>\n foo bar </pre>`, {
  2204. isPreTag: tag => tag === 'pre',
  2205. })
  2206. const preElement = ast.children[0] as ElementNode
  2207. expect(preElement.children).toHaveLength(2)
  2208. expect((preElement.children[1] as TextNode).content).toBe(
  2209. `\n foo bar `,
  2210. )
  2211. })
  2212. test('self-closing pre tag', () => {
  2213. const ast = parse(`<pre/><span>\n foo bar</span>`, {
  2214. isPreTag: tag => tag === 'pre',
  2215. })
  2216. const elementAfterPre = ast.children[1] as ElementNode
  2217. // should not affect the <span> and condense its whitespace inside
  2218. expect((elementAfterPre.children[0] as TextNode).content).toBe(` foo bar`)
  2219. })
  2220. test('should NOT condense whitespaces in RCDATA text mode', () => {
  2221. const ast = parse(`<textarea>Text:\n foo</textarea>`, {
  2222. parseMode: 'html',
  2223. })
  2224. const preElement = ast.children[0] as ElementNode
  2225. expect(preElement.children).toHaveLength(1)
  2226. expect((preElement.children[0] as TextNode).content).toBe(`Text:\n foo`)
  2227. })
  2228. })
  2229. describe('whitespace management when adopting strategy preserve', () => {
  2230. const parse = (content: string, options?: ParserOptions) =>
  2231. baseParse(content, {
  2232. whitespace: 'preserve',
  2233. ...options,
  2234. })
  2235. test('should still remove whitespaces at start/end inside an element', () => {
  2236. const ast = parse(`<div> <span/> </div>`)
  2237. expect((ast.children[0] as ElementNode).children.length).toBe(1)
  2238. })
  2239. test('should preserve whitespaces w/ newline between elements', () => {
  2240. const ast = parse(`<div/> \n <div/> \n <div/>`)
  2241. expect(ast.children.length).toBe(5)
  2242. expect(ast.children.map(c => c.type)).toMatchObject([
  2243. NodeTypes.ELEMENT,
  2244. NodeTypes.TEXT,
  2245. NodeTypes.ELEMENT,
  2246. NodeTypes.TEXT,
  2247. NodeTypes.ELEMENT,
  2248. ])
  2249. })
  2250. test('should preserve whitespaces adjacent to comments', () => {
  2251. const ast = parse(`<div/> \n <!--foo--> <div/>`)
  2252. expect(ast.children.length).toBe(5)
  2253. expect(ast.children.map(c => c.type)).toMatchObject([
  2254. NodeTypes.ELEMENT,
  2255. NodeTypes.TEXT,
  2256. NodeTypes.COMMENT,
  2257. NodeTypes.TEXT,
  2258. NodeTypes.ELEMENT,
  2259. ])
  2260. })
  2261. test('should preserve whitespaces w/ newline between comments and elements', () => {
  2262. const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
  2263. expect(ast.children.length).toBe(5)
  2264. expect(ast.children.map(c => c.type)).toMatchObject([
  2265. NodeTypes.ELEMENT,
  2266. NodeTypes.TEXT,
  2267. NodeTypes.COMMENT,
  2268. NodeTypes.TEXT,
  2269. NodeTypes.ELEMENT,
  2270. ])
  2271. })
  2272. test('should preserve whitespaces w/ newline between interpolations', () => {
  2273. const ast = parse(`{{ foo }} \n {{ bar }}`)
  2274. expect(ast.children.length).toBe(3)
  2275. expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
  2276. expect(ast.children[1]).toMatchObject({
  2277. type: NodeTypes.TEXT,
  2278. content: ' ',
  2279. })
  2280. expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
  2281. })
  2282. test('should preserve whitespaces w/o newline between elements', () => {
  2283. const ast = parse(`<div/> <div/> <div/>`)
  2284. expect(ast.children.length).toBe(5)
  2285. expect(ast.children.map(c => c.type)).toMatchObject([
  2286. NodeTypes.ELEMENT,
  2287. NodeTypes.TEXT,
  2288. NodeTypes.ELEMENT,
  2289. NodeTypes.TEXT,
  2290. NodeTypes.ELEMENT,
  2291. ])
  2292. })
  2293. test('should preserve consecutive whitespaces in text', () => {
  2294. const content = ` foo \n bar baz `
  2295. const ast = parse(content)
  2296. expect((ast.children[0] as TextNode).content).toBe(content)
  2297. })
  2298. })
  2299. describe('expression parsing', () => {
  2300. test('interpolation', () => {
  2301. const ast = baseParse(`{{ a + b }}`, { prefixIdentifiers: true })
  2302. // @ts-expect-error
  2303. expect((ast.children[0] as InterpolationNode).content.ast?.type).toBe(
  2304. 'BinaryExpression',
  2305. )
  2306. })
  2307. test('v-bind', () => {
  2308. const ast = baseParse(`<div :[key+1]="foo()" />`, {
  2309. prefixIdentifiers: true,
  2310. })
  2311. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2312. // @ts-expect-error
  2313. expect(dir.arg?.ast?.type).toBe('BinaryExpression')
  2314. // @ts-expect-error
  2315. expect(dir.exp?.ast?.type).toBe('CallExpression')
  2316. })
  2317. test('v-on multi statements', () => {
  2318. const ast = baseParse(`<div @click="a++;b++" />`, {
  2319. prefixIdentifiers: true,
  2320. })
  2321. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2322. // @ts-expect-error
  2323. expect(dir.exp?.ast?.type).toBe('Program')
  2324. expect((dir.exp?.ast as Program).body).toMatchObject([
  2325. { type: 'ExpressionStatement' },
  2326. { type: 'ExpressionStatement' },
  2327. ])
  2328. })
  2329. test('v-slot', () => {
  2330. const ast = baseParse(`<Comp #foo="{ a, b }" />`, {
  2331. prefixIdentifiers: true,
  2332. })
  2333. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2334. // @ts-expect-error
  2335. expect(dir.exp?.ast?.type).toBe('ArrowFunctionExpression')
  2336. })
  2337. test('v-for', () => {
  2338. const ast = baseParse(`<div v-for="({ a, b }, key, index) of a.b" />`, {
  2339. prefixIdentifiers: true,
  2340. })
  2341. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2342. const { source, value, key, index } = dir.forParseResult!
  2343. // @ts-expect-error
  2344. expect(source.ast?.type).toBe('MemberExpression')
  2345. // @ts-expect-error
  2346. expect(value?.ast?.type).toBe('ArrowFunctionExpression')
  2347. expect(key?.ast).toBeNull() // simple ident
  2348. expect(index?.ast).toBeNull() // simple ident
  2349. })
  2350. })
  2351. describe('Errors', () => {
  2352. // HTML parsing errors as specified at
  2353. // https://html.spec.whatwg.org/multipage/parsing.html#parse-errors
  2354. // We ignore some errors that do NOT affect parse result in meaningful ways
  2355. // but have non-trivial implementation cost.
  2356. const patterns: {
  2357. [key: string]: Array<{
  2358. code: string
  2359. errors: Array<{ type: ErrorCodes; loc: Position }>
  2360. options?: Partial<ParserOptions>
  2361. }>
  2362. } = {
  2363. // ABRUPT_CLOSING_OF_EMPTY_COMMENT: [
  2364. // {
  2365. // code: '<template><!--></template>',
  2366. // errors: [
  2367. // {
  2368. // type: ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  2369. // loc: { offset: 10, line: 1, column: 11 }
  2370. // }
  2371. // ]
  2372. // },
  2373. // {
  2374. // code: '<template><!---></template>',
  2375. // errors: [
  2376. // {
  2377. // type: ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  2378. // loc: { offset: 10, line: 1, column: 11 }
  2379. // }
  2380. // ]
  2381. // },
  2382. // {
  2383. // code: '<template><!----></template>',
  2384. // errors: []
  2385. // }
  2386. // ],
  2387. CDATA_IN_HTML_CONTENT: [
  2388. {
  2389. code: '<template><![CDATA[cdata]]></template>',
  2390. errors: [
  2391. {
  2392. type: ErrorCodes.CDATA_IN_HTML_CONTENT,
  2393. loc: { offset: 10, line: 1, column: 11 },
  2394. },
  2395. ],
  2396. },
  2397. {
  2398. code: '<template><svg><![CDATA[cdata]]></svg></template>',
  2399. errors: [],
  2400. },
  2401. {
  2402. // invalid root-level CDATA should report a parser error
  2403. code: '<![CDATA[cdata]]>',
  2404. errors: [
  2405. {
  2406. type: ErrorCodes.CDATA_IN_HTML_CONTENT,
  2407. loc: { offset: 0, line: 1, column: 1 },
  2408. },
  2409. ],
  2410. },
  2411. ],
  2412. DUPLICATE_ATTRIBUTE: [
  2413. {
  2414. code: '<template><div id="" id=""></div></template>',
  2415. errors: [
  2416. {
  2417. type: ErrorCodes.DUPLICATE_ATTRIBUTE,
  2418. loc: { offset: 21, line: 1, column: 22 },
  2419. },
  2420. ],
  2421. },
  2422. ],
  2423. // END_TAG_WITH_ATTRIBUTES: [
  2424. // {
  2425. // code: '<template><div></div id=""></template>',
  2426. // errors: [
  2427. // {
  2428. // type: ErrorCodes.END_TAG_WITH_ATTRIBUTES,
  2429. // loc: { offset: 21, line: 1, column: 22 }
  2430. // }
  2431. // ]
  2432. // }
  2433. // ],
  2434. // END_TAG_WITH_TRAILING_SOLIDUS: [
  2435. // {
  2436. // code: '<template><div></div/></template>',
  2437. // errors: [
  2438. // {
  2439. // type: ErrorCodes.END_TAG_WITH_TRAILING_SOLIDUS,
  2440. // loc: { offset: 20, line: 1, column: 21 }
  2441. // }
  2442. // ]
  2443. // }
  2444. // ],
  2445. EOF_BEFORE_TAG_NAME: [
  2446. {
  2447. code: '<template><',
  2448. errors: [
  2449. {
  2450. type: ErrorCodes.EOF_BEFORE_TAG_NAME,
  2451. loc: { offset: 11, line: 1, column: 12 },
  2452. },
  2453. {
  2454. type: ErrorCodes.X_MISSING_END_TAG,
  2455. loc: { offset: 0, line: 1, column: 1 },
  2456. },
  2457. ],
  2458. },
  2459. {
  2460. code: '<template></',
  2461. errors: [
  2462. {
  2463. type: ErrorCodes.EOF_BEFORE_TAG_NAME,
  2464. loc: { offset: 12, line: 1, column: 13 },
  2465. },
  2466. {
  2467. type: ErrorCodes.X_MISSING_END_TAG,
  2468. loc: { offset: 0, line: 1, column: 1 },
  2469. },
  2470. ],
  2471. },
  2472. ],
  2473. EOF_IN_CDATA: [
  2474. {
  2475. code: '<template><svg><![CDATA[cdata',
  2476. errors: [
  2477. {
  2478. type: ErrorCodes.EOF_IN_CDATA,
  2479. loc: { offset: 29, line: 1, column: 30 },
  2480. },
  2481. {
  2482. type: ErrorCodes.X_MISSING_END_TAG,
  2483. loc: { offset: 10, line: 1, column: 11 },
  2484. },
  2485. {
  2486. type: ErrorCodes.X_MISSING_END_TAG,
  2487. loc: { offset: 0, line: 1, column: 1 },
  2488. },
  2489. ],
  2490. },
  2491. {
  2492. code: '<template><svg><![CDATA[',
  2493. errors: [
  2494. {
  2495. type: ErrorCodes.EOF_IN_CDATA,
  2496. loc: { offset: 24, line: 1, column: 25 },
  2497. },
  2498. {
  2499. type: ErrorCodes.X_MISSING_END_TAG,
  2500. loc: { offset: 10, line: 1, column: 11 },
  2501. },
  2502. {
  2503. type: ErrorCodes.X_MISSING_END_TAG,
  2504. loc: { offset: 0, line: 1, column: 1 },
  2505. },
  2506. ],
  2507. },
  2508. ],
  2509. EOF_IN_COMMENT: [
  2510. {
  2511. code: '<template><!--comment',
  2512. errors: [
  2513. {
  2514. type: ErrorCodes.EOF_IN_COMMENT,
  2515. loc: { offset: 21, line: 1, column: 22 },
  2516. },
  2517. {
  2518. type: ErrorCodes.X_MISSING_END_TAG,
  2519. loc: { offset: 0, line: 1, column: 1 },
  2520. },
  2521. ],
  2522. },
  2523. {
  2524. code: '<template><!--',
  2525. errors: [
  2526. {
  2527. type: ErrorCodes.EOF_IN_COMMENT,
  2528. loc: { offset: 14, line: 1, column: 15 },
  2529. },
  2530. {
  2531. type: ErrorCodes.X_MISSING_END_TAG,
  2532. loc: { offset: 0, line: 1, column: 1 },
  2533. },
  2534. ],
  2535. },
  2536. // // Bogus comments don't throw eof-in-comment error.
  2537. // // https://html.spec.whatwg.org/multipage/parsing.html#bogus-comment-state
  2538. // {
  2539. // code: '<template><!',
  2540. // errors: [
  2541. // {
  2542. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2543. // loc: { offset: 10, line: 1, column: 11 }
  2544. // },
  2545. // {
  2546. // type: ErrorCodes.X_MISSING_END_TAG,
  2547. // loc: { offset: 0, line: 1, column: 1 }
  2548. // }
  2549. // ]
  2550. // },
  2551. // {
  2552. // code: '<template><!-',
  2553. // errors: [
  2554. // {
  2555. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2556. // loc: { offset: 10, line: 1, column: 11 }
  2557. // },
  2558. // {
  2559. // type: ErrorCodes.X_MISSING_END_TAG,
  2560. // loc: { offset: 0, line: 1, column: 1 }
  2561. // }
  2562. // ]
  2563. // },
  2564. // {
  2565. // code: '<template><!abc',
  2566. // errors: [
  2567. // {
  2568. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2569. // loc: { offset: 10, line: 1, column: 11 }
  2570. // },
  2571. // {
  2572. // type: ErrorCodes.X_MISSING_END_TAG,
  2573. // loc: { offset: 0, line: 1, column: 1 }
  2574. // }
  2575. // ]
  2576. // }
  2577. ],
  2578. // EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT: [
  2579. // {
  2580. // code: "<script><!--console.log('hello')",
  2581. // errors: [
  2582. // {
  2583. // type: ErrorCodes.X_MISSING_END_TAG,
  2584. // loc: { offset: 0, line: 1, column: 1 }
  2585. // },
  2586. // {
  2587. // type: ErrorCodes.EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT,
  2588. // loc: { offset: 32, line: 1, column: 33 }
  2589. // }
  2590. // ]
  2591. // },
  2592. // {
  2593. // code: "<script>console.log('hello')",
  2594. // errors: [
  2595. // {
  2596. // type: ErrorCodes.X_MISSING_END_TAG,
  2597. // loc: { offset: 0, line: 1, column: 1 }
  2598. // }
  2599. // ]
  2600. // }
  2601. // ],
  2602. EOF_IN_TAG: [
  2603. {
  2604. code: '<template><div',
  2605. errors: [
  2606. {
  2607. type: ErrorCodes.EOF_IN_TAG,
  2608. loc: { offset: 14, line: 1, column: 15 },
  2609. },
  2610. {
  2611. type: ErrorCodes.X_MISSING_END_TAG,
  2612. loc: { offset: 0, line: 1, column: 1 },
  2613. },
  2614. ],
  2615. },
  2616. {
  2617. code: '<template><div ',
  2618. errors: [
  2619. {
  2620. type: ErrorCodes.EOF_IN_TAG,
  2621. loc: { offset: 15, line: 1, column: 16 },
  2622. },
  2623. {
  2624. type: ErrorCodes.X_MISSING_END_TAG,
  2625. loc: { offset: 0, line: 1, column: 1 },
  2626. },
  2627. ],
  2628. },
  2629. {
  2630. code: '<template><div id',
  2631. errors: [
  2632. {
  2633. type: ErrorCodes.EOF_IN_TAG,
  2634. loc: { offset: 17, line: 1, column: 18 },
  2635. },
  2636. {
  2637. type: ErrorCodes.X_MISSING_END_TAG,
  2638. loc: { offset: 0, line: 1, column: 1 },
  2639. },
  2640. ],
  2641. },
  2642. {
  2643. code: '<template><div id ',
  2644. errors: [
  2645. {
  2646. type: ErrorCodes.EOF_IN_TAG,
  2647. loc: { offset: 18, line: 1, column: 19 },
  2648. },
  2649. {
  2650. type: ErrorCodes.X_MISSING_END_TAG,
  2651. loc: { offset: 0, line: 1, column: 1 },
  2652. },
  2653. ],
  2654. },
  2655. {
  2656. code: '<template><div id =',
  2657. errors: [
  2658. // {
  2659. // type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2660. // loc: { offset: 19, line: 1, column: 20 }
  2661. // },
  2662. {
  2663. type: ErrorCodes.EOF_IN_TAG,
  2664. loc: { offset: 19, line: 1, column: 20 },
  2665. },
  2666. {
  2667. type: ErrorCodes.X_MISSING_END_TAG,
  2668. loc: { offset: 0, line: 1, column: 1 },
  2669. },
  2670. ],
  2671. },
  2672. {
  2673. code: "<template><div id='abc",
  2674. errors: [
  2675. {
  2676. type: ErrorCodes.EOF_IN_TAG,
  2677. loc: { offset: 22, line: 1, column: 23 },
  2678. },
  2679. {
  2680. type: ErrorCodes.X_MISSING_END_TAG,
  2681. loc: { offset: 0, line: 1, column: 1 },
  2682. },
  2683. ],
  2684. },
  2685. {
  2686. code: '<template><div id="abc',
  2687. errors: [
  2688. {
  2689. type: ErrorCodes.EOF_IN_TAG,
  2690. loc: { offset: 22, line: 1, column: 23 },
  2691. },
  2692. {
  2693. type: ErrorCodes.X_MISSING_END_TAG,
  2694. loc: { offset: 0, line: 1, column: 1 },
  2695. },
  2696. ],
  2697. },
  2698. {
  2699. code: "<template><div id='abc'",
  2700. errors: [
  2701. {
  2702. type: ErrorCodes.EOF_IN_TAG,
  2703. loc: { offset: 23, line: 1, column: 24 },
  2704. },
  2705. {
  2706. type: ErrorCodes.X_MISSING_END_TAG,
  2707. loc: { offset: 0, line: 1, column: 1 },
  2708. },
  2709. ],
  2710. },
  2711. {
  2712. code: '<template><div id="abc"',
  2713. errors: [
  2714. {
  2715. type: ErrorCodes.EOF_IN_TAG,
  2716. loc: { offset: 23, line: 1, column: 24 },
  2717. },
  2718. {
  2719. type: ErrorCodes.X_MISSING_END_TAG,
  2720. loc: { offset: 0, line: 1, column: 1 },
  2721. },
  2722. ],
  2723. },
  2724. {
  2725. code: '<template><div id=abc',
  2726. errors: [
  2727. {
  2728. type: ErrorCodes.EOF_IN_TAG,
  2729. loc: { offset: 21, line: 1, column: 22 },
  2730. },
  2731. {
  2732. type: ErrorCodes.X_MISSING_END_TAG,
  2733. loc: { offset: 0, line: 1, column: 1 },
  2734. },
  2735. ],
  2736. },
  2737. {
  2738. code: "<template><div id='abc'/",
  2739. errors: [
  2740. {
  2741. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2742. loc: { offset: 23, line: 1, column: 24 },
  2743. },
  2744. {
  2745. type: ErrorCodes.EOF_IN_TAG,
  2746. loc: { offset: 24, line: 1, column: 25 },
  2747. },
  2748. {
  2749. type: ErrorCodes.X_MISSING_END_TAG,
  2750. loc: { offset: 0, line: 1, column: 1 },
  2751. },
  2752. ],
  2753. },
  2754. {
  2755. code: '<template><div id="abc"/',
  2756. errors: [
  2757. {
  2758. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2759. loc: { offset: 23, line: 1, column: 24 },
  2760. },
  2761. {
  2762. type: ErrorCodes.EOF_IN_TAG,
  2763. loc: { offset: 24, line: 1, column: 25 },
  2764. },
  2765. {
  2766. type: ErrorCodes.X_MISSING_END_TAG,
  2767. loc: { offset: 0, line: 1, column: 1 },
  2768. },
  2769. ],
  2770. },
  2771. {
  2772. code: '<template><div id=abc /',
  2773. errors: [
  2774. {
  2775. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2776. loc: { offset: 22, line: 1, column: 23 },
  2777. },
  2778. {
  2779. type: ErrorCodes.EOF_IN_TAG,
  2780. loc: { offset: 23, line: 1, column: 24 },
  2781. },
  2782. {
  2783. type: ErrorCodes.X_MISSING_END_TAG,
  2784. loc: { offset: 0, line: 1, column: 1 },
  2785. },
  2786. ],
  2787. },
  2788. {
  2789. code: '<div></div',
  2790. errors: [
  2791. {
  2792. type: ErrorCodes.EOF_IN_TAG,
  2793. loc: { offset: 10, line: 1, column: 11 },
  2794. },
  2795. {
  2796. type: ErrorCodes.X_MISSING_END_TAG,
  2797. loc: { offset: 0, line: 1, column: 1 },
  2798. },
  2799. ],
  2800. },
  2801. ],
  2802. // INCORRECTLY_CLOSED_COMMENT: [
  2803. // {
  2804. // code: '<template><!--comment--!></template>',
  2805. // errors: [
  2806. // {
  2807. // type: ErrorCodes.INCORRECTLY_CLOSED_COMMENT,
  2808. // loc: { offset: 10, line: 1, column: 11 }
  2809. // }
  2810. // ]
  2811. // }
  2812. // ],
  2813. // INCORRECTLY_OPENED_COMMENT: [
  2814. // {
  2815. // code: '<template><!></template>',
  2816. // errors: [
  2817. // {
  2818. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2819. // loc: { offset: 10, line: 1, column: 11 }
  2820. // }
  2821. // ]
  2822. // },
  2823. // {
  2824. // code: '<template><!-></template>',
  2825. // errors: [
  2826. // {
  2827. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2828. // loc: { offset: 10, line: 1, column: 11 }
  2829. // }
  2830. // ]
  2831. // },
  2832. // {
  2833. // code: '<template><!ELEMENT br EMPTY></template>',
  2834. // errors: [
  2835. // {
  2836. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2837. // loc: { offset: 10, line: 1, column: 11 }
  2838. // }
  2839. // ]
  2840. // },
  2841. // // Just ignore doctype.
  2842. // {
  2843. // code: '<!DOCTYPE html>',
  2844. // errors: []
  2845. // }
  2846. // ],
  2847. // INVALID_FIRST_CHARACTER_OF_TAG_NAME: [
  2848. // {
  2849. // code: '<template>a < b</template>',
  2850. // errors: [
  2851. // {
  2852. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2853. // loc: { offset: 13, line: 1, column: 14 }
  2854. // }
  2855. // ]
  2856. // },
  2857. // {
  2858. // code: '<template><�></template>',
  2859. // errors: [
  2860. // {
  2861. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2862. // loc: { offset: 11, line: 1, column: 12 }
  2863. // }
  2864. // ]
  2865. // },
  2866. // {
  2867. // code: '<template>a </ b</template>',
  2868. // errors: [
  2869. // {
  2870. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2871. // loc: { offset: 14, line: 1, column: 15 }
  2872. // },
  2873. // {
  2874. // type: ErrorCodes.X_MISSING_END_TAG,
  2875. // loc: { offset: 0, line: 1, column: 1 }
  2876. // }
  2877. // ]
  2878. // },
  2879. // {
  2880. // code: '<template></�></template>',
  2881. // errors: [
  2882. // {
  2883. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2884. // loc: { offset: 12, line: 1, column: 13 }
  2885. // }
  2886. // ]
  2887. // },
  2888. // // Don't throw invalid-first-character-of-tag-name in interpolation
  2889. // {
  2890. // code: '<template>{{a < b}}</template>',
  2891. // errors: []
  2892. // }
  2893. // ],
  2894. MISSING_ATTRIBUTE_VALUE: [
  2895. {
  2896. code: '<template><div id=></div></template>',
  2897. errors: [
  2898. {
  2899. type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2900. loc: { offset: 18, line: 1, column: 19 },
  2901. },
  2902. ],
  2903. },
  2904. {
  2905. code: '<template><div id= ></div></template>',
  2906. errors: [
  2907. {
  2908. type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2909. loc: { offset: 19, line: 1, column: 20 },
  2910. },
  2911. ],
  2912. },
  2913. {
  2914. code: '<template><div id= /></div></template>',
  2915. errors: [],
  2916. },
  2917. ],
  2918. MISSING_END_TAG_NAME: [
  2919. {
  2920. code: '<template></></template>',
  2921. errors: [
  2922. {
  2923. type: ErrorCodes.MISSING_END_TAG_NAME,
  2924. loc: { offset: 12, line: 1, column: 13 },
  2925. },
  2926. ],
  2927. },
  2928. ],
  2929. // MISSING_WHITESPACE_BETWEEN_ATTRIBUTES: [
  2930. // {
  2931. // code: '<template><div id="foo"class="bar"></div></template>',
  2932. // errors: [
  2933. // {
  2934. // type: ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES,
  2935. // loc: { offset: 23, line: 1, column: 24 }
  2936. // }
  2937. // ]
  2938. // },
  2939. // // CR doesn't appear in tokenization phase, but all CR are removed in preprocessing.
  2940. // // https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
  2941. // {
  2942. // code: '<template><div id="foo"\r\nclass="bar"></div></template>',
  2943. // errors: []
  2944. // }
  2945. // ],
  2946. // NESTED_COMMENT: [
  2947. // {
  2948. // code: '<template><!--a<!--b--></template>',
  2949. // errors: [
  2950. // {
  2951. // type: ErrorCodes.NESTED_COMMENT,
  2952. // loc: { offset: 15, line: 1, column: 16 }
  2953. // }
  2954. // ]
  2955. // },
  2956. // {
  2957. // code: '<template><!--a<!--b<!--c--></template>',
  2958. // errors: [
  2959. // {
  2960. // type: ErrorCodes.NESTED_COMMENT,
  2961. // loc: { offset: 15, line: 1, column: 16 }
  2962. // },
  2963. // {
  2964. // type: ErrorCodes.NESTED_COMMENT,
  2965. // loc: { offset: 20, line: 1, column: 21 }
  2966. // }
  2967. // ]
  2968. // },
  2969. // {
  2970. // code: '<template><!--a<!--b<!----></template>',
  2971. // errors: [
  2972. // {
  2973. // type: ErrorCodes.NESTED_COMMENT,
  2974. // loc: { offset: 15, line: 1, column: 16 }
  2975. // }
  2976. // ]
  2977. // },
  2978. // {
  2979. // code: '<template><!--a<!--></template>',
  2980. // errors: []
  2981. // },
  2982. // {
  2983. // code: '<template><!--a<!--',
  2984. // errors: [
  2985. // {
  2986. // type: ErrorCodes.EOF_IN_COMMENT,
  2987. // loc: { offset: 19, line: 1, column: 20 }
  2988. // },
  2989. // {
  2990. // type: ErrorCodes.X_MISSING_END_TAG,
  2991. // loc: { offset: 0, line: 1, column: 1 }
  2992. // }
  2993. // ]
  2994. // }
  2995. // ],
  2996. UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME: [
  2997. {
  2998. code: "<template><div a\"bc=''></div></template>",
  2999. errors: [
  3000. {
  3001. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  3002. loc: { offset: 16, line: 1, column: 17 },
  3003. },
  3004. ],
  3005. },
  3006. {
  3007. code: "<template><div a'bc=''></div></template>",
  3008. errors: [
  3009. {
  3010. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  3011. loc: { offset: 16, line: 1, column: 17 },
  3012. },
  3013. ],
  3014. },
  3015. {
  3016. code: "<template><div a<bc=''></div></template>",
  3017. errors: [
  3018. {
  3019. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  3020. loc: { offset: 16, line: 1, column: 17 },
  3021. },
  3022. ],
  3023. },
  3024. ],
  3025. UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE: [
  3026. {
  3027. code: '<template><div foo=bar"></div></template>',
  3028. errors: [
  3029. {
  3030. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  3031. loc: { offset: 22, line: 1, column: 23 },
  3032. },
  3033. ],
  3034. },
  3035. {
  3036. code: "<template><div foo=bar'></div></template>",
  3037. errors: [
  3038. {
  3039. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  3040. loc: { offset: 22, line: 1, column: 23 },
  3041. },
  3042. ],
  3043. },
  3044. {
  3045. code: '<template><div foo=bar<div></div></template>',
  3046. errors: [
  3047. {
  3048. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  3049. loc: { offset: 22, line: 1, column: 23 },
  3050. },
  3051. ],
  3052. },
  3053. {
  3054. code: '<template><div foo=bar=baz></div></template>',
  3055. errors: [
  3056. {
  3057. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  3058. loc: { offset: 22, line: 1, column: 23 },
  3059. },
  3060. ],
  3061. },
  3062. {
  3063. code: '<template><div foo=bar`></div></template>',
  3064. errors: [
  3065. {
  3066. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  3067. loc: { offset: 22, line: 1, column: 23 },
  3068. },
  3069. ],
  3070. },
  3071. ],
  3072. UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME: [
  3073. {
  3074. code: '<template><div =foo=bar></div></template>',
  3075. errors: [
  3076. {
  3077. type: ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  3078. loc: { offset: 15, line: 1, column: 16 },
  3079. },
  3080. ],
  3081. },
  3082. {
  3083. code: '<template><div =></div></template>',
  3084. errors: [
  3085. {
  3086. type: ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  3087. loc: { offset: 15, line: 1, column: 16 },
  3088. },
  3089. ],
  3090. },
  3091. ],
  3092. UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME: [
  3093. {
  3094. code: '<template><?xml?></template>',
  3095. errors: [
  3096. {
  3097. type: ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME,
  3098. loc: { offset: 11, line: 1, column: 12 },
  3099. },
  3100. ],
  3101. },
  3102. ],
  3103. UNEXPECTED_SOLIDUS_IN_TAG: [
  3104. {
  3105. code: '<template><div a/b></div></template>',
  3106. errors: [
  3107. {
  3108. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  3109. loc: { offset: 16, line: 1, column: 17 },
  3110. },
  3111. ],
  3112. },
  3113. ],
  3114. X_INVALID_END_TAG: [
  3115. {
  3116. code: '<template></div></template>',
  3117. errors: [
  3118. {
  3119. type: ErrorCodes.X_INVALID_END_TAG,
  3120. loc: { offset: 10, line: 1, column: 11 },
  3121. },
  3122. ],
  3123. },
  3124. {
  3125. code: '<template></div></div></template>',
  3126. errors: [
  3127. {
  3128. type: ErrorCodes.X_INVALID_END_TAG,
  3129. loc: { offset: 10, line: 1, column: 11 },
  3130. },
  3131. {
  3132. type: ErrorCodes.X_INVALID_END_TAG,
  3133. loc: { offset: 16, line: 1, column: 17 },
  3134. },
  3135. ],
  3136. },
  3137. {
  3138. code: '<template>a </ b</template>',
  3139. errors: [
  3140. {
  3141. type: ErrorCodes.X_INVALID_END_TAG,
  3142. loc: { offset: 12, line: 1, column: 13 },
  3143. },
  3144. {
  3145. type: ErrorCodes.X_MISSING_END_TAG,
  3146. loc: { offset: 0, line: 1, column: 1 },
  3147. },
  3148. ],
  3149. },
  3150. {
  3151. code: "<template>{{'</div>'}}</template>",
  3152. errors: [],
  3153. },
  3154. {
  3155. code: '<textarea></div></textarea>',
  3156. errors: [],
  3157. },
  3158. {
  3159. code: '<svg><![CDATA[</div>]]></svg>',
  3160. errors: [],
  3161. },
  3162. {
  3163. code: '<svg><!--</div>--></svg>',
  3164. errors: [],
  3165. },
  3166. ],
  3167. X_MISSING_END_TAG: [
  3168. {
  3169. code: '<template><div></template>',
  3170. errors: [
  3171. {
  3172. type: ErrorCodes.X_MISSING_END_TAG,
  3173. loc: { offset: 10, line: 1, column: 11 },
  3174. },
  3175. ],
  3176. },
  3177. {
  3178. code: '<template><div>',
  3179. errors: [
  3180. {
  3181. type: ErrorCodes.X_MISSING_END_TAG,
  3182. loc: { offset: 10, line: 1, column: 11 },
  3183. },
  3184. {
  3185. type: ErrorCodes.X_MISSING_END_TAG,
  3186. loc: { offset: 0, line: 1, column: 1 },
  3187. },
  3188. ],
  3189. },
  3190. ],
  3191. X_MISSING_INTERPOLATION_END: [
  3192. {
  3193. code: '{{ foo',
  3194. errors: [
  3195. {
  3196. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  3197. loc: { offset: 0, line: 1, column: 1 },
  3198. },
  3199. ],
  3200. },
  3201. {
  3202. code: '{{',
  3203. errors: [
  3204. {
  3205. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  3206. loc: { offset: 0, line: 1, column: 1 },
  3207. },
  3208. ],
  3209. },
  3210. {
  3211. code: '<div>{{ foo</div>',
  3212. errors: [
  3213. {
  3214. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  3215. loc: { offset: 5, line: 1, column: 6 },
  3216. },
  3217. {
  3218. type: ErrorCodes.X_MISSING_END_TAG,
  3219. loc: { offset: 0, line: 1, column: 1 },
  3220. },
  3221. ],
  3222. },
  3223. {
  3224. code: '{{}}',
  3225. errors: [],
  3226. },
  3227. ],
  3228. X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END: [
  3229. {
  3230. code: `<div v-foo:[sef fsef] />`,
  3231. errors: [
  3232. {
  3233. type: ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
  3234. loc: { offset: 15, line: 1, column: 16 },
  3235. },
  3236. ],
  3237. },
  3238. ],
  3239. }
  3240. for (const key of Object.keys(patterns)) {
  3241. describe(key, () => {
  3242. for (const { code, errors, options } of patterns[key]) {
  3243. test(
  3244. code.replace(
  3245. /[\r\n]/g,
  3246. c => `\\x0${c.codePointAt(0)!.toString(16)};`,
  3247. ),
  3248. () => {
  3249. const spy = vi.fn()
  3250. const ast = baseParse(code, {
  3251. parseMode: 'html',
  3252. getNamespace: tag =>
  3253. tag === 'svg' ? Namespaces.SVG : Namespaces.HTML,
  3254. ...options,
  3255. onError: spy,
  3256. })
  3257. expect(
  3258. spy.mock.calls.map(([err]) => ({
  3259. type: err.code,
  3260. loc: err.loc.start,
  3261. })),
  3262. ).toMatchObject(errors)
  3263. expect(ast).toMatchSnapshot()
  3264. },
  3265. )
  3266. }
  3267. })
  3268. }
  3269. })
  3270. })