parse.spec.ts 99 KB

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