parse.spec.ts 86 KB

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