style-to-js
Version:
Parses CSS inline style to JavaScript object (camelCased).
68 lines (67 loc) • 2.53 kB
JavaScript
//#region \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
let style_to_object = require("style-to-object");
style_to_object = __toESM(style_to_object);
//#region src/utilities.ts
const CUSTOM_PROPERTY_REGEX = /^--[a-zA-Z0-9_-]+$/;
const HYPHEN_REGEX = /-([a-z])/g;
const NO_HYPHEN_REGEX = /^[^-]+$/;
const VENDOR_PREFIX_REGEX = /^-(webkit|moz|ms|o|khtml)-/;
const MS_VENDOR_PREFIX_REGEX = /^-(ms)-/;
/**
* Checks whether to skip camelCase.
*/
const skipCamelCase = (property) => !property || NO_HYPHEN_REGEX.test(property) || CUSTOM_PROPERTY_REGEX.test(property);
/**
* Replacer that capitalizes first character.
*/
const capitalize = (match, character) => character.toUpperCase();
/**
* Replacer that removes beginning hyphen of vendor prefix property.
*/
const trimHyphen = (match, prefix) => `${prefix}-`;
/**
* CamelCases a CSS property.
*/
const camelCase = (property, options = {}) => {
if (skipCamelCase(property)) return property;
property = property.toLowerCase();
if (options.reactCompat) property = property.replace(MS_VENDOR_PREFIX_REGEX, trimHyphen);
else property = property.replace(VENDOR_PREFIX_REGEX, trimHyphen);
return property.replace(HYPHEN_REGEX, capitalize);
};
//#endregion
//#region src/index.ts
/**
* Parses CSS inline style to JavaScript object (camelCased).
*/
function StyleToJS(style, options) {
const output = {};
if (!style || typeof style !== "string") return output;
(0, style_to_object.default)(style, (property, value) => {
if (property && value) output[camelCase(property, options)] = value;
});
return output;
}
//#endregion
module.exports = StyleToJS;