source: frontend/node_modules/@svgr/hast-util-to-babel-ast/lib/stringToObjectStyle.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[9af201e]1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6var t = _interopRequireWildcard(require("@babel/types"));
7
8var _util = require("./util");
9
10function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
11
12function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13
14// Inspired by https://github.com/reactjs/react-magic/blob/master/src/htmltojsx.js
15
16/**
17 * Determines if the CSS value can be converted from a
18 * 'px' suffixed string to a numeric value.
19 *
20 * @param {string} value CSS property value
21 * @return {boolean}
22 */
23function isConvertiblePixelValue(value) {
24 return /^\d+px$/.test(value);
25}
26/**
27 * Format style key into JSX style object key.
28 *
29 * @param {string} key
30 * @return {string}
31 */
32
33
34function formatKey(key) {
35 key = key.toLowerCase(); // Don't capitalize -ms- prefix
36
37 if (/^-ms-/.test(key)) key = key.substr(1);
38 return t.identifier((0, _util.hyphenToCamelCase)(key));
39}
40/**
41 * Format style value into JSX style object value.
42 *
43 * @param {string} key
44 * @return {string}
45 */
46
47
48function formatValue(value) {
49 if ((0, _util.isNumeric)(value)) return t.numericLiteral(Number(value));
50 if (isConvertiblePixelValue(value)) return t.numericLiteral(Number((0, _util.trimEnd)(value, 'px')));
51 return t.stringLiteral(value);
52}
53/**
54 * Handle parsing of inline styles.
55 *
56 * @param {string} rawStyle
57 * @returns {object}
58 */
59
60
61function stringToObjectStyle(rawStyle) {
62 const entries = rawStyle.split(';');
63 const properties = [];
64 let index = -1;
65
66 while (++index < entries.length) {
67 const entry = entries[index];
68 const style = entry.trim();
69 const firstColon = style.indexOf(':');
70 const value = style.substr(firstColon + 1).trim();
71 const key = style.substr(0, firstColon);
72
73 if (key !== '') {
74 const property = t.objectProperty(formatKey(key), formatValue(value));
75 properties.push(property);
76 }
77 }
78
79 return t.objectExpression(properties);
80}
81
82var _default = stringToObjectStyle;
83exports.default = _default;
Note: See TracBrowser for help on using the repository browser.