parse.spec.ts 79 KB

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