parse.spec.ts 87 KB

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