| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461 |
- import type { ParserOptions } from '../src/options'
- import { ErrorCodes } from '../src/errors'
- import {
- type CommentNode,
- ConstantTypes,
- type DirectiveNode,
- type ElementNode,
- ElementTypes,
- type InterpolationNode,
- NodeTypes,
- type Position,
- type TextNode,
- } from '../src/ast'
- import { baseParse } from '../src/parser'
- import type { Program } from '@babel/types'
- import { Namespaces } from '@vue/shared'
- describe('compiler: parse', () => {
- describe('Text', () => {
- test('simple text', () => {
- const ast = baseParse('some text')
- const text = ast.children[0] as TextNode
- expect(text).toStrictEqual({
- type: NodeTypes.TEXT,
- content: 'some text',
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 9, line: 1, column: 10 },
- source: 'some text',
- },
- })
- })
- test('simple text with invalid end tag', () => {
- const onError = vi.fn()
- const ast = baseParse('some text</div>', { onError })
- const text = ast.children[0] as TextNode
- expect(onError.mock.calls).toMatchObject([
- [
- {
- code: ErrorCodes.X_INVALID_END_TAG,
- loc: {
- start: { column: 10, line: 1, offset: 9 },
- end: { column: 10, line: 1, offset: 9 },
- },
- },
- ],
- ])
- expect(text).toStrictEqual({
- type: NodeTypes.TEXT,
- content: 'some text',
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 9, line: 1, column: 10 },
- source: 'some text',
- },
- })
- })
- test('text with interpolation', () => {
- const ast = baseParse('some {{ foo + bar }} text')
- const text1 = ast.children[0] as TextNode
- const text2 = ast.children[2] as TextNode
- expect(text1).toStrictEqual({
- type: NodeTypes.TEXT,
- content: 'some ',
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 5, line: 1, column: 6 },
- source: 'some ',
- },
- })
- expect(text2).toStrictEqual({
- type: NodeTypes.TEXT,
- content: ' text',
- loc: {
- start: { offset: 20, line: 1, column: 21 },
- end: { offset: 25, line: 1, column: 26 },
- source: ' text',
- },
- })
- })
- test('text with interpolation which has `<`', () => {
- const ast = baseParse('some {{ a<b && c>d }} text')
- const text1 = ast.children[0] as TextNode
- const text2 = ast.children[2] as TextNode
- expect(text1).toStrictEqual({
- type: NodeTypes.TEXT,
- content: 'some ',
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 5, line: 1, column: 6 },
- source: 'some ',
- },
- })
- expect(text2).toStrictEqual({
- type: NodeTypes.TEXT,
- content: ' text',
- loc: {
- start: { offset: 21, line: 1, column: 22 },
- end: { offset: 26, line: 1, column: 27 },
- source: ' text',
- },
- })
- })
- test('text with mix of tags and interpolations', () => {
- const ast = baseParse('some <span>{{ foo < bar + foo }} text</span>')
- const text1 = ast.children[0] as TextNode
- const text2 = (ast.children[1] as ElementNode).children![1] as TextNode
- expect(text1).toStrictEqual({
- type: NodeTypes.TEXT,
- content: 'some ',
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 5, line: 1, column: 6 },
- source: 'some ',
- },
- })
- expect(text2).toStrictEqual({
- type: NodeTypes.TEXT,
- content: ' text',
- loc: {
- start: { offset: 32, line: 1, column: 33 },
- end: { offset: 37, line: 1, column: 38 },
- source: ' text',
- },
- })
- })
- test('lonely "<" doesn\'t separate nodes', () => {
- const ast = baseParse('a < b', {
- onError: err => {
- if (err.code !== ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME) {
- throw err
- }
- },
- })
- const text = ast.children[0] as TextNode
- expect(text).toStrictEqual({
- type: NodeTypes.TEXT,
- content: 'a < b',
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 5, line: 1, column: 6 },
- source: 'a < b',
- },
- })
- })
- test('lonely "{{" doesn\'t separate nodes', () => {
- const ast = baseParse('a {{ b', {
- onError: error => {
- if (error.code !== ErrorCodes.X_MISSING_INTERPOLATION_END) {
- throw error
- }
- },
- })
- const text = ast.children[0] as TextNode
- expect(text).toStrictEqual({
- type: NodeTypes.TEXT,
- content: 'a {{ b',
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 6, line: 1, column: 7 },
- source: 'a {{ b',
- },
- })
- })
- })
- describe('Interpolation', () => {
- test('simple interpolation', () => {
- const ast = baseParse('{{message}}')
- const interpolation = ast.children[0] as InterpolationNode
- expect(interpolation).toStrictEqual({
- type: NodeTypes.INTERPOLATION,
- content: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `message`,
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 2, line: 1, column: 3 },
- end: { offset: 9, line: 1, column: 10 },
- source: 'message',
- },
- },
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 11, line: 1, column: 12 },
- source: '{{message}}',
- },
- })
- })
- test('it can have tag-like notation', () => {
- const ast = baseParse('{{ a<b }}')
- const interpolation = ast.children[0] as InterpolationNode
- expect(interpolation).toStrictEqual({
- type: NodeTypes.INTERPOLATION,
- content: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `a<b`,
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 3, line: 1, column: 4 },
- end: { offset: 6, line: 1, column: 7 },
- source: 'a<b',
- },
- },
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 9, line: 1, column: 10 },
- source: '{{ a<b }}',
- },
- })
- })
- test('it can have tag-like notation (2)', () => {
- const ast = baseParse('{{ a<b }}{{ c>d }}')
- const interpolation1 = ast.children[0] as InterpolationNode
- const interpolation2 = ast.children[1] as InterpolationNode
- expect(interpolation1).toStrictEqual({
- type: NodeTypes.INTERPOLATION,
- content: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `a<b`,
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 3, line: 1, column: 4 },
- end: { offset: 6, line: 1, column: 7 },
- source: 'a<b',
- },
- },
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 9, line: 1, column: 10 },
- source: '{{ a<b }}',
- },
- })
- expect(interpolation2).toStrictEqual({
- type: NodeTypes.INTERPOLATION,
- content: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- content: 'c>d',
- loc: {
- start: { offset: 12, line: 1, column: 13 },
- end: { offset: 15, line: 1, column: 16 },
- source: 'c>d',
- },
- },
- loc: {
- start: { offset: 9, line: 1, column: 10 },
- end: { offset: 18, line: 1, column: 19 },
- source: '{{ c>d }}',
- },
- })
- })
- test('it can have tag-like notation (3)', () => {
- const ast = baseParse('<div>{{ "</div>" }}</div>')
- const element = ast.children[0] as ElementNode
- const interpolation = element.children[0] as InterpolationNode
- expect(interpolation).toStrictEqual({
- type: NodeTypes.INTERPOLATION,
- content: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- isStatic: false,
- // The `constType` is the default value and will be determined in `transformExpression`.
- constType: ConstantTypes.NOT_CONSTANT,
- content: '"</div>"',
- loc: {
- start: { offset: 8, line: 1, column: 9 },
- end: { offset: 16, line: 1, column: 17 },
- source: '"</div>"',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 19, line: 1, column: 20 },
- source: '{{ "</div>" }}',
- },
- })
- })
- test('custom delimiters', () => {
- const ast = baseParse('<p>{msg}</p>', {
- delimiters: ['{', '}'],
- })
- const element = ast.children[0] as ElementNode
- const interpolation = element.children[0] as InterpolationNode
- expect(interpolation).toStrictEqual({
- type: NodeTypes.INTERPOLATION,
- content: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `msg`,
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 4, line: 1, column: 5 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'msg',
- },
- },
- loc: {
- start: { offset: 3, line: 1, column: 4 },
- end: { offset: 8, line: 1, column: 9 },
- source: '{msg}',
- },
- })
- })
- })
- describe('Comment', () => {
- test('empty comment', () => {
- const ast = baseParse('<!---->')
- const comment = ast.children[0] as CommentNode
- expect(comment).toStrictEqual({
- type: NodeTypes.COMMENT,
- content: '',
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 7, line: 1, column: 8 },
- source: '<!---->',
- },
- })
- })
- test('simple comment', () => {
- const ast = baseParse('<!--abc-->')
- const comment = ast.children[0] as CommentNode
- expect(comment).toStrictEqual({
- type: NodeTypes.COMMENT,
- content: 'abc',
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 10, line: 1, column: 11 },
- source: '<!--abc-->',
- },
- })
- })
- test('two comments', () => {
- const ast = baseParse('<!--abc--><!--def-->')
- const comment1 = ast.children[0] as CommentNode
- const comment2 = ast.children[1] as CommentNode
- expect(comment1).toStrictEqual({
- type: NodeTypes.COMMENT,
- content: 'abc',
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 10, line: 1, column: 11 },
- source: '<!--abc-->',
- },
- })
- expect(comment2).toStrictEqual({
- type: NodeTypes.COMMENT,
- content: 'def',
- loc: {
- start: { offset: 10, line: 1, column: 11 },
- end: { offset: 20, line: 1, column: 21 },
- source: '<!--def-->',
- },
- })
- })
- test('comments option', () => {
- const astOptionNoComment = baseParse('<!--abc-->', { comments: false })
- const astOptionWithComments = baseParse('<!--abc-->', { comments: true })
- expect(astOptionNoComment.children).toHaveLength(0)
- expect(astOptionWithComments.children).toHaveLength(1)
- })
- // #2217
- test('comments in the <pre> tag should be removed when comments option requires it', () => {
- const rawText = `<p/><!-- foo --><p/>`
- const astWithComments = baseParse(`<pre>${rawText}</pre>`, {
- comments: true,
- })
- expect(
- (astWithComments.children[0] as ElementNode).children,
- ).toMatchObject([
- {
- type: NodeTypes.ELEMENT,
- tag: 'p',
- },
- {
- type: NodeTypes.COMMENT,
- },
- {
- type: NodeTypes.ELEMENT,
- tag: 'p',
- },
- ])
- const astWithoutComments = baseParse(`<pre>${rawText}</pre>`, {
- comments: false,
- })
- expect(
- (astWithoutComments.children[0] as ElementNode).children,
- ).toMatchObject([
- {
- type: NodeTypes.ELEMENT,
- tag: 'p',
- },
- {
- type: NodeTypes.ELEMENT,
- tag: 'p',
- },
- ])
- })
- })
- describe('Element', () => {
- test('simple div', () => {
- const ast = baseParse('<div>hello</div>')
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [],
- children: [
- {
- type: NodeTypes.TEXT,
- content: 'hello',
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 10, line: 1, column: 11 },
- source: 'hello',
- },
- },
- ],
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 16, line: 1, column: 17 },
- source: '<div>hello</div>',
- },
- })
- })
- test('empty', () => {
- const ast = baseParse('<div></div>')
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [],
- children: [],
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 11, line: 1, column: 12 },
- source: '<div></div>',
- },
- })
- })
- test('self closing', () => {
- const ast = baseParse('<div/>after')
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [],
- children: [],
- isSelfClosing: true,
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 6, line: 1, column: 7 },
- source: '<div/>',
- },
- })
- })
- test('void element', () => {
- const ast = baseParse('<img>after', {
- isVoidTag: tag => tag === 'img',
- })
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'img',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [],
- children: [],
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 5, line: 1, column: 6 },
- source: '<img>',
- },
- })
- })
- test('self-closing void element', () => {
- const ast = baseParse('<img/>after', {
- isVoidTag: tag => tag === 'img',
- })
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'img',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [],
- children: [],
- isSelfClosing: true,
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 6, line: 1, column: 7 },
- source: '<img/>',
- },
- })
- })
- test('template element with directives', () => {
- const ast = baseParse('<template v-if="ok"></template>')
- const element = ast.children[0]
- expect(element).toMatchObject({
- type: NodeTypes.ELEMENT,
- tagType: ElementTypes.TEMPLATE,
- })
- })
- test('template element without directives', () => {
- const ast = baseParse('<template></template>')
- const element = ast.children[0]
- expect(element).toMatchObject({
- type: NodeTypes.ELEMENT,
- tagType: ElementTypes.ELEMENT,
- })
- })
- test('native element with `isNativeTag`', () => {
- const ast = baseParse('<div></div><comp></comp><Comp></Comp>', {
- isNativeTag: tag => tag === 'div',
- })
- expect(ast.children[0]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- })
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'comp',
- tagType: ElementTypes.COMPONENT,
- })
- expect(ast.children[2]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'Comp',
- tagType: ElementTypes.COMPONENT,
- })
- })
- test('native element without `isNativeTag`', () => {
- const ast = baseParse('<div></div><comp></comp><Comp></Comp>')
- expect(ast.children[0]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- })
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'comp',
- tagType: ElementTypes.ELEMENT,
- })
- expect(ast.children[2]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'Comp',
- tagType: ElementTypes.COMPONENT,
- })
- })
- test('is casting with `isNativeTag`', () => {
- const ast = baseParse(
- `<div></div><div is="vue:foo"></div><Comp></Comp>`,
- {
- isNativeTag: tag => tag === 'div',
- },
- )
- expect(ast.children[0]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- })
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'div',
- tagType: ElementTypes.COMPONENT,
- })
- expect(ast.children[2]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'Comp',
- tagType: ElementTypes.COMPONENT,
- })
- })
- test('is casting without `isNativeTag`', () => {
- const ast = baseParse(`<div></div><div is="vue:foo"></div><Comp></Comp>`)
- expect(ast.children[0]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- })
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'div',
- tagType: ElementTypes.COMPONENT,
- })
- expect(ast.children[2]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'Comp',
- tagType: ElementTypes.COMPONENT,
- })
- })
- test('custom element', () => {
- const ast = baseParse('<div></div><comp></comp>', {
- isNativeTag: tag => tag === 'div',
- isCustomElement: tag => tag === 'comp',
- })
- expect(ast.children[0]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- })
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'comp',
- tagType: ElementTypes.ELEMENT,
- })
- })
- test('built-in component', () => {
- const ast = baseParse('<div></div><comp></comp>', {
- isBuiltInComponent: tag => (tag === 'comp' ? Symbol() : void 0),
- })
- expect(ast.children[0]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- })
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'comp',
- tagType: ElementTypes.COMPONENT,
- })
- })
- test('slot element', () => {
- const ast = baseParse('<slot></slot><Comp></Comp>')
- expect(ast.children[0]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'slot',
- tagType: ElementTypes.SLOT,
- })
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'Comp',
- tagType: ElementTypes.COMPONENT,
- })
- })
- test('attribute with no value', () => {
- const ast = baseParse('<div id></div>')
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [
- {
- type: NodeTypes.ATTRIBUTE,
- name: 'id',
- nameLoc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'id',
- },
- value: undefined,
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'id',
- },
- },
- ],
- children: [],
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 14, line: 1, column: 15 },
- source: '<div id></div>',
- },
- })
- })
- test('attribute with empty value, double quote', () => {
- const ast = baseParse('<div id=""></div>')
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [
- {
- type: NodeTypes.ATTRIBUTE,
- name: 'id',
- nameLoc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'id',
- },
- value: {
- type: NodeTypes.TEXT,
- content: '',
- loc: {
- start: { offset: 8, line: 1, column: 9 },
- end: { offset: 10, line: 1, column: 11 },
- source: '""',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 10, line: 1, column: 11 },
- source: 'id=""',
- },
- },
- ],
- children: [],
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 17, line: 1, column: 18 },
- source: '<div id=""></div>',
- },
- })
- })
- test('attribute with empty value, single quote', () => {
- const ast = baseParse("<div id=''></div>")
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [
- {
- type: NodeTypes.ATTRIBUTE,
- name: 'id',
- nameLoc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'id',
- },
- value: {
- type: NodeTypes.TEXT,
- content: '',
- loc: {
- start: { offset: 8, line: 1, column: 9 },
- end: { offset: 10, line: 1, column: 11 },
- source: "''",
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 10, line: 1, column: 11 },
- source: "id=''",
- },
- },
- ],
- children: [],
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 17, line: 1, column: 18 },
- source: "<div id=''></div>",
- },
- })
- })
- test('attribute with value, double quote', () => {
- const ast = baseParse('<div id=">\'"></div>')
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [
- {
- type: NodeTypes.ATTRIBUTE,
- name: 'id',
- nameLoc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'id',
- },
- value: {
- type: NodeTypes.TEXT,
- content: ">'",
- loc: {
- start: { offset: 8, line: 1, column: 9 },
- end: { offset: 12, line: 1, column: 13 },
- source: '">\'"',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 12, line: 1, column: 13 },
- source: 'id=">\'"',
- },
- },
- ],
- children: [],
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 19, line: 1, column: 20 },
- source: '<div id=">\'"></div>',
- },
- })
- })
- test('attribute with value, single quote', () => {
- const ast = baseParse("<div id='>\"'></div>")
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [
- {
- type: NodeTypes.ATTRIBUTE,
- name: 'id',
- nameLoc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'id',
- },
- value: {
- type: NodeTypes.TEXT,
- content: '>"',
- loc: {
- start: { offset: 8, line: 1, column: 9 },
- end: { offset: 12, line: 1, column: 13 },
- source: "'>\"'",
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 12, line: 1, column: 13 },
- source: "id='>\"'",
- },
- },
- ],
- children: [],
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 19, line: 1, column: 20 },
- source: "<div id='>\"'></div>",
- },
- })
- })
- test('attribute with value, unquoted', () => {
- const ast = baseParse('<div id=a/></div>')
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [
- {
- type: NodeTypes.ATTRIBUTE,
- name: 'id',
- nameLoc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'id',
- },
- value: {
- type: NodeTypes.TEXT,
- content: 'a/',
- loc: {
- start: { offset: 8, line: 1, column: 9 },
- end: { offset: 10, line: 1, column: 11 },
- source: 'a/',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 10, line: 1, column: 11 },
- source: 'id=a/',
- },
- },
- ],
- children: [],
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 17, line: 1, column: 18 },
- source: '<div id=a/></div>',
- },
- })
- })
- test('attribute value with >', () => {
- const ast = baseParse(
- '<script setup lang="ts" generic="T extends Record<string,string>"></script>',
- { parseMode: 'sfc' },
- )
- const element = ast.children[0] as ElementNode
- expect(element).toMatchObject({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'script',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- children: [],
- innerLoc: {
- start: { column: 67, line: 1, offset: 66 },
- end: { column: 67, line: 1, offset: 66 },
- },
- props: [
- {
- loc: {
- source: 'setup',
- end: { column: 14, line: 1, offset: 13 },
- start: { column: 9, line: 1, offset: 8 },
- },
- name: 'setup',
- nameLoc: {
- source: 'setup',
- end: { column: 14, line: 1, offset: 13 },
- start: { column: 9, line: 1, offset: 8 },
- },
- type: NodeTypes.ATTRIBUTE,
- value: undefined,
- },
- {
- loc: {
- source: 'lang="ts"',
- end: { column: 24, line: 1, offset: 23 },
- start: { column: 15, line: 1, offset: 14 },
- },
- name: 'lang',
- nameLoc: {
- source: 'lang',
- end: { column: 19, line: 1, offset: 18 },
- start: { column: 15, line: 1, offset: 14 },
- },
- type: NodeTypes.ATTRIBUTE,
- value: {
- content: 'ts',
- loc: {
- source: '"ts"',
- end: { column: 24, line: 1, offset: 23 },
- start: { column: 20, line: 1, offset: 19 },
- },
- type: NodeTypes.TEXT,
- },
- },
- {
- loc: {
- source: 'generic="T extends Record<string,string>"',
- end: { column: 66, line: 1, offset: 65 },
- start: { column: 25, line: 1, offset: 24 },
- },
- name: 'generic',
- nameLoc: {
- source: 'generic',
- end: { column: 32, line: 1, offset: 31 },
- start: { column: 25, line: 1, offset: 24 },
- },
- type: NodeTypes.ATTRIBUTE,
- value: {
- content: 'T extends Record<string,string>',
- loc: {
- source: '"T extends Record<string,string>"',
- end: { column: 66, line: 1, offset: 65 },
- start: { column: 33, line: 1, offset: 32 },
- },
- type: NodeTypes.TEXT,
- },
- },
- ],
- })
- })
- test('multiple attributes', () => {
- const ast = baseParse('<div id=a class="c" inert style=\'\'></div>')
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- type: NodeTypes.ELEMENT,
- ns: Namespaces.HTML,
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- codegenNode: undefined,
- props: [
- {
- type: NodeTypes.ATTRIBUTE,
- name: 'id',
- nameLoc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'id',
- },
- value: {
- type: NodeTypes.TEXT,
- content: 'a',
- loc: {
- start: { offset: 8, line: 1, column: 9 },
- end: { offset: 9, line: 1, column: 10 },
- source: 'a',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 9, line: 1, column: 10 },
- source: 'id=a',
- },
- },
- {
- type: NodeTypes.ATTRIBUTE,
- name: 'class',
- nameLoc: {
- start: { offset: 10, line: 1, column: 11 },
- end: { offset: 15, line: 1, column: 16 },
- source: 'class',
- },
- value: {
- type: NodeTypes.TEXT,
- content: 'c',
- loc: {
- start: { offset: 16, line: 1, column: 17 },
- end: { offset: 19, line: 1, column: 20 },
- source: '"c"',
- },
- },
- loc: {
- start: { offset: 10, line: 1, column: 11 },
- end: { offset: 19, line: 1, column: 20 },
- source: 'class="c"',
- },
- },
- {
- type: NodeTypes.ATTRIBUTE,
- name: 'inert',
- nameLoc: {
- start: { offset: 20, line: 1, column: 21 },
- end: { offset: 25, line: 1, column: 26 },
- source: 'inert',
- },
- value: undefined,
- loc: {
- start: { offset: 20, line: 1, column: 21 },
- end: { offset: 25, line: 1, column: 26 },
- source: 'inert',
- },
- },
- {
- type: NodeTypes.ATTRIBUTE,
- name: 'style',
- nameLoc: {
- start: { offset: 26, line: 1, column: 27 },
- end: { offset: 31, line: 1, column: 32 },
- source: 'style',
- },
- value: {
- type: NodeTypes.TEXT,
- content: '',
- loc: {
- start: { offset: 32, line: 1, column: 33 },
- end: { offset: 34, line: 1, column: 35 },
- source: "''",
- },
- },
- loc: {
- start: { offset: 26, line: 1, column: 27 },
- end: { offset: 34, line: 1, column: 35 },
- source: "style=''",
- },
- },
- ],
- children: [],
- loc: {
- start: { offset: 0, line: 1, column: 1 },
- end: { offset: 41, line: 1, column: 42 },
- source: '<div id=a class="c" inert style=\'\'></div>',
- },
- })
- })
- // https://github.com/vuejs/core/issues/4251
- test('class attribute should ignore whitespace when parsed', () => {
- const ast = baseParse('<div class=" \n\t c \t\n "></div>')
- const element = ast.children[0] as ElementNode
- expect(element).toStrictEqual({
- children: [],
- codegenNode: undefined,
- loc: {
- start: { column: 1, line: 1, offset: 0 },
- end: { column: 10, line: 3, offset: 29 },
- source: '<div class=" \n\t c \t\n "></div>',
- },
- ns: Namespaces.HTML,
- props: [
- {
- name: 'class',
- nameLoc: {
- start: { column: 6, line: 1, offset: 5 },
- end: { column: 11, line: 1, offset: 10 },
- source: 'class',
- },
- type: NodeTypes.ATTRIBUTE,
- value: {
- content: 'c',
- loc: {
- start: { column: 12, line: 1, offset: 11 },
- end: { column: 3, line: 3, offset: 22 },
- source: '" \n\t c \t\n "',
- },
- type: NodeTypes.TEXT,
- },
- loc: {
- start: { column: 6, line: 1, offset: 5 },
- end: { column: 3, line: 3, offset: 22 },
- source: 'class=" \n\t c \t\n "',
- },
- },
- ],
- tag: 'div',
- tagType: ElementTypes.ELEMENT,
- type: NodeTypes.ELEMENT,
- })
- })
- test('directive with no value', () => {
- const ast = baseParse('<div v-if/>')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'if',
- rawName: 'v-if',
- arg: undefined,
- modifiers: [],
- exp: undefined,
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 9, line: 1, column: 10 },
- source: 'v-if',
- },
- })
- })
- test('directive with value', () => {
- const ast = baseParse('<div v-if="a"/>')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'if',
- rawName: 'v-if',
- arg: undefined,
- modifiers: [],
- exp: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'a',
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 11, line: 1, column: 12 },
- end: { offset: 12, line: 1, column: 13 },
- source: 'a',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 13, line: 1, column: 14 },
- source: 'v-if="a"',
- },
- })
- })
- test('directive with argument', () => {
- const ast = baseParse('<div v-on:click/>')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'on',
- rawName: 'v-on:click',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'click',
- isStatic: true,
- constType: ConstantTypes.CAN_STRINGIFY,
- loc: {
- start: { column: 11, line: 1, offset: 10 },
- end: { column: 16, line: 1, offset: 15 },
- source: 'click',
- },
- },
- modifiers: [],
- exp: undefined,
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 15, line: 1, column: 16 },
- source: 'v-on:click',
- },
- })
- })
- // #3494
- test('directive argument edge case', () => {
- const ast = baseParse('<div v-slot:slot />')
- const directive = (ast.children[0] as ElementNode)
- .props[0] as DirectiveNode
- expect(directive.arg).toMatchObject({
- loc: {
- start: { offset: 12, line: 1, column: 13 },
- end: { offset: 16, line: 1, column: 17 },
- },
- })
- })
- // https://github.com/vuejs/language-tools/issues/2710
- test('directive argument edge case (2)', () => {
- const ast = baseParse('<div #item.item />')
- const directive = (ast.children[0] as ElementNode)
- .props[0] as DirectiveNode
- expect(directive.arg).toMatchObject({
- content: 'item.item',
- loc: {
- start: { offset: 6, line: 1, column: 7 },
- end: { offset: 15, line: 1, column: 16 },
- },
- })
- })
- test('directive with dynamic argument', () => {
- const ast = baseParse('<div v-on:[event]/>')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'on',
- rawName: 'v-on:[event]',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'event',
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { column: 11, line: 1, offset: 10 },
- end: { column: 18, line: 1, offset: 17 },
- source: '[event]',
- },
- },
- modifiers: [],
- exp: undefined,
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 17, line: 1, column: 18 },
- source: 'v-on:[event]',
- },
- })
- })
- test('directive with a modifier', () => {
- const ast = baseParse('<div v-on.enter/>')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'on',
- rawName: 'v-on.enter',
- arg: undefined,
- modifiers: [
- {
- constType: 3,
- content: 'enter',
- isStatic: true,
- loc: {
- end: {
- column: 16,
- line: 1,
- offset: 15,
- },
- source: 'enter',
- start: {
- column: 11,
- line: 1,
- offset: 10,
- },
- },
- type: 4,
- },
- ],
- exp: undefined,
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 15, line: 1, column: 16 },
- source: 'v-on.enter',
- },
- })
- })
- test('directive with two modifiers', () => {
- const ast = baseParse('<div v-on.enter.exact/>')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'on',
- rawName: 'v-on.enter.exact',
- arg: undefined,
- modifiers: [
- {
- constType: 3,
- content: 'enter',
- isStatic: true,
- loc: {
- end: {
- column: 16,
- line: 1,
- offset: 15,
- },
- source: 'enter',
- start: {
- column: 11,
- line: 1,
- offset: 10,
- },
- },
- type: 4,
- },
- {
- constType: 3,
- content: 'exact',
- isStatic: true,
- loc: {
- end: {
- column: 22,
- line: 1,
- offset: 21,
- },
- source: 'exact',
- start: {
- column: 17,
- line: 1,
- offset: 16,
- },
- },
- type: 4,
- },
- ],
- exp: undefined,
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 21, line: 1, column: 22 },
- source: 'v-on.enter.exact',
- },
- })
- })
- test('directive with argument and modifiers', () => {
- const ast = baseParse('<div v-on:click.enter.exact/>')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'on',
- rawName: 'v-on:click.enter.exact',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'click',
- isStatic: true,
- constType: ConstantTypes.CAN_STRINGIFY,
- loc: {
- start: { column: 11, line: 1, offset: 10 },
- end: { column: 16, line: 1, offset: 15 },
- source: 'click',
- },
- },
- modifiers: [
- {
- constType: 3,
- content: 'enter',
- isStatic: true,
- loc: {
- end: {
- column: 22,
- line: 1,
- offset: 21,
- },
- source: 'enter',
- start: {
- column: 17,
- line: 1,
- offset: 16,
- },
- },
- type: 4,
- },
- {
- constType: 3,
- content: 'exact',
- isStatic: true,
- loc: {
- end: {
- column: 28,
- line: 1,
- offset: 27,
- },
- source: 'exact',
- start: {
- column: 23,
- line: 1,
- offset: 22,
- },
- },
- type: 4,
- },
- ],
- exp: undefined,
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 27, line: 1, column: 28 },
- source: 'v-on:click.enter.exact',
- },
- })
- })
- test('directive with dynamic argument and modifiers', () => {
- const ast = baseParse('<div v-on:[a.b].camel/>')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'on',
- rawName: 'v-on:[a.b].camel',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'a.b',
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { column: 11, line: 1, offset: 10 },
- end: { column: 16, line: 1, offset: 15 },
- source: '[a.b]',
- },
- },
- modifiers: [
- {
- constType: 3,
- content: 'camel',
- isStatic: true,
- loc: {
- end: {
- column: 22,
- line: 1,
- offset: 21,
- },
- source: 'camel',
- start: {
- column: 17,
- line: 1,
- offset: 16,
- },
- },
- type: 4,
- },
- ],
- exp: undefined,
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 21, line: 1, column: 22 },
- source: 'v-on:[a.b].camel',
- },
- })
- })
- test('directive with no name', () => {
- let errorCode = -1
- const ast = baseParse('<div v-/>', {
- onError: err => {
- errorCode = err.code as number
- },
- })
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(errorCode).toBe(ErrorCodes.X_MISSING_DIRECTIVE_NAME)
- expect(directive).toStrictEqual({
- type: NodeTypes.ATTRIBUTE,
- name: 'v-',
- value: undefined,
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'v-',
- },
- nameLoc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 7, line: 1, column: 8 },
- source: 'v-',
- },
- })
- })
- test('v-bind shorthand', () => {
- const ast = baseParse('<div :a=b />')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'bind',
- rawName: ':a',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'a',
- isStatic: true,
- constType: ConstantTypes.CAN_STRINGIFY,
- loc: {
- start: { column: 7, line: 1, offset: 6 },
- end: { column: 8, line: 1, offset: 7 },
- source: 'a',
- },
- },
- modifiers: [],
- exp: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'b',
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 8, line: 1, column: 9 },
- end: { offset: 9, line: 1, column: 10 },
- source: 'b',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 9, line: 1, column: 10 },
- source: ':a=b',
- },
- })
- })
- test('v-bind .prop shorthand', () => {
- const ast = baseParse('<div .a=b />')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'bind',
- rawName: '.a',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'a',
- isStatic: true,
- constType: ConstantTypes.CAN_STRINGIFY,
- loc: {
- start: { column: 7, line: 1, offset: 6 },
- end: { column: 8, line: 1, offset: 7 },
- source: 'a',
- },
- },
- modifiers: [
- {
- constType: 0,
- content: 'prop',
- isStatic: false,
- loc: {
- end: {
- column: 1,
- line: 1,
- offset: 0,
- },
- source: '',
- start: {
- column: 1,
- line: 1,
- offset: 0,
- },
- },
- type: 4,
- },
- ],
- exp: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'b',
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 8, line: 1, column: 9 },
- end: { offset: 9, line: 1, column: 10 },
- source: 'b',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 9, line: 1, column: 10 },
- source: '.a=b',
- },
- })
- })
- test('v-bind shorthand with modifier', () => {
- const ast = baseParse('<div :a.sync=b />')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'bind',
- rawName: ':a.sync',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'a',
- isStatic: true,
- constType: ConstantTypes.CAN_STRINGIFY,
- loc: {
- start: { column: 7, line: 1, offset: 6 },
- end: { column: 8, line: 1, offset: 7 },
- source: 'a',
- },
- },
- modifiers: [
- {
- constType: 3,
- content: 'sync',
- isStatic: true,
- loc: {
- end: {
- column: 13,
- line: 1,
- offset: 12,
- },
- source: 'sync',
- start: {
- column: 9,
- line: 1,
- offset: 8,
- },
- },
- type: 4,
- },
- ],
- exp: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'b',
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 13, line: 1, column: 14 },
- end: { offset: 14, line: 1, column: 15 },
- source: 'b',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 14, line: 1, column: 15 },
- source: ':a.sync=b',
- },
- })
- })
- test('v-on shorthand', () => {
- const ast = baseParse('<div @a=b />')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'on',
- rawName: '@a',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'a',
- isStatic: true,
- constType: ConstantTypes.CAN_STRINGIFY,
- loc: {
- start: { column: 7, line: 1, offset: 6 },
- end: { column: 8, line: 1, offset: 7 },
- source: 'a',
- },
- },
- modifiers: [],
- exp: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'b',
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 8, line: 1, column: 9 },
- end: { offset: 9, line: 1, column: 10 },
- source: 'b',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 9, line: 1, column: 10 },
- source: '@a=b',
- },
- })
- })
- test('v-on shorthand with modifier', () => {
- const ast = baseParse('<div @a.enter=b />')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'on',
- rawName: '@a.enter',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'a',
- isStatic: true,
- constType: ConstantTypes.CAN_STRINGIFY,
- loc: {
- start: { column: 7, line: 1, offset: 6 },
- end: { column: 8, line: 1, offset: 7 },
- source: 'a',
- },
- },
- modifiers: [
- {
- constType: 3,
- content: 'enter',
- isStatic: true,
- loc: {
- end: {
- column: 14,
- line: 1,
- offset: 13,
- },
- source: 'enter',
- start: {
- column: 9,
- line: 1,
- offset: 8,
- },
- },
- type: 4,
- },
- ],
- exp: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'b',
- isStatic: false,
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 14, line: 1, column: 15 },
- end: { offset: 15, line: 1, column: 16 },
- source: 'b',
- },
- },
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 15, line: 1, column: 16 },
- source: '@a.enter=b',
- },
- })
- })
- test('v-slot shorthand', () => {
- const ast = baseParse('<Comp #a="{ b }" />')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toStrictEqual({
- type: NodeTypes.DIRECTIVE,
- name: 'slot',
- rawName: '#a',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'a',
- isStatic: true,
- constType: ConstantTypes.CAN_STRINGIFY,
- loc: {
- start: { column: 8, line: 1, offset: 7 },
- end: { column: 9, line: 1, offset: 8 },
- source: 'a',
- },
- },
- modifiers: [],
- exp: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: '{ b }',
- isStatic: false,
- // The `constType` is the default value and will be determined in transformExpression
- constType: ConstantTypes.NOT_CONSTANT,
- loc: {
- start: { offset: 10, line: 1, column: 11 },
- end: { offset: 15, line: 1, column: 16 },
- source: '{ b }',
- },
- },
- loc: {
- start: { offset: 6, line: 1, column: 7 },
- end: { offset: 16, line: 1, column: 17 },
- source: '#a="{ b }"',
- },
- })
- })
- // #1241 special case for 2.x compat
- test('v-slot arg containing dots', () => {
- const ast = baseParse('<Comp v-slot:foo.bar="{ a }" />')
- const directive = (ast.children[0] as ElementNode).props[0]
- expect(directive).toMatchObject({
- type: NodeTypes.DIRECTIVE,
- name: 'slot',
- rawName: 'v-slot:foo.bar',
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: 'foo.bar',
- isStatic: true,
- constType: ConstantTypes.CAN_STRINGIFY,
- loc: {
- start: {
- column: 14,
- line: 1,
- offset: 13,
- },
- end: {
- column: 21,
- line: 1,
- offset: 20,
- },
- },
- },
- })
- })
- test('v-pre', () => {
- const ast = baseParse(
- `<div v-pre :id="foo"><Comp/>{{ bar }}</div>\n` +
- `<div :id="foo"><Comp/>{{ bar }}</div>`,
- )
- const divWithPre = ast.children[0] as ElementNode
- expect(divWithPre.props).toMatchObject([
- {
- type: NodeTypes.ATTRIBUTE,
- name: `:id`,
- value: {
- type: NodeTypes.TEXT,
- content: `foo`,
- },
- loc: {
- start: { line: 1, column: 12 },
- end: { line: 1, column: 21 },
- },
- },
- ])
- expect(divWithPre.children[0]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tagType: ElementTypes.ELEMENT,
- tag: `Comp`,
- })
- expect(divWithPre.children[1]).toMatchObject({
- type: NodeTypes.TEXT,
- content: `{{ bar }}`,
- })
- // should not affect siblings after it
- const divWithoutPre = ast.children[1] as ElementNode
- expect(divWithoutPre.props).toMatchObject([
- {
- type: NodeTypes.DIRECTIVE,
- name: `bind`,
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- isStatic: true,
- content: `id`,
- },
- exp: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- isStatic: false,
- content: `foo`,
- },
- loc: {
- start: {
- line: 2,
- column: 6,
- },
- end: {
- line: 2,
- column: 15,
- },
- },
- },
- ])
- expect(divWithoutPre.children[0]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tagType: ElementTypes.COMPONENT,
- tag: `Comp`,
- })
- expect(divWithoutPre.children[1]).toMatchObject({
- type: NodeTypes.INTERPOLATION,
- content: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `bar`,
- isStatic: false,
- },
- })
- })
- // https://github.com/vuejs/docs/issues/2586
- test('v-pre with half-open interpolation', () => {
- const ast = baseParse(
- `<div v-pre>
- <span>{{ number </span>
- <span>}}</span>
- </div>
- `,
- )
- expect((ast.children[0] as ElementNode).children).toMatchObject([
- {
- type: NodeTypes.ELEMENT,
- children: [{ type: NodeTypes.TEXT, content: `{{ number ` }],
- },
- {
- type: NodeTypes.ELEMENT,
- children: [{ type: NodeTypes.TEXT, content: `}}` }],
- },
- ])
- const ast2 = baseParse(`<div v-pre><span>{{ number </span></div>`)
- expect((ast2.children[0] as ElementNode).children).toMatchObject([
- {
- type: NodeTypes.ELEMENT,
- children: [{ type: NodeTypes.TEXT, content: `{{ number ` }],
- },
- ])
- const ast3 = baseParse(`<div v-pre><textarea>{{ foo </textarea></div>`, {
- parseMode: 'html',
- })
- expect((ast3.children[0] as ElementNode).children).toMatchObject([
- {
- type: NodeTypes.ELEMENT,
- children: [
- {
- type: NodeTypes.TEXT,
- content: `{{ foo `,
- },
- ],
- },
- ])
- })
- test('self-closing v-pre', () => {
- const ast = baseParse(
- `<div v-pre/>\n<div :id="foo"><Comp/>{{ bar }}</div>`,
- )
- // should not affect siblings after it
- const divWithoutPre = ast.children[1] as ElementNode
- expect(divWithoutPre.props).toMatchObject([
- {
- type: NodeTypes.DIRECTIVE,
- name: `bind`,
- arg: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- isStatic: true,
- content: `id`,
- },
- exp: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- isStatic: false,
- content: `foo`,
- },
- loc: {
- start: {
- line: 2,
- column: 6,
- },
- end: {
- line: 2,
- column: 15,
- },
- },
- },
- ])
- expect(divWithoutPre.children[0]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tagType: ElementTypes.COMPONENT,
- tag: `Comp`,
- })
- expect(divWithoutPre.children[1]).toMatchObject({
- type: NodeTypes.INTERPOLATION,
- content: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `bar`,
- isStatic: false,
- },
- })
- })
- test('end tags are case-insensitive.', () => {
- const ast = baseParse('<div>hello</DIV>after')
- const element = ast.children[0] as ElementNode
- const text = element.children[0] as TextNode
- expect(text).toStrictEqual({
- type: NodeTypes.TEXT,
- content: 'hello',
- loc: {
- start: { offset: 5, line: 1, column: 6 },
- end: { offset: 10, line: 1, column: 11 },
- source: 'hello',
- },
- })
- })
- })
- describe('Edge Cases', () => {
- test('self closing single tag', () => {
- const ast = baseParse('<div :class="{ some: condition }" />')
- expect(ast.children).toHaveLength(1)
- expect(ast.children[0]).toMatchObject({ tag: 'div' })
- })
- test('self closing multiple tag', () => {
- const ast = baseParse(
- `<div :class="{ some: condition }" />\n` +
- `<p v-bind:style="{ color: 'red' }"/>`,
- )
- expect(ast).toMatchSnapshot()
- expect(ast.children).toHaveLength(2)
- expect(ast.children[0]).toMatchObject({ tag: 'div' })
- expect(ast.children[1]).toMatchObject({ tag: 'p' })
- })
- test('valid html', () => {
- const ast = baseParse(
- `<div :class="{ some: condition }">\n` +
- ` <p v-bind:style="{ color: 'red' }"/>\n` +
- ` <!-- a comment with <html> inside it -->\n` +
- `</div>`,
- )
- expect(ast).toMatchSnapshot()
- expect(ast.children).toHaveLength(1)
- const el = ast.children[0] as any
- expect(el).toMatchObject({
- tag: 'div',
- })
- expect(el.children).toHaveLength(2)
- expect(el.children[0]).toMatchObject({
- tag: 'p',
- })
- expect(el.children[1]).toMatchObject({
- type: NodeTypes.COMMENT,
- })
- })
- test('invalid html', () => {
- expect(() => {
- baseParse(`<div>\n<span>\n</div>\n</span>`)
- }).toThrow('Element is missing end tag.')
- const spy = vi.fn()
- const ast = baseParse(`<div>\n<span>\n</div>\n</span>`, {
- onError: spy,
- })
- expect(spy.mock.calls).toMatchObject([
- [
- {
- code: ErrorCodes.X_MISSING_END_TAG,
- loc: {
- start: {
- offset: 6,
- line: 2,
- column: 1,
- },
- },
- },
- ],
- [
- {
- code: ErrorCodes.X_INVALID_END_TAG,
- loc: {
- start: {
- offset: 20,
- line: 4,
- column: 1,
- },
- },
- },
- ],
- ])
- expect(ast).toMatchSnapshot()
- })
- test('parse with correct location info', () => {
- const fooSrc = `foo\n is `
- const barSrc = `{{ bar }}`
- const butSrc = ` but `
- const bazSrc = `{{ baz }}`
- const [foo, bar, but, baz] = baseParse(
- fooSrc + barSrc + butSrc + bazSrc,
- ).children
- let offset = 0
- expect(foo.loc.start).toEqual({ line: 1, column: 1, offset })
- offset += fooSrc.length
- expect(foo.loc.end).toEqual({ line: 2, column: 5, offset })
- expect(bar.loc.start).toEqual({ line: 2, column: 5, offset })
- const barInner = (bar as InterpolationNode).content
- offset += 3
- expect(barInner.loc.start).toEqual({ line: 2, column: 8, offset })
- offset += 3
- expect(barInner.loc.end).toEqual({ line: 2, column: 11, offset })
- offset += 3
- expect(bar.loc.end).toEqual({ line: 2, column: 14, offset })
- expect(but.loc.start).toEqual({ line: 2, column: 14, offset })
- offset += butSrc.length
- expect(but.loc.end).toEqual({ line: 2, column: 19, offset })
- expect(baz.loc.start).toEqual({ line: 2, column: 19, offset })
- const bazInner = (baz as InterpolationNode).content
- offset += 3
- expect(bazInner.loc.start).toEqual({ line: 2, column: 22, offset })
- offset += 3
- expect(bazInner.loc.end).toEqual({ line: 2, column: 25, offset })
- offset += 3
- expect(baz.loc.end).toEqual({ line: 2, column: 28, offset })
- })
- // With standard HTML parsing, the following input would ignore the slash
- // and treat "<" and "template" as attributes on the open tag of "Hello",
- // causing `<template>` to fail to close, and `<script>` being parsed as its
- // child. This is would never be intended in actual templates, but is a common
- // intermediate state from user input when parsing for IDE support. We want
- // the `<script>` to be at root-level to keep the SFC structure stable for
- // Volar to do incremental computations.
- test('tag termination handling for IDE', () => {
- const spy = vi.fn()
- const ast = baseParse(
- `<template><Hello\n</template><script>console.log(1)</script>`,
- {
- onError: spy,
- },
- )
- //
- expect(ast.children.length).toBe(2)
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.ELEMENT,
- tag: 'script',
- })
- })
- test('arg should be undefined on shorthand dirs with no arg', () => {
- const ast = baseParse(`<template #></template>`)
- const el = ast.children[0] as ElementNode
- expect(el.props[0]).toMatchObject({
- type: NodeTypes.DIRECTIVE,
- name: 'slot',
- exp: undefined,
- arg: undefined,
- })
- })
- // edge case found in vue-macros where the input is TS or JSX
- test('should reset inRCDATA state', () => {
- baseParse(`<Foo>`, { parseMode: 'sfc', onError() {} })
- expect(() => baseParse(`{ foo }`)).not.toThrow()
- })
- test('correct loc when the closing > is foarmatted', () => {
- const [span] = baseParse(`<span></span
-
- >`).children
- expect(span.loc.source).toBe('<span></span\n \n >')
- expect(span.loc.start.offset).toBe(0)
- expect(span.loc.end.offset).toBe(27)
- })
- test('correct loc when a line in attribute value ends with &', () => {
- const [span] = baseParse(`<span v-if="foo &&\nbar"></span>`).children
- expect(span.loc.end.line).toBe(2)
- })
- })
- describe('decodeEntities option', () => {
- test('use decode by default', () => {
- const ast: any = baseParse('><&'"&foo;')
- expect(ast.children.length).toBe(1)
- expect(ast.children[0].type).toBe(NodeTypes.TEXT)
- expect(ast.children[0].content).toBe('><&\'"&foo;')
- })
- test('should warn in non-browser build', () => {
- baseParse('&∪︀', {
- decodeEntities: text => text.replace('∪︀', '\u222A\uFE00'),
- onError: () => {}, // Ignore errors
- })
- expect(
- `decodeEntities option is passed but will be ignored`,
- ).toHaveBeenWarned()
- })
- })
- describe('whitespace management when adopting strategy condense', () => {
- const parse = (content: string, options?: ParserOptions) =>
- baseParse(content, {
- whitespace: 'condense',
- ...options,
- })
- test('should remove whitespaces at start/end inside an element', () => {
- const ast = parse(`<div> <span/> </div>`)
- expect((ast.children[0] as ElementNode).children.length).toBe(1)
- })
- test('should remove whitespaces w/ newline between elements', () => {
- const ast = parse(`<div/> \n <div/> \n <div/>`)
- expect(ast.children.length).toBe(3)
- expect(ast.children.every(c => c.type === NodeTypes.ELEMENT)).toBe(true)
- })
- test('should remove whitespaces adjacent to comments', () => {
- const ast = parse(`<div/> \n <!--foo--> <div/>`)
- expect(ast.children.length).toBe(3)
- expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
- expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
- expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
- })
- test('should remove whitespaces w/ newline between comments and elements', () => {
- const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
- expect(ast.children.length).toBe(3)
- expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
- expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
- expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
- })
- test('should NOT remove whitespaces w/ newline between interpolations', () => {
- const ast = parse(`{{ foo }} \n {{ bar }}`)
- expect(ast.children.length).toBe(3)
- expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.TEXT,
- content: ' ',
- })
- expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
- })
- test('should NOT remove whitespaces w/ newline between interpolation and comment', () => {
- const ast = parse(`<!-- foo --> \n {{msg}}`)
- expect(ast.children.length).toBe(3)
- expect(ast.children[0].type).toBe(NodeTypes.COMMENT)
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.TEXT,
- content: ' ',
- })
- expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
- })
- test('should NOT remove whitespaces w/o newline between elements', () => {
- const ast = parse(`<div/> <div/> <div/>`)
- expect(ast.children.length).toBe(5)
- expect(ast.children.map(c => c.type)).toMatchObject([
- NodeTypes.ELEMENT,
- NodeTypes.TEXT,
- NodeTypes.ELEMENT,
- NodeTypes.TEXT,
- NodeTypes.ELEMENT,
- ])
- })
- test('should condense consecutive whitespaces in text', () => {
- const ast = parse(` foo \n bar baz `)
- expect((ast.children[0] as TextNode).content).toBe(` foo bar baz `)
- })
- test('should remove leading newline character immediately following the pre element start tag', () => {
- const ast = parse(`<pre>\n foo bar </pre>`, {
- isPreTag: tag => tag === 'pre',
- isIgnoreNewlineTag: tag => tag === 'pre',
- })
- expect(ast.children).toHaveLength(1)
- const preElement = ast.children[0] as ElementNode
- expect(preElement.children).toHaveLength(1)
- expect((preElement.children[0] as TextNode).content).toBe(` foo bar `)
- })
- test('should NOT remove leading newline character immediately following child-tag of pre element', () => {
- const ast = parse(`<pre><span></span>\n foo bar </pre>`, {
- isPreTag: tag => tag === 'pre',
- })
- const preElement = ast.children[0] as ElementNode
- expect(preElement.children).toHaveLength(2)
- expect((preElement.children[1] as TextNode).content).toBe(
- `\n foo bar `,
- )
- })
- test('self-closing pre tag', () => {
- const ast = parse(`<pre/><span>\n foo bar</span>`, {
- isPreTag: tag => tag === 'pre',
- })
- const elementAfterPre = ast.children[1] as ElementNode
- // should not affect the <span> and condense its whitespace inside
- expect((elementAfterPre.children[0] as TextNode).content).toBe(` foo bar`)
- })
- test('should NOT condense whitespaces in RCDATA text mode', () => {
- const ast = parse(`<textarea>Text:\n foo</textarea>`, {
- parseMode: 'html',
- })
- const preElement = ast.children[0] as ElementNode
- expect(preElement.children).toHaveLength(1)
- expect((preElement.children[0] as TextNode).content).toBe(`Text:\n foo`)
- })
- })
- describe('whitespace management when adopting strategy preserve', () => {
- const parse = (content: string, options?: ParserOptions) =>
- baseParse(content, {
- whitespace: 'preserve',
- ...options,
- })
- test('should still remove whitespaces at start/end inside an element', () => {
- const ast = parse(`<div> <span/> </div>`)
- expect((ast.children[0] as ElementNode).children.length).toBe(1)
- })
- test('should preserve whitespaces w/ newline between elements', () => {
- const ast = parse(`<div/> \n <div/> \n <div/>`)
- expect(ast.children.length).toBe(5)
- expect(ast.children.map(c => c.type)).toMatchObject([
- NodeTypes.ELEMENT,
- NodeTypes.TEXT,
- NodeTypes.ELEMENT,
- NodeTypes.TEXT,
- NodeTypes.ELEMENT,
- ])
- })
- test('should preserve whitespaces adjacent to comments', () => {
- const ast = parse(`<div/> \n <!--foo--> <div/>`)
- expect(ast.children.length).toBe(5)
- expect(ast.children.map(c => c.type)).toMatchObject([
- NodeTypes.ELEMENT,
- NodeTypes.TEXT,
- NodeTypes.COMMENT,
- NodeTypes.TEXT,
- NodeTypes.ELEMENT,
- ])
- })
- test('should preserve whitespaces w/ newline between comments and elements', () => {
- const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
- expect(ast.children.length).toBe(5)
- expect(ast.children.map(c => c.type)).toMatchObject([
- NodeTypes.ELEMENT,
- NodeTypes.TEXT,
- NodeTypes.COMMENT,
- NodeTypes.TEXT,
- NodeTypes.ELEMENT,
- ])
- })
- test('should preserve whitespaces w/ newline between interpolations', () => {
- const ast = parse(`{{ foo }} \n {{ bar }}`)
- expect(ast.children.length).toBe(3)
- expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
- expect(ast.children[1]).toMatchObject({
- type: NodeTypes.TEXT,
- content: ' ',
- })
- expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
- })
- test('should preserve whitespaces w/o newline between elements', () => {
- const ast = parse(`<div/> <div/> <div/>`)
- expect(ast.children.length).toBe(5)
- expect(ast.children.map(c => c.type)).toMatchObject([
- NodeTypes.ELEMENT,
- NodeTypes.TEXT,
- NodeTypes.ELEMENT,
- NodeTypes.TEXT,
- NodeTypes.ELEMENT,
- ])
- })
- test('should preserve consecutive whitespaces in text', () => {
- const content = ` foo \n bar baz `
- const ast = parse(content)
- expect((ast.children[0] as TextNode).content).toBe(content)
- })
- })
- describe('expression parsing', () => {
- test('interpolation', () => {
- const ast = baseParse(`{{ a + b }}`, { prefixIdentifiers: true })
- // @ts-expect-error
- expect((ast.children[0] as InterpolationNode).content.ast?.type).toBe(
- 'BinaryExpression',
- )
- })
- test('v-bind', () => {
- const ast = baseParse(`<div :[key+1]="foo()" />`, {
- prefixIdentifiers: true,
- })
- const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
- // @ts-expect-error
- expect(dir.arg?.ast?.type).toBe('BinaryExpression')
- // @ts-expect-error
- expect(dir.exp?.ast?.type).toBe('CallExpression')
- })
- test('v-on multi statements', () => {
- const ast = baseParse(`<div @click="a++;b++" />`, {
- prefixIdentifiers: true,
- })
- const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
- // @ts-expect-error
- expect(dir.exp?.ast?.type).toBe('Program')
- expect((dir.exp?.ast as Program).body).toMatchObject([
- { type: 'ExpressionStatement' },
- { type: 'ExpressionStatement' },
- ])
- })
- test('v-slot', () => {
- const ast = baseParse(`<Comp #foo="{ a, b }" />`, {
- prefixIdentifiers: true,
- })
- const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
- // @ts-expect-error
- expect(dir.exp?.ast?.type).toBe('ArrowFunctionExpression')
- })
- test('v-for', () => {
- const ast = baseParse(`<div v-for="({ a, b }, key, index) of a.b" />`, {
- prefixIdentifiers: true,
- })
- const dir = (ast.children[0] as ElementNode).props[0] as DirectiveNode
- const { source, value, key, index } = dir.forParseResult!
- // @ts-expect-error
- expect(source.ast?.type).toBe('MemberExpression')
- // @ts-expect-error
- expect(value?.ast?.type).toBe('ArrowFunctionExpression')
- expect(key?.ast).toBeNull() // simple ident
- expect(index?.ast).toBeNull() // simple ident
- })
- })
- describe('Errors', () => {
- // HTML parsing errors as specified at
- // https://html.spec.whatwg.org/multipage/parsing.html#parse-errors
- // We ignore some errors that do NOT affect parse result in meaningful ways
- // but have non-trivial implementation cost.
- const patterns: {
- [key: string]: Array<{
- code: string
- errors: Array<{ type: ErrorCodes; loc: Position }>
- options?: Partial<ParserOptions>
- }>
- } = {
- // ABRUPT_CLOSING_OF_EMPTY_COMMENT: [
- // {
- // code: '<template><!--></template>',
- // errors: [
- // {
- // type: ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT,
- // loc: { offset: 10, line: 1, column: 11 }
- // }
- // ]
- // },
- // {
- // code: '<template><!---></template>',
- // errors: [
- // {
- // type: ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT,
- // loc: { offset: 10, line: 1, column: 11 }
- // }
- // ]
- // },
- // {
- // code: '<template><!----></template>',
- // errors: []
- // }
- // ],
- CDATA_IN_HTML_CONTENT: [
- {
- code: '<template><![CDATA[cdata]]></template>',
- errors: [
- {
- type: ErrorCodes.CDATA_IN_HTML_CONTENT,
- loc: { offset: 10, line: 1, column: 11 },
- },
- ],
- },
- {
- code: '<template><svg><![CDATA[cdata]]></svg></template>',
- errors: [],
- },
- ],
- DUPLICATE_ATTRIBUTE: [
- {
- code: '<template><div id="" id=""></div></template>',
- errors: [
- {
- type: ErrorCodes.DUPLICATE_ATTRIBUTE,
- loc: { offset: 21, line: 1, column: 22 },
- },
- ],
- },
- ],
- // END_TAG_WITH_ATTRIBUTES: [
- // {
- // code: '<template><div></div id=""></template>',
- // errors: [
- // {
- // type: ErrorCodes.END_TAG_WITH_ATTRIBUTES,
- // loc: { offset: 21, line: 1, column: 22 }
- // }
- // ]
- // }
- // ],
- // END_TAG_WITH_TRAILING_SOLIDUS: [
- // {
- // code: '<template><div></div/></template>',
- // errors: [
- // {
- // type: ErrorCodes.END_TAG_WITH_TRAILING_SOLIDUS,
- // loc: { offset: 20, line: 1, column: 21 }
- // }
- // ]
- // }
- // ],
- EOF_BEFORE_TAG_NAME: [
- {
- code: '<template><',
- errors: [
- {
- type: ErrorCodes.EOF_BEFORE_TAG_NAME,
- loc: { offset: 11, line: 1, column: 12 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template></',
- errors: [
- {
- type: ErrorCodes.EOF_BEFORE_TAG_NAME,
- loc: { offset: 12, line: 1, column: 13 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- ],
- EOF_IN_CDATA: [
- {
- code: '<template><svg><![CDATA[cdata',
- errors: [
- {
- type: ErrorCodes.EOF_IN_CDATA,
- loc: { offset: 29, line: 1, column: 30 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 10, line: 1, column: 11 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><svg><![CDATA[',
- errors: [
- {
- type: ErrorCodes.EOF_IN_CDATA,
- loc: { offset: 24, line: 1, column: 25 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 10, line: 1, column: 11 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- ],
- EOF_IN_COMMENT: [
- {
- code: '<template><!--comment',
- errors: [
- {
- type: ErrorCodes.EOF_IN_COMMENT,
- loc: { offset: 21, line: 1, column: 22 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><!--',
- errors: [
- {
- type: ErrorCodes.EOF_IN_COMMENT,
- loc: { offset: 14, line: 1, column: 15 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- // // Bogus comments don't throw eof-in-comment error.
- // // https://html.spec.whatwg.org/multipage/parsing.html#bogus-comment-state
- // {
- // code: '<template><!',
- // errors: [
- // {
- // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
- // loc: { offset: 10, line: 1, column: 11 }
- // },
- // {
- // type: ErrorCodes.X_MISSING_END_TAG,
- // loc: { offset: 0, line: 1, column: 1 }
- // }
- // ]
- // },
- // {
- // code: '<template><!-',
- // errors: [
- // {
- // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
- // loc: { offset: 10, line: 1, column: 11 }
- // },
- // {
- // type: ErrorCodes.X_MISSING_END_TAG,
- // loc: { offset: 0, line: 1, column: 1 }
- // }
- // ]
- // },
- // {
- // code: '<template><!abc',
- // errors: [
- // {
- // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
- // loc: { offset: 10, line: 1, column: 11 }
- // },
- // {
- // type: ErrorCodes.X_MISSING_END_TAG,
- // loc: { offset: 0, line: 1, column: 1 }
- // }
- // ]
- // }
- ],
- // EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT: [
- // {
- // code: "<script><!--console.log('hello')",
- // errors: [
- // {
- // type: ErrorCodes.X_MISSING_END_TAG,
- // loc: { offset: 0, line: 1, column: 1 }
- // },
- // {
- // type: ErrorCodes.EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT,
- // loc: { offset: 32, line: 1, column: 33 }
- // }
- // ]
- // },
- // {
- // code: "<script>console.log('hello')",
- // errors: [
- // {
- // type: ErrorCodes.X_MISSING_END_TAG,
- // loc: { offset: 0, line: 1, column: 1 }
- // }
- // ]
- // }
- // ],
- EOF_IN_TAG: [
- {
- code: '<template><div',
- errors: [
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 14, line: 1, column: 15 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><div ',
- errors: [
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 15, line: 1, column: 16 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><div id',
- errors: [
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 17, line: 1, column: 18 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><div id ',
- errors: [
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 18, line: 1, column: 19 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><div id =',
- errors: [
- // {
- // type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
- // loc: { offset: 19, line: 1, column: 20 }
- // },
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 19, line: 1, column: 20 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: "<template><div id='abc",
- errors: [
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 22, line: 1, column: 23 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><div id="abc',
- errors: [
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 22, line: 1, column: 23 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: "<template><div id='abc'",
- errors: [
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 23, line: 1, column: 24 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><div id="abc"',
- errors: [
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 23, line: 1, column: 24 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><div id=abc',
- errors: [
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 21, line: 1, column: 22 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: "<template><div id='abc'/",
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
- loc: { offset: 23, line: 1, column: 24 },
- },
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 24, line: 1, column: 25 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><div id="abc"/',
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
- loc: { offset: 23, line: 1, column: 24 },
- },
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 24, line: 1, column: 25 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<template><div id=abc /',
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
- loc: { offset: 22, line: 1, column: 23 },
- },
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 23, line: 1, column: 24 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<div></div',
- errors: [
- {
- type: ErrorCodes.EOF_IN_TAG,
- loc: { offset: 10, line: 1, column: 11 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- ],
- // INCORRECTLY_CLOSED_COMMENT: [
- // {
- // code: '<template><!--comment--!></template>',
- // errors: [
- // {
- // type: ErrorCodes.INCORRECTLY_CLOSED_COMMENT,
- // loc: { offset: 10, line: 1, column: 11 }
- // }
- // ]
- // }
- // ],
- // INCORRECTLY_OPENED_COMMENT: [
- // {
- // code: '<template><!></template>',
- // errors: [
- // {
- // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
- // loc: { offset: 10, line: 1, column: 11 }
- // }
- // ]
- // },
- // {
- // code: '<template><!-></template>',
- // errors: [
- // {
- // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
- // loc: { offset: 10, line: 1, column: 11 }
- // }
- // ]
- // },
- // {
- // code: '<template><!ELEMENT br EMPTY></template>',
- // errors: [
- // {
- // type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
- // loc: { offset: 10, line: 1, column: 11 }
- // }
- // ]
- // },
- // // Just ignore doctype.
- // {
- // code: '<!DOCTYPE html>',
- // errors: []
- // }
- // ],
- // INVALID_FIRST_CHARACTER_OF_TAG_NAME: [
- // {
- // code: '<template>a < b</template>',
- // errors: [
- // {
- // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
- // loc: { offset: 13, line: 1, column: 14 }
- // }
- // ]
- // },
- // {
- // code: '<template><�></template>',
- // errors: [
- // {
- // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
- // loc: { offset: 11, line: 1, column: 12 }
- // }
- // ]
- // },
- // {
- // code: '<template>a </ b</template>',
- // errors: [
- // {
- // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
- // loc: { offset: 14, line: 1, column: 15 }
- // },
- // {
- // type: ErrorCodes.X_MISSING_END_TAG,
- // loc: { offset: 0, line: 1, column: 1 }
- // }
- // ]
- // },
- // {
- // code: '<template></�></template>',
- // errors: [
- // {
- // type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
- // loc: { offset: 12, line: 1, column: 13 }
- // }
- // ]
- // },
- // // Don't throw invalid-first-character-of-tag-name in interpolation
- // {
- // code: '<template>{{a < b}}</template>',
- // errors: []
- // }
- // ],
- MISSING_ATTRIBUTE_VALUE: [
- {
- code: '<template><div id=></div></template>',
- errors: [
- {
- type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
- loc: { offset: 18, line: 1, column: 19 },
- },
- ],
- },
- {
- code: '<template><div id= ></div></template>',
- errors: [
- {
- type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
- loc: { offset: 19, line: 1, column: 20 },
- },
- ],
- },
- {
- code: '<template><div id= /></div></template>',
- errors: [],
- },
- ],
- MISSING_END_TAG_NAME: [
- {
- code: '<template></></template>',
- errors: [
- {
- type: ErrorCodes.MISSING_END_TAG_NAME,
- loc: { offset: 12, line: 1, column: 13 },
- },
- ],
- },
- ],
- // MISSING_WHITESPACE_BETWEEN_ATTRIBUTES: [
- // {
- // code: '<template><div id="foo"class="bar"></div></template>',
- // errors: [
- // {
- // type: ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES,
- // loc: { offset: 23, line: 1, column: 24 }
- // }
- // ]
- // },
- // // CR doesn't appear in tokenization phase, but all CR are removed in preprocessing.
- // // https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
- // {
- // code: '<template><div id="foo"\r\nclass="bar"></div></template>',
- // errors: []
- // }
- // ],
- // NESTED_COMMENT: [
- // {
- // code: '<template><!--a<!--b--></template>',
- // errors: [
- // {
- // type: ErrorCodes.NESTED_COMMENT,
- // loc: { offset: 15, line: 1, column: 16 }
- // }
- // ]
- // },
- // {
- // code: '<template><!--a<!--b<!--c--></template>',
- // errors: [
- // {
- // type: ErrorCodes.NESTED_COMMENT,
- // loc: { offset: 15, line: 1, column: 16 }
- // },
- // {
- // type: ErrorCodes.NESTED_COMMENT,
- // loc: { offset: 20, line: 1, column: 21 }
- // }
- // ]
- // },
- // {
- // code: '<template><!--a<!--b<!----></template>',
- // errors: [
- // {
- // type: ErrorCodes.NESTED_COMMENT,
- // loc: { offset: 15, line: 1, column: 16 }
- // }
- // ]
- // },
- // {
- // code: '<template><!--a<!--></template>',
- // errors: []
- // },
- // {
- // code: '<template><!--a<!--',
- // errors: [
- // {
- // type: ErrorCodes.EOF_IN_COMMENT,
- // loc: { offset: 19, line: 1, column: 20 }
- // },
- // {
- // type: ErrorCodes.X_MISSING_END_TAG,
- // loc: { offset: 0, line: 1, column: 1 }
- // }
- // ]
- // }
- // ],
- UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME: [
- {
- code: "<template><div a\"bc=''></div></template>",
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
- loc: { offset: 16, line: 1, column: 17 },
- },
- ],
- },
- {
- code: "<template><div a'bc=''></div></template>",
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
- loc: { offset: 16, line: 1, column: 17 },
- },
- ],
- },
- {
- code: "<template><div a<bc=''></div></template>",
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
- loc: { offset: 16, line: 1, column: 17 },
- },
- ],
- },
- ],
- UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE: [
- {
- code: '<template><div foo=bar"></div></template>',
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
- loc: { offset: 22, line: 1, column: 23 },
- },
- ],
- },
- {
- code: "<template><div foo=bar'></div></template>",
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
- loc: { offset: 22, line: 1, column: 23 },
- },
- ],
- },
- {
- code: '<template><div foo=bar<div></div></template>',
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
- loc: { offset: 22, line: 1, column: 23 },
- },
- ],
- },
- {
- code: '<template><div foo=bar=baz></div></template>',
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
- loc: { offset: 22, line: 1, column: 23 },
- },
- ],
- },
- {
- code: '<template><div foo=bar`></div></template>',
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
- loc: { offset: 22, line: 1, column: 23 },
- },
- ],
- },
- ],
- UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME: [
- {
- code: '<template><div =foo=bar></div></template>',
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
- loc: { offset: 15, line: 1, column: 16 },
- },
- ],
- },
- {
- code: '<template><div =></div></template>',
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
- loc: { offset: 15, line: 1, column: 16 },
- },
- ],
- },
- ],
- UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME: [
- {
- code: '<template><?xml?></template>',
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME,
- loc: { offset: 11, line: 1, column: 12 },
- },
- ],
- },
- ],
- UNEXPECTED_SOLIDUS_IN_TAG: [
- {
- code: '<template><div a/b></div></template>',
- errors: [
- {
- type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
- loc: { offset: 16, line: 1, column: 17 },
- },
- ],
- },
- ],
- X_INVALID_END_TAG: [
- {
- code: '<template></div></template>',
- errors: [
- {
- type: ErrorCodes.X_INVALID_END_TAG,
- loc: { offset: 10, line: 1, column: 11 },
- },
- ],
- },
- {
- code: '<template></div></div></template>',
- errors: [
- {
- type: ErrorCodes.X_INVALID_END_TAG,
- loc: { offset: 10, line: 1, column: 11 },
- },
- {
- type: ErrorCodes.X_INVALID_END_TAG,
- loc: { offset: 16, line: 1, column: 17 },
- },
- ],
- },
- {
- code: '<template>a </ b</template>',
- errors: [
- {
- type: ErrorCodes.X_INVALID_END_TAG,
- loc: { offset: 12, line: 1, column: 13 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: "<template>{{'</div>'}}</template>",
- errors: [],
- },
- {
- code: '<textarea></div></textarea>',
- errors: [],
- },
- {
- code: '<svg><![CDATA[</div>]]></svg>',
- errors: [],
- },
- {
- code: '<svg><!--</div>--></svg>',
- errors: [],
- },
- ],
- X_MISSING_END_TAG: [
- {
- code: '<template><div></template>',
- errors: [
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 10, line: 1, column: 11 },
- },
- ],
- },
- {
- code: '<template><div>',
- errors: [
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 10, line: 1, column: 11 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- ],
- X_MISSING_INTERPOLATION_END: [
- {
- code: '{{ foo',
- errors: [
- {
- type: ErrorCodes.X_MISSING_INTERPOLATION_END,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '{{',
- errors: [
- {
- type: ErrorCodes.X_MISSING_INTERPOLATION_END,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '<div>{{ foo</div>',
- errors: [
- {
- type: ErrorCodes.X_MISSING_INTERPOLATION_END,
- loc: { offset: 5, line: 1, column: 6 },
- },
- {
- type: ErrorCodes.X_MISSING_END_TAG,
- loc: { offset: 0, line: 1, column: 1 },
- },
- ],
- },
- {
- code: '{{}}',
- errors: [],
- },
- ],
- X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END: [
- {
- code: `<div v-foo:[sef fsef] />`,
- errors: [
- {
- type: ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
- loc: { offset: 15, line: 1, column: 16 },
- },
- ],
- },
- ],
- }
- for (const key of Object.keys(patterns)) {
- describe(key, () => {
- for (const { code, errors, options } of patterns[key]) {
- test(
- code.replace(
- /[\r\n]/g,
- c => `\\x0${c.codePointAt(0)!.toString(16)};`,
- ),
- () => {
- const spy = vi.fn()
- const ast = baseParse(code, {
- parseMode: 'html',
- getNamespace: tag =>
- tag === 'svg' ? Namespaces.SVG : Namespaces.HTML,
- ...options,
- onError: spy,
- })
- expect(
- spy.mock.calls.map(([err]) => ({
- type: err.code,
- loc: err.loc.start,
- })),
- ).toMatchObject(errors)
- expect(ast).toMatchSnapshot()
- },
- )
- }
- })
- }
- })
- })
|