parse.spec.ts 77 KB

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