stylelint-css-in-js-syntax
Version:
This plugin provides [ESLint](https://eslint.org/) rules that limit the line length of your comments. Furthermore, an **automatic fix** is included such that you can save time manually formatting your comments. As such it is recommended to apply this rule
82 lines (80 loc) • 2.93 kB
JavaScript
function extractInlineCss(content) {
let cssInJSOccurrence = 0;
const surrounding = [];
const css = [];
let current = "";
let hasUnclosedBacktick = false;
let isExtractingCss = false;
let currentCssCommentType = void 0;
let expressionEvaluationIndentation = 0;
for (let cursor = 0; cursor < content.length; cursor++) {
const currentChar = content[cursor];
if (!isExtractingCss && !currentCssCommentType && currentChar === "/" && content[cursor + 1] === "*") {
currentCssCommentType = "multi-line";
}
if (!isExtractingCss && !currentCssCommentType && currentChar === "/" && content[cursor + 1] === "/") {
currentCssCommentType = "single-line";
}
if (!hasUnclosedBacktick && !currentCssCommentType && !isExtractingCss && currentChar === "c" && content.slice(cursor, cursor + 4) === "css`" && content.slice(cursor - 1, cursor + 4) !== '"css`') {
isExtractingCss = true;
hasUnclosedBacktick = true;
const newlineCount2 = current.split("\n").length - 1;
surrounding.push(
`/*___js___${Buffer.from(current).toString("base64")}___js-end___${"\n".repeat(newlineCount2)}*/`
);
current = "";
cursor += 3;
continue;
}
if (!isExtractingCss && currentChar === "`") {
hasUnclosedBacktick = !hasUnclosedBacktick;
}
if (expressionEvaluationIndentation && currentChar === "{") {
expressionEvaluationIndentation++;
}
if (!expressionEvaluationIndentation && isExtractingCss && currentChar === "$" && content.slice(cursor, cursor + 2) === "${") {
expressionEvaluationIndentation++;
cursor += 1;
current += "${";
continue;
}
if (expressionEvaluationIndentation && currentChar === "}") {
expressionEvaluationIndentation--;
}
if (isExtractingCss && !expressionEvaluationIndentation && currentChar === "`") {
isExtractingCss = false;
hasUnclosedBacktick = false;
css.push(
`/*___start___*/.css${cssInJSOccurrence++}{${current}}/*___end___*/`
);
current = "";
continue;
}
if (currentCssCommentType === "multi-line" && currentChar === "*" && content[cursor + 1] === "/") {
currentCssCommentType = void 0;
}
if (currentCssCommentType === "single-line" && currentChar === "\n") {
currentCssCommentType = void 0;
}
current += content[cursor];
}
const newlineCount = current.split("\n").length - 1;
surrounding.push(
`/*___js___${Buffer.from(current).toString("base64")}___js-end___${"\n".repeat(newlineCount)}*/`
);
let cssString = "";
for (let i = 0; i < surrounding.length; i++) {
const currJS = surrounding[i];
const nextCss = css[i];
if (!currJS) {
continue;
}
cssString += currJS;
if (nextCss) {
cssString += nextCss;
}
}
return cssString;
}
export { extractInlineCss };
//# sourceMappingURL=util.extract-inline-css.mjs.map