parse.spec.ts 87 KB

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