2
2
* function that is called by create-migrations.ts
3
3
* **/
4
4
5
+ // These are documented at https://www.npmjs.com/package/css/v/3.0.0.
5
6
import { parse , stringify , Rule , Stylesheet , Declaration } from "css" ;
6
7
export interface Migration {
7
8
modifiedCss : string ;
@@ -144,12 +145,13 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
144
145
// We're looking for rules that affect the marginBox's size and position (top/left/height/width)
145
146
// because the new theme system controls these with variables that affect the .bloom-page margin
146
147
// 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
148
149
// 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.
153
155
const unknownDeclarations = rule . declarations ?. filter (
154
156
( d : Declaration ) =>
155
157
d . property !== "height" &&
@@ -164,7 +166,7 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
164
166
selectors : rule . selectors ,
165
167
declarations : unknownDeclarations ,
166
168
} ;
167
- sortClassesInSelector ( newRule ) ;
169
+ sortClassesInSelectors ( newRule ) ;
168
170
AddWarningCommentsIfNeeded ( newRule ) ;
169
171
otherRules . push ( newRule ) ;
170
172
ruleIndices . push ( index + 1 ) ;
@@ -230,7 +232,7 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
230
232
}
231
233
} ) ;
232
234
233
- sortClassesInSelector ( rule ) ;
235
+ sortClassesInSelectors ( rule ) ;
234
236
sortDeclarations ( rule ) ;
235
237
}
236
238
} ) ;
@@ -255,6 +257,8 @@ function sortDeclarations(rule: Rule) {
255
257
256
258
// Define the property conversions object
257
259
const orderedProperties : PropertyConversions = {
260
+ "--cover-margin-top" : 0 ,
261
+ "--cover-margin-bottom" : 1 ,
258
262
"--page-margin-top" : 0 ,
259
263
"--page-margin-bottom" : 1 ,
260
264
"--page-margin-left" : 2 ,
@@ -277,15 +281,24 @@ function sortDeclarations(rule: Rule) {
277
281
} ) ;
278
282
}
279
283
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.
282
287
if ( ! rule . selectors || ! rule . selectors . length ) return ;
283
- var selector = rule . selectors [ 0 ] . trim ( ) ;
284
- if ( / ^ \. [ A - Z a - z ] [ - . A - Z a - z 0 - 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 - Z a - z ] [ - . A - Z a - z 0 - 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
+ }
289
302
}
290
303
}
291
304
0 commit comments