parse.spec.ts 69 KB

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