parse.spec.ts 91 KB

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