parse.spec.ts 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761
  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('custom element', () => {
  592. const ast = baseParse('<div></div><comp></comp>', {
  593. isNativeTag: tag => tag === 'div',
  594. isCustomElement: tag => tag === 'comp'
  595. })
  596. expect(ast.children[0]).toMatchObject({
  597. type: NodeTypes.ELEMENT,
  598. tag: 'div',
  599. tagType: ElementTypes.ELEMENT
  600. })
  601. expect(ast.children[1]).toMatchObject({
  602. type: NodeTypes.ELEMENT,
  603. tag: 'comp',
  604. tagType: ElementTypes.ELEMENT
  605. })
  606. })
  607. test('attribute with no value', () => {
  608. const ast = baseParse('<div id></div>')
  609. const element = ast.children[0] as ElementNode
  610. expect(element).toStrictEqual({
  611. type: NodeTypes.ELEMENT,
  612. ns: Namespaces.HTML,
  613. tag: 'div',
  614. tagType: ElementTypes.ELEMENT,
  615. codegenNode: undefined,
  616. props: [
  617. {
  618. type: NodeTypes.ATTRIBUTE,
  619. name: 'id',
  620. value: undefined,
  621. loc: {
  622. start: { offset: 5, line: 1, column: 6 },
  623. end: { offset: 7, line: 1, column: 8 },
  624. source: 'id'
  625. }
  626. }
  627. ],
  628. isSelfClosing: false,
  629. children: [],
  630. loc: {
  631. start: { offset: 0, line: 1, column: 1 },
  632. end: { offset: 14, line: 1, column: 15 },
  633. source: '<div id></div>'
  634. }
  635. })
  636. })
  637. test('attribute with empty value, double quote', () => {
  638. const ast = baseParse('<div id=""></div>')
  639. const element = ast.children[0] as ElementNode
  640. expect(element).toStrictEqual({
  641. type: NodeTypes.ELEMENT,
  642. ns: Namespaces.HTML,
  643. tag: 'div',
  644. tagType: ElementTypes.ELEMENT,
  645. codegenNode: undefined,
  646. props: [
  647. {
  648. type: NodeTypes.ATTRIBUTE,
  649. name: 'id',
  650. value: {
  651. type: NodeTypes.TEXT,
  652. content: '',
  653. loc: {
  654. start: { offset: 8, line: 1, column: 9 },
  655. end: { offset: 10, line: 1, column: 11 },
  656. source: '""'
  657. }
  658. },
  659. loc: {
  660. start: { offset: 5, line: 1, column: 6 },
  661. end: { offset: 10, line: 1, column: 11 },
  662. source: 'id=""'
  663. }
  664. }
  665. ],
  666. isSelfClosing: false,
  667. children: [],
  668. loc: {
  669. start: { offset: 0, line: 1, column: 1 },
  670. end: { offset: 17, line: 1, column: 18 },
  671. source: '<div id=""></div>'
  672. }
  673. })
  674. })
  675. test('attribute with empty value, single quote', () => {
  676. const ast = baseParse("<div id=''></div>")
  677. const element = ast.children[0] as ElementNode
  678. expect(element).toStrictEqual({
  679. type: NodeTypes.ELEMENT,
  680. ns: Namespaces.HTML,
  681. tag: 'div',
  682. tagType: ElementTypes.ELEMENT,
  683. codegenNode: undefined,
  684. props: [
  685. {
  686. type: NodeTypes.ATTRIBUTE,
  687. name: 'id',
  688. value: {
  689. type: NodeTypes.TEXT,
  690. content: '',
  691. loc: {
  692. start: { offset: 8, line: 1, column: 9 },
  693. end: { offset: 10, line: 1, column: 11 },
  694. source: "''"
  695. }
  696. },
  697. loc: {
  698. start: { offset: 5, line: 1, column: 6 },
  699. end: { offset: 10, line: 1, column: 11 },
  700. source: "id=''"
  701. }
  702. }
  703. ],
  704. isSelfClosing: false,
  705. children: [],
  706. loc: {
  707. start: { offset: 0, line: 1, column: 1 },
  708. end: { offset: 17, line: 1, column: 18 },
  709. source: "<div id=''></div>"
  710. }
  711. })
  712. })
  713. test('attribute with value, double quote', () => {
  714. const ast = baseParse('<div id=">\'"></div>')
  715. const element = ast.children[0] as ElementNode
  716. expect(element).toStrictEqual({
  717. type: NodeTypes.ELEMENT,
  718. ns: Namespaces.HTML,
  719. tag: 'div',
  720. tagType: ElementTypes.ELEMENT,
  721. codegenNode: undefined,
  722. props: [
  723. {
  724. type: NodeTypes.ATTRIBUTE,
  725. name: 'id',
  726. value: {
  727. type: NodeTypes.TEXT,
  728. content: ">'",
  729. loc: {
  730. start: { offset: 8, line: 1, column: 9 },
  731. end: { offset: 12, line: 1, column: 13 },
  732. source: '">\'"'
  733. }
  734. },
  735. loc: {
  736. start: { offset: 5, line: 1, column: 6 },
  737. end: { offset: 12, line: 1, column: 13 },
  738. source: 'id=">\'"'
  739. }
  740. }
  741. ],
  742. isSelfClosing: false,
  743. children: [],
  744. loc: {
  745. start: { offset: 0, line: 1, column: 1 },
  746. end: { offset: 19, line: 1, column: 20 },
  747. source: '<div id=">\'"></div>'
  748. }
  749. })
  750. })
  751. test('attribute with value, single quote', () => {
  752. const ast = baseParse("<div id='>\"'></div>")
  753. const element = ast.children[0] as ElementNode
  754. expect(element).toStrictEqual({
  755. type: NodeTypes.ELEMENT,
  756. ns: Namespaces.HTML,
  757. tag: 'div',
  758. tagType: ElementTypes.ELEMENT,
  759. codegenNode: undefined,
  760. props: [
  761. {
  762. type: NodeTypes.ATTRIBUTE,
  763. name: 'id',
  764. value: {
  765. type: NodeTypes.TEXT,
  766. content: '>"',
  767. loc: {
  768. start: { offset: 8, line: 1, column: 9 },
  769. end: { offset: 12, line: 1, column: 13 },
  770. source: "'>\"'"
  771. }
  772. },
  773. loc: {
  774. start: { offset: 5, line: 1, column: 6 },
  775. end: { offset: 12, line: 1, column: 13 },
  776. source: "id='>\"'"
  777. }
  778. }
  779. ],
  780. isSelfClosing: false,
  781. children: [],
  782. loc: {
  783. start: { offset: 0, line: 1, column: 1 },
  784. end: { offset: 19, line: 1, column: 20 },
  785. source: "<div id='>\"'></div>"
  786. }
  787. })
  788. })
  789. test('attribute with value, unquoted', () => {
  790. const ast = baseParse('<div id=a/></div>')
  791. const element = ast.children[0] as ElementNode
  792. expect(element).toStrictEqual({
  793. type: NodeTypes.ELEMENT,
  794. ns: Namespaces.HTML,
  795. tag: 'div',
  796. tagType: ElementTypes.ELEMENT,
  797. codegenNode: undefined,
  798. props: [
  799. {
  800. type: NodeTypes.ATTRIBUTE,
  801. name: 'id',
  802. value: {
  803. type: NodeTypes.TEXT,
  804. content: 'a/',
  805. loc: {
  806. start: { offset: 8, line: 1, column: 9 },
  807. end: { offset: 10, line: 1, column: 11 },
  808. source: 'a/'
  809. }
  810. },
  811. loc: {
  812. start: { offset: 5, line: 1, column: 6 },
  813. end: { offset: 10, line: 1, column: 11 },
  814. source: 'id=a/'
  815. }
  816. }
  817. ],
  818. isSelfClosing: false,
  819. children: [],
  820. loc: {
  821. start: { offset: 0, line: 1, column: 1 },
  822. end: { offset: 17, line: 1, column: 18 },
  823. source: '<div id=a/></div>'
  824. }
  825. })
  826. })
  827. test('multiple attributes', () => {
  828. const ast = baseParse('<div id=a class="c" inert style=\'\'></div>')
  829. const element = ast.children[0] as ElementNode
  830. expect(element).toStrictEqual({
  831. type: NodeTypes.ELEMENT,
  832. ns: Namespaces.HTML,
  833. tag: 'div',
  834. tagType: ElementTypes.ELEMENT,
  835. codegenNode: undefined,
  836. props: [
  837. {
  838. type: NodeTypes.ATTRIBUTE,
  839. name: 'id',
  840. value: {
  841. type: NodeTypes.TEXT,
  842. content: 'a',
  843. loc: {
  844. start: { offset: 8, line: 1, column: 9 },
  845. end: { offset: 9, line: 1, column: 10 },
  846. source: 'a'
  847. }
  848. },
  849. loc: {
  850. start: { offset: 5, line: 1, column: 6 },
  851. end: { offset: 9, line: 1, column: 10 },
  852. source: 'id=a'
  853. }
  854. },
  855. {
  856. type: NodeTypes.ATTRIBUTE,
  857. name: 'class',
  858. value: {
  859. type: NodeTypes.TEXT,
  860. content: 'c',
  861. loc: {
  862. start: { offset: 16, line: 1, column: 17 },
  863. end: { offset: 19, line: 1, column: 20 },
  864. source: '"c"'
  865. }
  866. },
  867. loc: {
  868. start: { offset: 10, line: 1, column: 11 },
  869. end: { offset: 19, line: 1, column: 20 },
  870. source: 'class="c"'
  871. }
  872. },
  873. {
  874. type: NodeTypes.ATTRIBUTE,
  875. name: 'inert',
  876. value: undefined,
  877. loc: {
  878. start: { offset: 20, line: 1, column: 21 },
  879. end: { offset: 25, line: 1, column: 26 },
  880. source: 'inert'
  881. }
  882. },
  883. {
  884. type: NodeTypes.ATTRIBUTE,
  885. name: 'style',
  886. value: {
  887. type: NodeTypes.TEXT,
  888. content: '',
  889. loc: {
  890. start: { offset: 32, line: 1, column: 33 },
  891. end: { offset: 34, line: 1, column: 35 },
  892. source: "''"
  893. }
  894. },
  895. loc: {
  896. start: { offset: 26, line: 1, column: 27 },
  897. end: { offset: 34, line: 1, column: 35 },
  898. source: "style=''"
  899. }
  900. }
  901. ],
  902. isSelfClosing: false,
  903. children: [],
  904. loc: {
  905. start: { offset: 0, line: 1, column: 1 },
  906. end: { offset: 41, line: 1, column: 42 },
  907. source: '<div id=a class="c" inert style=\'\'></div>'
  908. }
  909. })
  910. })
  911. test('directive with no value', () => {
  912. const ast = baseParse('<div v-if/>')
  913. const directive = (ast.children[0] as ElementNode).props[0]
  914. expect(directive).toStrictEqual({
  915. type: NodeTypes.DIRECTIVE,
  916. name: 'if',
  917. arg: undefined,
  918. modifiers: [],
  919. exp: undefined,
  920. loc: {
  921. start: { offset: 5, line: 1, column: 6 },
  922. end: { offset: 9, line: 1, column: 10 },
  923. source: 'v-if'
  924. }
  925. })
  926. })
  927. test('directive with value', () => {
  928. const ast = baseParse('<div v-if="a"/>')
  929. const directive = (ast.children[0] as ElementNode).props[0]
  930. expect(directive).toStrictEqual({
  931. type: NodeTypes.DIRECTIVE,
  932. name: 'if',
  933. arg: undefined,
  934. modifiers: [],
  935. exp: {
  936. type: NodeTypes.SIMPLE_EXPRESSION,
  937. content: 'a',
  938. isStatic: false,
  939. isConstant: false,
  940. loc: {
  941. start: { offset: 11, line: 1, column: 12 },
  942. end: { offset: 12, line: 1, column: 13 },
  943. source: 'a'
  944. }
  945. },
  946. loc: {
  947. start: { offset: 5, line: 1, column: 6 },
  948. end: { offset: 13, line: 1, column: 14 },
  949. source: 'v-if="a"'
  950. }
  951. })
  952. })
  953. test('directive with argument', () => {
  954. const ast = baseParse('<div v-on:click/>')
  955. const directive = (ast.children[0] as ElementNode).props[0]
  956. expect(directive).toStrictEqual({
  957. type: NodeTypes.DIRECTIVE,
  958. name: 'on',
  959. arg: {
  960. type: NodeTypes.SIMPLE_EXPRESSION,
  961. content: 'click',
  962. isStatic: true,
  963. isConstant: true,
  964. loc: {
  965. source: 'click',
  966. start: {
  967. column: 11,
  968. line: 1,
  969. offset: 10
  970. },
  971. end: {
  972. column: 16,
  973. line: 1,
  974. offset: 15
  975. }
  976. }
  977. },
  978. modifiers: [],
  979. exp: undefined,
  980. loc: {
  981. start: { offset: 5, line: 1, column: 6 },
  982. end: { offset: 15, line: 1, column: 16 },
  983. source: 'v-on:click'
  984. }
  985. })
  986. })
  987. test('directive with a modifier', () => {
  988. const ast = baseParse('<div v-on.enter/>')
  989. const directive = (ast.children[0] as ElementNode).props[0]
  990. expect(directive).toStrictEqual({
  991. type: NodeTypes.DIRECTIVE,
  992. name: 'on',
  993. arg: undefined,
  994. modifiers: ['enter'],
  995. exp: undefined,
  996. loc: {
  997. start: { offset: 5, line: 1, column: 6 },
  998. end: { offset: 15, line: 1, column: 16 },
  999. source: 'v-on.enter'
  1000. }
  1001. })
  1002. })
  1003. test('directive with two modifiers', () => {
  1004. const ast = baseParse('<div v-on.enter.exact/>')
  1005. const directive = (ast.children[0] as ElementNode).props[0]
  1006. expect(directive).toStrictEqual({
  1007. type: NodeTypes.DIRECTIVE,
  1008. name: 'on',
  1009. arg: undefined,
  1010. modifiers: ['enter', 'exact'],
  1011. exp: undefined,
  1012. loc: {
  1013. start: { offset: 5, line: 1, column: 6 },
  1014. end: { offset: 21, line: 1, column: 22 },
  1015. source: 'v-on.enter.exact'
  1016. }
  1017. })
  1018. })
  1019. test('directive with argument and modifiers', () => {
  1020. const ast = baseParse('<div v-on:click.enter.exact/>')
  1021. const directive = (ast.children[0] as ElementNode).props[0]
  1022. expect(directive).toStrictEqual({
  1023. type: NodeTypes.DIRECTIVE,
  1024. name: 'on',
  1025. arg: {
  1026. type: NodeTypes.SIMPLE_EXPRESSION,
  1027. content: 'click',
  1028. isStatic: true,
  1029. isConstant: true,
  1030. loc: {
  1031. source: 'click',
  1032. start: {
  1033. column: 11,
  1034. line: 1,
  1035. offset: 10
  1036. },
  1037. end: {
  1038. column: 16,
  1039. line: 1,
  1040. offset: 15
  1041. }
  1042. }
  1043. },
  1044. modifiers: ['enter', 'exact'],
  1045. exp: undefined,
  1046. loc: {
  1047. start: { offset: 5, line: 1, column: 6 },
  1048. end: { offset: 27, line: 1, column: 28 },
  1049. source: 'v-on:click.enter.exact'
  1050. }
  1051. })
  1052. })
  1053. test('v-bind shorthand', () => {
  1054. const ast = baseParse('<div :a=b />')
  1055. const directive = (ast.children[0] as ElementNode).props[0]
  1056. expect(directive).toStrictEqual({
  1057. type: NodeTypes.DIRECTIVE,
  1058. name: 'bind',
  1059. arg: {
  1060. type: NodeTypes.SIMPLE_EXPRESSION,
  1061. content: 'a',
  1062. isStatic: true,
  1063. isConstant: true,
  1064. loc: {
  1065. source: 'a',
  1066. start: {
  1067. column: 7,
  1068. line: 1,
  1069. offset: 6
  1070. },
  1071. end: {
  1072. column: 8,
  1073. line: 1,
  1074. offset: 7
  1075. }
  1076. }
  1077. },
  1078. modifiers: [],
  1079. exp: {
  1080. type: NodeTypes.SIMPLE_EXPRESSION,
  1081. content: 'b',
  1082. isStatic: false,
  1083. isConstant: false,
  1084. loc: {
  1085. start: { offset: 8, line: 1, column: 9 },
  1086. end: { offset: 9, line: 1, column: 10 },
  1087. source: 'b'
  1088. }
  1089. },
  1090. loc: {
  1091. start: { offset: 5, line: 1, column: 6 },
  1092. end: { offset: 9, line: 1, column: 10 },
  1093. source: ':a=b'
  1094. }
  1095. })
  1096. })
  1097. test('v-bind shorthand with modifier', () => {
  1098. const ast = baseParse('<div :a.sync=b />')
  1099. const directive = (ast.children[0] as ElementNode).props[0]
  1100. expect(directive).toStrictEqual({
  1101. type: NodeTypes.DIRECTIVE,
  1102. name: 'bind',
  1103. arg: {
  1104. type: NodeTypes.SIMPLE_EXPRESSION,
  1105. content: 'a',
  1106. isStatic: true,
  1107. isConstant: true,
  1108. loc: {
  1109. source: 'a',
  1110. start: {
  1111. column: 7,
  1112. line: 1,
  1113. offset: 6
  1114. },
  1115. end: {
  1116. column: 8,
  1117. line: 1,
  1118. offset: 7
  1119. }
  1120. }
  1121. },
  1122. modifiers: ['sync'],
  1123. exp: {
  1124. type: NodeTypes.SIMPLE_EXPRESSION,
  1125. content: 'b',
  1126. isStatic: false,
  1127. isConstant: false,
  1128. loc: {
  1129. start: { offset: 13, line: 1, column: 14 },
  1130. end: { offset: 14, line: 1, column: 15 },
  1131. source: 'b'
  1132. }
  1133. },
  1134. loc: {
  1135. start: { offset: 5, line: 1, column: 6 },
  1136. end: { offset: 14, line: 1, column: 15 },
  1137. source: ':a.sync=b'
  1138. }
  1139. })
  1140. })
  1141. test('v-on shorthand', () => {
  1142. const ast = baseParse('<div @a=b />')
  1143. const directive = (ast.children[0] as ElementNode).props[0]
  1144. expect(directive).toStrictEqual({
  1145. type: NodeTypes.DIRECTIVE,
  1146. name: 'on',
  1147. arg: {
  1148. type: NodeTypes.SIMPLE_EXPRESSION,
  1149. content: 'a',
  1150. isStatic: true,
  1151. isConstant: true,
  1152. loc: {
  1153. source: 'a',
  1154. start: {
  1155. column: 7,
  1156. line: 1,
  1157. offset: 6
  1158. },
  1159. end: {
  1160. column: 8,
  1161. line: 1,
  1162. offset: 7
  1163. }
  1164. }
  1165. },
  1166. modifiers: [],
  1167. exp: {
  1168. type: NodeTypes.SIMPLE_EXPRESSION,
  1169. content: 'b',
  1170. isStatic: false,
  1171. isConstant: false,
  1172. loc: {
  1173. start: { offset: 8, line: 1, column: 9 },
  1174. end: { offset: 9, line: 1, column: 10 },
  1175. source: 'b'
  1176. }
  1177. },
  1178. loc: {
  1179. start: { offset: 5, line: 1, column: 6 },
  1180. end: { offset: 9, line: 1, column: 10 },
  1181. source: '@a=b'
  1182. }
  1183. })
  1184. })
  1185. test('v-on shorthand with modifier', () => {
  1186. const ast = baseParse('<div @a.enter=b />')
  1187. const directive = (ast.children[0] as ElementNode).props[0]
  1188. expect(directive).toStrictEqual({
  1189. type: NodeTypes.DIRECTIVE,
  1190. name: 'on',
  1191. arg: {
  1192. type: NodeTypes.SIMPLE_EXPRESSION,
  1193. content: 'a',
  1194. isStatic: true,
  1195. isConstant: true,
  1196. loc: {
  1197. source: 'a',
  1198. start: {
  1199. column: 7,
  1200. line: 1,
  1201. offset: 6
  1202. },
  1203. end: {
  1204. column: 8,
  1205. line: 1,
  1206. offset: 7
  1207. }
  1208. }
  1209. },
  1210. modifiers: ['enter'],
  1211. exp: {
  1212. type: NodeTypes.SIMPLE_EXPRESSION,
  1213. content: 'b',
  1214. isStatic: false,
  1215. isConstant: false,
  1216. loc: {
  1217. start: { offset: 14, line: 1, column: 15 },
  1218. end: { offset: 15, line: 1, column: 16 },
  1219. source: 'b'
  1220. }
  1221. },
  1222. loc: {
  1223. start: { offset: 5, line: 1, column: 6 },
  1224. end: { offset: 15, line: 1, column: 16 },
  1225. source: '@a.enter=b'
  1226. }
  1227. })
  1228. })
  1229. test('v-slot shorthand', () => {
  1230. const ast = baseParse('<Comp #a="{ b }" />')
  1231. const directive = (ast.children[0] as ElementNode).props[0]
  1232. expect(directive).toStrictEqual({
  1233. type: NodeTypes.DIRECTIVE,
  1234. name: 'slot',
  1235. arg: {
  1236. type: NodeTypes.SIMPLE_EXPRESSION,
  1237. content: 'a',
  1238. isStatic: true,
  1239. isConstant: true,
  1240. loc: {
  1241. source: 'a',
  1242. start: {
  1243. column: 8,
  1244. line: 1,
  1245. offset: 7
  1246. },
  1247. end: {
  1248. column: 9,
  1249. line: 1,
  1250. offset: 8
  1251. }
  1252. }
  1253. },
  1254. modifiers: [],
  1255. exp: {
  1256. type: NodeTypes.SIMPLE_EXPRESSION,
  1257. content: '{ b }',
  1258. isStatic: false,
  1259. // The `isConstant` is the default value and will be determined in transformExpression
  1260. isConstant: false,
  1261. loc: {
  1262. start: { offset: 10, line: 1, column: 11 },
  1263. end: { offset: 15, line: 1, column: 16 },
  1264. source: '{ b }'
  1265. }
  1266. },
  1267. loc: {
  1268. start: { offset: 6, line: 1, column: 7 },
  1269. end: { offset: 16, line: 1, column: 17 },
  1270. source: '#a="{ b }"'
  1271. }
  1272. })
  1273. })
  1274. test('v-pre', () => {
  1275. const ast = baseParse(
  1276. `<div v-pre :id="foo"><Comp/>{{ bar }}</div>\n` +
  1277. `<div :id="foo"><Comp/>{{ bar }}</div>`
  1278. )
  1279. const divWithPre = ast.children[0] as ElementNode
  1280. expect(divWithPre.props).toMatchObject([
  1281. {
  1282. type: NodeTypes.ATTRIBUTE,
  1283. name: `:id`,
  1284. value: {
  1285. type: NodeTypes.TEXT,
  1286. content: `foo`
  1287. },
  1288. loc: {
  1289. source: `:id="foo"`,
  1290. start: {
  1291. line: 1,
  1292. column: 12
  1293. },
  1294. end: {
  1295. line: 1,
  1296. column: 21
  1297. }
  1298. }
  1299. }
  1300. ])
  1301. expect(divWithPre.children[0]).toMatchObject({
  1302. type: NodeTypes.ELEMENT,
  1303. tagType: ElementTypes.ELEMENT,
  1304. tag: `Comp`
  1305. })
  1306. expect(divWithPre.children[1]).toMatchObject({
  1307. type: NodeTypes.TEXT,
  1308. content: `{{ bar }}`
  1309. })
  1310. // should not affect siblings after it
  1311. const divWithoutPre = ast.children[1] as ElementNode
  1312. expect(divWithoutPre.props).toMatchObject([
  1313. {
  1314. type: NodeTypes.DIRECTIVE,
  1315. name: `bind`,
  1316. arg: {
  1317. type: NodeTypes.SIMPLE_EXPRESSION,
  1318. isStatic: true,
  1319. content: `id`
  1320. },
  1321. exp: {
  1322. type: NodeTypes.SIMPLE_EXPRESSION,
  1323. isStatic: false,
  1324. content: `foo`
  1325. },
  1326. loc: {
  1327. source: `:id="foo"`,
  1328. start: {
  1329. line: 2,
  1330. column: 6
  1331. },
  1332. end: {
  1333. line: 2,
  1334. column: 15
  1335. }
  1336. }
  1337. }
  1338. ])
  1339. expect(divWithoutPre.children[0]).toMatchObject({
  1340. type: NodeTypes.ELEMENT,
  1341. tagType: ElementTypes.COMPONENT,
  1342. tag: `Comp`
  1343. })
  1344. expect(divWithoutPre.children[1]).toMatchObject({
  1345. type: NodeTypes.INTERPOLATION,
  1346. content: {
  1347. type: NodeTypes.SIMPLE_EXPRESSION,
  1348. content: `bar`,
  1349. isStatic: false
  1350. }
  1351. })
  1352. })
  1353. test('end tags are case-insensitive.', () => {
  1354. const ast = baseParse('<div>hello</DIV>after')
  1355. const element = ast.children[0] as ElementNode
  1356. const text = element.children[0] as TextNode
  1357. expect(text).toStrictEqual({
  1358. type: NodeTypes.TEXT,
  1359. content: 'hello',
  1360. loc: {
  1361. start: { offset: 5, line: 1, column: 6 },
  1362. end: { offset: 10, line: 1, column: 11 },
  1363. source: 'hello'
  1364. }
  1365. })
  1366. })
  1367. })
  1368. test('self closing single tag', () => {
  1369. const ast = baseParse('<div :class="{ some: condition }" />')
  1370. expect(ast.children).toHaveLength(1)
  1371. expect(ast.children[0]).toMatchObject({ tag: 'div' })
  1372. })
  1373. test('self closing multiple tag', () => {
  1374. const ast = baseParse(
  1375. `<div :class="{ some: condition }" />\n` +
  1376. `<p v-bind:style="{ color: 'red' }"/>`
  1377. )
  1378. expect(ast).toMatchSnapshot()
  1379. expect(ast.children).toHaveLength(2)
  1380. expect(ast.children[0]).toMatchObject({ tag: 'div' })
  1381. expect(ast.children[1]).toMatchObject({ tag: 'p' })
  1382. })
  1383. test('valid html', () => {
  1384. const ast = baseParse(
  1385. `<div :class="{ some: condition }">\n` +
  1386. ` <p v-bind:style="{ color: 'red' }"/>\n` +
  1387. ` <!-- a comment with <html> inside it -->\n` +
  1388. `</div>`
  1389. )
  1390. expect(ast).toMatchSnapshot()
  1391. expect(ast.children).toHaveLength(1)
  1392. const el = ast.children[0] as any
  1393. expect(el).toMatchObject({
  1394. tag: 'div'
  1395. })
  1396. expect(el.children).toHaveLength(2)
  1397. expect(el.children[0]).toMatchObject({
  1398. tag: 'p'
  1399. })
  1400. expect(el.children[1]).toMatchObject({
  1401. type: NodeTypes.COMMENT
  1402. })
  1403. })
  1404. test('invalid html', () => {
  1405. expect(() => {
  1406. baseParse(`<div>\n<span>\n</div>\n</span>`)
  1407. }).toThrow('Element is missing end tag.')
  1408. const spy = jest.fn()
  1409. const ast = baseParse(`<div>\n<span>\n</div>\n</span>`, {
  1410. onError: spy
  1411. })
  1412. expect(spy.mock.calls).toMatchObject([
  1413. [
  1414. {
  1415. code: ErrorCodes.X_MISSING_END_TAG,
  1416. loc: {
  1417. start: {
  1418. offset: 6,
  1419. line: 2,
  1420. column: 1
  1421. }
  1422. }
  1423. }
  1424. ],
  1425. [
  1426. {
  1427. code: ErrorCodes.X_INVALID_END_TAG,
  1428. loc: {
  1429. start: {
  1430. offset: 20,
  1431. line: 4,
  1432. column: 1
  1433. }
  1434. }
  1435. }
  1436. ]
  1437. ])
  1438. expect(ast).toMatchSnapshot()
  1439. })
  1440. test('parse with correct location info', () => {
  1441. const [foo, bar, but, baz] = baseParse(
  1442. `
  1443. foo
  1444. is {{ bar }} but {{ baz }}`.trim()
  1445. ).children
  1446. let offset = 0
  1447. expect(foo.loc.start).toEqual({ line: 1, column: 1, offset })
  1448. offset += foo.loc.source.length
  1449. expect(foo.loc.end).toEqual({ line: 2, column: 5, offset })
  1450. expect(bar.loc.start).toEqual({ line: 2, column: 5, offset })
  1451. const barInner = (bar as InterpolationNode).content
  1452. offset += 3
  1453. expect(barInner.loc.start).toEqual({ line: 2, column: 8, offset })
  1454. offset += barInner.loc.source.length
  1455. expect(barInner.loc.end).toEqual({ line: 2, column: 11, offset })
  1456. offset += 3
  1457. expect(bar.loc.end).toEqual({ line: 2, column: 14, offset })
  1458. expect(but.loc.start).toEqual({ line: 2, column: 14, offset })
  1459. offset += but.loc.source.length
  1460. expect(but.loc.end).toEqual({ line: 2, column: 19, offset })
  1461. expect(baz.loc.start).toEqual({ line: 2, column: 19, offset })
  1462. const bazInner = (baz as InterpolationNode).content
  1463. offset += 3
  1464. expect(bazInner.loc.start).toEqual({ line: 2, column: 22, offset })
  1465. offset += bazInner.loc.source.length
  1466. expect(bazInner.loc.end).toEqual({ line: 2, column: 25, offset })
  1467. offset += 3
  1468. expect(baz.loc.end).toEqual({ line: 2, column: 28, offset })
  1469. })
  1470. describe('namedCharacterReferences option', () => {
  1471. test('use the given map', () => {
  1472. const ast: any = baseParse('&amp;&cups;', {
  1473. namedCharacterReferences: {
  1474. 'cups;': '\u222A\uFE00' // UNION with serifs
  1475. },
  1476. onError: () => {} // Ignore errors
  1477. })
  1478. expect(ast.children.length).toBe(1)
  1479. expect(ast.children[0].type).toBe(NodeTypes.TEXT)
  1480. expect(ast.children[0].content).toBe('&amp;\u222A\uFE00')
  1481. })
  1482. })
  1483. describe('whitespace management', () => {
  1484. it('should remove whitespaces at start/end inside an element', () => {
  1485. const ast = baseParse(`<div> <span/> </div>`)
  1486. expect((ast.children[0] as ElementNode).children.length).toBe(1)
  1487. })
  1488. it('should remove whitespaces w/ newline between elements', () => {
  1489. const ast = baseParse(`<div/> \n <div/> \n <div/>`)
  1490. expect(ast.children.length).toBe(3)
  1491. expect(ast.children.every(c => c.type === NodeTypes.ELEMENT)).toBe(true)
  1492. })
  1493. it('should remove whitespaces adjacent to comments', () => {
  1494. const ast = baseParse(`<div/> \n <!--foo--> <div/>`)
  1495. expect(ast.children.length).toBe(3)
  1496. expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
  1497. expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
  1498. expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
  1499. })
  1500. it('should remove whitespaces w/ newline between comments and elements', () => {
  1501. const ast = baseParse(`<div/> \n <!--foo--> \n <div/>`)
  1502. expect(ast.children.length).toBe(3)
  1503. expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
  1504. expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
  1505. expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
  1506. })
  1507. it('should NOT remove whitespaces w/ newline between interpolations', () => {
  1508. const ast = baseParse(`{{ foo }} \n {{ bar }}`)
  1509. expect(ast.children.length).toBe(3)
  1510. expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
  1511. expect(ast.children[1]).toMatchObject({
  1512. type: NodeTypes.TEXT,
  1513. content: ' '
  1514. })
  1515. expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
  1516. })
  1517. it('should NOT remove whitespaces w/o newline between elements', () => {
  1518. const ast = baseParse(`<div/> <div/> <div/>`)
  1519. expect(ast.children.length).toBe(5)
  1520. expect(ast.children.map(c => c.type)).toMatchObject([
  1521. NodeTypes.ELEMENT,
  1522. NodeTypes.TEXT,
  1523. NodeTypes.ELEMENT,
  1524. NodeTypes.TEXT,
  1525. NodeTypes.ELEMENT
  1526. ])
  1527. })
  1528. it('should condense consecutive whitespaces in text', () => {
  1529. const ast = baseParse(` foo \n bar baz `)
  1530. expect((ast.children[0] as TextNode).content).toBe(` foo bar baz `)
  1531. })
  1532. })
  1533. describe('Errors', () => {
  1534. const patterns: {
  1535. [key: string]: Array<{
  1536. code: string
  1537. errors: Array<{ type: ErrorCodes; loc: Position }>
  1538. options?: Partial<ParserOptions>
  1539. }>
  1540. } = {
  1541. ABRUPT_CLOSING_OF_EMPTY_COMMENT: [
  1542. {
  1543. code: '<template><!--></template>',
  1544. errors: [
  1545. {
  1546. type: ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  1547. loc: { offset: 10, line: 1, column: 11 }
  1548. }
  1549. ]
  1550. },
  1551. {
  1552. code: '<template><!---></template>',
  1553. errors: [
  1554. {
  1555. type: ErrorCodes.ABRUPT_CLOSING_OF_EMPTY_COMMENT,
  1556. loc: { offset: 10, line: 1, column: 11 }
  1557. }
  1558. ]
  1559. },
  1560. {
  1561. code: '<template><!----></template>',
  1562. errors: []
  1563. }
  1564. ],
  1565. ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE: [
  1566. {
  1567. code: '<template>&#a;</template>',
  1568. errors: [
  1569. {
  1570. type: ErrorCodes.ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE,
  1571. loc: { offset: 10, line: 1, column: 11 }
  1572. }
  1573. ]
  1574. },
  1575. {
  1576. code: '<template>&#xg;</template>',
  1577. errors: [
  1578. {
  1579. type: ErrorCodes.ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE,
  1580. loc: { offset: 10, line: 1, column: 11 }
  1581. }
  1582. ]
  1583. },
  1584. {
  1585. code: '<template>&#99;</template>',
  1586. errors: []
  1587. },
  1588. {
  1589. code: '<template>&#xff;</template>',
  1590. errors: []
  1591. },
  1592. {
  1593. code: '<template attr="&#a;"></template>',
  1594. errors: [
  1595. {
  1596. type: ErrorCodes.ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE,
  1597. loc: { offset: 16, line: 1, column: 17 }
  1598. }
  1599. ]
  1600. },
  1601. {
  1602. code: '<template attr="&#xg;"></template>',
  1603. errors: [
  1604. {
  1605. type: ErrorCodes.ABSENCE_OF_DIGITS_IN_NUMERIC_CHARACTER_REFERENCE,
  1606. loc: { offset: 16, line: 1, column: 17 }
  1607. }
  1608. ]
  1609. },
  1610. {
  1611. code: '<template attr="&#99;"></template>',
  1612. errors: []
  1613. },
  1614. {
  1615. code: '<template attr="&#xff;"></template>',
  1616. errors: []
  1617. }
  1618. ],
  1619. CDATA_IN_HTML_CONTENT: [
  1620. {
  1621. code: '<template><![CDATA[cdata]]></template>',
  1622. errors: [
  1623. {
  1624. type: ErrorCodes.CDATA_IN_HTML_CONTENT,
  1625. loc: { offset: 10, line: 1, column: 11 }
  1626. }
  1627. ]
  1628. },
  1629. {
  1630. code: '<template><svg><![CDATA[cdata]]></svg></template>',
  1631. errors: []
  1632. }
  1633. ],
  1634. CHARACTER_REFERENCE_OUTSIDE_UNICODE_RANGE: [
  1635. {
  1636. code: '<template>&#1234567;</template>',
  1637. errors: [
  1638. {
  1639. type: ErrorCodes.CHARACTER_REFERENCE_OUTSIDE_UNICODE_RANGE,
  1640. loc: { offset: 10, line: 1, column: 11 }
  1641. }
  1642. ]
  1643. }
  1644. ],
  1645. CONTROL_CHARACTER_REFERENCE: [
  1646. {
  1647. code: '<template>&#0003;</template>',
  1648. errors: [
  1649. {
  1650. type: ErrorCodes.CONTROL_CHARACTER_REFERENCE,
  1651. loc: { offset: 10, line: 1, column: 11 }
  1652. }
  1653. ]
  1654. },
  1655. {
  1656. code: '<template>&#x7F;</template>',
  1657. errors: [
  1658. {
  1659. type: ErrorCodes.CONTROL_CHARACTER_REFERENCE,
  1660. loc: { offset: 10, line: 1, column: 11 }
  1661. }
  1662. ]
  1663. }
  1664. ],
  1665. DUPLICATE_ATTRIBUTE: [
  1666. {
  1667. code: '<template><div id="" id=""></div></template>',
  1668. errors: [
  1669. {
  1670. type: ErrorCodes.DUPLICATE_ATTRIBUTE,
  1671. loc: { offset: 21, line: 1, column: 22 }
  1672. }
  1673. ]
  1674. }
  1675. ],
  1676. END_TAG_WITH_ATTRIBUTES: [
  1677. {
  1678. code: '<template><div></div id=""></template>',
  1679. errors: [
  1680. {
  1681. type: ErrorCodes.END_TAG_WITH_ATTRIBUTES,
  1682. loc: { offset: 21, line: 1, column: 22 }
  1683. }
  1684. ]
  1685. }
  1686. ],
  1687. END_TAG_WITH_TRAILING_SOLIDUS: [
  1688. {
  1689. code: '<template><div></div/></template>',
  1690. errors: [
  1691. {
  1692. type: ErrorCodes.END_TAG_WITH_TRAILING_SOLIDUS,
  1693. loc: { offset: 20, line: 1, column: 21 }
  1694. }
  1695. ]
  1696. }
  1697. ],
  1698. EOF_BEFORE_TAG_NAME: [
  1699. {
  1700. code: '<template><',
  1701. errors: [
  1702. {
  1703. type: ErrorCodes.EOF_BEFORE_TAG_NAME,
  1704. loc: { offset: 11, line: 1, column: 12 }
  1705. },
  1706. {
  1707. type: ErrorCodes.X_MISSING_END_TAG,
  1708. loc: { offset: 0, line: 1, column: 1 }
  1709. }
  1710. ]
  1711. },
  1712. {
  1713. code: '<template></',
  1714. errors: [
  1715. {
  1716. type: ErrorCodes.EOF_BEFORE_TAG_NAME,
  1717. loc: { offset: 12, line: 1, column: 13 }
  1718. },
  1719. {
  1720. type: ErrorCodes.X_MISSING_END_TAG,
  1721. loc: { offset: 0, line: 1, column: 1 }
  1722. }
  1723. ]
  1724. }
  1725. ],
  1726. EOF_IN_CDATA: [
  1727. {
  1728. code: '<template><svg><![CDATA[cdata',
  1729. errors: [
  1730. {
  1731. type: ErrorCodes.EOF_IN_CDATA,
  1732. loc: { offset: 29, line: 1, column: 30 }
  1733. },
  1734. {
  1735. type: ErrorCodes.X_MISSING_END_TAG,
  1736. loc: { offset: 10, line: 1, column: 11 }
  1737. },
  1738. {
  1739. type: ErrorCodes.X_MISSING_END_TAG,
  1740. loc: { offset: 0, line: 1, column: 1 }
  1741. }
  1742. ]
  1743. },
  1744. {
  1745. code: '<template><svg><![CDATA[',
  1746. errors: [
  1747. {
  1748. type: ErrorCodes.EOF_IN_CDATA,
  1749. loc: { offset: 24, line: 1, column: 25 }
  1750. },
  1751. {
  1752. type: ErrorCodes.X_MISSING_END_TAG,
  1753. loc: { offset: 10, line: 1, column: 11 }
  1754. },
  1755. {
  1756. type: ErrorCodes.X_MISSING_END_TAG,
  1757. loc: { offset: 0, line: 1, column: 1 }
  1758. }
  1759. ]
  1760. }
  1761. ],
  1762. EOF_IN_COMMENT: [
  1763. {
  1764. code: '<template><!--comment',
  1765. errors: [
  1766. {
  1767. type: ErrorCodes.EOF_IN_COMMENT,
  1768. loc: { offset: 21, line: 1, column: 22 }
  1769. },
  1770. {
  1771. type: ErrorCodes.X_MISSING_END_TAG,
  1772. loc: { offset: 0, line: 1, column: 1 }
  1773. }
  1774. ]
  1775. },
  1776. {
  1777. code: '<template><!--',
  1778. errors: [
  1779. {
  1780. type: ErrorCodes.EOF_IN_COMMENT,
  1781. loc: { offset: 14, line: 1, column: 15 }
  1782. },
  1783. {
  1784. type: ErrorCodes.X_MISSING_END_TAG,
  1785. loc: { offset: 0, line: 1, column: 1 }
  1786. }
  1787. ]
  1788. },
  1789. // Bogus comments don't throw eof-in-comment error.
  1790. // https://html.spec.whatwg.org/multipage/parsing.html#bogus-comment-state
  1791. {
  1792. code: '<template><!',
  1793. errors: [
  1794. {
  1795. type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  1796. loc: { offset: 10, line: 1, column: 11 }
  1797. },
  1798. {
  1799. type: ErrorCodes.X_MISSING_END_TAG,
  1800. loc: { offset: 0, line: 1, column: 1 }
  1801. }
  1802. ]
  1803. },
  1804. {
  1805. code: '<template><!-',
  1806. errors: [
  1807. {
  1808. type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  1809. loc: { offset: 10, line: 1, column: 11 }
  1810. },
  1811. {
  1812. type: ErrorCodes.X_MISSING_END_TAG,
  1813. loc: { offset: 0, line: 1, column: 1 }
  1814. }
  1815. ]
  1816. },
  1817. {
  1818. code: '<template><!abc',
  1819. errors: [
  1820. {
  1821. type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  1822. loc: { offset: 10, line: 1, column: 11 }
  1823. },
  1824. {
  1825. type: ErrorCodes.X_MISSING_END_TAG,
  1826. loc: { offset: 0, line: 1, column: 1 }
  1827. }
  1828. ]
  1829. }
  1830. ],
  1831. EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT: [
  1832. {
  1833. code: "<script><!--console.log('hello')",
  1834. errors: [
  1835. {
  1836. type: ErrorCodes.X_MISSING_END_TAG,
  1837. loc: { offset: 0, line: 1, column: 1 }
  1838. },
  1839. {
  1840. type: ErrorCodes.EOF_IN_SCRIPT_HTML_COMMENT_LIKE_TEXT,
  1841. loc: { offset: 32, line: 1, column: 33 }
  1842. }
  1843. ]
  1844. },
  1845. {
  1846. code: "<script>console.log('hello')",
  1847. errors: [
  1848. {
  1849. type: ErrorCodes.X_MISSING_END_TAG,
  1850. loc: { offset: 0, line: 1, column: 1 }
  1851. }
  1852. ]
  1853. }
  1854. ],
  1855. EOF_IN_TAG: [
  1856. {
  1857. code: '<template><div',
  1858. errors: [
  1859. {
  1860. type: ErrorCodes.EOF_IN_TAG,
  1861. loc: { offset: 14, line: 1, column: 15 }
  1862. },
  1863. {
  1864. type: ErrorCodes.X_MISSING_END_TAG,
  1865. loc: { offset: 10, line: 1, column: 11 }
  1866. },
  1867. {
  1868. type: ErrorCodes.X_MISSING_END_TAG,
  1869. loc: { offset: 0, line: 1, column: 1 }
  1870. }
  1871. ]
  1872. },
  1873. {
  1874. code: '<template><div ',
  1875. errors: [
  1876. {
  1877. type: ErrorCodes.EOF_IN_TAG,
  1878. loc: { offset: 15, line: 1, column: 16 }
  1879. },
  1880. {
  1881. type: ErrorCodes.X_MISSING_END_TAG,
  1882. loc: { offset: 10, line: 1, column: 11 }
  1883. },
  1884. {
  1885. type: ErrorCodes.X_MISSING_END_TAG,
  1886. loc: { offset: 0, line: 1, column: 1 }
  1887. }
  1888. ]
  1889. },
  1890. {
  1891. code: '<template><div id',
  1892. errors: [
  1893. {
  1894. type: ErrorCodes.EOF_IN_TAG,
  1895. loc: { offset: 17, line: 1, column: 18 }
  1896. },
  1897. {
  1898. type: ErrorCodes.X_MISSING_END_TAG,
  1899. loc: { offset: 10, line: 1, column: 11 }
  1900. },
  1901. {
  1902. type: ErrorCodes.X_MISSING_END_TAG,
  1903. loc: { offset: 0, line: 1, column: 1 }
  1904. }
  1905. ]
  1906. },
  1907. {
  1908. code: '<template><div id ',
  1909. errors: [
  1910. {
  1911. type: ErrorCodes.EOF_IN_TAG,
  1912. loc: { offset: 18, line: 1, column: 19 }
  1913. },
  1914. {
  1915. type: ErrorCodes.X_MISSING_END_TAG,
  1916. loc: { offset: 10, line: 1, column: 11 }
  1917. },
  1918. {
  1919. type: ErrorCodes.X_MISSING_END_TAG,
  1920. loc: { offset: 0, line: 1, column: 1 }
  1921. }
  1922. ]
  1923. },
  1924. {
  1925. code: '<template><div id =',
  1926. errors: [
  1927. {
  1928. type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  1929. loc: { offset: 19, line: 1, column: 20 }
  1930. },
  1931. {
  1932. type: ErrorCodes.EOF_IN_TAG,
  1933. loc: { offset: 19, line: 1, column: 20 }
  1934. },
  1935. {
  1936. type: ErrorCodes.X_MISSING_END_TAG,
  1937. loc: { offset: 10, line: 1, column: 11 }
  1938. },
  1939. {
  1940. type: ErrorCodes.X_MISSING_END_TAG,
  1941. loc: { offset: 0, line: 1, column: 1 }
  1942. }
  1943. ]
  1944. },
  1945. {
  1946. code: "<template><div id='abc",
  1947. errors: [
  1948. {
  1949. type: ErrorCodes.EOF_IN_TAG,
  1950. loc: { offset: 22, line: 1, column: 23 }
  1951. },
  1952. {
  1953. type: ErrorCodes.X_MISSING_END_TAG,
  1954. loc: { offset: 10, line: 1, column: 11 }
  1955. },
  1956. {
  1957. type: ErrorCodes.X_MISSING_END_TAG,
  1958. loc: { offset: 0, line: 1, column: 1 }
  1959. }
  1960. ]
  1961. },
  1962. {
  1963. code: '<template><div id="abc',
  1964. errors: [
  1965. {
  1966. type: ErrorCodes.EOF_IN_TAG,
  1967. loc: { offset: 22, line: 1, column: 23 }
  1968. },
  1969. {
  1970. type: ErrorCodes.X_MISSING_END_TAG,
  1971. loc: { offset: 10, line: 1, column: 11 }
  1972. },
  1973. {
  1974. type: ErrorCodes.X_MISSING_END_TAG,
  1975. loc: { offset: 0, line: 1, column: 1 }
  1976. }
  1977. ]
  1978. },
  1979. {
  1980. code: "<template><div id='abc'",
  1981. errors: [
  1982. {
  1983. type: ErrorCodes.EOF_IN_TAG,
  1984. loc: { offset: 23, line: 1, column: 24 }
  1985. },
  1986. {
  1987. type: ErrorCodes.X_MISSING_END_TAG,
  1988. loc: { offset: 10, line: 1, column: 11 }
  1989. },
  1990. {
  1991. type: ErrorCodes.X_MISSING_END_TAG,
  1992. loc: { offset: 0, line: 1, column: 1 }
  1993. }
  1994. ]
  1995. },
  1996. {
  1997. code: '<template><div id="abc"',
  1998. errors: [
  1999. {
  2000. type: ErrorCodes.EOF_IN_TAG,
  2001. loc: { offset: 23, line: 1, column: 24 }
  2002. },
  2003. {
  2004. type: ErrorCodes.X_MISSING_END_TAG,
  2005. loc: { offset: 10, line: 1, column: 11 }
  2006. },
  2007. {
  2008. type: ErrorCodes.X_MISSING_END_TAG,
  2009. loc: { offset: 0, line: 1, column: 1 }
  2010. }
  2011. ]
  2012. },
  2013. {
  2014. code: '<template><div id=abc',
  2015. errors: [
  2016. {
  2017. type: ErrorCodes.EOF_IN_TAG,
  2018. loc: { offset: 21, line: 1, column: 22 }
  2019. },
  2020. {
  2021. type: ErrorCodes.X_MISSING_END_TAG,
  2022. loc: { offset: 10, line: 1, column: 11 }
  2023. },
  2024. {
  2025. type: ErrorCodes.X_MISSING_END_TAG,
  2026. loc: { offset: 0, line: 1, column: 1 }
  2027. }
  2028. ]
  2029. },
  2030. {
  2031. code: "<template><div id='abc'/",
  2032. errors: [
  2033. {
  2034. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2035. loc: { offset: 23, line: 1, column: 24 }
  2036. },
  2037. {
  2038. type: ErrorCodes.EOF_IN_TAG,
  2039. loc: { offset: 24, line: 1, column: 25 }
  2040. },
  2041. {
  2042. type: ErrorCodes.X_MISSING_END_TAG,
  2043. loc: { offset: 10, line: 1, column: 11 }
  2044. },
  2045. {
  2046. type: ErrorCodes.X_MISSING_END_TAG,
  2047. loc: { offset: 0, line: 1, column: 1 }
  2048. }
  2049. ]
  2050. },
  2051. {
  2052. code: '<template><div id="abc"/',
  2053. errors: [
  2054. {
  2055. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2056. loc: { offset: 23, line: 1, column: 24 }
  2057. },
  2058. {
  2059. type: ErrorCodes.EOF_IN_TAG,
  2060. loc: { offset: 24, line: 1, column: 25 }
  2061. },
  2062. {
  2063. type: ErrorCodes.X_MISSING_END_TAG,
  2064. loc: { offset: 10, line: 1, column: 11 }
  2065. },
  2066. {
  2067. type: ErrorCodes.X_MISSING_END_TAG,
  2068. loc: { offset: 0, line: 1, column: 1 }
  2069. }
  2070. ]
  2071. },
  2072. {
  2073. code: '<template><div id=abc /',
  2074. errors: [
  2075. {
  2076. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2077. loc: { offset: 22, line: 1, column: 23 }
  2078. },
  2079. {
  2080. type: ErrorCodes.EOF_IN_TAG,
  2081. loc: { offset: 23, line: 1, column: 24 }
  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. INCORRECTLY_CLOSED_COMMENT: [
  2095. {
  2096. code: '<template><!--comment--!></template>',
  2097. errors: [
  2098. {
  2099. type: ErrorCodes.INCORRECTLY_CLOSED_COMMENT,
  2100. loc: { offset: 10, line: 1, column: 11 }
  2101. }
  2102. ]
  2103. }
  2104. ],
  2105. INCORRECTLY_OPENED_COMMENT: [
  2106. {
  2107. code: '<template><!></template>',
  2108. errors: [
  2109. {
  2110. type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2111. loc: { offset: 10, line: 1, column: 11 }
  2112. }
  2113. ]
  2114. },
  2115. {
  2116. code: '<template><!-></template>',
  2117. errors: [
  2118. {
  2119. type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2120. loc: { offset: 10, line: 1, column: 11 }
  2121. }
  2122. ]
  2123. },
  2124. {
  2125. code: '<template><!ELEMENT br EMPTY></template>',
  2126. errors: [
  2127. {
  2128. type: ErrorCodes.INCORRECTLY_OPENED_COMMENT,
  2129. loc: { offset: 10, line: 1, column: 11 }
  2130. }
  2131. ]
  2132. },
  2133. // Just ignore doctype.
  2134. {
  2135. code: '<!DOCTYPE html>',
  2136. errors: []
  2137. }
  2138. ],
  2139. INVALID_FIRST_CHARACTER_OF_TAG_NAME: [
  2140. {
  2141. code: '<template>a < b</template>',
  2142. errors: [
  2143. {
  2144. type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2145. loc: { offset: 13, line: 1, column: 14 }
  2146. }
  2147. ]
  2148. },
  2149. {
  2150. code: '<template><�></template>',
  2151. errors: [
  2152. {
  2153. type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2154. loc: { offset: 11, line: 1, column: 12 }
  2155. }
  2156. ]
  2157. },
  2158. {
  2159. code: '<template>a </ b</template>',
  2160. errors: [
  2161. {
  2162. type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2163. loc: { offset: 14, line: 1, column: 15 }
  2164. },
  2165. {
  2166. type: ErrorCodes.X_MISSING_END_TAG,
  2167. loc: { offset: 0, line: 1, column: 1 }
  2168. }
  2169. ]
  2170. },
  2171. {
  2172. code: '<template></�></template>',
  2173. errors: [
  2174. {
  2175. type: ErrorCodes.INVALID_FIRST_CHARACTER_OF_TAG_NAME,
  2176. loc: { offset: 12, line: 1, column: 13 }
  2177. }
  2178. ]
  2179. },
  2180. // Don't throw invalid-first-character-of-tag-name in interpolation
  2181. {
  2182. code: '<template>{{a < b}}</template>',
  2183. errors: []
  2184. }
  2185. ],
  2186. MISSING_ATTRIBUTE_VALUE: [
  2187. {
  2188. code: '<template><div id=></div></template>',
  2189. errors: [
  2190. {
  2191. type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2192. loc: { offset: 18, line: 1, column: 19 }
  2193. }
  2194. ]
  2195. },
  2196. {
  2197. code: '<template><div id= ></div></template>',
  2198. errors: [
  2199. {
  2200. type: ErrorCodes.MISSING_ATTRIBUTE_VALUE,
  2201. loc: { offset: 19, line: 1, column: 20 }
  2202. }
  2203. ]
  2204. },
  2205. {
  2206. code: '<template><div id= /></div></template>',
  2207. errors: []
  2208. }
  2209. ],
  2210. MISSING_END_TAG_NAME: [
  2211. {
  2212. code: '<template></></template>',
  2213. errors: [
  2214. {
  2215. type: ErrorCodes.MISSING_END_TAG_NAME,
  2216. loc: { offset: 12, line: 1, column: 13 }
  2217. }
  2218. ]
  2219. }
  2220. ],
  2221. MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE: [
  2222. {
  2223. code: '<template>&amp</template>',
  2224. options: { namedCharacterReferences: { amp: '&' } },
  2225. errors: [
  2226. {
  2227. type: ErrorCodes.MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE,
  2228. loc: { offset: 14, line: 1, column: 15 }
  2229. }
  2230. ]
  2231. },
  2232. {
  2233. code: '<template>&#40</template>',
  2234. errors: [
  2235. {
  2236. type: ErrorCodes.MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE,
  2237. loc: { offset: 14, line: 1, column: 15 }
  2238. }
  2239. ]
  2240. },
  2241. {
  2242. code: '<template>&#x40</template>',
  2243. errors: [
  2244. {
  2245. type: ErrorCodes.MISSING_SEMICOLON_AFTER_CHARACTER_REFERENCE,
  2246. loc: { offset: 15, line: 1, column: 16 }
  2247. }
  2248. ]
  2249. }
  2250. ],
  2251. MISSING_WHITESPACE_BETWEEN_ATTRIBUTES: [
  2252. {
  2253. code: '<template><div id="foo"class="bar"></div></template>',
  2254. errors: [
  2255. {
  2256. type: ErrorCodes.MISSING_WHITESPACE_BETWEEN_ATTRIBUTES,
  2257. loc: { offset: 23, line: 1, column: 24 }
  2258. }
  2259. ]
  2260. },
  2261. // CR doesn't appear in tokenization phase, but all CR are removed in preprocessing.
  2262. // https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream
  2263. {
  2264. code: '<template><div id="foo"\r\nclass="bar"></div></template>',
  2265. errors: []
  2266. }
  2267. ],
  2268. NESTED_COMMENT: [
  2269. {
  2270. code: '<template><!--a<!--b--></template>',
  2271. errors: [
  2272. {
  2273. type: ErrorCodes.NESTED_COMMENT,
  2274. loc: { offset: 15, line: 1, column: 16 }
  2275. }
  2276. ]
  2277. },
  2278. {
  2279. code: '<template><!--a<!--b<!--c--></template>',
  2280. errors: [
  2281. {
  2282. type: ErrorCodes.NESTED_COMMENT,
  2283. loc: { offset: 15, line: 1, column: 16 }
  2284. },
  2285. {
  2286. type: ErrorCodes.NESTED_COMMENT,
  2287. loc: { offset: 20, line: 1, column: 21 }
  2288. }
  2289. ]
  2290. },
  2291. {
  2292. code: '<template><!--a<!--></template>',
  2293. errors: []
  2294. },
  2295. {
  2296. code: '<template><!--a<!--',
  2297. errors: [
  2298. {
  2299. type: ErrorCodes.EOF_IN_COMMENT,
  2300. loc: { offset: 19, line: 1, column: 20 }
  2301. },
  2302. {
  2303. type: ErrorCodes.X_MISSING_END_TAG,
  2304. loc: { offset: 0, line: 1, column: 1 }
  2305. }
  2306. ]
  2307. }
  2308. ],
  2309. NONCHARACTER_CHARACTER_REFERENCE: [
  2310. {
  2311. code: '<template>&#xFFFE;</template>',
  2312. errors: [
  2313. {
  2314. type: ErrorCodes.NONCHARACTER_CHARACTER_REFERENCE,
  2315. loc: { offset: 10, line: 1, column: 11 }
  2316. }
  2317. ]
  2318. },
  2319. {
  2320. code: '<template>&#x1FFFF;</template>',
  2321. errors: [
  2322. {
  2323. type: ErrorCodes.NONCHARACTER_CHARACTER_REFERENCE,
  2324. loc: { offset: 10, line: 1, column: 11 }
  2325. }
  2326. ]
  2327. }
  2328. ],
  2329. NULL_CHARACTER_REFERENCE: [
  2330. {
  2331. code: '<template>&#0000;</template>',
  2332. errors: [
  2333. {
  2334. type: ErrorCodes.NULL_CHARACTER_REFERENCE,
  2335. loc: { offset: 10, line: 1, column: 11 }
  2336. }
  2337. ]
  2338. }
  2339. ],
  2340. SURROGATE_CHARACTER_REFERENCE: [
  2341. {
  2342. code: '<template>&#xD800;</template>',
  2343. errors: [
  2344. {
  2345. type: ErrorCodes.SURROGATE_CHARACTER_REFERENCE,
  2346. loc: { offset: 10, line: 1, column: 11 }
  2347. }
  2348. ]
  2349. }
  2350. ],
  2351. UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME: [
  2352. {
  2353. code: "<template><div a\"bc=''></div></template>",
  2354. errors: [
  2355. {
  2356. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  2357. loc: { offset: 16, line: 1, column: 17 }
  2358. }
  2359. ]
  2360. },
  2361. {
  2362. code: "<template><div a'bc=''></div></template>",
  2363. errors: [
  2364. {
  2365. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  2366. loc: { offset: 16, line: 1, column: 17 }
  2367. }
  2368. ]
  2369. },
  2370. {
  2371. code: "<template><div a<bc=''></div></template>",
  2372. errors: [
  2373. {
  2374. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_ATTRIBUTE_NAME,
  2375. loc: { offset: 16, line: 1, column: 17 }
  2376. }
  2377. ]
  2378. }
  2379. ],
  2380. UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE: [
  2381. {
  2382. code: '<template><div foo=bar"></div></template>',
  2383. errors: [
  2384. {
  2385. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  2386. loc: { offset: 22, line: 1, column: 23 }
  2387. }
  2388. ]
  2389. },
  2390. {
  2391. code: "<template><div foo=bar'></div></template>",
  2392. errors: [
  2393. {
  2394. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  2395. loc: { offset: 22, line: 1, column: 23 }
  2396. }
  2397. ]
  2398. },
  2399. {
  2400. code: '<template><div foo=bar<div></div></template>',
  2401. errors: [
  2402. {
  2403. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  2404. loc: { offset: 22, line: 1, column: 23 }
  2405. }
  2406. ]
  2407. },
  2408. {
  2409. code: '<template><div foo=bar=baz></div></template>',
  2410. errors: [
  2411. {
  2412. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  2413. loc: { offset: 22, line: 1, column: 23 }
  2414. }
  2415. ]
  2416. },
  2417. {
  2418. code: '<template><div foo=bar`></div></template>',
  2419. errors: [
  2420. {
  2421. type: ErrorCodes.UNEXPECTED_CHARACTER_IN_UNQUOTED_ATTRIBUTE_VALUE,
  2422. loc: { offset: 22, line: 1, column: 23 }
  2423. }
  2424. ]
  2425. }
  2426. ],
  2427. UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME: [
  2428. {
  2429. code: '<template><div =foo=bar></div></template>',
  2430. errors: [
  2431. {
  2432. type: ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  2433. loc: { offset: 15, line: 1, column: 16 }
  2434. }
  2435. ]
  2436. },
  2437. {
  2438. code: '<template><div =></div></template>',
  2439. errors: [
  2440. {
  2441. type: ErrorCodes.UNEXPECTED_EQUALS_SIGN_BEFORE_ATTRIBUTE_NAME,
  2442. loc: { offset: 15, line: 1, column: 16 }
  2443. }
  2444. ]
  2445. }
  2446. ],
  2447. UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME: [
  2448. {
  2449. code: '<template><?xml?></template>',
  2450. errors: [
  2451. {
  2452. type: ErrorCodes.UNEXPECTED_QUESTION_MARK_INSTEAD_OF_TAG_NAME,
  2453. loc: { offset: 11, line: 1, column: 12 }
  2454. }
  2455. ]
  2456. }
  2457. ],
  2458. UNEXPECTED_SOLIDUS_IN_TAG: [
  2459. {
  2460. code: '<template><div a/b></div></template>',
  2461. errors: [
  2462. {
  2463. type: ErrorCodes.UNEXPECTED_SOLIDUS_IN_TAG,
  2464. loc: { offset: 16, line: 1, column: 17 }
  2465. }
  2466. ]
  2467. }
  2468. ],
  2469. X_INVALID_END_TAG: [
  2470. {
  2471. code: '<template></div></template>',
  2472. errors: [
  2473. {
  2474. type: ErrorCodes.X_INVALID_END_TAG,
  2475. loc: { offset: 10, line: 1, column: 11 }
  2476. }
  2477. ]
  2478. },
  2479. {
  2480. code: '<template></div></div></template>',
  2481. errors: [
  2482. {
  2483. type: ErrorCodes.X_INVALID_END_TAG,
  2484. loc: { offset: 10, line: 1, column: 11 }
  2485. },
  2486. {
  2487. type: ErrorCodes.X_INVALID_END_TAG,
  2488. loc: { offset: 16, line: 1, column: 17 }
  2489. }
  2490. ]
  2491. },
  2492. {
  2493. code: "<template>{{'</div>'}}</template>",
  2494. errors: []
  2495. },
  2496. {
  2497. code: '<textarea></div></textarea>',
  2498. errors: []
  2499. },
  2500. {
  2501. code: '<svg><![CDATA[</div>]]></svg>',
  2502. errors: []
  2503. },
  2504. {
  2505. code: '<svg><!--</div>--></svg>',
  2506. errors: []
  2507. }
  2508. ],
  2509. X_MISSING_END_TAG: [
  2510. {
  2511. code: '<template><div></template>',
  2512. errors: [
  2513. {
  2514. type: ErrorCodes.X_MISSING_END_TAG,
  2515. loc: { offset: 10, line: 1, column: 11 }
  2516. }
  2517. ]
  2518. },
  2519. {
  2520. code: '<template><div>',
  2521. errors: [
  2522. {
  2523. type: ErrorCodes.X_MISSING_END_TAG,
  2524. loc: { offset: 10, line: 1, column: 11 }
  2525. },
  2526. {
  2527. type: ErrorCodes.X_MISSING_END_TAG,
  2528. loc: { offset: 0, line: 1, column: 1 }
  2529. }
  2530. ]
  2531. }
  2532. ],
  2533. X_MISSING_INTERPOLATION_END: [
  2534. {
  2535. code: '{{ foo',
  2536. errors: [
  2537. {
  2538. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  2539. loc: { offset: 0, line: 1, column: 1 }
  2540. }
  2541. ]
  2542. },
  2543. {
  2544. code: '{{',
  2545. errors: [
  2546. {
  2547. type: ErrorCodes.X_MISSING_INTERPOLATION_END,
  2548. loc: { offset: 0, line: 1, column: 1 }
  2549. }
  2550. ]
  2551. },
  2552. {
  2553. code: '{{}}',
  2554. errors: []
  2555. }
  2556. ],
  2557. X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END: [
  2558. {
  2559. code: `<div v-foo:[sef fsef] />`,
  2560. errors: [
  2561. {
  2562. type: ErrorCodes.X_MISSING_DYNAMIC_DIRECTIVE_ARGUMENT_END,
  2563. loc: { offset: 15, line: 1, column: 16 }
  2564. }
  2565. ]
  2566. }
  2567. ]
  2568. }
  2569. for (const key of Object.keys(patterns) as (keyof (typeof patterns))[]) {
  2570. describe(key, () => {
  2571. for (const { code, errors, options } of patterns[key]) {
  2572. test(
  2573. code.replace(
  2574. /[\r\n]/g,
  2575. c => `\\x0${c.codePointAt(0)!.toString(16)};`
  2576. ),
  2577. () => {
  2578. const spy = jest.fn()
  2579. const ast = baseParse(code, {
  2580. getNamespace: (tag, parent) => {
  2581. const ns = parent ? parent.ns : Namespaces.HTML
  2582. if (ns === Namespaces.HTML) {
  2583. if (tag === 'svg') {
  2584. return (Namespaces.HTML + 1) as any
  2585. }
  2586. }
  2587. return ns
  2588. },
  2589. getTextMode: tag => {
  2590. if (tag === 'textarea') {
  2591. return TextModes.RCDATA
  2592. }
  2593. if (tag === 'script') {
  2594. return TextModes.RAWTEXT
  2595. }
  2596. return TextModes.DATA
  2597. },
  2598. ...options,
  2599. onError: spy
  2600. })
  2601. expect(
  2602. spy.mock.calls.map(([err]) => ({
  2603. type: err.code,
  2604. loc: err.loc.start
  2605. }))
  2606. ).toMatchObject(errors)
  2607. expect(ast).toMatchSnapshot()
  2608. }
  2609. )
  2610. }
  2611. })
  2612. }
  2613. })
  2614. })