css-kits
Version:
Parse css to javascript object. Support change class and id
157 lines (156 loc) • 5.68 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"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.selectorsMap = exports.styleForEach = exports.mkResult = void 0;
function mkResult(callbackFn) {
return callbackFn();
}
exports.mkResult = mkResult;
/**
* forEach ignore string and comment
*/
function styleForEach(props) {
const { subject } = props;
let beingInsideOfComment = false;
let currentStringMark = undefined;
let isBreakForEach = false;
const dispatchCallback = mkResult(() => {
if (props.callbackFn)
return props.callbackFn;
return (e) => {
return;
};
});
const dispatchEvent = mkResult(() => {
if (props.eventFn)
return props.eventFn;
return (e) => {
return;
};
});
const breakForEach = () => {
isBreakForEach = true;
};
for (let i = 0; i < subject.length; i++) {
if (isBreakForEach)
return;
const currentLetter = subject[i];
switch (currentLetter) {
case '\r':
case '\n': {
continue;
}
//! string
case '"':
case "'": {
// inside of comment
if (beingInsideOfComment)
continue;
// start of the string
if (currentStringMark === undefined) {
currentStringMark = currentLetter;
dispatchEvent({
type: 'open-string',
index: i,
value: currentLetter,
breakForEach,
});
continue;
}
// end of the string
if (currentStringMark === currentLetter) {
currentStringMark = undefined;
dispatchEvent({
type: 'close-string',
index: i,
value: currentLetter,
breakForEach,
});
continue;
}
// inside of the string
continue;
}
case '/': {
// Being inside of string
if (currentStringMark !== undefined) {
continue;
}
// inside of comment
if (beingInsideOfComment) {
// close comment
if (subject.charAt(i - 1) === '*') {
beingInsideOfComment = false;
dispatchEvent({
type: 'close-comment',
index: i,
value: currentLetter,
breakForEach,
});
continue;
}
continue;
}
// outside of comment
// open comment
if (subject.charAt(i + 1) === '*') {
beingInsideOfComment = true;
dispatchEvent({
type: 'open-comment',
index: i,
value: currentLetter,
breakForEach,
});
continue;
}
continue;
}
default: {
// Being Inside of string or inside of comment.
if (currentStringMark !== undefined || beingInsideOfComment) {
continue;
}
}
}
dispatchCallback({
breakForEach,
index: i,
value: currentLetter,
});
}
}
exports.styleForEach = styleForEach;
function selectorsMap(styleSheetList, callbackFn, clone = true) {
const styleSheetListCopy = mkResult(() => {
if (clone) {
return structuredClone(styleSheetList);
}
return styleSheetList;
});
styleSheetListCopy.forEach((item) => {
switch (item.type) {
case 'rule-set': {
item.selectors = item.selectors.map(callbackFn);
return;
}
case 'nested-at-rule': {
selectorsMap(item.styleSheetList, callbackFn, false);
return;
}
default: {
return;
}
}
});
return styleSheetListCopy;
}
exports.selectorsMap = selectorsMap;
});