parse.spec.ts 72 KB

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