Skip to content

Commit d76120a

Browse files
committed
katex [nfc]: Make KatexSpanStyles immutable
Once the parsing is done, we want these to remain unchanged, just like the other objects in the parse tree. So, like ContentNode and its subclasses, make the class immutable. The parser needs to mutate its own draft of what styles to apply to a given span; but it can do that with its own local variables corresponding to the fields, and construct a styles object at the end of the loop.
1 parent 403b157 commit d76120a

File tree

1 file changed

+56
-45
lines changed

1 file changed

+56
-45
lines changed

lib/model/katex.dart

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ class _KatexParser {
136136
KatexNode _parseSpan(dom.Element element) {
137137
// TODO maybe check if the sequence of ancestors matter for spans.
138138

139-
final spanClasses = List<String>.unmodifiable(element.className.split(' '));
140-
141139
// Aggregate the CSS styles that apply, in the same order as the CSS
142140
// classes specified for this span, mimicking the behaviour on web.
143141
//
@@ -146,7 +144,12 @@ class _KatexParser {
146144
// https://github.com/KaTeX/KaTeX/blob/2fe1941b/src/styles/katex.scss
147145
// A copy of class definition (where possible) is accompanied in a comment
148146
// with each case statement to keep track of updates.
149-
var styles = KatexSpanStyles();
147+
final spanClasses = List<String>.unmodifiable(element.className.split(' '));
148+
String? fontFamily;
149+
double? fontSizeEm;
150+
KatexSpanFontWeight? fontWeight;
151+
KatexSpanFontStyle? fontStyle;
152+
KatexSpanTextAlign? textAlign;
150153
var index = 0;
151154
while (index < spanClasses.length) {
152155
final spanClass = spanClasses[index++];
@@ -163,15 +166,15 @@ class _KatexParser {
163166

164167
case 'textbf':
165168
// .textbf { font-weight: bold; }
166-
styles.fontWeight = KatexSpanFontWeight.bold;
169+
fontWeight = KatexSpanFontWeight.bold;
167170

168171
case 'textit':
169172
// .textit { font-style: italic; }
170-
styles.fontStyle = KatexSpanFontStyle.italic;
173+
fontStyle = KatexSpanFontStyle.italic;
171174

172175
case 'textrm':
173176
// .textrm { font-family: KaTeX_Main; }
174-
styles.fontFamily = 'KaTeX_Main';
177+
fontFamily = 'KaTeX_Main';
175178

176179
// case 'textsf':
177180
// // .textsf { font-family: KaTeX_SansSerif; }
@@ -180,96 +183,96 @@ class _KatexParser {
180183

181184
case 'texttt':
182185
// .texttt { font-family: KaTeX_Typewriter; }
183-
styles.fontFamily = 'KaTeX_Typewriter';
186+
fontFamily = 'KaTeX_Typewriter';
184187

185188
case 'mathnormal':
186189
// .mathnormal { font-family: KaTeX_Math; font-style: italic; }
187-
styles.fontFamily = 'KaTeX_Math';
188-
styles.fontStyle = KatexSpanFontStyle.italic;
190+
fontFamily = 'KaTeX_Math';
191+
fontStyle = KatexSpanFontStyle.italic;
189192

190193
case 'mathit':
191194
// .mathit { font-family: KaTeX_Main; font-style: italic; }
192-
styles.fontFamily = 'KaTeX_Main';
193-
styles.fontStyle = KatexSpanFontStyle.italic;
195+
fontFamily = 'KaTeX_Main';
196+
fontStyle = KatexSpanFontStyle.italic;
194197

195198
case 'mathrm':
196199
// .mathrm { font-style: normal; }
197-
styles.fontStyle = KatexSpanFontStyle.normal;
200+
fontStyle = KatexSpanFontStyle.normal;
198201

199202
case 'mathbf':
200203
// .mathbf { font-family: KaTeX_Main; font-weight: bold; }
201-
styles.fontFamily = 'KaTeX_Main';
202-
styles.fontWeight = KatexSpanFontWeight.bold;
204+
fontFamily = 'KaTeX_Main';
205+
fontWeight = KatexSpanFontWeight.bold;
203206

204207
case 'boldsymbol':
205208
// .boldsymbol { font-family: KaTeX_Math; font-weight: bold; font-style: italic; }
206-
styles.fontFamily = 'KaTeX_Math';
207-
styles.fontWeight = KatexSpanFontWeight.bold;
208-
styles.fontStyle = KatexSpanFontStyle.italic;
209+
fontFamily = 'KaTeX_Math';
210+
fontWeight = KatexSpanFontWeight.bold;
211+
fontStyle = KatexSpanFontStyle.italic;
209212

210213
case 'amsrm':
211214
// .amsrm { font-family: KaTeX_AMS; }
212-
styles.fontFamily = 'KaTeX_AMS';
215+
fontFamily = 'KaTeX_AMS';
213216

214217
case 'mathbb':
215218
case 'textbb':
216219
// .mathbb,
217220
// .textbb { font-family: KaTeX_AMS; }
218-
styles.fontFamily = 'KaTeX_AMS';
221+
fontFamily = 'KaTeX_AMS';
219222

220223
case 'mathcal':
221224
// .mathcal { font-family: KaTeX_Caligraphic; }
222-
styles.fontFamily = 'KaTeX_Caligraphic';
225+
fontFamily = 'KaTeX_Caligraphic';
223226

224227
case 'mathfrak':
225228
case 'textfrak':
226229
// .mathfrak,
227230
// .textfrak { font-family: KaTeX_Fraktur; }
228-
styles.fontFamily = 'KaTeX_Fraktur';
231+
fontFamily = 'KaTeX_Fraktur';
229232

230233
case 'mathboldfrak':
231234
case 'textboldfrak':
232235
// .mathboldfrak,
233236
// .textboldfrak { font-family: KaTeX_Fraktur; font-weight: bold; }
234-
styles.fontFamily = 'KaTeX_Fraktur';
235-
styles.fontWeight = KatexSpanFontWeight.bold;
237+
fontFamily = 'KaTeX_Fraktur';
238+
fontWeight = KatexSpanFontWeight.bold;
236239

237240
case 'mathtt':
238241
// .mathtt { font-family: KaTeX_Typewriter; }
239-
styles.fontFamily = 'KaTeX_Typewriter';
242+
fontFamily = 'KaTeX_Typewriter';
240243

241244
case 'mathscr':
242245
case 'textscr':
243246
// .mathscr,
244247
// .textscr { font-family: KaTeX_Script; }
245-
styles.fontFamily = 'KaTeX_Script';
248+
fontFamily = 'KaTeX_Script';
246249

247250
case 'mathsf':
248251
case 'textsf':
249252
// .mathsf,
250253
// .textsf { font-family: KaTeX_SansSerif; }
251-
styles.fontFamily = 'KaTeX_SansSerif';
254+
fontFamily = 'KaTeX_SansSerif';
252255

253256
case 'mathboldsf':
254257
case 'textboldsf':
255258
// .mathboldsf,
256259
// .textboldsf { font-family: KaTeX_SansSerif; font-weight: bold; }
257-
styles.fontFamily = 'KaTeX_SansSerif';
258-
styles.fontWeight = KatexSpanFontWeight.bold;
260+
fontFamily = 'KaTeX_SansSerif';
261+
fontWeight = KatexSpanFontWeight.bold;
259262

260263
case 'mathsfit':
261264
case 'mathitsf':
262265
case 'textitsf':
263266
// .mathsfit,
264267
// .mathitsf,
265268
// .textitsf { font-family: KaTeX_SansSerif; font-style: italic; }
266-
styles.fontFamily = 'KaTeX_SansSerif';
267-
styles.fontStyle = KatexSpanFontStyle.italic;
269+
fontFamily = 'KaTeX_SansSerif';
270+
fontStyle = KatexSpanFontStyle.italic;
268271

269272
case 'mainrm':
270273
// .mainrm { font-family: KaTeX_Main; font-style: normal; }
271-
styles.fontFamily = 'KaTeX_Main';
272-
styles.fontStyle = KatexSpanFontStyle.normal;
274+
fontFamily = 'KaTeX_Main';
275+
fontStyle = KatexSpanFontStyle.normal;
273276

274277
// TODO handle skipped class declarations between .mainrm and
275278
// .sizing .
@@ -295,20 +298,20 @@ class _KatexParser {
295298
// These indexes start at 1.
296299
if (resetSizeIdx > sizes.length) throw KatexHtmlParseError();
297300
if (sizeIdx > sizes.length) throw KatexHtmlParseError();
298-
styles.fontSizeEm = sizes[sizeIdx - 1] / sizes[resetSizeIdx - 1];
301+
fontSizeEm = sizes[sizeIdx - 1] / sizes[resetSizeIdx - 1];
299302

300303
case 'delimsizing':
301304
// .delimsizing { ... }
302305
if (index + 1 > spanClasses.length) throw KatexHtmlParseError();
303306
switch (spanClasses[index++]) {
304307
case 'size1':
305-
styles.fontFamily = 'KaTeX_Size1';
308+
fontFamily = 'KaTeX_Size1';
306309
case 'size2':
307-
styles.fontFamily = 'KaTeX_Size2';
310+
fontFamily = 'KaTeX_Size2';
308311
case 'size3':
309-
styles.fontFamily = 'KaTeX_Size3';
312+
fontFamily = 'KaTeX_Size3';
310313
case 'size4':
311-
styles.fontFamily = 'KaTeX_Size4';
314+
fontFamily = 'KaTeX_Size4';
312315

313316
case 'mult':
314317
// TODO handle nested spans with `.delim-size{1,4}` class.
@@ -325,9 +328,9 @@ class _KatexParser {
325328
if (index + 1 > spanClasses.length) throw KatexHtmlParseError();
326329
switch (spanClasses[index++]) {
327330
case 'small-op':
328-
styles.fontFamily = 'KaTeX_Size1';
331+
fontFamily = 'KaTeX_Size1';
329332
case 'large-op':
330-
styles.fontFamily = 'KaTeX_Size2';
333+
fontFamily = 'KaTeX_Size2';
331334
default:
332335
throw KatexHtmlParseError();
333336
}
@@ -344,6 +347,13 @@ class _KatexParser {
344347
_logError('KaTeX: Unsupported CSS class: $spanClass');
345348
}
346349
}
350+
final styles = KatexSpanStyles(
351+
fontFamily: fontFamily,
352+
fontSizeEm: fontSizeEm,
353+
fontWeight: fontWeight,
354+
fontStyle: fontStyle,
355+
textAlign: textAlign,
356+
);
347357

348358
String? text;
349359
List<KatexNode>? spans;
@@ -376,14 +386,15 @@ enum KatexSpanTextAlign {
376386
right,
377387
}
378388

389+
@immutable
379390
class KatexSpanStyles {
380-
String? fontFamily;
381-
double? fontSizeEm;
382-
KatexSpanFontWeight? fontWeight;
383-
KatexSpanFontStyle? fontStyle;
384-
KatexSpanTextAlign? textAlign;
391+
final String? fontFamily;
392+
final double? fontSizeEm;
393+
final KatexSpanFontWeight? fontWeight;
394+
final KatexSpanFontStyle? fontStyle;
395+
final KatexSpanTextAlign? textAlign;
385396

386-
KatexSpanStyles({
397+
const KatexSpanStyles({
387398
this.fontFamily,
388399
this.fontSizeEm,
389400
this.fontWeight,

0 commit comments

Comments
 (0)