parse.spec.ts 76 KB

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