parse.spec.ts 86 KB

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