mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-26 01:58:06 +02:00
Use the regular preprocess
-function for the CSS files as well
An old shortcoming of the `preprocessCSS`-function is its complete lack of support for our "normal" defines, which makes it very difficult to have build-specific CSS rules. Recently we've started using specially crafted comments to remove CSS rules from the MOZCENTRAL build, but (ab)using the `preprocessCSS`-function in this way really doesn't feel great. However, it turns out to be surprisingly simple to instead use the "regular" `preprocess`-function for the CSS files as well. The only special-handling that's still necessary is the helper-function for dealing with CSS-imports, but apart from that everything seems to just work. One reason, as far as I can tell, for having a separate `preprocessCSS`-function was likely that we originally used *lots* of vendor-prefixed CSS rules in our CSS files. With improvements over the years, especially thanks to Autoprefixer and PostCSS, we've been able to remove *almost* all non-standard CSS rules and the need for special-casing the CSS parsing has mostly vanished. *Please note:* As part of testing this patch I've diffed the output of `gulp generic`, `gulp mozcentral`, and `gulp chromium` against the `master`-branch to check that there was no obvious breakage.
This commit is contained in:
parent
f8838eb794
commit
d1f13a6af3
5 changed files with 61 additions and 91 deletions
98
external/builder/builder.js
vendored
98
external/builder/builder.js
vendored
|
@ -38,9 +38,25 @@ function preprocess(inFilename, outFilename, defines) {
|
|||
return fs.realpathSync(inFilename) + ":" + lineNumber;
|
||||
}
|
||||
|
||||
function expandCssImports(content, baseUrl) {
|
||||
return content.replace(
|
||||
/^\s*@import\s+url\(([^)]+)\);\s*$/gm,
|
||||
function (all, url) {
|
||||
const file = path.join(path.dirname(baseUrl), url);
|
||||
const imported = fs.readFileSync(file, "utf8").toString();
|
||||
return expandCssImports(imported, file);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// TODO make this really read line by line.
|
||||
const lines = fs.readFileSync(inFilename).toString().split("\n");
|
||||
const totalLines = lines.length;
|
||||
let content = fs.readFileSync(inFilename, "utf8").toString();
|
||||
// Handle CSS-imports first, when necessary.
|
||||
if (/\.css$/i.test(inFilename)) {
|
||||
content = expandCssImports(content, inFilename);
|
||||
}
|
||||
const lines = content.split("\n"),
|
||||
totalLines = lines.length;
|
||||
let out = "";
|
||||
let i = 0;
|
||||
function readLine() {
|
||||
|
@ -123,7 +139,7 @@ function preprocess(inFilename, outFilename, defines) {
|
|||
let state = STATE_NONE;
|
||||
const stack = [];
|
||||
const control =
|
||||
/^(?:\/\/|<!--)\s*#(if|elif|else|endif|expand|include|error)\b(?:\s+(.*?)(?:-->)?$)?/;
|
||||
/^(?:\/\/|\s*\/\*|<!--)\s*#(if|elif|else|endif|expand|include|error)\b(?:\s+(.*?)(?:\*\/|-->)?$)?/;
|
||||
|
||||
while ((line = readLine()) !== null) {
|
||||
++lineNumber;
|
||||
|
@ -199,82 +215,6 @@ function preprocess(inFilename, outFilename, defines) {
|
|||
}
|
||||
exports.preprocess = preprocess;
|
||||
|
||||
function preprocessCSS(inFilename, outFilename, defines) {
|
||||
function hasPrefixedMozcentral(line) {
|
||||
return /(^|\W)-(ms|o|webkit)-\w/.test(line);
|
||||
}
|
||||
|
||||
function expandImports(content, baseUrl) {
|
||||
return content.replace(
|
||||
/^\s*@import\s+url\(([^)]+)\);\s*$/gm,
|
||||
function (all, url) {
|
||||
const file = path.join(path.dirname(baseUrl), url);
|
||||
const imported = fs.readFileSync(file, "utf8").toString();
|
||||
return expandImports(imported, file);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function removePrefixed(content, hasPrefixedFilter) {
|
||||
const lines = content.split(/\r?\n/g);
|
||||
let i = 0;
|
||||
while (i < lines.length) {
|
||||
const line = lines[i];
|
||||
if (!hasPrefixedFilter(line)) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (/\{\s*$/.test(line)) {
|
||||
let bracketLevel = 1;
|
||||
let j = i + 1;
|
||||
while (j < lines.length && bracketLevel > 0) {
|
||||
const checkBracket = /([{}])\s*$/.exec(lines[j]);
|
||||
if (checkBracket) {
|
||||
if (checkBracket[1] === "{") {
|
||||
bracketLevel++;
|
||||
} else if (!lines[j].includes("{")) {
|
||||
bracketLevel--;
|
||||
}
|
||||
}
|
||||
j++;
|
||||
}
|
||||
lines.splice(i, j - i);
|
||||
} else if (/[};]\s*$/.test(line)) {
|
||||
lines.splice(i, 1);
|
||||
} else {
|
||||
// multiline? skipping until next directive or bracket
|
||||
do {
|
||||
lines.splice(i, 1);
|
||||
} while (
|
||||
i < lines.length &&
|
||||
!/\}\s*$/.test(lines[i]) &&
|
||||
!lines[i].includes(":")
|
||||
);
|
||||
if (i < lines.length && /\S\s*}\s*$/.test(lines[i])) {
|
||||
lines[i] = lines[i].substring(lines[i].indexOf("}"));
|
||||
}
|
||||
}
|
||||
// collapse whitespaces
|
||||
while (lines[i] === "" && lines[i - 1] === "") {
|
||||
lines.splice(i, 1);
|
||||
}
|
||||
}
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
if (!defines) {
|
||||
throw new Error("Missing CSS preprocessor defines.");
|
||||
}
|
||||
|
||||
let content = fs.readFileSync(inFilename, "utf8").toString();
|
||||
content = expandImports(content, inFilename);
|
||||
if (defines.MOZCENTRAL) {
|
||||
content = removePrefixed(content, hasPrefixedMozcentral);
|
||||
}
|
||||
fs.writeFileSync(outFilename, content);
|
||||
}
|
||||
exports.preprocessCSS = preprocessCSS;
|
||||
|
||||
/**
|
||||
* Merge two defines arrays. Values in the second param will override values in
|
||||
* the first.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue