parse.spec.ts 88 KB

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