Skip to content

Commit 4cfabd0

Browse files
Improve sortClassesInSelectors method and add comments (BL-12857)
1 parent bba77c7 commit 4cfabd0

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

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)