parse.spec.ts 82 KB

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