@@ -5,7 +5,7 @@ import 'package:csslib/parser.dart' as cssparser;
5
5
import 'package:flutter/cupertino.dart' ;
6
6
import 'package:flutter_html/style.dart' ;
7
7
8
- Style declarationsToStyle (Map <String , List <css.Expression >> declarations) {
8
+ Style declarationsToStyle (Map <String ? , List <css.Expression >> declarations) {
9
9
Style style = new Style ();
10
10
declarations.forEach ((property, value) {
11
11
switch (property) {
@@ -43,26 +43,27 @@ Style declarationsToStyle(Map<String, List<css.Expression>> declarations) {
43
43
style.textAlign = ExpressionMapping .expressionToTextAlign (value.first);
44
44
break ;
45
45
case 'text-decoration' :
46
- List <css.LiteralTerm > textDecorationList = value.whereType< css.LiteralTerm > ().toList ();
46
+ List <css.LiteralTerm ?> ? textDecorationList = value.whereType< css.LiteralTerm > ().toList ();
47
47
/// List<css.LiteralTerm> might include other values than the ones we want for [textDecorationList] , so make sure to remove those before passing it to [ExpressionMapping]
48
- textDecorationList.removeWhere ((element) => element.text != "none" && element.text != "overline" && element.text != "underline" && element.text != "line-through" );
49
- css.Expression textDecorationColor = value.firstWhere ((element) => element is css.HexColorTerm || element is css.FunctionTerm , orElse: null );
50
- List <css.LiteralTerm > temp = value.whereType< css.LiteralTerm > ().toList ();
48
+ textDecorationList.removeWhere ((element) => element != null && element .text != "none" && element.text != "overline" && element.text != "underline" && element.text != "line-through" );
49
+ css.Expression ? textDecorationColor = value.firstWhere ((element) => element is css.HexColorTerm || element is css.FunctionTerm , orElse: null );
50
+ List <css.LiteralTerm ?> ? temp = value.whereType< css.LiteralTerm > ().toList ();
51
51
/// List<css.LiteralTerm> might include other values than the ones we want for [textDecorationStyle] , so make sure to remove those before passing it to [ExpressionMapping]
52
- temp.removeWhere ((element) => element.text != "solid" && element.text != "double" && element.text != "dashed" && element.text != "dotted" && element.text != "wavy" );
53
- css.LiteralTerm textDecorationStyle = temp.last ?? null ;
52
+ temp.removeWhere ((element) => element != null && element .text != "solid" && element.text != "double" && element.text != "dashed" && element.text != "dotted" && element.text != "wavy" );
53
+ css.LiteralTerm ? textDecorationStyle = temp.last;
54
54
style.textDecoration = ExpressionMapping .expressionToTextDecorationLine (textDecorationList);
55
+ /// flutter tracing isn't working properly here, [textDecorationColor] could be null, ignore this warning for now
55
56
if (textDecorationColor != null ) style.textDecorationColor = ExpressionMapping .expressionToColor (textDecorationColor);
56
57
if (textDecorationStyle != null ) style.textDecorationStyle = ExpressionMapping .expressionToTextDecorationStyle (textDecorationStyle);
57
58
break ;
58
59
case 'text-decoration-color' :
59
60
style.textDecorationColor = ExpressionMapping .expressionToColor (value.first);
60
61
break ;
61
62
case 'text-decoration-line' :
62
- style.textDecoration = ExpressionMapping .expressionToTextDecorationLine (value);
63
+ style.textDecoration = ExpressionMapping .expressionToTextDecorationLine (value as List <css. LiteralTerm > );
63
64
break ;
64
65
case 'text-decoration-style' :
65
- style.textDecorationStyle = ExpressionMapping .expressionToTextDecorationStyle (value.first);
66
+ style.textDecorationStyle = ExpressionMapping .expressionToTextDecorationStyle (value.first as css. LiteralTerm );
66
67
break ;
67
68
case 'text-shadow' :
68
69
style.textShadow = ExpressionMapping .expressionToTextShadow (value);
@@ -72,47 +73,47 @@ Style declarationsToStyle(Map<String, List<css.Expression>> declarations) {
72
73
return style;
73
74
}
74
75
75
- Style inlineCSSToStyle (String inlineStyle) {
76
+ Style inlineCSSToStyle (String ? inlineStyle) {
76
77
final sheet = cssparser.parse ("*{$inlineStyle }" );
77
- final declarations = DeclarationVisitor ().getDeclarations (sheet);
78
+ final declarations = DeclarationVisitor ().getDeclarations (sheet)! ;
78
79
return declarationsToStyle (declarations);
79
80
}
80
81
81
82
class DeclarationVisitor extends css.Visitor {
82
- Map <String , List <css.Expression >> _result;
83
- String _currentProperty;
83
+ Map <String ? , List <css.Expression >>? _result;
84
+ String ? _currentProperty;
84
85
85
- Map <String , List <css.Expression >> getDeclarations (css.StyleSheet sheet) {
86
- _result = new Map <String , List <css.Expression >>();
86
+ Map <String ? , List <css.Expression >>? getDeclarations (css.StyleSheet sheet) {
87
+ _result = new Map <String ? , List <css.Expression >>();
87
88
sheet.visit (this );
88
89
return _result;
89
90
}
90
91
91
92
@override
92
93
void visitDeclaration (css.Declaration node) {
93
94
_currentProperty = node.property;
94
- _result[_currentProperty] = < css.Expression > [];
95
- node.expression.visit (this );
95
+ _result! [_currentProperty] = < css.Expression > [];
96
+ node.expression! .visit (this );
96
97
}
97
98
98
99
@override
99
100
void visitExpressions (css.Expressions node) {
100
101
node.expressions.forEach ((expression) {
101
- _result[_currentProperty].add (expression);
102
+ _result! [_currentProperty]! .add (expression);
102
103
});
103
104
}
104
105
}
105
106
106
107
//Mapping functions
107
108
class ExpressionMapping {
108
- static Color expressionToColor (css.Expression value) {
109
+ static Color ? expressionToColor (css.Expression value) {
109
110
if (value is css.HexColorTerm ) {
110
111
return stringToColor (value.text);
111
112
} else if (value is css.FunctionTerm ) {
112
113
if (value.text == 'rgba' ) {
113
- return rgbOrRgbaToColor (value.span.text);
114
+ return rgbOrRgbaToColor (value.span! .text);
114
115
} else if (value.text == 'rgb' ) {
115
- return rgbOrRgbaToColor (value.span.text);
116
+ return rgbOrRgbaToColor (value.span! .text);
116
117
}
117
118
}
118
119
return null ;
@@ -171,15 +172,15 @@ class ExpressionMapping {
171
172
return finalFontFeatures;
172
173
}
173
174
174
- static FontSize expressionToFontSize (css.Expression value) {
175
+ static FontSize ? expressionToFontSize (css.Expression value) {
175
176
if (value is css.NumberTerm ) {
176
177
return FontSize (double .tryParse (value.text));
177
178
} else if (value is css.PercentageTerm ) {
178
- return FontSize .percent (int .tryParse (value.text));
179
+ return FontSize .percent (int .tryParse (value.text)! );
179
180
} else if (value is css.EmTerm ) {
180
181
return FontSize .em (double .tryParse (value.text));
181
182
} else if (value is css.RemTerm ) {
182
- return FontSize .rem (double .tryParse (value.text));
183
+ return FontSize .rem (double .tryParse (value.text)! );
183
184
} else if (value is css.LengthTerm ) {
184
185
return FontSize (double .tryParse (value.text.replaceAll (new RegExp (r'\s+(\d+\.\d+)\s+' ), '' )));
185
186
} else if (value is css.LiteralTerm ) {
@@ -251,20 +252,20 @@ class ExpressionMapping {
251
252
return FontWeight .normal;
252
253
}
253
254
254
- static String expressionToFontFamily (css.Expression value) {
255
+ static String ? expressionToFontFamily (css.Expression value) {
255
256
if (value is css.LiteralTerm ) return value.text;
256
257
return null ;
257
258
}
258
259
259
260
static LineHeight expressionToLineHeight (css.Expression value) {
260
261
if (value is css.NumberTerm ) {
261
- return LineHeight .number (double .tryParse (value.text));
262
+ return LineHeight .number (double .tryParse (value.text)! );
262
263
} else if (value is css.PercentageTerm ) {
263
- return LineHeight .percent (double .tryParse (value.text));
264
+ return LineHeight .percent (double .tryParse (value.text)! );
264
265
} else if (value is css.EmTerm ) {
265
- return LineHeight .em (double .tryParse (value.text));
266
+ return LineHeight .em (double .tryParse (value.text)! );
266
267
} else if (value is css.RemTerm ) {
267
- return LineHeight .rem (double .tryParse (value.text));
268
+ return LineHeight .rem (double .tryParse (value.text)! );
268
269
} else if (value is css.LengthTerm ) {
269
270
return LineHeight (double .tryParse (value.text.replaceAll (new RegExp (r'\s+(\d+\.\d+)\s+' ), '' )), units: "length" );
270
271
}
@@ -291,22 +292,24 @@ class ExpressionMapping {
291
292
return TextAlign .start;
292
293
}
293
294
294
- static TextDecoration expressionToTextDecorationLine (List <css.LiteralTerm > value) {
295
+ static TextDecoration expressionToTextDecorationLine (List <css.LiteralTerm ? > value) {
295
296
List <TextDecoration > decorationList = [];
296
- for (css.LiteralTerm term in value) {
297
- switch (term.text) {
298
- case "overline" :
299
- decorationList.add (TextDecoration .overline);
300
- break ;
301
- case "underline" :
302
- decorationList.add (TextDecoration .underline);
303
- break ;
304
- case "line-through" :
305
- decorationList.add (TextDecoration .lineThrough);
306
- break ;
307
- default :
308
- decorationList.add (TextDecoration .none);
309
- break ;
297
+ for (css.LiteralTerm ? term in value) {
298
+ if (term != null ) {
299
+ switch (term.text) {
300
+ case "overline" :
301
+ decorationList.add (TextDecoration .overline);
302
+ break ;
303
+ case "underline" :
304
+ decorationList.add (TextDecoration .underline);
305
+ break ;
306
+ case "line-through" :
307
+ decorationList.add (TextDecoration .lineThrough);
308
+ break ;
309
+ default :
310
+ decorationList.add (TextDecoration .none);
311
+ break ;
312
+ }
310
313
}
311
314
}
312
315
if (decorationList.contains (TextDecoration .none)) decorationList = [TextDecoration .none];
@@ -346,31 +349,31 @@ class ExpressionMapping {
346
349
for (List <css.Expression > list in valueList) {
347
350
css.Expression exp = list[0 ];
348
351
css.Expression exp2 = list[1 ];
349
- css.LiteralTerm exp3 = list.length > 2 ? list[2 ] : null ;
350
- css.LiteralTerm exp4 = list.length > 3 ? list[3 ] : null ;
352
+ css.LiteralTerm ? exp3 = list.length > 2 ? list[2 ] as css. LiteralTerm ? : null ;
353
+ css.LiteralTerm ? exp4 = list.length > 3 ? list[3 ] as css. LiteralTerm ? : null ;
351
354
RegExp nonNumberRegex = RegExp (r'\s+(\d+\.\d+)\s+' );
352
355
if (exp is css.LiteralTerm && exp2 is css.LiteralTerm ) {
353
356
if (exp3 != null && (exp3 is css.HexColorTerm || exp3 is css.FunctionTerm )) {
354
357
shadow.add (Shadow (
355
- color: expressionToColor (exp3),
356
- offset: Offset (double .tryParse (exp.text.replaceAll (nonNumberRegex, '' )), double .tryParse (exp2.text.replaceAll (nonNumberRegex, '' )))
358
+ color: expressionToColor (exp3)! ,
359
+ offset: Offset (double .tryParse (exp.text.replaceAll (nonNumberRegex, '' ))! , double .tryParse (exp2.text.replaceAll (nonNumberRegex, '' ))! )
357
360
));
358
361
} else if (exp3 != null && exp3 is css.LiteralTerm ) {
359
362
if (exp4 != null && (exp4 is css.HexColorTerm || exp4 is css.FunctionTerm )) {
360
363
shadow.add (Shadow (
361
- color: expressionToColor (exp4),
362
- offset: Offset (double .tryParse (exp.text.replaceAll (nonNumberRegex, '' )), double .tryParse (exp2.text.replaceAll (nonNumberRegex, '' ))),
363
- blurRadius: double .tryParse (exp3.text.replaceAll (nonNumberRegex, '' ))
364
+ color: expressionToColor (exp4)! ,
365
+ offset: Offset (double .tryParse (exp.text.replaceAll (nonNumberRegex, '' ))! , double .tryParse (exp2.text.replaceAll (nonNumberRegex, '' ))! ),
366
+ blurRadius: double .tryParse (exp3.text.replaceAll (nonNumberRegex, '' ))!
364
367
));
365
368
} else {
366
369
shadow.add (Shadow (
367
- offset: Offset (double .tryParse (exp.text.replaceAll (nonNumberRegex, '' )), double .tryParse (exp2.text.replaceAll (nonNumberRegex, '' ))),
368
- blurRadius: double .tryParse (exp3.text.replaceAll (nonNumberRegex, '' ))
370
+ offset: Offset (double .tryParse (exp.text.replaceAll (nonNumberRegex, '' ))! , double .tryParse (exp2.text.replaceAll (nonNumberRegex, '' ))! ),
371
+ blurRadius: double .tryParse (exp3.text.replaceAll (nonNumberRegex, '' ))!
369
372
));
370
373
}
371
374
} else {
372
375
shadow.add (Shadow (
373
- offset: Offset (double .tryParse (exp.text.replaceAll (nonNumberRegex, '' )), double .tryParse (exp2.text.replaceAll (nonNumberRegex, '' )))
376
+ offset: Offset (double .tryParse (exp.text.replaceAll (nonNumberRegex, '' ))! , double .tryParse (exp2.text.replaceAll (nonNumberRegex, '' ))! )
374
377
));
375
378
}
376
379
}
@@ -393,7 +396,7 @@ class ExpressionMapping {
393
396
}
394
397
}
395
398
396
- static Color rgbOrRgbaToColor (String text) {
399
+ static Color ? rgbOrRgbaToColor (String text) {
397
400
final rgbaText = text.replaceAll (')' , '' ).replaceAll (' ' , '' );
398
401
try {
399
402
final rgbaValues =
0 commit comments