parse.spec.ts 99 KB

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