parse.spec.ts 88 KB

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