parse.spec.ts 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456
  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. })
  2107. describe('decodeEntities option', () => {
  2108. test('use decode by default', () => {
  2109. const ast: any = baseParse('&gt;&lt;&amp;&apos;&quot;&foo;')
  2110. expect(ast.children.length).toBe(1)
  2111. expect(ast.children[0].type).toBe(NodeTypes.TEXT)
  2112. expect(ast.children[0].content).toBe('><&\'"&foo;')
  2113. })
  2114. test('should warn in non-browser build', () => {
  2115. baseParse('&amp;&cups;', {
  2116. decodeEntities: text => text.replace('&cups;', '\u222A\uFE00'),
  2117. onError: () => {}, // Ignore errors
  2118. })
  2119. expect(
  2120. `decodeEntities option is passed but will be ignored`,
  2121. ).toHaveBeenWarned()
  2122. })
  2123. })
  2124. describe('whitespace management when adopting strategy condense', () => {
  2125. const parse = (content: string, options?: ParserOptions) =>
  2126. baseParse(content, {
  2127. whitespace: 'condense',
  2128. ...options,
  2129. })
  2130. test('should remove whitespaces at start/end inside an element', () => {
  2131. const ast = parse(`<div> <span/> </div>`)
  2132. expect((ast.children[0] as ElementNode).children.length).toBe(1)
  2133. })
  2134. test('should remove whitespaces w/ newline between elements', () => {
  2135. const ast = parse(`<div/> \n <div/> \n <div/>`)
  2136. expect(ast.children.length).toBe(3)
  2137. expect(ast.children.every(c => c.type === NodeTypes.ELEMENT)).toBe(true)
  2138. })
  2139. test('should remove whitespaces adjacent to comments', () => {
  2140. const ast = parse(`<div/> \n <!--foo--> <div/>`)
  2141. expect(ast.children.length).toBe(3)
  2142. expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
  2143. expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
  2144. expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
  2145. })
  2146. test('should remove whitespaces w/ newline between comments and elements', () => {
  2147. const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
  2148. expect(ast.children.length).toBe(3)
  2149. expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
  2150. expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
  2151. expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
  2152. })
  2153. test('should NOT remove whitespaces w/ newline between interpolations', () => {
  2154. const ast = parse(`{{ foo }} \n {{ bar }}`)
  2155. expect(ast.children.length).toBe(3)
  2156. expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
  2157. expect(ast.children[1]).toMatchObject({
  2158. type: NodeTypes.TEXT,
  2159. content: ' ',
  2160. })
  2161. expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
  2162. })
  2163. test('should NOT remove whitespaces w/ newline between interpolation and comment', () => {
  2164. const ast = parse(`<!-- foo --> \n {{msg}}`)
  2165. expect(ast.children.length).toBe(3)
  2166. expect(ast.children[0].type).toBe(NodeTypes.COMMENT)
  2167. expect(ast.children[1]).toMatchObject({
  2168. type: NodeTypes.TEXT,
  2169. content: ' ',
  2170. })
  2171. expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
  2172. })
  2173. test('should NOT remove whitespaces w/o newline between elements', () => {
  2174. const ast = parse(`<div/> <div/> <div/>`)
  2175. expect(ast.children.length).toBe(5)
  2176. expect(ast.children.map(c => c.type)).toMatchObject([
  2177. NodeTypes.ELEMENT,
  2178. NodeTypes.TEXT,
  2179. NodeTypes.ELEMENT,
  2180. NodeTypes.TEXT,
  2181. NodeTypes.ELEMENT,
  2182. ])
  2183. })
  2184. test('should condense consecutive whitespaces in text', () => {
  2185. const ast = parse(` foo \n bar baz `)
  2186. expect((ast.children[0] as TextNode).content).toBe(` foo bar baz `)
  2187. })
  2188. test('should remove leading newline character immediately following the pre element start tag', () => {
  2189. const ast = parse(`<pre>\n foo bar </pre>`, {
  2190. isPreTag: tag => tag === 'pre',
  2191. isIgnoreNewlineTag: tag => tag === 'pre',
  2192. })
  2193. expect(ast.children).toHaveLength(1)
  2194. const preElement = ast.children[0] as ElementNode
  2195. expect(preElement.children).toHaveLength(1)
  2196. expect((preElement.children[0] as TextNode).content).toBe(` foo bar `)
  2197. })
  2198. test('should NOT remove leading newline character immediately following child-tag of pre element', () => {
  2199. const ast = parse(`<pre><span></span>\n foo bar </pre>`, {
  2200. isPreTag: tag => tag === 'pre',
  2201. })
  2202. const preElement = ast.children[0] as ElementNode
  2203. expect(preElement.children).toHaveLength(2)
  2204. expect((preElement.children[1] as TextNode).content).toBe(
  2205. `\n foo bar `,
  2206. )
  2207. })
  2208. test('self-closing pre tag', () => {
  2209. const ast = parse(`<pre/><span>\n foo bar</span>`, {
  2210. isPreTag: tag => tag === 'pre',
  2211. })
  2212. const elementAfterPre = ast.children[1] as ElementNode
  2213. // should not affect the <span> and condense its whitespace inside
  2214. expect((elementAfterPre.children[0] as TextNode).content).toBe(` foo bar`)
  2215. })
  2216. test('should NOT condense whitespaces in RCDATA text mode', () => {
  2217. const ast = parse(`<textarea>Text:\n foo</textarea>`, {
  2218. parseMode: 'html',
  2219. })
  2220. const preElement = ast.children[0] as ElementNode
  2221. expect(preElement.children).toHaveLength(1)
  2222. expect((preElement.children[0] as TextNode).content).toBe(`Text:\n foo`)
  2223. })
  2224. })
  2225. describe('whitespace management when adopting strategy preserve', () => {
  2226. const parse = (content: string, options?: ParserOptions) =>
  2227. baseParse(content, {
  2228. whitespace: 'preserve',
  2229. ...options,
  2230. })
  2231. test('should still remove whitespaces at start/end inside an element', () => {
  2232. const ast = parse(`<div> <span/> </div>`)
  2233. expect((ast.children[0] as ElementNode).children.length).toBe(1)
  2234. })
  2235. test('should preserve whitespaces w/ newline between elements', () => {
  2236. const ast = parse(`<div/> \n <div/> \n <div/>`)
  2237. expect(ast.children.length).toBe(5)
  2238. expect(ast.children.map(c => c.type)).toMatchObject([
  2239. NodeTypes.ELEMENT,
  2240. NodeTypes.TEXT,
  2241. NodeTypes.ELEMENT,
  2242. NodeTypes.TEXT,
  2243. NodeTypes.ELEMENT,
  2244. ])
  2245. })
  2246. test('should preserve whitespaces adjacent to comments', () => {
  2247. const ast = parse(`<div/> \n <!--foo--> <div/>`)
  2248. expect(ast.children.length).toBe(5)
  2249. expect(ast.children.map(c => c.type)).toMatchObject([
  2250. NodeTypes.ELEMENT,
  2251. NodeTypes.TEXT,
  2252. NodeTypes.COMMENT,
  2253. NodeTypes.TEXT,
  2254. NodeTypes.ELEMENT,
  2255. ])
  2256. })
  2257. test('should preserve whitespaces w/ newline between comments and elements', () => {
  2258. const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
  2259. expect(ast.children.length).toBe(5)
  2260. expect(ast.children.map(c => c.type)).toMatchObject([
  2261. NodeTypes.ELEMENT,
  2262. NodeTypes.TEXT,
  2263. NodeTypes.COMMENT,
  2264. NodeTypes.TEXT,
  2265. NodeTypes.ELEMENT,
  2266. ])
  2267. })
  2268. test('should preserve whitespaces w/ newline between interpolations', () => {
  2269. const ast = parse(`{{ foo }} \n {{ bar }}`)
  2270. expect(ast.children.length).toBe(3)
  2271. expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
  2272. expect(ast.children[1]).toMatchObject({
  2273. type: NodeTypes.TEXT,
  2274. content: ' ',
  2275. })
  2276. expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
  2277. })
  2278. test('should preserve whitespaces w/o newline between elements', () => {
  2279. const ast = parse(`<div/> <div/> <div/>`)
  2280. expect(ast.children.length).toBe(5)
  2281. expect(ast.children.map(c => c.type)).toMatchObject([
  2282. NodeTypes.ELEMENT,
  2283. NodeTypes.TEXT,
  2284. NodeTypes.ELEMENT,
  2285. NodeTypes.TEXT,
  2286. NodeTypes.ELEMENT,
  2287. ])
  2288. })
  2289. test('should preserve consecutive whitespaces in text', () => {
  2290. const content = ` foo \n bar baz `
  2291. const ast = parse(content)
  2292. expect((ast.children[0] as TextNode).content).toBe(content)
  2293. })
  2294. })
  2295. describe('expression parsing', () => {
  2296. test('interpolation', () => {
  2297. const ast = baseParse(`{{ a + b }}`, { prefixIdentifiers: true })
  2298. // @ts-expect-error
  2299. expect((ast.children[0] as InterpolationNode).content.ast?.type).toBe(
  2300. 'BinaryExpression',
  2301. )
  2302. })
  2303. test('v-bind', () => {
  2304. const ast = baseParse(`<div :[key+1]="foo()" />`, {
  2305. prefixIdentifiers: true,
  2306. })
  2307. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2308. // @ts-expect-error
  2309. expect(dir.arg?.ast?.type).toBe('BinaryExpression')
  2310. // @ts-expect-error
  2311. expect(dir.exp?.ast?.type).toBe('CallExpression')
  2312. })
  2313. test('v-on multi statements', () => {
  2314. const ast = baseParse(`<div @click="a++;b++" />`, {
  2315. prefixIdentifiers: true,
  2316. })
  2317. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2318. // @ts-expect-error
  2319. expect(dir.exp?.ast?.type).toBe('Program')
  2320. expect((dir.exp?.ast as Program).body).toMatchObject([
  2321. { type: 'ExpressionStatement' },
  2322. { type: 'ExpressionStatement' },
  2323. ])
  2324. })
  2325. test('v-slot', () => {
  2326. const ast = baseParse(`<Comp #foo="{ a, b }" />`, {
  2327. prefixIdentifiers: true,
  2328. })
  2329. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2330. // @ts-expect-error
  2331. expect(dir.exp?.ast?.type).toBe('ArrowFunctionExpression')
  2332. })
  2333. test('v-for', () => {
  2334. const ast = baseParse(`<div v-for="({ a, b }, key, index) of a.b" />`, {
  2335. prefixIdentifiers: true,
  2336. })
  2337. const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
  2338. const { source, value, key, index } = dir.forParseResult!
  2339. // @ts-expect-error
  2340. expect(source.ast?.type).toBe('MemberExpression')
  2341. // @ts-expect-error
  2342. expect(value?.ast?.type).toBe('ArrowFunctionExpression')
  2343. expect(key?.ast).toBeNull() // simple ident
  2344. expect(index?.ast).toBeNull() // simple ident
  2345. })
  2346. })
  2347. describe('Errors', () => {
  2348. // HTML parsing errors as specified at
  2349. // https://html.spec.whatwg.org/multipage/parsing.html#parse-errors
  2350. // We ignore some errors that do NOT affect parse result in meaningful ways
  2351. // but have non-trivial implementation cost.
  2352. const patterns: {
  2353. [key: string]: Array<{
  2354. code: string
  2355. errors: Array<{ type: ErrorCodes; loc: Position }>
  2356. options?: Partial<ParserOptions>
  2357. }>
  2358. } = {
  2359. // ABRUPT_CLOSING_OF_EMPTY_COMMENT: [
  2360. // {
  2361. // code: '<template><!--></template>',
  2362. // errors: [
  2363. // {
  2364. // type: ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  2365. // loc: { offset: 10, line: 1, column: 11 }
  2366. // }
  2367. // ]
  2368. // },
  2369. // {
  2370. // code: '<template><!---></template>',
  2371. // errors: [
  2372. // {
  2373. // type: ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  2374. // loc: { offset: 10, line: 1, column: 11 }
  2375. // }
  2376. // ]
  2377. // },
  2378. // {
  2379. // code: '<template><!----></template>',
  2380. // errors: []
  2381. // }
  2382. // ],
  2383. CDATA_IN_HTML_CONTENT: [
  2384. {
  2385. code: '<template><![CDATA[cdata]]></template>',
  2386. errors: [
  2387. {
  2388. type: ErrorCodes.CDATA_IN_HTML_CONTENT,
  2389. loc: { offset: 10, line: 1, column: 11 },
  2390. },
  2391. ],
  2392. },
  2393. {
  2394. code: '<template><svg><![CDATA[cdata]]></svg></template>',
  2395. errors: [],
  2396. },
  2397. ],
  2398. DUPLICATE_ATTRIBUTE: [
  2399. {
  2400. code: '<template><div id="" id=""></div></template>',
  2401. errors: [
  2402. {
  2403. type: ErrorCodes.DUPLICATE_ATTRIBUTE,
  2404. loc: { offset: 21, line: 1, column: 22 },
  2405. },
  2406. ],
  2407. },
  2408. ],
  2409. // END_TAG_WITH_ATTRIBUTES: [
  2410. // {
  2411. // code: '<template><div></div id=""></template>',
  2412. // errors: [
  2413. // {
  2414. // type: ErrorCodes.END_TAG_WITH_ATTRIBUTES,
  2415. // loc: { offset: 21, line: 1, column: 22 }
  2416. // }
  2417. // ]
  2418. // }
  2419. // ],
  2420. // END_TAG_WITH_TRAILING_SOLIDUS: [
  2421. // {
  2422. // code: '<template><div></div/></template>',
  2423. // errors: [
  2424. // {
  2425. // type: ErrorCodes.END_TAG_WITH_TRAILING_SOLIDUS,
  2426. // loc: { offset: 20, line: 1, column: 21 }
  2427. // }
  2428. // ]
  2429. // }
  2430. // ],
  2431. EOF_BEFORE_TAG_NAME: [
  2432. {
  2433. code: '<template><',
  2434. errors: [
  2435. {
  2436. type: ErrorCodes.EOF_BEFORE_TAG_NAME,
  2437. loc: { offset: 11, line: 1, column: 12 },
  2438. },
  2439. {
  2440. type: ErrorCodes.X_MISSING_END_TAG,
  2441. loc: { offset: 0, line: 1, column: 1 },
  2442. },
  2443. ],
  2444. },
  2445. {
  2446. code: '<template></',
  2447. errors: [
  2448. {
  2449. type: ErrorCodes.EOF_BEFORE_TAG_NAME,
  2450. loc: { offset: 12, line: 1, column: 13 },
  2451. },
  2452. {
  2453. type: ErrorCodes.X_MISSING_END_TAG,
  2454. loc: { offset: 0, line: 1, column: 1 },
  2455. },
  2456. ],
  2457. },
  2458. ],
  2459. EOF_IN_CDATA: [
  2460. {
  2461. code: '<template><svg><![CDATA[cdata',
  2462. errors: [
  2463. {
  2464. type: ErrorCodes.EOF_IN_CDATA,
  2465. loc: { offset: 29, line: 1, column: 30 },
  2466. },
  2467. {
  2468. type: ErrorCodes.X_MISSING_END_TAG,
  2469. loc: { offset: 10, line: 1, column: 11 },
  2470. },
  2471. {
  2472. type: ErrorCodes.X_MISSING_END_TAG,
  2473. loc: { offset: 0, line: 1, column: 1 },
  2474. },
  2475. ],
  2476. },
  2477. {
  2478. code: '<template><svg><![CDATA[',
  2479. errors: [
  2480. {
  2481. type: ErrorCodes.EOF_IN_CDATA,
  2482. loc: { offset: 24, line: 1, column: 25 },
  2483. },
  2484. {
  2485. type: ErrorCodes.X_MISSING_END_TAG,
  2486. loc: { offset: 10, line: 1, column: 11 },
  2487. },
  2488. {
  2489. type: ErrorCodes.X_MISSING_END_TAG,
  2490. loc: { offset: 0, line: 1, column: 1 },
  2491. },
  2492. ],
  2493. },
  2494. ],
  2495. EOF_IN_COMMENT: [
  2496. {
  2497. code: '<template><!--comment',
  2498. errors: [
  2499. {
  2500. type: ErrorCodes.EOF_IN_COMMENT,
  2501. loc: { offset: 21, line: 1, column: 22 },
  2502. },
  2503. {
  2504. type: ErrorCodes.X_MISSING_END_TAG,
  2505. loc: { offset: 0, line: 1, column: 1 },
  2506. },
  2507. ],
  2508. },
  2509. {
  2510. code: '<template><!--',
  2511. errors: [
  2512. {
  2513. type: ErrorCodes.EOF_IN_COMMENT,
  2514. loc: { offset: 14, line: 1, column: 15 },
  2515. },
  2516. {
  2517. type: ErrorCodes.X_MISSING_END_TAG,
  2518. loc: { offset: 0, line: 1, column: 1 },
  2519. },
  2520. ],
  2521. },
  2522. // // Bogus comments don't throw eof-in-comment error.
  2523. // // https://html.spec.whatwg.org/multipage/parsing.html#bogus-comment-state
  2524. // {
  2525. // code: '<template><!',
  2526. // errors: [
  2527. // {
  2528. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2529. // loc: { offset: 10, line: 1, column: 11 }
  2530. // },
  2531. // {
  2532. // type: ErrorCodes.X_MISSING_END_TAG,
  2533. // loc: { offset: 0, line: 1, column: 1 }
  2534. // }
  2535. // ]
  2536. // },
  2537. // {
  2538. // code: '<template><!-',
  2539. // errors: [
  2540. // {
  2541. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2542. // loc: { offset: 10, line: 1, column: 11 }
  2543. // },
  2544. // {
  2545. // type: ErrorCodes.X_MISSING_END_TAG,
  2546. // loc: { offset: 0, line: 1, column: 1 }
  2547. // }
  2548. // ]
  2549. // },
  2550. // {
  2551. // code: '<template><!abc',
  2552. // errors: [
  2553. // {
  2554. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2555. // loc: { offset: 10, line: 1, column: 11 }
  2556. // },
  2557. // {
  2558. // type: ErrorCodes.X_MISSING_END_TAG,
  2559. // loc: { offset: 0, line: 1, column: 1 }
  2560. // }
  2561. // ]
  2562. // }
  2563. ],
  2564. // EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT: [
  2565. // {
  2566. // code: "<script><!--console.log('hello')",
  2567. // errors: [
  2568. // {
  2569. // type: ErrorCodes.X_MISSING_END_TAG,
  2570. // loc: { offset: 0, line: 1, column: 1 }
  2571. // },
  2572. // {
  2573. // type: ErrorCodes.EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT,
  2574. // loc: { offset: 32, line: 1, column: 33 }
  2575. // }
  2576. // ]
  2577. // },
  2578. // {
  2579. // code: "<script>console.log('hello')",
  2580. // errors: [
  2581. // {
  2582. // type: ErrorCodes.X_MISSING_END_TAG,
  2583. // loc: { offset: 0, line: 1, column: 1 }
  2584. // }
  2585. // ]
  2586. // }
  2587. // ],
  2588. EOF_IN_TAG: [
  2589. {
  2590. code: '<template><div',
  2591. errors: [
  2592. {
  2593. type: ErrorCodes.EOF_IN_TAG,
  2594. loc: { offset: 14, line: 1, column: 15 },
  2595. },
  2596. {
  2597. type: ErrorCodes.X_MISSING_END_TAG,
  2598. loc: { offset: 0, line: 1, column: 1 },
  2599. },
  2600. ],
  2601. },
  2602. {
  2603. code: '<template><div ',
  2604. errors: [
  2605. {
  2606. type: ErrorCodes.EOF_IN_TAG,
  2607. loc: { offset: 15, line: 1, column: 16 },
  2608. },
  2609. {
  2610. type: ErrorCodes.X_MISSING_END_TAG,
  2611. loc: { offset: 0, line: 1, column: 1 },
  2612. },
  2613. ],
  2614. },
  2615. {
  2616. code: '<template><div id',
  2617. errors: [
  2618. {
  2619. type: ErrorCodes.EOF_IN_TAG,
  2620. loc: { offset: 17, line: 1, column: 18 },
  2621. },
  2622. {
  2623. type: ErrorCodes.X_MISSING_END_TAG,
  2624. loc: { offset: 0, line: 1, column: 1 },
  2625. },
  2626. ],
  2627. },
  2628. {
  2629. code: '<template><div id ',
  2630. errors: [
  2631. {
  2632. type: ErrorCodes.EOF_IN_TAG,
  2633. loc: { offset: 18, line: 1, column: 19 },
  2634. },
  2635. {
  2636. type: ErrorCodes.X_MISSING_END_TAG,
  2637. loc: { offset: 0, line: 1, column: 1 },
  2638. },
  2639. ],
  2640. },
  2641. {
  2642. code: '<template><div id =',
  2643. errors: [
  2644. // {
  2645. // type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2646. // loc: { offset: 19, line: 1, column: 20 }
  2647. // },
  2648. {
  2649. type: ErrorCodes.EOF_IN_TAG,
  2650. loc: { offset: 19, line: 1, column: 20 },
  2651. },
  2652. {
  2653. type: ErrorCodes.X_MISSING_END_TAG,
  2654. loc: { offset: 0, line: 1, column: 1 },
  2655. },
  2656. ],
  2657. },
  2658. {
  2659. code: "<template><div id='abc",
  2660. errors: [
  2661. {
  2662. type: ErrorCodes.EOF_IN_TAG,
  2663. loc: { offset: 22, line: 1, column: 23 },
  2664. },
  2665. {
  2666. type: ErrorCodes.X_MISSING_END_TAG,
  2667. loc: { offset: 0, line: 1, column: 1 },
  2668. },
  2669. ],
  2670. },
  2671. {
  2672. code: '<template><div id="abc',
  2673. errors: [
  2674. {
  2675. type: ErrorCodes.EOF_IN_TAG,
  2676. loc: { offset: 22, line: 1, column: 23 },
  2677. },
  2678. {
  2679. type: ErrorCodes.X_MISSING_END_TAG,
  2680. loc: { offset: 0, line: 1, column: 1 },
  2681. },
  2682. ],
  2683. },
  2684. {
  2685. code: "<template><div id='abc'",
  2686. errors: [
  2687. {
  2688. type: ErrorCodes.EOF_IN_TAG,
  2689. loc: { offset: 23, line: 1, column: 24 },
  2690. },
  2691. {
  2692. type: ErrorCodes.X_MISSING_END_TAG,
  2693. loc: { offset: 0, line: 1, column: 1 },
  2694. },
  2695. ],
  2696. },
  2697. {
  2698. code: '<template><div id="abc"',
  2699. errors: [
  2700. {
  2701. type: ErrorCodes.EOF_IN_TAG,
  2702. loc: { offset: 23, line: 1, column: 24 },
  2703. },
  2704. {
  2705. type: ErrorCodes.X_MISSING_END_TAG,
  2706. loc: { offset: 0, line: 1, column: 1 },
  2707. },
  2708. ],
  2709. },
  2710. {
  2711. code: '<template><div id=abc',
  2712. errors: [
  2713. {
  2714. type: ErrorCodes.EOF_IN_TAG,
  2715. loc: { offset: 21, line: 1, column: 22 },
  2716. },
  2717. {
  2718. type: ErrorCodes.X_MISSING_END_TAG,
  2719. loc: { offset: 0, line: 1, column: 1 },
  2720. },
  2721. ],
  2722. },
  2723. {
  2724. code: "<template><div id='abc'/",
  2725. errors: [
  2726. {
  2727. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2728. loc: { offset: 23, line: 1, column: 24 },
  2729. },
  2730. {
  2731. type: ErrorCodes.EOF_IN_TAG,
  2732. loc: { offset: 24, line: 1, column: 25 },
  2733. },
  2734. {
  2735. type: ErrorCodes.X_MISSING_END_TAG,
  2736. loc: { offset: 0, line: 1, column: 1 },
  2737. },
  2738. ],
  2739. },
  2740. {
  2741. code: '<template><div id="abc"/',
  2742. errors: [
  2743. {
  2744. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2745. loc: { offset: 23, line: 1, column: 24 },
  2746. },
  2747. {
  2748. type: ErrorCodes.EOF_IN_TAG,
  2749. loc: { offset: 24, line: 1, column: 25 },
  2750. },
  2751. {
  2752. type: ErrorCodes.X_MISSING_END_TAG,
  2753. loc: { offset: 0, line: 1, column: 1 },
  2754. },
  2755. ],
  2756. },
  2757. {
  2758. code: '<template><div id=abc /',
  2759. errors: [
  2760. {
  2761. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2762. loc: { offset: 22, line: 1, column: 23 },
  2763. },
  2764. {
  2765. type: ErrorCodes.EOF_IN_TAG,
  2766. loc: { offset: 23, line: 1, column: 24 },
  2767. },
  2768. {
  2769. type: ErrorCodes.X_MISSING_END_TAG,
  2770. loc: { offset: 0, line: 1, column: 1 },
  2771. },
  2772. ],
  2773. },
  2774. {
  2775. code: '<div></div',
  2776. errors: [
  2777. {
  2778. type: ErrorCodes.EOF_IN_TAG,
  2779. loc: { offset: 10, line: 1, column: 11 },
  2780. },
  2781. {
  2782. type: ErrorCodes.X_MISSING_END_TAG,
  2783. loc: { offset: 0, line: 1, column: 1 },
  2784. },
  2785. ],
  2786. },
  2787. ],
  2788. // INCORRECTLY_CLOSED_COMMENT: [
  2789. // {
  2790. // code: '<template><!--comment--!></template>',
  2791. // errors: [
  2792. // {
  2793. // type: ErrorCodes.INCORRECTLY_CLOSED_COMMENT,
  2794. // loc: { offset: 10, line: 1, column: 11 }
  2795. // }
  2796. // ]
  2797. // }
  2798. // ],
  2799. // INCORRECTLY_OPENED_COMMENT: [
  2800. // {
  2801. // code: '<template><!></template>',
  2802. // errors: [
  2803. // {
  2804. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2805. // loc: { offset: 10, line: 1, column: 11 }
  2806. // }
  2807. // ]
  2808. // },
  2809. // {
  2810. // code: '<template><!-></template>',
  2811. // errors: [
  2812. // {
  2813. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2814. // loc: { offset: 10, line: 1, column: 11 }
  2815. // }
  2816. // ]
  2817. // },
  2818. // {
  2819. // code: '<template><!ELEMENT br EMPTY></template>',
  2820. // errors: [
  2821. // {
  2822. // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2823. // loc: { offset: 10, line: 1, column: 11 }
  2824. // }
  2825. // ]
  2826. // },
  2827. // // Just ignore doctype.
  2828. // {
  2829. // code: '<!DOCTYPE html>',
  2830. // errors: []
  2831. // }
  2832. // ],
  2833. // INVALID_FIRST_CHARACTER_OF_TAG_NAME: [
  2834. // {
  2835. // code: '<template>a < b</template>',
  2836. // errors: [
  2837. // {
  2838. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2839. // loc: { offset: 13, line: 1, column: 14 }
  2840. // }
  2841. // ]
  2842. // },
  2843. // {
  2844. // code: '<template><�></template>',
  2845. // errors: [
  2846. // {
  2847. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2848. // loc: { offset: 11, line: 1, column: 12 }
  2849. // }
  2850. // ]
  2851. // },
  2852. // {
  2853. // code: '<template>a </ b</template>',
  2854. // errors: [
  2855. // {
  2856. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2857. // loc: { offset: 14, line: 1, column: 15 }
  2858. // },
  2859. // {
  2860. // type: ErrorCodes.X_MISSING_END_TAG,
  2861. // loc: { offset: 0, line: 1, column: 1 }
  2862. // }
  2863. // ]
  2864. // },
  2865. // {
  2866. // code: '<template></�></template>',
  2867. // errors: [
  2868. // {
  2869. // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2870. // loc: { offset: 12, line: 1, column: 13 }
  2871. // }
  2872. // ]
  2873. // },
  2874. // // Don't throw invalid-first-character-of-tag-name in interpolation
  2875. // {
  2876. // code: '<template>{{a < b}}</template>',
  2877. // errors: []
  2878. // }
  2879. // ],
  2880. MISSING_ATTRIBUTE_VALUE: [
  2881. {
  2882. code: '<template><div id=></div></template>',
  2883. errors: [
  2884. {
  2885. type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2886. loc: { offset: 18, line: 1, column: 19 },
  2887. },
  2888. ],
  2889. },
  2890. {
  2891. code: '<template><div id= ></div></template>',
  2892. errors: [
  2893. {
  2894. type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2895. loc: { offset: 19, line: 1, column: 20 },
  2896. },
  2897. ],
  2898. },
  2899. {
  2900. code: '<template><div id= /></div></template>',
  2901. errors: [],
  2902. },
  2903. ],
  2904. MISSING_END_TAG_NAME: [
  2905. {
  2906. code: '<template></></template>',
  2907. errors: [
  2908. {
  2909. type: ErrorCodes.MISSING_END_TAG_NAME,
  2910. loc: { offset: 12, line: 1, column: 13 },
  2911. },
  2912. ],
  2913. },
  2914. ],
  2915. // MISSING_WHITESPACE_BETWEEN_ATTRIBUTES: [
  2916. // {
  2917. // code: '<template><div id="foo"class="bar"></div></template>',
  2918. // errors: [
  2919. // {
  2920. // type: ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES,
  2921. // loc: { offset: 23, line: 1, column: 24 }
  2922. // }
  2923. // ]
  2924. // },
  2925. // // CR doesn't appear in tokenization phase, but all CR are removed in preprocessing.
  2926. // // https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
  2927. // {
  2928. // code: '<template><div id="foo"\r\nclass="bar"></div></template>',
  2929. // errors: []
  2930. // }
  2931. // ],
  2932. // NESTED_COMMENT: [
  2933. // {
  2934. // code: '<template><!--a<!--b--></template>',
  2935. // errors: [
  2936. // {
  2937. // type: ErrorCodes.NESTED_COMMENT,
  2938. // loc: { offset: 15, line: 1, column: 16 }
  2939. // }
  2940. // ]
  2941. // },
  2942. // {
  2943. // code: '<template><!--a<!--b<!--c--></template>',
  2944. // errors: [
  2945. // {
  2946. // type: ErrorCodes.NESTED_COMMENT,
  2947. // loc: { offset: 15, line: 1, column: 16 }
  2948. // },
  2949. // {
  2950. // type: ErrorCodes.NESTED_COMMENT,
  2951. // loc: { offset: 20, line: 1, column: 21 }
  2952. // }
  2953. // ]
  2954. // },
  2955. // {
  2956. // code: '<template><!--a<!--b<!----></template>',
  2957. // errors: [
  2958. // {
  2959. // type: ErrorCodes.NESTED_COMMENT,
  2960. // loc: { offset: 15, line: 1, column: 16 }
  2961. // }
  2962. // ]
  2963. // },
  2964. // {
  2965. // code: '<template><!--a<!--></template>',
  2966. // errors: []
  2967. // },
  2968. // {
  2969. // code: '<template><!--a<!--',
  2970. // errors: [
  2971. // {
  2972. // type: ErrorCodes.EOF_IN_COMMENT,
  2973. // loc: { offset: 19, line: 1, column: 20 }
  2974. // },
  2975. // {
  2976. // type: ErrorCodes.X_MISSING_END_TAG,
  2977. // loc: { offset: 0, line: 1, column: 1 }
  2978. // }
  2979. // ]
  2980. // }
  2981. // ],
  2982. UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME: [
  2983. {
  2984. code: "<template><div a\"bc=''></div></template>",
  2985. errors: [
  2986. {
  2987. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  2988. loc: { offset: 16, line: 1, column: 17 },
  2989. },
  2990. ],
  2991. },
  2992. {
  2993. code: "<template><div a'bc=''></div></template>",
  2994. errors: [
  2995. {
  2996. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  2997. loc: { offset: 16, line: 1, column: 17 },
  2998. },
  2999. ],
  3000. },
  3001. {
  3002. code: "<template><div a<bc=''></div></template>",
  3003. errors: [
  3004. {
  3005. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  3006. loc: { offset: 16, line: 1, column: 17 },
  3007. },
  3008. ],
  3009. },
  3010. ],
  3011. UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE: [
  3012. {
  3013. code: '<template><div foo=bar"></div></template>',
  3014. errors: [
  3015. {
  3016. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  3017. loc: { offset: 22, line: 1, column: 23 },
  3018. },
  3019. ],
  3020. },
  3021. {
  3022. code: "<template><div foo=bar'></div></template>",
  3023. errors: [
  3024. {
  3025. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  3026. loc: { offset: 22, line: 1, column: 23 },
  3027. },
  3028. ],
  3029. },
  3030. {
  3031. code: '<template><div foo=bar<div></div></template>',
  3032. errors: [
  3033. {
  3034. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  3035. loc: { offset: 22, line: 1, column: 23 },
  3036. },
  3037. ],
  3038. },
  3039. {
  3040. code: '<template><div foo=bar=baz></div></template>',
  3041. errors: [
  3042. {
  3043. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  3044. loc: { offset: 22, line: 1, column: 23 },
  3045. },
  3046. ],
  3047. },
  3048. {
  3049. code: '<template><div foo=bar`></div></template>',
  3050. errors: [
  3051. {
  3052. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  3053. loc: { offset: 22, line: 1, column: 23 },
  3054. },
  3055. ],
  3056. },
  3057. ],
  3058. UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME: [
  3059. {
  3060. code: '<template><div =foo=bar></div></template>',
  3061. errors: [
  3062. {
  3063. type: ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  3064. loc: { offset: 15, line: 1, column: 16 },
  3065. },
  3066. ],
  3067. },
  3068. {
  3069. code: '<template><div =></div></template>',
  3070. errors: [
  3071. {
  3072. type: ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  3073. loc: { offset: 15, line: 1, column: 16 },
  3074. },
  3075. ],
  3076. },
  3077. ],
  3078. UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME: [
  3079. {
  3080. code: '<template><?xml?></template>',
  3081. errors: [
  3082. {
  3083. type: ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME,
  3084. loc: { offset: 11, line: 1, column: 12 },
  3085. },
  3086. ],
  3087. },
  3088. ],
  3089. UNEXPECTED_SOLIDUS_IN_TAG: [
  3090. {
  3091. code: '<template><div a/b></div></template>',
  3092. errors: [
  3093. {
  3094. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  3095. loc: { offset: 16, line: 1, column: 17 },
  3096. },
  3097. ],
  3098. },
  3099. ],
  3100. X_INVALID_END_TAG: [
  3101. {
  3102. code: '<template></div></template>',
  3103. errors: [
  3104. {
  3105. type: ErrorCodes.X_INVALID_END_TAG,
  3106. loc: { offset: 10, line: 1, column: 11 },
  3107. },
  3108. ],
  3109. },
  3110. {
  3111. code: '<template></div></div></template>',
  3112. errors: [
  3113. {
  3114. type: ErrorCodes.X_INVALID_END_TAG,
  3115. loc: { offset: 10, line: 1, column: 11 },
  3116. },
  3117. {
  3118. type: ErrorCodes.X_INVALID_END_TAG,
  3119. loc: { offset: 16, line: 1, column: 17 },
  3120. },
  3121. ],
  3122. },
  3123. {
  3124. code: '<template>a </ b</template>',
  3125. errors: [
  3126. {
  3127. type: ErrorCodes.X_INVALID_END_TAG,
  3128. loc: { offset: 12, line: 1, column: 13 },
  3129. },
  3130. {
  3131. type: ErrorCodes.X_MISSING_END_TAG,
  3132. loc: { offset: 0, line: 1, column: 1 },
  3133. },
  3134. ],
  3135. },
  3136. {
  3137. code: "<template>{{'</div>'}}</template>",
  3138. errors: [],
  3139. },
  3140. {
  3141. code: '<textarea></div></textarea>',
  3142. errors: [],
  3143. },
  3144. {
  3145. code: '<svg><![CDATA[</div>]]></svg>',
  3146. errors: [],
  3147. },
  3148. {
  3149. code: '<svg><!--</div>--></svg>',
  3150. errors: [],
  3151. },
  3152. ],
  3153. X_MISSING_END_TAG: [
  3154. {
  3155. code: '<template><div></template>',
  3156. errors: [
  3157. {
  3158. type: ErrorCodes.X_MISSING_END_TAG,
  3159. loc: { offset: 10, line: 1, column: 11 },
  3160. },
  3161. ],
  3162. },
  3163. {
  3164. code: '<template><div>',
  3165. errors: [
  3166. {
  3167. type: ErrorCodes.X_MISSING_END_TAG,
  3168. loc: { offset: 10, line: 1, column: 11 },
  3169. },
  3170. {
  3171. type: ErrorCodes.X_MISSING_END_TAG,
  3172. loc: { offset: 0, line: 1, column: 1 },
  3173. },
  3174. ],
  3175. },
  3176. ],
  3177. X_MISSING_INTERPOLATION_END: [
  3178. {
  3179. code: '{{ foo',
  3180. errors: [
  3181. {
  3182. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  3183. loc: { offset: 0, line: 1, column: 1 },
  3184. },
  3185. ],
  3186. },
  3187. {
  3188. code: '{{',
  3189. errors: [
  3190. {
  3191. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  3192. loc: { offset: 0, line: 1, column: 1 },
  3193. },
  3194. ],
  3195. },
  3196. {
  3197. code: '<div>{{ foo</div>',
  3198. errors: [
  3199. {
  3200. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  3201. loc: { offset: 5, line: 1, column: 6 },
  3202. },
  3203. {
  3204. type: ErrorCodes.X_MISSING_END_TAG,
  3205. loc: { offset: 0, line: 1, column: 1 },
  3206. },
  3207. ],
  3208. },
  3209. {
  3210. code: '{{}}',
  3211. errors: [],
  3212. },
  3213. ],
  3214. X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END: [
  3215. {
  3216. code: `<div v-foo:[sef fsef] />`,
  3217. errors: [
  3218. {
  3219. type: ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
  3220. loc: { offset: 15, line: 1, column: 16 },
  3221. },
  3222. ],
  3223. },
  3224. ],
  3225. }
  3226. for (const key of Object.keys(patterns)) {
  3227. describe(key, () => {
  3228. for (const { code, errors, options } of patterns[key]) {
  3229. test(
  3230. code.replace(
  3231. /[\r\n]/g,
  3232. c => `\\x0${c.codePointAt(0)!.toString(16)};`,
  3233. ),
  3234. () => {
  3235. const spy = vi.fn()
  3236. const ast = baseParse(code, {
  3237. parseMode: 'html',
  3238. getNamespace: tag =>
  3239. tag === 'svg' ? Namespaces.SVG : Namespaces.HTML,
  3240. ...options,
  3241. onError: spy,
  3242. })
  3243. expect(
  3244. spy.mock.calls.map(([err]) => ({
  3245. type: err.code,
  3246. loc: err.loc.start,
  3247. })),
  3248. ).toMatchObject(errors)
  3249. expect(ast).toMatchSnapshot()
  3250. },
  3251. )
  3252. }
  3253. })
  3254. }
  3255. })
  3256. })