You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stringToObjectStyle.js 1.6 KiB

пре 3 година
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _util = require("./util");
  7. // Inspired by https://github.com/reactjs/react-magic/blob/master/src/htmltojsx.js
  8. /**
  9. * Determines if the CSS value can be converted from a
  10. * 'px' suffixed string to a numeric value.
  11. *
  12. * @param {string} value CSS property value
  13. * @return {boolean}
  14. */
  15. function isConvertiblePixelValue(value) {
  16. return /^\d+px$/.test(value);
  17. }
  18. /**
  19. * Format style key into JSX style object key.
  20. *
  21. * @param {string} key
  22. * @return {string}
  23. */
  24. function formatKey(key) {
  25. key = key.toLowerCase(); // Don't capitalize -ms- prefix
  26. if (/^-ms-/.test(key)) key = key.substr(1);
  27. return (0, _util.hyphenToCamelCase)(key);
  28. }
  29. /**
  30. * Format style value into JSX style object value.
  31. *
  32. * @param {string} key
  33. * @return {string}
  34. */
  35. function formatValue(value) {
  36. if ((0, _util.isNumeric)(value)) return Number(value);
  37. if (isConvertiblePixelValue(value)) return Number((0, _util.trimEnd)(value, 'px'));
  38. return value;
  39. }
  40. /**
  41. * Handle parsing of inline styles.
  42. *
  43. * @param {string} rawStyle
  44. * @returns {object}
  45. */
  46. function stringToObjectStyle(rawStyle) {
  47. const entries = rawStyle.split(';');
  48. return entries.reduce((styles, style) => {
  49. style = style.trim();
  50. const firstColon = style.indexOf(':');
  51. const value = style.substr(firstColon + 1).trim();
  52. const key = style.substr(0, firstColon);
  53. if (key !== '') {
  54. styles[formatKey(key)] = formatValue(value);
  55. }
  56. return styles;
  57. }, {});
  58. }
  59. var _default = stringToObjectStyle;
  60. exports.default = _default;