Skip to content

Commit 0e5fb4e

Browse files
committed
Migrate src to nullsafety
1 parent dccdf30 commit 0e5fb4e

File tree

5 files changed

+198
-213
lines changed

5 files changed

+198
-213
lines changed

lib/src/css_parser.dart

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:csslib/parser.dart' as cssparser;
55
import 'package:flutter/cupertino.dart';
66
import 'package:flutter_html/style.dart';
77

8-
Style declarationsToStyle(Map<String, List<css.Expression>> declarations) {
8+
Style declarationsToStyle(Map<String?, List<css.Expression>> declarations) {
99
Style style = new Style();
1010
declarations.forEach((property, value) {
1111
switch (property) {
@@ -43,26 +43,27 @@ Style declarationsToStyle(Map<String, List<css.Expression>> declarations) {
4343
style.textAlign = ExpressionMapping.expressionToTextAlign(value.first);
4444
break;
4545
case 'text-decoration':
46-
List<css.LiteralTerm> textDecorationList = value.whereType<css.LiteralTerm>().toList();
46+
List<css.LiteralTerm?>? textDecorationList = value.whereType<css.LiteralTerm>().toList();
4747
/// 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();
5151
/// 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;
5454
style.textDecoration = ExpressionMapping.expressionToTextDecorationLine(textDecorationList);
55+
/// flutter tracing isn't working properly here, [textDecorationColor] could be null, ignore this warning for now
5556
if (textDecorationColor != null) style.textDecorationColor = ExpressionMapping.expressionToColor(textDecorationColor);
5657
if (textDecorationStyle != null) style.textDecorationStyle = ExpressionMapping.expressionToTextDecorationStyle(textDecorationStyle);
5758
break;
5859
case 'text-decoration-color':
5960
style.textDecorationColor = ExpressionMapping.expressionToColor(value.first);
6061
break;
6162
case 'text-decoration-line':
62-
style.textDecoration = ExpressionMapping.expressionToTextDecorationLine(value);
63+
style.textDecoration = ExpressionMapping.expressionToTextDecorationLine(value as List<css.LiteralTerm>);
6364
break;
6465
case 'text-decoration-style':
65-
style.textDecorationStyle = ExpressionMapping.expressionToTextDecorationStyle(value.first);
66+
style.textDecorationStyle = ExpressionMapping.expressionToTextDecorationStyle(value.first as css.LiteralTerm);
6667
break;
6768
case 'text-shadow':
6869
style.textShadow = ExpressionMapping.expressionToTextShadow(value);
@@ -72,47 +73,47 @@ Style declarationsToStyle(Map<String, List<css.Expression>> declarations) {
7273
return style;
7374
}
7475

75-
Style inlineCSSToStyle(String inlineStyle) {
76+
Style inlineCSSToStyle(String? inlineStyle) {
7677
final sheet = cssparser.parse("*{$inlineStyle}");
77-
final declarations = DeclarationVisitor().getDeclarations(sheet);
78+
final declarations = DeclarationVisitor().getDeclarations(sheet)!;
7879
return declarationsToStyle(declarations);
7980
}
8081

8182
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;
8485

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>>();
8788
sheet.visit(this);
8889
return _result;
8990
}
9091

9192
@override
9293
void visitDeclaration(css.Declaration node) {
9394
_currentProperty = node.property;
94-
_result[_currentProperty] = <css.Expression>[];
95-
node.expression.visit(this);
95+
_result![_currentProperty] = <css.Expression>[];
96+
node.expression!.visit(this);
9697
}
9798

9899
@override
99100
void visitExpressions(css.Expressions node) {
100101
node.expressions.forEach((expression) {
101-
_result[_currentProperty].add(expression);
102+
_result![_currentProperty]!.add(expression);
102103
});
103104
}
104105
}
105106

106107
//Mapping functions
107108
class ExpressionMapping {
108-
static Color expressionToColor(css.Expression value) {
109+
static Color? expressionToColor(css.Expression value) {
109110
if (value is css.HexColorTerm) {
110111
return stringToColor(value.text);
111112
} else if (value is css.FunctionTerm) {
112113
if (value.text == 'rgba') {
113-
return rgbOrRgbaToColor(value.span.text);
114+
return rgbOrRgbaToColor(value.span!.text);
114115
} else if (value.text == 'rgb') {
115-
return rgbOrRgbaToColor(value.span.text);
116+
return rgbOrRgbaToColor(value.span!.text);
116117
}
117118
}
118119
return null;
@@ -171,15 +172,15 @@ class ExpressionMapping {
171172
return finalFontFeatures;
172173
}
173174

174-
static FontSize expressionToFontSize(css.Expression value) {
175+
static FontSize? expressionToFontSize(css.Expression value) {
175176
if (value is css.NumberTerm) {
176177
return FontSize(double.tryParse(value.text));
177178
} else if (value is css.PercentageTerm) {
178-
return FontSize.percent(int.tryParse(value.text));
179+
return FontSize.percent(int.tryParse(value.text)!);
179180
} else if (value is css.EmTerm) {
180181
return FontSize.em(double.tryParse(value.text));
181182
} else if (value is css.RemTerm) {
182-
return FontSize.rem(double.tryParse(value.text));
183+
return FontSize.rem(double.tryParse(value.text)!);
183184
} else if (value is css.LengthTerm) {
184185
return FontSize(double.tryParse(value.text.replaceAll(new RegExp(r'\s+(\d+\.\d+)\s+'), '')));
185186
} else if (value is css.LiteralTerm) {
@@ -251,20 +252,20 @@ class ExpressionMapping {
251252
return FontWeight.normal;
252253
}
253254

254-
static String expressionToFontFamily(css.Expression value) {
255+
static String? expressionToFontFamily(css.Expression value) {
255256
if (value is css.LiteralTerm) return value.text;
256257
return null;
257258
}
258259

259260
static LineHeight expressionToLineHeight(css.Expression value) {
260261
if (value is css.NumberTerm) {
261-
return LineHeight.number(double.tryParse(value.text));
262+
return LineHeight.number(double.tryParse(value.text)!);
262263
} else if (value is css.PercentageTerm) {
263-
return LineHeight.percent(double.tryParse(value.text));
264+
return LineHeight.percent(double.tryParse(value.text)!);
264265
} else if (value is css.EmTerm) {
265-
return LineHeight.em(double.tryParse(value.text));
266+
return LineHeight.em(double.tryParse(value.text)!);
266267
} else if (value is css.RemTerm) {
267-
return LineHeight.rem(double.tryParse(value.text));
268+
return LineHeight.rem(double.tryParse(value.text)!);
268269
} else if (value is css.LengthTerm) {
269270
return LineHeight(double.tryParse(value.text.replaceAll(new RegExp(r'\s+(\d+\.\d+)\s+'), '')), units: "length");
270271
}
@@ -291,22 +292,24 @@ class ExpressionMapping {
291292
return TextAlign.start;
292293
}
293294

294-
static TextDecoration expressionToTextDecorationLine(List<css.LiteralTerm> value) {
295+
static TextDecoration expressionToTextDecorationLine(List<css.LiteralTerm?> value) {
295296
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+
}
310313
}
311314
}
312315
if (decorationList.contains(TextDecoration.none)) decorationList = [TextDecoration.none];
@@ -346,31 +349,31 @@ class ExpressionMapping {
346349
for (List<css.Expression> list in valueList) {
347350
css.Expression exp = list[0];
348351
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;
351354
RegExp nonNumberRegex = RegExp(r'\s+(\d+\.\d+)\s+');
352355
if (exp is css.LiteralTerm && exp2 is css.LiteralTerm) {
353356
if (exp3 != null && (exp3 is css.HexColorTerm || exp3 is css.FunctionTerm)) {
354357
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, ''))!)
357360
));
358361
} else if (exp3 != null && exp3 is css.LiteralTerm) {
359362
if (exp4 != null && (exp4 is css.HexColorTerm || exp4 is css.FunctionTerm)) {
360363
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, ''))!
364367
));
365368
} else {
366369
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, ''))!
369372
));
370373
}
371374
} else {
372375
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, ''))!)
374377
));
375378
}
376379
}
@@ -393,7 +396,7 @@ class ExpressionMapping {
393396
}
394397
}
395398

