zss-engine
Version:
A CSS-in-JS transpiler engine for building zero-runtime stylesheets at build time.
52 lines (51 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.overrideLonghand = void 0;
const helper_js_1 = require("./helper.js");
const shorthand_js_1 = require("./shorthand.js");
const isNestedGroup = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
const overrideLonghand = (style) => {
const props = Object.keys(style);
const queryStyle = style;
const propsToRemove = new Set();
const plainProps = [];
for (let i = 0; i < props.length; i++) {
if (!isNestedGroup(queryStyle[props[i]])) {
plainProps.push({ key: props[i], index: i });
}
}
const shorthandsInStyle = {};
for (const { key, index } of plainProps) {
const kebab = (0, helper_js_1.camelToKebabCase)(key);
if (shorthand_js_1.SHORTHAND_PROPERTIES[kebab]) {
shorthandsInStyle[kebab] = index;
}
}
for (const { key, index } of plainProps) {
const kebab = (0, helper_js_1.camelToKebabCase)(key);
if (!shorthand_js_1.SHORTHAND_PROPERTIES[kebab]) {
const longhands = shorthand_js_1.LONG_TO_SHORT[kebab] || [];
for (const shorthand of longhands) {
if (shorthandsInStyle[shorthand] > index) {
propsToRemove.add(key);
break;
}
}
}
}
const finalStyle = {};
for (const prop of props) {
if (propsToRemove.has(prop)) {
continue;
}
const value = queryStyle[prop];
if (isNestedGroup(value)) {
finalStyle[prop] = (0, exports.overrideLonghand)(value);
}
else {
finalStyle[prop] = value;
}
}
return finalStyle;
};
exports.overrideLonghand = overrideLonghand;