| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | exports.__esModule = true;
|
|---|
| 4 | exports.default = void 0;
|
|---|
| 5 |
|
|---|
| 6 | var t = _interopRequireWildcard(require("@babel/types"));
|
|---|
| 7 |
|
|---|
| 8 | var _util = require("./util");
|
|---|
| 9 |
|
|---|
| 10 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
|---|
| 11 |
|
|---|
| 12 | function _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 | */
|
|---|
| 23 | function 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 |
|
|---|
| 34 | function 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 |
|
|---|
| 48 | function 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 |
|
|---|
| 61 | function 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 |
|
|---|
| 82 | var _default = stringToObjectStyle;
|
|---|
| 83 | exports.default = _default; |
|---|