parse.spec.ts 71 KB

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