parse.spec.ts 85 KB

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