Skip to content

Commit d9f04b1

Browse files
committed
Pre-nullsafety changes
1 parent f3da867 commit d9f04b1

9 files changed

+162
-146
lines changed

lib/flutter_html.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class Html extends StatelessWidget {
3636
Key key,
3737
@required this.data,
3838
this.onLinkTap,
39-
this.customRender,
39+
this.customRender = const {},
4040
this.customImageRenders = const {},
4141
this.onImageError,
4242
this.shrinkWrap = false,
4343
this.onImageTap,
4444
this.blacklistedElements = const [],
45-
this.style,
45+
this.style = const {},
4646
this.navigationDelegateForIframe,
4747
}) : super(key: key);
4848

lib/html_parser.dart

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ class HtmlParser extends StatelessWidget {
3939

4040
HtmlParser({
4141
@required this.htmlData,
42-
this.onLinkTap,
43-
this.onImageTap,
44-
this.onImageError,
45-
this.shrinkWrap,
46-
this.style,
47-
this.customRender,
48-
this.imageRenders,
49-
this.blacklistedElements,
50-
this.navigationDelegateForIframe,
42+
@required this.onLinkTap,
43+
@required this.onImageTap,
44+
@required this.onImageError,
45+
@required this.shrinkWrap,
46+
@required this.style,
47+
@required this.customRender,
48+
@required this.imageRenders,
49+
@required this.blacklistedElements,
50+
@required this.navigationDelegateForIframe,
5151
});
5252

5353
@override
@@ -108,8 +108,9 @@ class HtmlParser extends StatelessWidget {
108108
) {
109109
StyledElement tree = StyledElement(
110110
name: "[Tree Root]",
111-
children: new List<StyledElement>(),
111+
children: <StyledElement>[],
112112
node: html.documentElement,
113+
style: Style(),
113114
);
114115

115116
html.nodes.forEach((node) {
@@ -134,7 +135,7 @@ class HtmlParser extends StatelessWidget {
134135
List<String> blacklistedElements,
135136
NavigationDelegate navigationDelegateForIframe,
136137
) {
137-
List<StyledElement> children = List<StyledElement>();
138+
List<StyledElement> children = <StyledElement>[];
138139

139140
node.nodes.forEach((childNode) {
140141
children.add(_recursiveLexer(
@@ -168,7 +169,7 @@ class HtmlParser extends StatelessWidget {
168169
return EmptyContentElement();
169170
}
170171
} else if (node is dom.Text) {
171-
return TextContentElement(text: node.text);
172+
return TextContentElement(text: node.text, style: Style());
172173
} else {
173174
return EmptyContentElement();
174175
}
@@ -667,7 +668,7 @@ class HtmlParser extends StatelessWidget {
667668
/// or any block-level [TextContentElement] that contains only whitespace and doesn't follow
668669
/// a block element or a line break.
669670
static StyledElement _removeEmptyElements(StyledElement tree) {
670-
List<StyledElement> toRemove = new List<StyledElement>();
671+
List<StyledElement> toRemove = <StyledElement>[];
671672
bool lastChildBlock = true;
672673
tree.children?.forEach((child) {
673674
if (child is EmptyContentElement || child is EmptyLayoutElement) {
@@ -723,9 +724,9 @@ class RenderContext {
723724
final Style style;
724725

725726
RenderContext({
726-
this.buildContext,
727-
this.parser,
728-
this.style,
727+
@required this.buildContext,
728+
@required this.parser,
729+
@required this.style,
729730
});
730731
}
731732

@@ -743,8 +744,8 @@ class ContainerSpan extends StatelessWidget {
743744
ContainerSpan({
744745
this.child,
745746
this.children,
746-
this.style,
747-
this.newContext,
747+
@required this.style,
748+
@required this.newContext,
748749
this.shrinkWrap = false,
749750
});
750751

@@ -780,10 +781,10 @@ class StyledText extends StatelessWidget {
780781
final RenderContext renderContext;
781782

782783
const StyledText({
783-
this.textSpan,
784-
this.style,
784+
@required this.textSpan,
785+
@required this.style,
785786
this.textScaleFactor = 1.0,
786-
this.renderContext,
787+
@required this.renderContext,
787788
});
788789

789790
@override

lib/src/css_parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class DeclarationVisitor extends css.Visitor {
9191
@override
9292
void visitDeclaration(css.Declaration node) {
9393
_currentProperty = node.property;
94-
_result[_currentProperty] = new List<css.Expression>();
94+
_result[_currentProperty] = <css.Expression>[];
9595
node.expression.visit(this);
9696
}
9797

lib/src/interactable_element.dart

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class InteractableElement extends StyledElement {
88
String href;
99

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

@@ -23,21 +23,26 @@ enum Gesture {
2323

2424
InteractableElement parseInteractableElement(
2525
dom.Element element, List<StyledElement> children) {
26-
InteractableElement interactableElement = InteractableElement(
27-
name: element.localName,
28-
children: children,
29-
node: element,
30-
);
31-
3226
switch (element.localName) {
3327
case "a":
34-
interactableElement.href = element.attributes['href'];
35-
interactableElement.style = Style(
36-
color: Colors.blue,
37-
textDecoration: TextDecoration.underline,
28+
return InteractableElement(
29+
name: element.localName,
30+
children: children,
31+
href: element.attributes['href'],
32+
style: Style(
33+
color: Colors.blue,
34+
textDecoration: TextDecoration.underline,
35+
),
36+
node: element,
37+
);
38+
/// will never be called, just to suppress missing return warning
39+
default:
40+
return InteractableElement(
41+
name: element.localName,
42+
children: children,
43+
node: element,
44+
href: '',
45+
style: Style(),
3846
);
39-
break;
4047
}
41-
42-
return interactableElement;
4348
}

lib/src/layout_element.dart

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@ import 'package:html/dom.dart' as dom;
1212
/// an html document with a more complex layout. LayoutElements handle
1313
abstract class LayoutElement extends StyledElement {
1414
LayoutElement({
15-
String name,
16-
List<StyledElement> children,
17-
Style style,
15+
String name = "[[No Name]]",
16+
@required List<StyledElement> children,
1817
dom.Element node,
19-
}) : super(name: name, children: children, style: style, node: node);
18+
}) : super(name: name, children: children, style: Style(), node: node);
2019

2120
Widget toWidget(RenderContext context);
2221
}
2322

2423
class TableLayoutElement extends LayoutElement {
2524
TableLayoutElement({
26-
String name,
27-
Style style,
25+
@required String name,
2826
@required List<StyledElement> children,
29-
dom.Element node,
30-
}) : super(name: name, style: style, children: children, node: node);
27+
@required dom.Element node,
28+
}) : super(name: name, children: children, node: node);
3129

3230
@override
3331
Widget toWidget(RenderContext context) {
@@ -51,8 +49,7 @@ class TableLayoutElement extends LayoutElement {
5149
columnSizes = child.children
5250
.where((c) => c.name == "col")
5351
.map((c) {
54-
final span =
55-
int.parse(c.attributes["span"] ?? "1", onError: (_) => 1);
52+
final span = int.tryParse(c.attributes["span"] ?? "1") ?? 1;
5653
final colWidth = c.attributes["width"];
5754
return List.generate(span, (index) {
5855
if (colWidth != null && colWidth.endsWith("%")) {
@@ -145,16 +142,16 @@ class TableLayoutElement extends LayoutElement {
145142

146143
return LayoutGrid(
147144
gridFit: GridFit.loose,
148-
templateColumnSizes: finalColumnSizes,
149-
templateRowSizes: rowSizes,
145+
columnSizes: finalColumnSizes,
146+
rowSizes: rowSizes,
150147
children: cells,
151148
);
152149
}
153150
}
154151

155152
class TableSectionLayoutElement extends LayoutElement {
156153
TableSectionLayoutElement({
157-
String name,
154+
@required String name,
158155
@required List<StyledElement> children,
159156
}) : super(name: name, children: children);
160157

@@ -167,9 +164,9 @@ class TableSectionLayoutElement extends LayoutElement {
167164

168165
class TableRowLayoutElement extends LayoutElement {
169166
TableRowLayoutElement({
170-
String name,
167+
@required String name,
171168
@required List<StyledElement> children,
172-
dom.Element node,
169+
@required dom.Element node,
173170
}) : super(name: name, children: children, node: node);
174171

175172
@override
@@ -184,12 +181,12 @@ class TableCellElement extends StyledElement {
184181
int rowspan = 1;
185182

186183
TableCellElement({
187-
String name,
188-
String elementId,
189-
List<String> elementClasses,
184+
@required String name,
185+
@required String elementId,
186+
@required List<String> elementClasses,
190187
@required List<StyledElement> children,
191-
Style style,
192-
dom.Element node,
188+
@required Style style,
189+
@required dom.Element node,
193190
}) : super(
194191
name: name,
195192
elementId: elementId,
@@ -217,6 +214,7 @@ TableCellElement parseTableCellElement(
217214
elementClasses: element.classes.toList(),
218215
children: children,
219216
node: element,
217+
style: Style(),
220218
);
221219
if (element.localName == "th") {
222220
cell.style = Style(
@@ -228,10 +226,10 @@ TableCellElement parseTableCellElement(
228226

229227
class TableStyleElement extends StyledElement {
230228
TableStyleElement({
231-
String name,
232-
List<StyledElement> children,
233-
Style style,
234-
dom.Element node,
229+
@required String name,
230+
@required List<StyledElement> children,
231+
@required Style style,
232+
@required dom.Element node,
235233
}) : super(name: name, children: children, style: style, node: node);
236234
}
237235

@@ -246,20 +244,26 @@ TableStyleElement parseTableDefinitionElement(
246244
name: element.localName,
247245
children: children,
248246
node: element,
247+
style: Style(),
249248
);
250249
default:
251-
return TableStyleElement();
250+
return TableStyleElement(
251+
name: "[[No Name]]",
252+
children: children,
253+
node: element,
254+
style: Style(),
255+
);
252256
}
253257
}
254258

255259
class DetailsContentElement extends LayoutElement {
256260
List<dom.Element> elementList;
257261

258262
DetailsContentElement({
259-
String name,
260-
List<StyledElement> children,
261-
dom.Element node,
262-
this.elementList,
263+
@required String name,
264+
@required List<StyledElement> children,
265+
@required dom.Element node,
266+
@required this.elementList,
263267
}) : super(name: name, node: node, children: children);
264268

265269
@override
@@ -311,7 +315,7 @@ class DetailsContentElement extends LayoutElement {
311315
}
312316

313317
class EmptyLayoutElement extends LayoutElement {
314-
EmptyLayoutElement({String name = "empty"}) : super(name: name);
318+
EmptyLayoutElement({@required String name}) : super(name: name, children: []);
315319

316320
@override
317321
Widget toWidget(_) => null;
@@ -324,7 +328,7 @@ LayoutElement parseLayoutElement(
324328
switch (element.localName) {
325329
case "details":
326330
if (children?.isEmpty ?? false) {
327-
return EmptyLayoutElement();
331+
return EmptyLayoutElement(name: "empty");
328332
}
329333
return DetailsContentElement(
330334
node: element,
@@ -355,6 +359,10 @@ LayoutElement parseLayoutElement(
355359
);
356360
break;
357361
default:
358-
return TableLayoutElement(children: children);
362+
return TableLayoutElement(
363+
children: children,
364+
name: "[[No Name]]",
365+
node: element
366+
);
359367
}
360368
}

0 commit comments

Comments
 (0)