parse.spec.ts 98 KB

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