Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed handling of calc() function in CSS minifier #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,58 @@ export default (options = {}) => {
return result;
};

const findClosingParenthesis = (content, from) => {
let depth = 1;
for (let i = from; i < content.length; i += 1) {
const char = content[i];
if (char == "(") {
depth += 1;
} else if (char == ")") {
if (depth === 1) {
return i;
}

depth -= 1;
}
}

return undefined;
};

const replaceCalc = (content) => {
const calc_functions = [];
const calc_start_regex = /\bcalc\([^)]/g;
let match;
while ((match = calc_start_regex.exec(content)) !== null) {
if (match.index === calc_start_regex.lastIndex) {
break;
}

const start = match.index + 5;
const end = findClosingParenthesis(content, start);
if (end === undefined) {
throw new Error("Missing closing parenthesis in calc() function");
}

const calc_function = content.slice(start, end);
calc_functions.push(calc_function);

const before = content.slice(0, match.index);
const after = content.slice(end + 1);
content = `${before}__CALC__${after}`;
}

return { calc_functions, result: content };
};

/* minify css */
const minifyCSS = (content) => {
const calc_functions = [];
const calc_regex = /\bcalc\(([^)]+)\)/g;
const comments = /("(?:[^"\\]+|\\.)*"|'(?:[^'\\]+|\\.)*')|\/\*[\s\S]*?\*\//g;
const syntax = /("(?:[^"\\]+|\\.)*"|'(?:[^'\\]+|\\.)*')|\s*([{};,>~])\s*|\s*([*$~^|]?=)\s*|\s+([+-])(?=.*\{)|([[(:])\s+|\s+([\])])|\s+(:)(?![^}]*\{)|^\s+|\s+$|(\s)\s+(?![^(]*\))/g;

return content
.replace(calc_regex, (_, group) => {
calc_functions.push(group);
return "__CALC__";
})
const { result, calc_functions } = replaceCalc(content);

return result
.replace(comments, "$1")
.replace(syntax, "$1$2$3$4$5$6$7$8")
.replace(/__CALC__/g, () => `calc(${calc_functions.shift()})`)
Expand Down