parse.spec.ts 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399
  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.props[0] as AttributeNode).value
  198. const text2 = (element.props[1] as AttributeNode).value
  199. const text3 = (element.props[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. codegenNode: undefined,
  394. props: [],
  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. codegenNode: undefined,
  424. props: [],
  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. codegenNode: undefined,
  443. props: [],
  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. codegenNode: undefined,
  464. props: [],
  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. codegenNode: undefined,
  483. props: [
  484. {
  485. type: NodeTypes.ATTRIBUTE,
  486. name: 'id',
  487. value: undefined,
  488. loc: {
  489. start: { offset: 5, line: 1, column: 6 },
  490. end: { offset: 7, line: 1, column: 8 },
  491. source: 'id'
  492. }
  493. }
  494. ],
  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. codegenNode: undefined,
  513. props: [
  514. {
  515. type: NodeTypes.ATTRIBUTE,
  516. name: 'id',
  517. value: {
  518. type: NodeTypes.TEXT,
  519. content: '',
  520. isEmpty: true,
  521. loc: {
  522. start: { offset: 8, line: 1, column: 9 },
  523. end: { offset: 10, line: 1, column: 11 },
  524. source: '""'
  525. }
  526. },
  527. loc: {
  528. start: { offset: 5, line: 1, column: 6 },
  529. end: { offset: 10, line: 1, column: 11 },
  530. source: 'id=""'
  531. }
  532. }
  533. ],
  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. codegenNode: undefined,
  552. props: [
  553. {
  554. type: NodeTypes.ATTRIBUTE,
  555. name: 'id',
  556. value: {
  557. type: NodeTypes.TEXT,
  558. content: '',
  559. isEmpty: true,
  560. loc: {
  561. start: { offset: 8, line: 1, column: 9 },
  562. end: { offset: 10, line: 1, column: 11 },
  563. source: "''"
  564. }
  565. },
  566. loc: {
  567. start: { offset: 5, line: 1, column: 6 },
  568. end: { offset: 10, line: 1, column: 11 },
  569. source: "id=''"
  570. }
  571. }
  572. ],
  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. codegenNode: undefined,
  591. props: [
  592. {
  593. type: NodeTypes.ATTRIBUTE,
  594. name: 'id',
  595. value: {
  596. type: NodeTypes.TEXT,
  597. content: ">'",
  598. isEmpty: false,
  599. loc: {
  600. start: { offset: 8, line: 1, column: 9 },
  601. end: { offset: 12, line: 1, column: 13 },
  602. source: '">\'"'
  603. }
  604. },
  605. loc: {
  606. start: { offset: 5, line: 1, column: 6 },
  607. end: { offset: 12, line: 1, column: 13 },
  608. source: 'id=">\'"'
  609. }
  610. }
  611. ],
  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. codegenNode: undefined,
  630. props: [
  631. {
  632. type: NodeTypes.ATTRIBUTE,
  633. name: 'id',
  634. value: {
  635. type: NodeTypes.TEXT,
  636. content: '>"',
  637. isEmpty: false,
  638. loc: {
  639. start: { offset: 8, line: 1, column: 9 },
  640. end: { offset: 12, line: 1, column: 13 },
  641. source: "'>\"'"
  642. }
  643. },
  644. loc: {
  645. start: { offset: 5, line: 1, column: 6 },
  646. end: { offset: 12, line: 1, column: 13 },
  647. source: "id='>\"'"
  648. }
  649. }
  650. ],
  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. codegenNode: undefined,
  669. props: [
  670. {
  671. type: NodeTypes.ATTRIBUTE,
  672. name: 'id',
  673. value: {
  674. type: NodeTypes.TEXT,
  675. content: 'a/',
  676. isEmpty: false,
  677. loc: {
  678. start: { offset: 8, line: 1, column: 9 },
  679. end: { offset: 10, line: 1, column: 11 },
  680. source: 'a/'
  681. }
  682. },
  683. loc: {
  684. start: { offset: 5, line: 1, column: 6 },
  685. end: { offset: 10, line: 1, column: 11 },
  686. source: 'id=a/'
  687. }
  688. }
  689. ],
  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. codegenNode: undefined,
  708. props: [
  709. {
  710. type: NodeTypes.ATTRIBUTE,
  711. name: 'id',
  712. value: {
  713. type: NodeTypes.TEXT,
  714. content: 'a',
  715. isEmpty: false,
  716. loc: {
  717. start: { offset: 8, line: 1, column: 9 },
  718. end: { offset: 9, line: 1, column: 10 },
  719. source: 'a'
  720. }
  721. },
  722. loc: {
  723. start: { offset: 5, line: 1, column: 6 },
  724. end: { offset: 9, line: 1, column: 10 },
  725. source: 'id=a'
  726. }
  727. },
  728. {
  729. type: NodeTypes.ATTRIBUTE,
  730. name: 'class',
  731. value: {
  732. type: NodeTypes.TEXT,
  733. content: 'c',
  734. isEmpty: false,
  735. loc: {
  736. start: { offset: 16, line: 1, column: 17 },
  737. end: { offset: 19, line: 1, column: 20 },
  738. source: '"c"'
  739. }
  740. },
  741. loc: {
  742. start: { offset: 10, line: 1, column: 11 },
  743. end: { offset: 19, line: 1, column: 20 },
  744. source: 'class="c"'
  745. }
  746. },
  747. {
  748. type: NodeTypes.ATTRIBUTE,
  749. name: 'inert',
  750. value: undefined,
  751. loc: {
  752. start: { offset: 20, line: 1, column: 21 },
  753. end: { offset: 25, line: 1, column: 26 },
  754. source: 'inert'
  755. }
  756. },
  757. {
  758. type: NodeTypes.ATTRIBUTE,
  759. name: 'style',
  760. value: {
  761. type: NodeTypes.TEXT,
  762. content: '',
  763. isEmpty: true,
  764. loc: {
  765. start: { offset: 32, line: 1, column: 33 },
  766. end: { offset: 34, line: 1, column: 35 },
  767. source: "''"
  768. }
  769. },
  770. loc: {
  771. start: { offset: 26, line: 1, column: 27 },
  772. end: { offset: 34, line: 1, column: 35 },
  773. source: "style=''"
  774. }
  775. }
  776. ],
  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).props[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).props[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).props[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).props[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).props[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).props[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).props[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).props[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).props[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).props[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. })