parse.spec.ts 83 KB

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