@platform/css
Version:
Helpers for working with inline CSS.
108 lines (107 loc) • 3.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toObject = void 0;
var tslib_1 = require("tslib");
function toObject(args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var root, object;
return tslib_1.__generator(this, function (_a) {
root = args;
object = toDeclarations(args.text).reduce(function (acc, next) {
acc[next.selector] = next.items.reduce(function (acc, next) {
acc[next.name] = next.value;
return acc;
}, {});
return acc;
}, {});
return [2, {
object: object,
toString: function (args) {
if (args === void 0) { args = {}; }
var text = root.inspect(object, { colors: false, compact: false });
text = args.const ? "const ".concat(args.const, " = ").concat(text, ";") : text;
text = args.export ? "export ".concat(text) : text;
var header = (args.header || root.header || '').trim();
text = header ? "".concat(header, "\n\n").concat(text) : text;
text = "".concat(text, "\n");
return text;
},
}];
});
});
}
exports.toObject = toObject;
function toDeclarations(text) {
var lines = stripComments(text.split('\n'));
var result = [];
var isStart = function (line) {
return line.length > 2 && !line.startsWith('/') && line.endsWith('{');
};
var isSelector = function (line) {
return isStart(line) || (line.length > 2 && line.endsWith(','));
};
var isEnd = function (line) {
return line.endsWith('}');
};
var isStyleLine = function (line) {
return line.length > 2 && !line.startsWith('/') && line.includes(':');
};
var selector = [];
var current;
for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
var item = lines_1[_i];
var line = trimLine(item || '');
if (!current && isSelector(line)) {
selector.push(line.replace(/\{$/, '').replace(/,$/, '').trim().replace(/'/g, '"'));
}
if (!current && isStart(line)) {
current = { selector: selector.join(', '), items: [] };
}
if (current && !isStart(line) && !isEnd(line) && isStyleLine(line)) {
current.items.push(toStyle(line));
}
if (current && isEnd(line)) {
result.push(current);
current = undefined;
selector = [];
}
}
return result;
}
function stripComments(lines) {
var result = [];
var withinComment = false;
for (var _i = 0, lines_2 = lines; _i < lines_2.length; _i++) {
var line = lines_2[_i];
var trimmed = line.trim();
if (trimmed.startsWith('/*')) {
withinComment = true;
}
if (withinComment && line.endsWith('*/')) {
withinComment = false;
}
if (!withinComment) {
result.push(line);
}
}
return result;
}
function formatName(name) {
return name
.split('-')
.map(function (part, i) { return (i === 0 ? part : "".concat(part[0].toUpperCase()).concat(part.substring(1))); })
.join('')
.replace(/'/g, '"');
}
function trimLine(line) {
line = line.trim();
line = line.replace(/\/\*.*\*\/$/, '').trim();
line = line.replace(/;$/, '');
return line.trim();
}
function toStyle(line) {
var parts = trimLine(line).split(':');
var name = formatName(parts[0].trim());
var value = parts[1].trim().replace(/;$/, '');
return { name: name, value: value };
}