css-kits
Version:
Parse css to javascript object. Support change class and id
65 lines (64 loc) • 2.47 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./helpful", "strkits"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.standardizeStyleSheet = exports.removeAllComments = void 0;
const helpful_1 = require("./helpful");
const strkits_1 = require("strkits");
function removeAllComments(subject) {
const commentRanges = [];
let currentStart = 0;
(0, helpful_1.styleForEach)({
subject,
eventFn: (e) => {
switch (e.type) {
case 'open-comment': {
currentStart = e.index;
return;
}
case 'close-comment': {
if (currentStart < 0) {
throw new SyntaxError(subject.slice(e.index + 10, e.index - 10));
}
commentRanges.push({
end: e.index + 1,
start: currentStart,
});
currentStart = -1;
return;
}
}
},
});
if (currentStart >= 0 && commentRanges.length > 0) {
throw new SyntaxError(subject.slice(currentStart + 10, currentStart - 10));
}
if (commentRanges.length === 0) {
return subject;
}
const EMPTY_STRING = '';
return strkits_1.StrKits.replaceRange(subject, commentRanges.map((range) => {
return {
...range,
value: EMPTY_STRING,
};
}));
}
exports.removeAllComments = removeAllComments;
function standardizeStyleSheet(subject) {
const lineBreakChar = strkits_1.StrKits.getLineBreakChar(subject);
return removeAllComments(subject)
.split(lineBreakChar)
.map((line) => line.trim())
.filter((line) => line.length > 0)
.join(' ');
}
exports.standardizeStyleSheet = standardizeStyleSheet;
});