Skip to content

Commit b93e79b

Browse files
committed
Format files
1 parent 0c68d36 commit b93e79b

10 files changed

+157
-89
lines changed

lib/flutter_html.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ class Html extends StatelessWidget {
121121
}
122122

123123
return Container(
124-
color: backgroundColor,
125-
width: width,
126-
child: HtmlParser(
127-
htmlData: data,
128-
cssData: css,
129-
onLinkTap: onLinkTap,
130-
onImageTap: onImageTap,
131-
onImageError: onImageError,
132-
shrinkWrap: shrinkWrap,
133-
style: style,
134-
customRender: customRender,
135-
blacklistedElements: blacklistedElements,
136-
),
124+
color: backgroundColor,
125+
width: width,
126+
child: HtmlParser(
127+
htmlData: data,
128+
cssData: css,
129+
onLinkTap: onLinkTap,
130+
onImageTap: onImageTap,
131+
onImageError: onImageError,
132+
shrinkWrap: shrinkWrap,
133+
style: style,
134+
customRender: customRender,
135+
blacklistedElements: blacklistedElements,
136+
),
137137
);
138138
}
139139
}

lib/html_parser.dart

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,19 @@ class HtmlParser extends StatelessWidget {
7878

7979
/// [lexDomTree] converts a DOM document to a simplified tree of [StyledElement]s.
8080
static StyledElement lexDomTree(
81-
dom.Document html, List<String> customRenderTags, List<String> blacklistedElements) {
81+
dom.Document html,
82+
List<String> customRenderTags,
83+
List<String> blacklistedElements,
84+
) {
8285
StyledElement tree = StyledElement(
8386
name: "[Tree Root]",
8487
children: new List<StyledElement>(),
8588
node: html.documentElement,
8689
);
8790

8891
html.nodes.forEach((node) {
89-
tree.children.add(_recursiveLexer(node, customRenderTags, blacklistedElements));
92+
tree.children
93+
.add(_recursiveLexer(node, customRenderTags, blacklistedElements));
9094
});
9195

9296
return tree;
@@ -97,11 +101,15 @@ class HtmlParser extends StatelessWidget {
97101
/// It runs the parse functions of every type of
98102
/// element and returns a [StyledElement] tree representing the element.
99103
static StyledElement _recursiveLexer(
100-
dom.Node node, List<String> customRenderTags, List<String> blacklistedElements) {
104+
dom.Node node,
105+
List<String> customRenderTags,
106+
List<String> blacklistedElements,
107+
) {
101108
List<StyledElement> children = List<StyledElement>();
102109

103110
node.nodes.forEach((childNode) {
104-
children.add(_recursiveLexer(childNode, customRenderTags, blacklistedElements));
111+
children.add(
112+
_recursiveLexer(childNode, customRenderTags, blacklistedElements));
105113
});
106114

107115
//TODO(Sub6Resources): There's probably a more efficient way to look this up.
@@ -226,7 +234,10 @@ class HtmlParser extends StatelessWidget {
226234
newContext: newContext,
227235
style: tree.style,
228236
shrinkWrap: context.parser.shrinkWrap,
229-
children: tree.children?.map((tree) => parseTree(newContext, tree))?.toList() ?? [],
237+
children: tree.children
238+
?.map((tree) => parseTree(newContext, tree))
239+
?.toList() ??
240+
[],
230241
),
231242
tree.attributes,
232243
),
@@ -241,7 +252,10 @@ class HtmlParser extends StatelessWidget {
241252
newContext: newContext,
242253
style: tree.style,
243254
shrinkWrap: context.parser.shrinkWrap,
244-
children: tree.children?.map((tree) => parseTree(newContext, tree))?.toList() ?? [],
255+
children: tree.children
256+
?.map((tree) => parseTree(newContext, tree))
257+
?.toList() ??
258+
[],
245259
),
246260
);
247261
} else if (tree.style?.display == Display.LIST_ITEM) {
@@ -256,14 +270,18 @@ class HtmlParser extends StatelessWidget {
256270
width: 30, //TODO derive this from list padding.
257271
start: 0,
258272
child: Text('${newContext.style.markerContent}\t',
259-
textAlign: TextAlign.right, style: newContext.style.generateTextStyle()),
273+
textAlign: TextAlign.right,
274+
style: newContext.style.generateTextStyle()),
260275
),
261276
Padding(
262-
padding: EdgeInsets.only(left: 30), //TODO derive this from list padding.
277+
padding: EdgeInsets.only(
278+
left: 30), //TODO derive this from list padding.
263279
child: RichText(
264280
text: TextSpan(
265-
children:
266-
tree.children?.map((tree) => parseTree(newContext, tree))?.toList() ?? [],
281+
children: tree.children
282+
?.map((tree) => parseTree(newContext, tree))
283+
?.toList() ??
284+
[],
267285
style: newContext.style.generateTextStyle(),
268286
),
269287
),
@@ -286,17 +304,21 @@ class HtmlParser extends StatelessWidget {
286304
return WidgetSpan(
287305
child: RawGestureDetector(
288306
gestures: {
289-
MultipleTapGestureRecognizer: GestureRecognizerFactoryWithHandlers<MultipleTapGestureRecognizer>(
290-
() => MultipleTapGestureRecognizer(),
291-
(instance) {
292-
instance..onTap = () => onLinkTap?.call(tree.href);
293-
},
307+
MultipleTapGestureRecognizer: GestureRecognizerFactoryWithHandlers<
308+
MultipleTapGestureRecognizer>(
309+
() => MultipleTapGestureRecognizer(),
310+
(instance) {
311+
instance..onTap = () => onLinkTap?.call(tree.href);
312+
},
294313
),
295314
},
296315
child: RichText(
297316
text: TextSpan(
298317
style: newContext.style.generateTextStyle(),
299-
children: tree.children.map((tree) => parseTree(newContext, tree)).toList() ?? [],
318+
children: tree.children
319+
.map((tree) => parseTree(newContext, tree))
320+
.toList() ??
321+
[],
300322
),
301323
),
302324
),
@@ -325,7 +347,10 @@ class HtmlParser extends StatelessWidget {
325347
child: RichText(
326348
text: TextSpan(
327349
style: newContext.style.generateTextStyle(),
328-
children: tree.children.map((tree) => parseTree(newContext, tree)).toList() ?? [],
350+
children: tree.children
351+
.map((tree) => parseTree(newContext, tree))
352+
.toList() ??
353+
[],
329354
),
330355
),
331356
),
@@ -334,7 +359,8 @@ class HtmlParser extends StatelessWidget {
334359
///[tree] is an inline element.
335360
return TextSpan(
336361
style: newContext.style.generateTextStyle(),
337-
children: tree.children.map((tree) => parseTree(newContext, tree)).toList(),
362+
children:
363+
tree.children.map((tree) => parseTree(newContext, tree)).toList(),
338364
);
339365
}
340366
}
@@ -367,7 +393,10 @@ class HtmlParser extends StatelessWidget {
367393
/// [_processInlineWhitespaceRecursive] analyzes the whitespace between and among different
368394
/// inline elements, and replaces any instance of two or more spaces with a single space, according
369395
/// to the w3's HTML whitespace processing specification linked to above.
370-
static StyledElement _processInlineWhitespaceRecursive(StyledElement tree, Context<bool> wpc) {
396+
static StyledElement _processInlineWhitespaceRecursive(
397+
StyledElement tree,
398+
Context<bool> wpc,
399+
) {
371400
if (tree.style.display == Display.BLOCK) {
372401
wpc.data = false;
373402
}
@@ -500,7 +529,8 @@ class HtmlParser extends StatelessWidget {
500529
if (tree.children.first.style.margin == null) {
501530
tree.children.first.style.margin = EdgeInsets.zero;
502531
} else {
503-
tree.children.first.style.margin = tree.children.first.style.margin.copyWith(top: 0);
532+
tree.children.first.style.margin =
533+
tree.children.first.style.margin.copyWith(top: 0);
504534
}
505535
}
506536

@@ -515,33 +545,38 @@ class HtmlParser extends StatelessWidget {
515545
if (tree.style.margin == null) {
516546
tree.style.margin = EdgeInsets.only(bottom: newOuterMarginBottom);
517547
} else {
518-
tree.style.margin = tree.style.margin.copyWith(bottom: newOuterMarginBottom);
548+
tree.style.margin =
549+
tree.style.margin.copyWith(bottom: newOuterMarginBottom);
519550
}
520551

521552
// And remove the child's margin
522553
if (tree.children.last.style.margin == null) {
523554
tree.children.last.style.margin = EdgeInsets.zero;
524555
} else {
525-
tree.children.last.style.margin = tree.children.last.style.margin.copyWith(bottom: 0);
556+
tree.children.last.style.margin =
557+
tree.children.last.style.margin.copyWith(bottom: 0);
526558
}
527559
}
528560

529561
// Handle case (2) from above.
530562
if (tree.children.length > 1) {
531563
for (int i = 1; i < tree.children.length; i++) {
532-
final previousSiblingBottom = tree.children[i - 1].style.margin?.bottom ?? 0;
564+
final previousSiblingBottom =
565+
tree.children[i - 1].style.margin?.bottom ?? 0;
533566
final thisTop = tree.children[i].style.margin?.top ?? 0;
534567
final newInternalMargin = max(previousSiblingBottom, thisTop) / 2;
535568

536569
if (tree.children[i - 1].style.margin == null) {
537-
tree.children[i - 1].style.margin = EdgeInsets.only(bottom: newInternalMargin);
538-
} else {
539570
tree.children[i - 1].style.margin =
540-
tree.children[i - 1].style.margin.copyWith(bottom: newInternalMargin);
571+
EdgeInsets.only(bottom: newInternalMargin);
572+
} else {
573+
tree.children[i - 1].style.margin = tree.children[i - 1].style.margin
574+
.copyWith(bottom: newInternalMargin);
541575
}
542576

543577
if (tree.children[i].style.margin == null) {
544-
tree.children[i].style.margin = EdgeInsets.only(top: newInternalMargin);
578+
tree.children[i].style.margin =
579+
EdgeInsets.only(top: newInternalMargin);
545580
} else {
546581
tree.children[i].style.margin =
547582
tree.children[i].style.margin.copyWith(top: newInternalMargin);
@@ -584,7 +619,8 @@ class HtmlParser extends StatelessWidget {
584619

585620
tree.children?.forEach((child) {
586621
if ((child.style.fontSize?.size ?? parentFontSize) < 0) {
587-
child.style.fontSize = FontSize(parentFontSize * -child.style.fontSize.size);
622+
child.style.fontSize =
623+
FontSize(parentFontSize * -child.style.fontSize.size);
588624
}
589625

590626
_processFontSize(child);
@@ -639,7 +675,7 @@ class ContainerSpan extends StatelessWidget {
639675
width: style?.width,
640676
padding: style?.padding,
641677
margin: style?.margin,
642-
alignment: shrinkWrap? null: style?.alignment,
678+
alignment: shrinkWrap ? null : style?.alignment,
643679
child: child ??
644680
RichText(
645681
text: TextSpan(

lib/rich_text_parser.dart

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class LinkTextSpan extends TextSpan {
3434

3535
LinkTextSpan(
3636
{TextStyle style,
37-
this.url,
38-
String text,
39-
OnTap onLinkTap,
40-
List<TextSpan> children})
37+
this.url,
38+
String text,
39+
OnTap onLinkTap,
40+
List<TextSpan> children})
4141
: super(
4242
style: style,
4343
text: text,
@@ -410,7 +410,8 @@ class HtmlRichTextParser extends StatelessWidget {
410410
Decoration decoration;
411411
if (parseContext.blockType == 'blockquote') {
412412
decoration = BoxDecoration(
413-
border: Border(left: BorderSide(color: Colors.black38, width: 2.0)),
413+
border:
414+
Border(left: BorderSide(color: Colors.black38, width: 2.0)),
414415
);
415416
parseContext.childStyle = parseContext.childStyle.merge(TextStyle(
416417
fontStyle: FontStyle.italic,
@@ -444,7 +445,8 @@ class HtmlRichTextParser extends StatelessWidget {
444445
} else if (parseContext.parentElement is LinkTextSpan) {
445446
// add this node to the parent as another LinkTextSpan
446447
parseContext.parentElement.children.add(LinkTextSpan(
447-
style: parseContext.parentElement.style.merge(parseContext.childStyle),
448+
style:
449+
parseContext.parentElement.style.merge(parseContext.childStyle),
448450
url: parseContext.parentElement.url,
449451
text: finalText,
450452
onLinkTap: onLinkTap,
@@ -628,9 +630,10 @@ class HtmlRichTextParser extends StatelessWidget {
628630
crossAxisAlignment: CrossAxisAlignment.start,
629631
children: <Widget>[],
630632
);
631-
nextContext.rootWidgetList.add(Container(
632-
margin: EdgeInsets.symmetric(vertical: 12.0),
633-
child: nextContext.parentElement),
633+
nextContext.rootWidgetList.add(
634+
Container(
635+
margin: EdgeInsets.symmetric(vertical: 12.0),
636+
child: nextContext.parentElement),
634637
);
635638
break;
636639

@@ -650,7 +653,8 @@ class HtmlRichTextParser extends StatelessWidget {
650653
fontWeight: (node.localName == 'th')
651654
? FontWeight.bold
652655
: FontWeight.normal));
653-
RichText text = RichText(text: TextSpan(text: '', children: <TextSpan>[]));
656+
RichText text =
657+
RichText(text: TextSpan(text: '', children: <TextSpan>[]));
654658
Expanded cell = Expanded(
655659
flex: colspan,
656660
child: Container(padding: EdgeInsets.all(1.0), child: text),
@@ -784,7 +788,8 @@ class HtmlRichTextParser extends StatelessWidget {
784788
},
785789
));
786790
} else if (node.attributes['src'].startsWith('asset:')) {
787-
final assetPath = node.attributes['src'].replaceFirst('asset:', '');
791+
final assetPath =
792+
node.attributes['src'].replaceFirst('asset:', '');
788793
precacheImage(
789794
AssetImage(assetPath),
790795
buildContext,
@@ -965,7 +970,8 @@ class HtmlRichTextParser extends StatelessWidget {
965970
Decoration decoration;
966971
if (parseContext.blockType == 'blockquote') {
967972
decoration = BoxDecoration(
968-
border: Border(left: BorderSide(color: Colors.black38, width: 2.0)),
973+
border:
974+
Border(left: BorderSide(color: Colors.black38, width: 2.0)),
969975
);
970976
nextContext.childStyle = nextContext.childStyle.merge(TextStyle(
971977
fontStyle: FontStyle.italic,
@@ -1019,4 +1025,4 @@ class HtmlRichTextParser extends StatelessWidget {
10191025
}
10201026
return stringToTrim;
10211027
}
1022-
}
1028+
}

lib/src/html_elements.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ const LAYOUT_ELEMENTS = [
9797
"thead",
9898
];
9999

100-
const TABLE_STYLE_ELEMENTS = [
101-
"col",
102-
"colgroup"
103-
];
100+
const TABLE_STYLE_ELEMENTS = ["col", "colgroup"];
104101

105102
/**
106103
Here is a list of elements with planned support:
@@ -188,4 +185,4 @@ const TABLE_STYLE_ELEMENTS = [
188185
var - s [x]
189186
video - c [x]
190187
wbr - s [x]
191-
*/
188+
*/

lib/src/interactable_element.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ InteractableElement parseInteractableElement(
3333
case "a":
3434
interactableElement.href = element.attributes['href'];
3535
interactableElement.style = Style(
36-
color: Colors.blue,
37-
textDecoration: TextDecoration.underline,
36+
color: Colors.blue,
37+
textDecoration: TextDecoration.underline,
3838
);
3939
break;
4040
}

0 commit comments

Comments
 (0)