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