@@ -99,9 +99,7 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
99
99
let hideL2TitleOnCover = false ;
100
100
101
101
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 ) => {
105
103
//console.log(`DEBUG rule = ${JSON.stringify(rule)}`);
106
104
if (
107
105
rule . type === "rule" &&
@@ -140,7 +138,17 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
140
138
return ; // leave this rule unchanged.
141
139
}
142
140
// 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.
144
152
145
153
const unknownDeclarations = rule . declarations ?. filter (
146
154
( d : Declaration ) =>
@@ -159,7 +167,7 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
159
167
sortClassesInSelector ( newRule ) ;
160
168
AddWarningCommentsIfNeeded ( newRule ) ;
161
169
otherRules . push ( newRule ) ;
162
- ruleIndices . push ( ruleIndex ) ;
170
+ ruleIndices . push ( index + 1 ) ;
163
171
rule . declarations = rule . declarations . filter (
164
172
( d : Declaration ) =>
165
173
d . property === "height" ||
@@ -169,7 +177,10 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
169
177
) ;
170
178
}
171
179
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.
173
184
rule . selectors = rule . selectors . map ( ( s : string ) =>
174
185
s . replace ( ".marginBox" , "" )
175
186
) ;
@@ -183,13 +194,20 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
183
194
if ( v === undefined || left === undefined || top === undefined )
184
195
declaration . value = ` ignore /* error: ${ rule . declarations ?. toString ( ) } */` ;
185
196
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.)
186
204
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 ;
188
206
if ( Math . abs ( val ) < 0.05 ) val = 0 ;
189
207
declaration . value = val . toFixed ( 1 ) + "mm" ;
190
208
}
191
209
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 ;
193
211
if ( Math . abs ( val ) < 0.05 ) val = 0 ;
194
212
declaration . value = val . toFixed ( 1 ) + "mm" ;
195
213
}
@@ -199,6 +217,9 @@ export function migrateCssToAppearance(cssFileContent: string): Migration {
199
217
const isCover = rule . selectors ! . some ( ( sel ) =>
200
218
sel . includes ( "Cover" )
201
219
) ;
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.)
202
223
const map = isCover
203
224
? coverPropertyConversions
204
225
: propertyConversions ;
@@ -260,7 +281,7 @@ function sortClassesInSelector(rule: any): void {
260
281
// sort the classes in the first selector if appropriate
261
282
if ( ! rule . selectors || ! rule . selectors . length ) return ;
262
283
var selector = rule . selectors [ 0 ] . trim ( ) ;
263
- if ( / ^ .[ A - Z a - z ] [ - . A - Z a - z 0 - 9 ] * $ / . test ( selector ) )
284
+ if ( / ^ \ .[ A - Z a - z ] [ - . A - Z a - z 0 - 9 ] * $ / . test ( selector ) )
264
285
{
265
286
const classes = rule . selectors [ 0 ] . trim ( ) . split ( "." ) . filter ( Boolean ) . sort ( ) ;
266
287
const sortedSelector = "." + classes . join ( "." ) ;
@@ -332,7 +353,9 @@ function AddWarningCommentsIfNeeded(rule: Rule) {
332
353
if ( d . property === "bottom" ||
333
354
d . property === "right" ||
334
355
d . property ?. includes ( "margin-" ) ||
335
- d . property ?. includes ( "padding-" ) ) {
356
+ d . property ?. includes ( "margin:" ) ||
357
+ d . property ?. includes ( "padding-" ) ||
358
+ d . property ?. includes ( "padding:" ) ) {
336
359
const comment : Declaration = { } as Declaration ;
337
360
comment . type = "comment" ;
338
361
comment . comment = ` ${ d . property } : NOT MIGRATED ` ;
0 commit comments