parse.spec.ts 69 KB

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