parse.spec.ts 65 KB

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