396-
static Color rgbOrRgbaToColor(String text) {
399+
static Color? rgbOrRgbaToColor(String text) {
397400
final rgbaText = text.replaceAll(')', '').replaceAll(' ', '');
398401
try {
399402
final rgbaValues =

lib/src/interactable_element.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import 'package:html/dom.dart' as dom;
55

66
/// An [InteractableElement] is a [StyledElement] that takes user gestures (e.g. tap).
77
class InteractableElement extends StyledElement {
8-
String href;
8+
String? href;
99

1010
InteractableElement({
11-
@required String name,
12-
@required List<StyledElement> children,
13-
@required Style style,
14-
@required this.href,
15-
@required dom.Node node,
16-
}) : super(name: name, children: children, style: style, node: node);
11+
required String name,
12+
required List<StyledElement> children,
13+
required Style style,
14+
required this.href,
15+
required dom.Node node,
16+
}) : super(name: name, children: children, style: style, node: node as dom.Element?);
1717
}
1818

1919
/// A [Gesture] indicates the type of interaction by a user.
@@ -26,7 +26,7 @@ InteractableElement parseInteractableElement(
2626
switch (element.localName) {
2727
case "a":
2828
return InteractableElement(
29-
name: element.localName,
29+
name: element.localName!,
3030
children: children,
3131
href: element.attributes['href'],
3232
style: Style(
@@ -38,7 +38,7 @@ InteractableElement parseInteractableElement(
3838
/// will never be called, just to suppress missing return warning
3939
default:
4040
return InteractableElement(
41-
name: element.localName,
41+
name: element.localName!,
4242
children: children,
4343
node: element,
4444
href: '',

0 commit comments

Comments
 (0)