index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. 'use strict';
  2. var t = require('@babel/types');
  3. var entities = require('entities');
  4. function _interopNamespace(e) {
  5. if (e && e.__esModule) return e;
  6. var n = Object.create(null);
  7. if (e) {
  8. Object.keys(e).forEach(function (k) {
  9. if (k !== 'default') {
  10. var d = Object.getOwnPropertyDescriptor(e, k);
  11. Object.defineProperty(n, k, d.get ? d : {
  12. enumerable: true,
  13. get: function () { return e[k]; }
  14. });
  15. }
  16. });
  17. }
  18. n["default"] = e;
  19. return Object.freeze(n);
  20. }
  21. var t__namespace = /*#__PURE__*/_interopNamespace(t);
  22. const one = (h, node, parent) => {
  23. const type = node && node.type;
  24. const fn = h.handlers[type];
  25. if (!type) {
  26. throw new Error(`Expected node, got \`${node}\``);
  27. }
  28. if (!fn) {
  29. throw new Error(`Node of type ${type} is unknown`);
  30. }
  31. return fn(h, node, parent);
  32. };
  33. const all = (helpers, parent) => {
  34. const nodes = parent.children || [];
  35. const { length } = nodes;
  36. const values = [];
  37. let index = -1;
  38. while (++index < length) {
  39. const node = nodes[index];
  40. if (typeof node !== "string") {
  41. const result = one(helpers, node, parent);
  42. values.push(result);
  43. }
  44. }
  45. return values.filter(Boolean);
  46. };
  47. const isNumeric = (value) => {
  48. return !Number.isNaN(value - parseFloat(value));
  49. };
  50. const hyphenToCamelCase = (string) => {
  51. return string.replace(/-(.)/g, (_, chr) => chr.toUpperCase());
  52. };
  53. const trimEnd = (haystack, needle) => {
  54. return haystack.endsWith(needle) ? haystack.slice(0, -needle.length) : haystack;
  55. };
  56. const KEBAB_REGEX = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g;
  57. const kebabCase = (str) => {
  58. return str.replace(KEBAB_REGEX, (match) => `-${match.toLowerCase()}`);
  59. };
  60. const SPACES_REGEXP = /[\t\r\n\u0085\u2028\u2029]+/g;
  61. const replaceSpaces = (str) => {
  62. return str.replace(SPACES_REGEXP, " ");
  63. };
  64. const PX_REGEX = /^\d+px$/;
  65. const MS_REGEX = /^-ms-/;
  66. const VAR_REGEX = /^--/;
  67. const isConvertiblePixelValue = (value) => {
  68. return PX_REGEX.test(value);
  69. };
  70. const formatKey = (key) => {
  71. if (VAR_REGEX.test(key)) {
  72. return t__namespace.stringLiteral(key);
  73. }
  74. key = key.toLowerCase();
  75. if (MS_REGEX.test(key))
  76. key = key.substr(1);
  77. return t__namespace.identifier(hyphenToCamelCase(key));
  78. };
  79. const formatValue = (value) => {
  80. if (isNumeric(value))
  81. return t__namespace.numericLiteral(Number(value));
  82. if (isConvertiblePixelValue(value))
  83. return t__namespace.numericLiteral(Number(trimEnd(value, "px")));
  84. return t__namespace.stringLiteral(value);
  85. };
  86. const stringToObjectStyle = (rawStyle) => {
  87. const entries = rawStyle.split(";");
  88. const properties = [];
  89. let index = -1;
  90. while (++index < entries.length) {
  91. const entry = entries[index];
  92. const style = entry.trim();
  93. const firstColon = style.indexOf(":");
  94. const value = style.substr(firstColon + 1).trim();
  95. const key = style.substr(0, firstColon);
  96. if (key !== "") {
  97. const property = t__namespace.objectProperty(formatKey(key), formatValue(value));
  98. properties.push(property);
  99. }
  100. }
  101. return t__namespace.objectExpression(properties);
  102. };
  103. const ATTRIBUTE_MAPPING = {
  104. accept: "accept",
  105. acceptcharset: "acceptCharset",
  106. "accept-charset": "acceptCharset",
  107. accesskey: "accessKey",
  108. action: "action",
  109. allowfullscreen: "allowFullScreen",
  110. alt: "alt",
  111. as: "as",
  112. async: "async",
  113. autocapitalize: "autoCapitalize",
  114. autocomplete: "autoComplete",
  115. autocorrect: "autoCorrect",
  116. autofocus: "autoFocus",
  117. autoplay: "autoPlay",
  118. autosave: "autoSave",
  119. capture: "capture",
  120. cellpadding: "cellPadding",
  121. cellspacing: "cellSpacing",
  122. challenge: "challenge",
  123. charset: "charSet",
  124. checked: "checked",
  125. children: "children",
  126. cite: "cite",
  127. class: "className",
  128. classid: "classID",
  129. classname: "className",
  130. cols: "cols",
  131. colspan: "colSpan",
  132. content: "content",
  133. contenteditable: "contentEditable",
  134. contextmenu: "contextMenu",
  135. controls: "controls",
  136. controlslist: "controlsList",
  137. coords: "coords",
  138. crossorigin: "crossOrigin",
  139. dangerouslysetinnerhtml: "dangerouslySetInnerHTML",
  140. data: "data",
  141. datetime: "dateTime",
  142. default: "default",
  143. defaultchecked: "defaultChecked",
  144. defaultvalue: "defaultValue",
  145. defer: "defer",
  146. dir: "dir",
  147. disabled: "disabled",
  148. download: "download",
  149. draggable: "draggable",
  150. enctype: "encType",
  151. for: "htmlFor",
  152. form: "form",
  153. formmethod: "formMethod",
  154. formaction: "formAction",
  155. formenctype: "formEncType",
  156. formnovalidate: "formNoValidate",
  157. formtarget: "formTarget",
  158. frameborder: "frameBorder",
  159. headers: "headers",
  160. height: "height",
  161. hidden: "hidden",
  162. high: "high",
  163. href: "href",
  164. hreflang: "hrefLang",
  165. htmlfor: "htmlFor",
  166. httpequiv: "httpEquiv",
  167. "http-equiv": "httpEquiv",
  168. icon: "icon",
  169. id: "id",
  170. innerhtml: "innerHTML",
  171. inputmode: "inputMode",
  172. integrity: "integrity",
  173. is: "is",
  174. itemid: "itemID",
  175. itemprop: "itemProp",
  176. itemref: "itemRef",
  177. itemscope: "itemScope",
  178. itemtype: "itemType",
  179. keyparams: "keyParams",
  180. keytype: "keyType",
  181. kind: "kind",
  182. label: "label",
  183. lang: "lang",
  184. list: "list",
  185. loop: "loop",
  186. low: "low",
  187. manifest: "manifest",
  188. marginwidth: "marginWidth",
  189. marginheight: "marginHeight",
  190. max: "max",
  191. maxlength: "maxLength",
  192. media: "media",
  193. mediagroup: "mediaGroup",
  194. method: "method",
  195. min: "min",
  196. minlength: "minLength",
  197. multiple: "multiple",
  198. muted: "muted",
  199. name: "name",
  200. nomodule: "noModule",
  201. nonce: "nonce",
  202. novalidate: "noValidate",
  203. open: "open",
  204. optimum: "optimum",
  205. pattern: "pattern",
  206. placeholder: "placeholder",
  207. playsinline: "playsInline",
  208. poster: "poster",
  209. preload: "preload",
  210. profile: "profile",
  211. radiogroup: "radioGroup",
  212. readonly: "readOnly",
  213. referrerpolicy: "referrerPolicy",
  214. rel: "rel",
  215. required: "required",
  216. reversed: "reversed",
  217. role: "role",
  218. rows: "rows",
  219. rowspan: "rowSpan",
  220. sandbox: "sandbox",
  221. scope: "scope",
  222. scoped: "scoped",
  223. scrolling: "scrolling",
  224. seamless: "seamless",
  225. selected: "selected",
  226. shape: "shape",
  227. size: "size",
  228. sizes: "sizes",
  229. span: "span",
  230. spellcheck: "spellCheck",
  231. src: "src",
  232. srcdoc: "srcDoc",
  233. srclang: "srcLang",
  234. srcset: "srcSet",
  235. start: "start",
  236. step: "step",
  237. style: "style",
  238. summary: "summary",
  239. tabindex: "tabIndex",
  240. target: "target",
  241. title: "title",
  242. type: "type",
  243. usemap: "useMap",
  244. value: "value",
  245. width: "width",
  246. wmode: "wmode",
  247. wrap: "wrap",
  248. about: "about",
  249. accentheight: "accentHeight",
  250. "accent-height": "accentHeight",
  251. accumulate: "accumulate",
  252. additive: "additive",
  253. alignmentbaseline: "alignmentBaseline",
  254. "alignment-baseline": "alignmentBaseline",
  255. allowreorder: "allowReorder",
  256. alphabetic: "alphabetic",
  257. amplitude: "amplitude",
  258. arabicform: "arabicForm",
  259. "arabic-form": "arabicForm",
  260. ascent: "ascent",
  261. attributename: "attributeName",
  262. attributetype: "attributeType",
  263. autoreverse: "autoReverse",
  264. azimuth: "azimuth",
  265. basefrequency: "baseFrequency",
  266. baselineshift: "baselineShift",
  267. "baseline-shift": "baselineShift",
  268. baseprofile: "baseProfile",
  269. bbox: "bbox",
  270. begin: "begin",
  271. bias: "bias",
  272. by: "by",
  273. calcmode: "calcMode",
  274. capheight: "capHeight",
  275. "cap-height": "capHeight",
  276. clip: "clip",
  277. clippath: "clipPath",
  278. "clip-path": "clipPath",
  279. clippathunits: "clipPathUnits",
  280. cliprule: "clipRule",
  281. "clip-rule": "clipRule",
  282. color: "color",
  283. colorinterpolation: "colorInterpolation",
  284. "color-interpolation": "colorInterpolation",
  285. colorinterpolationfilters: "colorInterpolationFilters",
  286. "color-interpolation-filters": "colorInterpolationFilters",
  287. colorprofile: "colorProfile",
  288. "color-profile": "colorProfile",
  289. colorrendering: "colorRendering",
  290. "color-rendering": "colorRendering",
  291. contentscripttype: "contentScriptType",
  292. contentstyletype: "contentStyleType",
  293. cursor: "cursor",
  294. cx: "cx",
  295. cy: "cy",
  296. d: "d",
  297. datatype: "datatype",
  298. decelerate: "decelerate",
  299. descent: "descent",
  300. diffuseconstant: "diffuseConstant",
  301. direction: "direction",
  302. display: "display",
  303. divisor: "divisor",
  304. dominantbaseline: "dominantBaseline",
  305. "dominant-baseline": "dominantBaseline",
  306. dur: "dur",
  307. dx: "dx",
  308. dy: "dy",
  309. edgemode: "edgeMode",
  310. elevation: "elevation",
  311. enablebackground: "enableBackground",
  312. "enable-background": "enableBackground",
  313. end: "end",
  314. exponent: "exponent",
  315. externalresourcesrequired: "externalResourcesRequired",
  316. fill: "fill",
  317. fillopacity: "fillOpacity",
  318. "fill-opacity": "fillOpacity",
  319. fillrule: "fillRule",
  320. "fill-rule": "fillRule",
  321. filter: "filter",
  322. filterres: "filterRes",
  323. filterunits: "filterUnits",
  324. floodopacity: "floodOpacity",
  325. "flood-opacity": "floodOpacity",
  326. floodcolor: "floodColor",
  327. "flood-color": "floodColor",
  328. focusable: "focusable",
  329. fontfamily: "fontFamily",
  330. "font-family": "fontFamily",
  331. fontsize: "fontSize",
  332. "font-size": "fontSize",
  333. fontsizeadjust: "fontSizeAdjust",
  334. "font-size-adjust": "fontSizeAdjust",
  335. fontstretch: "fontStretch",
  336. "font-stretch": "fontStretch",
  337. fontstyle: "fontStyle",
  338. "font-style": "fontStyle",
  339. fontvariant: "fontVariant",
  340. "font-variant": "fontVariant",
  341. fontweight: "fontWeight",
  342. "font-weight": "fontWeight",
  343. format: "format",
  344. from: "from",
  345. fx: "fx",
  346. fy: "fy",
  347. g1: "g1",
  348. g2: "g2",
  349. glyphname: "glyphName",
  350. "glyph-name": "glyphName",
  351. glyphorientationhorizontal: "glyphOrientationHorizontal",
  352. "glyph-orientation-horizontal": "glyphOrientationHorizontal",
  353. glyphorientationvertical: "glyphOrientationVertical",
  354. "glyph-orientation-vertical": "glyphOrientationVertical",
  355. glyphref: "glyphRef",
  356. gradienttransform: "gradientTransform",
  357. gradientunits: "gradientUnits",
  358. hanging: "hanging",
  359. horizadvx: "horizAdvX",
  360. "horiz-adv-x": "horizAdvX",
  361. horizoriginx: "horizOriginX",
  362. "horiz-origin-x": "horizOriginX",
  363. ideographic: "ideographic",
  364. imagerendering: "imageRendering",
  365. "image-rendering": "imageRendering",
  366. in2: "in2",
  367. in: "in",
  368. inlist: "inlist",
  369. intercept: "intercept",
  370. k1: "k1",
  371. k2: "k2",
  372. k3: "k3",
  373. k4: "k4",
  374. k: "k",
  375. kernelmatrix: "kernelMatrix",
  376. kernelunitlength: "kernelUnitLength",
  377. kerning: "kerning",
  378. keypoints: "keyPoints",
  379. keysplines: "keySplines",
  380. keytimes: "keyTimes",
  381. lengthadjust: "lengthAdjust",
  382. letterspacing: "letterSpacing",
  383. "letter-spacing": "letterSpacing",
  384. lightingcolor: "lightingColor",
  385. "lighting-color": "lightingColor",
  386. limitingconeangle: "limitingConeAngle",
  387. local: "local",
  388. markerend: "markerEnd",
  389. "marker-end": "markerEnd",
  390. markerheight: "markerHeight",
  391. markermid: "markerMid",
  392. "marker-mid": "markerMid",
  393. markerstart: "markerStart",
  394. "marker-start": "markerStart",
  395. markerunits: "markerUnits",
  396. markerwidth: "markerWidth",
  397. mask: "mask",
  398. maskcontentunits: "maskContentUnits",
  399. maskunits: "maskUnits",
  400. mathematical: "mathematical",
  401. mode: "mode",
  402. numoctaves: "numOctaves",
  403. offset: "offset",
  404. opacity: "opacity",
  405. operator: "operator",
  406. order: "order",
  407. orient: "orient",
  408. orientation: "orientation",
  409. origin: "origin",
  410. overflow: "overflow",
  411. overlineposition: "overlinePosition",
  412. "overline-position": "overlinePosition",
  413. overlinethickness: "overlineThickness",
  414. "overline-thickness": "overlineThickness",
  415. paintorder: "paintOrder",
  416. "paint-order": "paintOrder",
  417. panose1: "panose1",
  418. "panose-1": "panose1",
  419. pathlength: "pathLength",
  420. patterncontentunits: "patternContentUnits",
  421. patterntransform: "patternTransform",
  422. patternunits: "patternUnits",
  423. pointerevents: "pointerEvents",
  424. "pointer-events": "pointerEvents",
  425. points: "points",
  426. pointsatx: "pointsAtX",
  427. pointsaty: "pointsAtY",
  428. pointsatz: "pointsAtZ",
  429. prefix: "prefix",
  430. preservealpha: "preserveAlpha",
  431. preserveaspectratio: "preserveAspectRatio",
  432. primitiveunits: "primitiveUnits",
  433. property: "property",
  434. r: "r",
  435. radius: "radius",
  436. refx: "refX",
  437. refy: "refY",
  438. renderingintent: "renderingIntent",
  439. "rendering-intent": "renderingIntent",
  440. repeatcount: "repeatCount",
  441. repeatdur: "repeatDur",
  442. requiredextensions: "requiredExtensions",
  443. requiredfeatures: "requiredFeatures",
  444. resource: "resource",
  445. restart: "restart",
  446. result: "result",
  447. results: "results",
  448. rotate: "rotate",
  449. rx: "rx",
  450. ry: "ry",
  451. scale: "scale",
  452. security: "security",
  453. seed: "seed",
  454. shaperendering: "shapeRendering",
  455. "shape-rendering": "shapeRendering",
  456. slope: "slope",
  457. spacing: "spacing",
  458. specularconstant: "specularConstant",
  459. specularexponent: "specularExponent",
  460. speed: "speed",
  461. spreadmethod: "spreadMethod",
  462. startoffset: "startOffset",
  463. stddeviation: "stdDeviation",
  464. stemh: "stemh",
  465. stemv: "stemv",
  466. stitchtiles: "stitchTiles",
  467. stopcolor: "stopColor",
  468. "stop-color": "stopColor",
  469. stopopacity: "stopOpacity",
  470. "stop-opacity": "stopOpacity",
  471. strikethroughposition: "strikethroughPosition",
  472. "strikethrough-position": "strikethroughPosition",
  473. strikethroughthickness: "strikethroughThickness",
  474. "strikethrough-thickness": "strikethroughThickness",
  475. string: "string",
  476. stroke: "stroke",
  477. strokedasharray: "strokeDasharray",
  478. "stroke-dasharray": "strokeDasharray",
  479. strokedashoffset: "strokeDashoffset",
  480. "stroke-dashoffset": "strokeDashoffset",
  481. strokelinecap: "strokeLinecap",
  482. "stroke-linecap": "strokeLinecap",
  483. strokelinejoin: "strokeLinejoin",
  484. "stroke-linejoin": "strokeLinejoin",
  485. strokemiterlimit: "strokeMiterlimit",
  486. "stroke-miterlimit": "strokeMiterlimit",
  487. strokewidth: "strokeWidth",
  488. "stroke-width": "strokeWidth",
  489. strokeopacity: "strokeOpacity",
  490. "stroke-opacity": "strokeOpacity",
  491. suppresscontenteditablewarning: "suppressContentEditableWarning",
  492. suppresshydrationwarning: "suppressHydrationWarning",
  493. surfacescale: "surfaceScale",
  494. systemlanguage: "systemLanguage",
  495. tablevalues: "tableValues",
  496. targetx: "targetX",
  497. targety: "targetY",
  498. textanchor: "textAnchor",
  499. "text-anchor": "textAnchor",
  500. textdecoration: "textDecoration",
  501. "text-decoration": "textDecoration",
  502. textlength: "textLength",
  503. textrendering: "textRendering",
  504. "text-rendering": "textRendering",
  505. to: "to",
  506. transform: "transform",
  507. typeof: "typeof",
  508. u1: "u1",
  509. u2: "u2",
  510. underlineposition: "underlinePosition",
  511. "underline-position": "underlinePosition",
  512. underlinethickness: "underlineThickness",
  513. "underline-thickness": "underlineThickness",
  514. unicode: "unicode",
  515. unicodebidi: "unicodeBidi",
  516. "unicode-bidi": "unicodeBidi",
  517. unicoderange: "unicodeRange",
  518. "unicode-range": "unicodeRange",
  519. unitsperem: "unitsPerEm",
  520. "units-per-em": "unitsPerEm",
  521. unselectable: "unselectable",
  522. valphabetic: "vAlphabetic",
  523. "v-alphabetic": "vAlphabetic",
  524. values: "values",
  525. vectoreffect: "vectorEffect",
  526. "vector-effect": "vectorEffect",
  527. version: "version",
  528. vertadvy: "vertAdvY",
  529. "vert-adv-y": "vertAdvY",
  530. vertoriginx: "vertOriginX",
  531. "vert-origin-x": "vertOriginX",
  532. vertoriginy: "vertOriginY",
  533. "vert-origin-y": "vertOriginY",
  534. vhanging: "vHanging",
  535. "v-hanging": "vHanging",
  536. videographic: "vIdeographic",
  537. "v-ideographic": "vIdeographic",
  538. viewbox: "viewBox",
  539. viewtarget: "viewTarget",
  540. visibility: "visibility",
  541. vmathematical: "vMathematical",
  542. "v-mathematical": "vMathematical",
  543. vocab: "vocab",
  544. widths: "widths",
  545. wordspacing: "wordSpacing",
  546. "word-spacing": "wordSpacing",
  547. writingmode: "writingMode",
  548. "writing-mode": "writingMode",
  549. x1: "x1",
  550. x2: "x2",
  551. x: "x",
  552. xchannelselector: "xChannelSelector",
  553. xheight: "xHeight",
  554. "x-height": "xHeight",
  555. xlinkactuate: "xlinkActuate",
  556. "xlink:actuate": "xlinkActuate",
  557. xlinkarcrole: "xlinkArcrole",
  558. "xlink:arcrole": "xlinkArcrole",
  559. xlinkhref: "xlinkHref",
  560. "xlink:href": "xlinkHref",
  561. xlinkrole: "xlinkRole",
  562. "xlink:role": "xlinkRole",
  563. xlinkshow: "xlinkShow",
  564. "xlink:show": "xlinkShow",
  565. xlinktitle: "xlinkTitle",
  566. "xlink:title": "xlinkTitle",
  567. xlinktype: "xlinkType",
  568. "xlink:type": "xlinkType",
  569. xmlbase: "xmlBase",
  570. "xml:base": "xmlBase",
  571. xmllang: "xmlLang",
  572. "xml:lang": "xmlLang",
  573. xmlns: "xmlns",
  574. "xml:space": "xmlSpace",
  575. xmlnsxlink: "xmlnsXlink",
  576. "xmlns:xlink": "xmlnsXlink",
  577. xmlspace: "xmlSpace",
  578. y1: "y1",
  579. y2: "y2",
  580. y: "y",
  581. ychannelselector: "yChannelSelector",
  582. z: "z",
  583. zoomandpan: "zoomAndPan"
  584. };
  585. const ELEMENT_ATTRIBUTE_MAPPING = {
  586. input: {
  587. checked: "defaultChecked",
  588. value: "defaultValue",
  589. maxlength: "maxLength"
  590. },
  591. form: {
  592. enctype: "encType"
  593. }
  594. };
  595. const ELEMENT_TAG_NAME_MAPPING = {
  596. a: "a",
  597. altglyph: "altGlyph",
  598. altglyphdef: "altGlyphDef",
  599. altglyphitem: "altGlyphItem",
  600. animate: "animate",
  601. animatecolor: "animateColor",
  602. animatemotion: "animateMotion",
  603. animatetransform: "animateTransform",
  604. audio: "audio",
  605. canvas: "canvas",
  606. circle: "circle",
  607. clippath: "clipPath",
  608. "color-profile": "colorProfile",
  609. cursor: "cursor",
  610. defs: "defs",
  611. desc: "desc",
  612. discard: "discard",
  613. ellipse: "ellipse",
  614. feblend: "feBlend",
  615. fecolormatrix: "feColorMatrix",
  616. fecomponenttransfer: "feComponentTransfer",
  617. fecomposite: "feComposite",
  618. feconvolvematrix: "feConvolveMatrix",
  619. fediffuselighting: "feDiffuseLighting",
  620. fedisplacementmap: "feDisplacementMap",
  621. fedistantlight: "feDistantLight",
  622. fedropshadow: "feDropShadow",
  623. feflood: "feFlood",
  624. fefunca: "feFuncA",
  625. fefuncb: "feFuncB",
  626. fefuncg: "feFuncG",
  627. fefuncr: "feFuncR",
  628. fegaussianblur: "feGaussianBlur",
  629. feimage: "feImage",
  630. femerge: "feMerge",
  631. femergenode: "feMergeNode",
  632. femorphology: "feMorphology",
  633. feoffset: "feOffset",
  634. fepointlight: "fePointLight",
  635. fespecularlighting: "feSpecularLighting",
  636. fespotlight: "feSpotLight",
  637. fetile: "feTile",
  638. feturbulence: "feTurbulence",
  639. filter: "filter",
  640. font: "font",
  641. "font-face": "fontFace",
  642. "font-face-format": "fontFaceFormat",
  643. "font-face-name": "fontFaceName",
  644. "font-face-src": "fontFaceSrc",
  645. "font-face-uri": "fontFaceUri",
  646. foreignobject: "foreignObject",
  647. g: "g",
  648. glyph: "glyph",
  649. glyphref: "glyphRef",
  650. hatch: "hatch",
  651. hatchpath: "hatchpath",
  652. hkern: "hkern",
  653. iframe: "iframe",
  654. image: "image",
  655. line: "line",
  656. lineargradient: "linearGradient",
  657. marker: "marker",
  658. mask: "mask",
  659. mesh: "mesh",
  660. meshgradient: "meshgradient",
  661. meshpatch: "meshpatch",
  662. meshrow: "meshrow",
  663. metadata: "metadata",
  664. "missing-glyph": "missingGlyph",
  665. mpath: "mpath",
  666. path: "path",
  667. pattern: "pattern",
  668. polygon: "polygon",
  669. polyline: "polyline",
  670. radialgradient: "radialGradient",
  671. rect: "rect",
  672. script: "script",
  673. set: "set",
  674. solidcolor: "solidcolor",
  675. stop: "stop",
  676. style: "style",
  677. svg: "svg",
  678. switch: "switch",
  679. symbol: "symbol",
  680. text: "text",
  681. textpath: "textPath",
  682. title: "title",
  683. tref: "tref",
  684. tspan: "tspan",
  685. unknown: "unknown",
  686. use: "use",
  687. video: "video",
  688. view: "view",
  689. vkern: "vkern"
  690. };
  691. const convertAriaAttribute = (kebabKey) => {
  692. const [aria, ...parts] = kebabKey.split("-");
  693. return `${aria}-${parts.join("").toLowerCase()}`;
  694. };
  695. const getKey = (key, node) => {
  696. const lowerCaseKey = key.toLowerCase();
  697. const mappedElementAttribute = ELEMENT_ATTRIBUTE_MAPPING[node.name] && ELEMENT_ATTRIBUTE_MAPPING[node.name][lowerCaseKey];
  698. const mappedAttribute = ATTRIBUTE_MAPPING[lowerCaseKey];
  699. if (mappedElementAttribute || mappedAttribute) {
  700. return t__namespace.jsxIdentifier(mappedElementAttribute || mappedAttribute);
  701. }
  702. const kebabKey = kebabCase(key);
  703. if (kebabKey.startsWith("aria-")) {
  704. return t__namespace.jsxIdentifier(convertAriaAttribute(kebabKey));
  705. }
  706. if (kebabKey.startsWith("data-")) {
  707. return t__namespace.jsxIdentifier(kebabKey);
  708. }
  709. return t__namespace.jsxIdentifier(key);
  710. };
  711. const getValue = (key, value) => {
  712. if (Array.isArray(value)) {
  713. return t__namespace.stringLiteral(replaceSpaces(value.join(" ")));
  714. }
  715. if (key === "style") {
  716. return t__namespace.jsxExpressionContainer(stringToObjectStyle(value));
  717. }
  718. if (typeof value === "number" || isNumeric(value)) {
  719. return t__namespace.jsxExpressionContainer(t__namespace.numericLiteral(Number(value)));
  720. }
  721. return t__namespace.stringLiteral(replaceSpaces(value));
  722. };
  723. const getAttributes = (node) => {
  724. if (!node.properties)
  725. return [];
  726. const keys = Object.keys(node.properties);
  727. const attributes = [];
  728. let index = -1;
  729. while (++index < keys.length) {
  730. const key = keys[index];
  731. const value = node.properties[key];
  732. const attribute = t__namespace.jsxAttribute(getKey(key, node), getValue(key, value));
  733. attributes.push(attribute);
  734. }
  735. return attributes;
  736. };
  737. const root = (h, node) => t__namespace.program(all(h, node));
  738. const comment = (_, node, parent) => {
  739. if (parent.type === "root" || !node.value)
  740. return null;
  741. const expression = t__namespace.jsxEmptyExpression();
  742. t__namespace.addComment(expression, "inner", node.value);
  743. return t__namespace.jsxExpressionContainer(expression);
  744. };
  745. const SPACE_REGEX = /^\s+$/;
  746. const text = (h, node, parent) => {
  747. if (parent.type === "root")
  748. return null;
  749. if (typeof node.value === "string" && SPACE_REGEX.test(node.value))
  750. return null;
  751. return t__namespace.jsxExpressionContainer(
  752. t__namespace.stringLiteral(entities.decodeXML(String(node.value)))
  753. );
  754. };
  755. const element = (h, node, parent) => {
  756. if (!node.tagName)
  757. return null;
  758. const children = all(h, node);
  759. const selfClosing = children.length === 0;
  760. const name = ELEMENT_TAG_NAME_MAPPING[node.tagName] || node.tagName;
  761. const openingElement = t__namespace.jsxOpeningElement(
  762. t__namespace.jsxIdentifier(name),
  763. getAttributes(node),
  764. selfClosing
  765. );
  766. const closingElement = !selfClosing ? t__namespace.jsxClosingElement(t__namespace.jsxIdentifier(name)) : null;
  767. const jsxElement = t__namespace.jsxElement(openingElement, closingElement, children);
  768. if (parent.type === "root") {
  769. return t__namespace.expressionStatement(jsxElement);
  770. }
  771. return jsxElement;
  772. };
  773. var handlers = /*#__PURE__*/Object.freeze({
  774. __proto__: null,
  775. root: root,
  776. comment: comment,
  777. text: text,
  778. element: element
  779. });
  780. const helpers = { handlers };
  781. const toBabelAST = (tree) => root(helpers, tree);
  782. module.exports = toBabelAST;
  783. //# sourceMappingURL=index.js.map