Skip to content

Commit 2d1cffb

Browse files
Improve sortClassesInSelectors method and add comments (BL-12857)
Also fix bugs caused by shortening paths in filter output.
1 parent bba77c7 commit 2d1cffb

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

Diff for: custom-css-analysis-and-migration/create-migrations.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ for (const record of records) {
3737

3838
++count;
3939
if (Bun.argv.length > 2 && count > Number.parseInt(Bun.argv[2])) break;
40-
console.log(`css = ${record.css}`);
40+
//console.log(`css = ${record.css}`);
4141
const checksum = crypto.createHash("md5").update(record.css).digest("hex");
4242
const folder = `./output/migration/${count} ${checksum}`;
4343
console.log(`output folder=${folder}`);
@@ -59,7 +59,7 @@ for (const record of records) {
5959

6060
const brandingSet = new Set();
6161
for (let i = 0; i < record.paths.length; ++i) {
62-
const metaPath = record.paths[i] + "/meta.json";
62+
const metaPath = "./output/downloads/" + record.paths[i] + "/meta.json";
6363
try {
6464
const metaString: string = fs.readFileSync(metaPath, "utf8");
6565
const metadata = JSON.parse(metaString);
@@ -69,14 +69,14 @@ for (const record of records) {
6969
brandingProject.toLowerCase() !== "local-community")
7070
brandingSet.add(brandingProject);
7171
} catch (e) {
72-
console.log("Could not extract brandingProject from " + metaPath);
72+
console.log("Could not extract brandingProjectName from " + metaPath + ": " + e);
7373
}
7474
}
7575

7676
const uniqueUploaders = [
7777
...new Set(
7878
record.paths.map((path: string) => {
79-
const email = path.split("/")[2];
79+
const email = path.split("/")[0];
8080
const emailParts = email.split("@");
8181
const obfuscatedEmail =
8282
emailParts[0].slice(0, -2) + "..." + "@" + emailParts[1];
@@ -100,8 +100,6 @@ for (const record of records) {
100100
// A replacement customBookStyles.css has been generated.
101101
{
102102
"cssThemeName": "default",
103-
"coverShowTitleL2": ${migration.hideL2TitleOnCover ? "false" : "true"},
104-
"coverShowTitleL3": false,
105103
}
106104
`;
107105
let appearancePath = `${folder}/appearance.json`;

Diff for: custom-css-analysis-and-migration/filter-stylesheets.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const recordsWithUniqueifiedPaths = filteredRecords.map((record) => {
3434
const uniquePaths = uniqueFilenames.map((filename) => {
3535
return paths.find((path) => path.endsWith(filename));
3636
});
37-
const instanceId = paths[0].split("/")[3];
37+
const instanceId = paths[0].split("/")[1];
3838
return {
3939
book_count: paths.length,
4040
unique_named_books: uniquePaths.length,

Diff for: custom-css-analysis-and-migration/migrateCssToAppearance.ts

+28-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* function that is called by create-migrations.ts
33
* **/
44

5+
// These are documented at https://www.npmjs.com/package/css/v/3.0.0.
56
import { parse, stringify, Rule, Stylesheet, Declaration } from "css";
67
export interface Migration {
78
modifiedCss: string;
@@ -144,12 +145,13 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
144145
// We're looking for rules that affect the marginBox's size and position (top/left/height/width)
145146
// because the new theme system controls these with variables that affect the .bloom-page margin
146147
// 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+
// and setting its height or width will probably do something unwanted. If such a rule also has
148149
// 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.
152-
150+
// we try to fix to have the new values needed) and one that has only the safe declarations.
151+
// The .marginBox class is removed from the selectors for the rules with the updated new
152+
// declarations since it's no longer needed.
153+
// Producing split rules also helps human reviewers to see what's going on, and to check that the
154+
// new rules are correct.
153155
const unknownDeclarations = rule.declarations?.filter(
154156
(d: Declaration) =>
155157
d.property !== "height" &&
@@ -164,7 +166,7 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
164166
selectors: rule.selectors,
165167
declarations: unknownDeclarations,
166168
};
167-
sortClassesInSelector(newRule);
169+
sortClassesInSelectors(newRule);
168170
AddWarningCommentsIfNeeded(newRule);
169171
otherRules.push(newRule);
170172
ruleIndices.push(index + 1);
@@ -230,7 +232,7 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
230232
}
231233
});
232234

233-
sortClassesInSelector(rule);
235+
sortClassesInSelectors(rule);
234236
sortDeclarations(rule);
235237
}
236238
});
@@ -255,6 +257,8 @@ function sortDeclarations(rule: Rule) {
255257

256258
// Define the property conversions object
257259
const orderedProperties: PropertyConversions = {
260+
"--cover-margin-top": 0,
261+
"--cover-margin-bottom": 1,
258262
"--page-margin-top": 0,
259263
"--page-margin-bottom": 1,
260264
"--page-margin-left": 2,
@@ -277,15 +281,24 @@ function sortDeclarations(rule: Rule) {
277281
});
278282
}
279283

280-
function sortClassesInSelector(rule: any): void {
281-
// sort the classes in the first selector if appropriate
284+
function sortClassesInSelectors(rule: any): void {
285+
// Sort the classes in the selectors if feasible.
286+
// This makes the rules easier to read and to compare.
282287
if (!rule.selectors || !rule.selectors.length) return;
283-
var selector = rule.selectors[0].trim();
284-
if (/^\.[A-Za-z][-.A-Za-z0-9]*$/.test(selector))
285-
{
286-
const classes = rule.selectors[0].trim().split(".").filter(Boolean).sort();
287-
const sortedSelector = "." + classes.join(".");
288-
rule.selectors[0] = sortedSelector;
288+
for (let i = 0; i < rule.selectors.length; ++i) {
289+
// Note that rule.selectors is an Array of Strings split on commas.
290+
const selector = rule.selectors[i].trim();
291+
// If the selector is just a list of classes in the same element, sort them.
292+
// We don't try to sort selectors that have multiple element levels or that
293+
// have something other than classes.
294+
if (/^\.[A-Za-z][-.A-Za-z0-9]*$/.test(selector))
295+
{
296+
const classes = selector.split(".").filter(Boolean).sort();
297+
const sortedSelector = "." + classes.join(".");
298+
// We don't need to worry about leading or trailing spaces in the selector
299+
// because the output stringify function has its own ideas about how to format.
300+
rule.selectors[i] = sortedSelector;
301+
}
289302
}
290303
}
291304

0 commit comments

Comments
 (0)