stringToObjectStyle.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var t = _interopRequireWildcard(require("@babel/types"));
  5. var _util = require("./util");
  6. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  7. 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; }
  8. // Inspired by https://github.com/reactjs/react-magic/blob/master/src/htmltojsx.js
  9. /**
  10. * Determines if the CSS value can be converted from a
  11. * 'px' suffixed string to a numeric value.
  12. *
  13. * @param {string} value CSS property value
  14. * @return {boolean}
  15. */
  16. function isConvertiblePixelValue(value) {
  17. return /^\d+px$/.test(value);
  18. }
  19. /**
  20. * Format style key into JSX style object key.
  21. *
  22. * @param {string} key
  23. * @return {string}
  24. */
  25. function formatKey(key) {
  26. key = key.toLowerCase(); // Don't capitalize -ms- prefix
  27. if (/^-ms-/.test(key)) key = key.substr(1);
  28. return t.identifier((0, _util.hyphenToCamelCase)(key));
  29. }
  30. /**
  31. * Format style value into JSX style object value.
  32. *
  33. * @param {string} key
  34. * @return {string}
  35. */
  36. function formatValue(value) {
  37. if ((0, _util.isNumeric)(value)) return t.numericLiteral(Number(value));
  38. if (isConvertiblePixelValue(value)) return t.numericLiteral(Number((0, _util.trimEnd)(value, 'px')));
  39. return t.stringLiteral(value);
  40. }
  41. /**
  42. * Handle parsing of inline styles.
  43. *
  44. * @param {string} rawStyle
  45. * @returns {object}
  46. */
  47. function stringToObjectStyle(rawStyle) {
  48. const entries = rawStyle.split(';');
  49. const properties = [];
  50. let index = -1;
  51. while (++index < entries.length) {
  52. const entry = entries[index];
  53. const style = entry.trim();
  54. const firstColon = style.indexOf(':');
  55. const value = style.substr(firstColon + 1).trim();
  56. const key = style.substr(0, firstColon);
  57. if (key !== '') {
  58. const property = t.objectProperty(formatKey(key), formatValue(value));
  59. properties.push(property);
  60. }
  61. }
  62. return t.objectExpression(properties);
  63. }
  64. var _default = stringToObjectStyle;
  65. exports.default = _default;