Skip to content

Commit bba77c7

Browse files
Respond to code review comments (BL-12857)
1 parent 6908abb commit bba77c7

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

custom-css-analysis-and-migration/create-migrations.ts

-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ for (const record of records) {
4444
fs.mkdirSync(folder);
4545

4646
let cssPath = `${folder}/customBookStyles.css`;
47-
if (fs.existsSync(cssPath)) {
48-
cssPath = `${folder}/customBookStyles2.css`;
49-
console.log(`WARNING: multiple migrations in ${folder}`);
50-
}
5147

5248
const migration = migrateCssToAppearance(record.css);
5349

custom-css-analysis-and-migration/filter-stylesheets.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const count = records.reduce((acc, record) => acc + record.paths.length, 0);
1515
console.write(`total books with custom css rules: ${count}\r\n`);
1616
console.write(`total unique css files: ${records.length}\r\n`);
1717

18-
const kProbablyWillInterfere = `\\.marginBox\\s*\\{[^\\}]*?(?<![-\\w])(padding-|left:|top:|right:|bottom:|margin-|width:|height:)[^\\}]*\\}`;
18+
const kProbablyWillInterfere = `\\.marginBox\\s*\\{[^\\}]*?(?<![-\\w])(padding[-:]|left:|top:|right:|bottom:|margin[-:]|width:|height:)[^\\}]*\\}`;
1919
const kProbablyWillInterfereRegex = new RegExp(kProbablyWillInterfere, "gi");
2020

2121
const max = Bun.argv.length > 2 ? Number.parseInt(Bun.argv[2]) : 10000000;

custom-css-analysis-and-migration/group-stylesheets.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ function readFilesRecursively(
3535
++cssCount;
3636

3737
const fileData: FileData = fileMap.get(content) || { content, paths: [] };
38-
fileData.paths.push(dir.replace("./output/downloads", ""));
38+
fileData.paths.push(dir.replace(/^(\.\/)?output\/downloads\//, ""));
3939
fileMap.set(content, fileData);
4040
console.log(++count + " " + filePath);
4141
}
4242
}
4343
if (cssCount > 1)
44-
console.log(`WARNING: multiple CSS files with content in ${dir}`);
44+
console.log(`INFO: customCollectionStyles.css and customBookStyles.css both have content in ${dir}`);
4545
cssCount = 0;
4646
}
4747

custom-css-analysis-and-migration/migrateCssToAppearance.ts

+33-10
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
9999
let hideL2TitleOnCover = false;
100100

101101
if (cssObject.stylesheet && cssObject.stylesheet.rules) {
102-
let ruleIndex = 0;
103-
cssObject.stylesheet.rules.forEach((rule: Rule) => {
104-
++ruleIndex;
102+
cssObject.stylesheet.rules.forEach((rule: Rule, index: number) => {
105103
//console.log(`DEBUG rule = ${JSON.stringify(rule)}`);
106104
if (
107105
rule.type === "rule" &&
@@ -140,7 +138,17 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
140138
return; // leave this rule unchanged.
141139
}
142140
// A rule like .marginBox { background-color: red; } is just fine.
143-
// We preserve rules like this and add them after this loop changes the current rules.
141+
// (A rule like .marginBox .narrowStyle { width: 200px; } is also fine. But there's no reason for
142+
// such a rule to mention .marginBox, so there's no point in improving this code unless we
143+
// encounter a large number of such rules.)
144+
// We're looking for rules that affect the marginBox's size and position (top/left/height/width)
145+
// because the new theme system controls these with variables that affect the .bloom-page margin
146+
// instead of absolutely positioning the .marginBox, so setting its top or left will have no effect,
147+
// and setting its height or width will probably do something unwanted. If such a rule also changes
148+
// safe properties, we split it into two rules, one that has only the problem declarations (which
149+
// we try to fix to the new values needed) and one that has only the safe declarations.
150+
// We also remove the .marginBox class from the selectors for the rules with the updated new
151+
// declarations, since it's no longer needed.
144152

145153
const unknownDeclarations = rule.declarations?.filter(
146154
(d: Declaration) =>
@@ -159,7 +167,7 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
159167
sortClassesInSelector(newRule);
160168
AddWarningCommentsIfNeeded(newRule);
161169
otherRules.push(newRule);
162-
ruleIndices.push(ruleIndex);
170+
ruleIndices.push(index + 1);
163171
rule.declarations = rule.declarations.filter(
164172
(d: Declaration) =>
165173
d.property === "height" ||
@@ -169,7 +177,10 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
169177
);
170178
}
171179

172-
// remove the .marginBox class from the selectors
180+
// The remaining declarations in this rule are all safe to keep, but we need to change them
181+
// to use the new variables instead and to change the height and width to bottom and right.
182+
// Also, the .marginBox class is no longer needed in the selector since the new variable
183+
// settings apply to the page outside the .marginBox proper.
173184
rule.selectors = rule.selectors.map((s: string) =>
174185
s.replace(".marginBox", "")
175186
);
@@ -183,13 +194,20 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
183194
if (v === undefined || left === undefined || top === undefined)
184195
declaration.value = ` ignore /* error: ${rule.declarations?.toString()} */`;
185196
else {
197+
// Calculate the new value for the declaration from the old value, and round
198+
// off to zero if it's very close to zero. (The new value is either a bottom
199+
// or a right margin instead of a height or width.) Something less than 0.05mm
200+
// is effectively just a rounding error from these floating point subtractions.
201+
// We use val.toFixed(1) since precision greater than 0.1mm isn't worthwhile.
202+
// (Not rounding off to zero explicitly can results in values like -0.0 which
203+
// look odd even if they work okay.)
186204
if (key === "width") {
187-
let val = size.width - v - left; // round off to 0 if less than 0.05
205+
let val = size.width - v - left;
188206
if (Math.abs(val) < 0.05) val = 0;
189207
declaration.value = val.toFixed(1) + "mm";
190208
}
191209
if (key === "height") {
192-
let val = size.height - v - top; // round off to 0 if less than 0.05
210+
let val = size.height - v - top;
193211
if (Math.abs(val) < 0.05) val = 0;
194212
declaration.value = val.toFixed(1) + "mm";
195213
}
@@ -199,6 +217,9 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
199217
const isCover = rule.selectors!.some((sel) =>
200218
sel.includes("Cover")
201219
);
220+
// Map the existing property name to the new variable name.
221+
// (This takes care of having changed the value from width or height
222+
// to right or bottom above.)
202223
const map = isCover
203224
? coverPropertyConversions
204225
: propertyConversions;
@@ -260,7 +281,7 @@ function sortClassesInSelector(rule: any): void {
260281
// sort the classes in the first selector if appropriate
261282
if (!rule.selectors || !rule.selectors.length) return;
262283
var selector = rule.selectors[0].trim();
263-
if (/^.[A-Za-z][-.A-Za-z0-9]*$/.test(selector))
284+
if (/^\.[A-Za-z][-.A-Za-z0-9]*$/.test(selector))
264285
{
265286
const classes = rule.selectors[0].trim().split(".").filter(Boolean).sort();
266287
const sortedSelector = "." + classes.join(".");
@@ -332,7 +353,9 @@ function AddWarningCommentsIfNeeded(rule: Rule) {
332353
if (d.property === "bottom" ||
333354
d.property === "right" ||
334355
d.property?.includes("margin-") ||
335-
d.property?.includes("padding-")) {
356+
d.property?.includes("margin:") ||
357+
d.property?.includes("padding-") ||
358+
d.property?.includes("padding:")) {
336359
const comment: Declaration = {} as Declaration;
337360
comment.type = "comment";
338361
comment.comment = ` ${d.property}: NOT MIGRATED `;

0 commit comments

Comments
 (0)