parse.spec.ts 66 KB

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