parse.spec.ts 83 KB

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