Skip to content

Commit

Permalink
Run flutter formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sub6Resources committed Sep 26, 2022
1 parent 8b06d38 commit c53cb80
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 115 deletions.
83 changes: 50 additions & 33 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ class MyHomePageState extends State<MyHomePage> {
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.arrow_downward),
onPressed: () {
final anchorContext = AnchorKey.forId(staticAnchorKey, "bottom")?.currentContext;
final anchorContext =
AnchorKey.forId(staticAnchorKey, "bottom")?.currentContext;
if (anchorContext != null) {
Scrollable.ensureVisible(anchorContext);
}
Expand Down Expand Up @@ -294,51 +295,64 @@ class MyHomePageState extends State<MyHomePage> {
},
tagsList: Html.tags..addAll(['tex', 'bird', 'flutter']),
customRenders: {
tagMatcher("tex"): CustomRender.widget(widget: (context, buildChildren) => Math.tex(
context.tree.element?.innerHtml ?? '',
mathStyle: MathStyle.display,
textStyle: context.style.generateTextStyle(),
onErrorFallback: (FlutterMathException e) {
return Text(e.message);
},
)),
tagMatcher("bird"): CustomRender.inlineSpan(inlineSpan: (context, buildChildren) => const TextSpan(text: "🐦")),
tagMatcher("flutter"): CustomRender.widget(widget: (context, buildChildren) => FlutterLogo(
style: (context.tree.element!.attributes['horizontal'] != null)
? FlutterLogoStyle.horizontal
: FlutterLogoStyle.markOnly,
textColor: context.style.color!,
size: context.style.fontSize!.value * 5,
)),
tagMatcher("table"): CustomRender.widget(widget: (context, buildChildren) => SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: tableRender.call().widget!.call(context, buildChildren),
)),
tagMatcher("tex"): CustomRender.widget(
widget: (context, buildChildren) => Math.tex(
context.tree.element?.innerHtml ?? '',
mathStyle: MathStyle.display,
textStyle: context.style.generateTextStyle(),
onErrorFallback: (FlutterMathException e) {
return Text(e.message);
},
)),
tagMatcher("bird"): CustomRender.inlineSpan(
inlineSpan: (context, buildChildren) =>
const TextSpan(text: "🐦")),
tagMatcher("flutter"): CustomRender.widget(
widget: (context, buildChildren) => FlutterLogo(
style: (context.tree.element!.attributes['horizontal'] !=
null)
? FlutterLogoStyle.horizontal
: FlutterLogoStyle.markOnly,
textColor: context.style.color!,
size: context.style.fontSize!.value * 5,
)),
tagMatcher("table"): CustomRender.widget(
widget: (context, buildChildren) => SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: tableRender
.call()
.widget!
.call(context, buildChildren),
)),
audioMatcher(): audioRender(),
iframeMatcher(): iframeRender(),
mathMatcher(): mathRender(onMathError: (error, exception, exceptionWithType) {
mathMatcher():
mathRender(onMathError: (error, exception, exceptionWithType) {
debugPrint(exception);
return Text(exception);
}),
svgTagMatcher(): svgTagRender(),
svgDataUriMatcher(): svgDataImageRender(),
svgAssetUriMatcher(): svgAssetImageRender(),
svgNetworkSourceMatcher(): svgNetworkImageRender(),
networkSourceMatcher(domains: ["flutter.dev"]): CustomRender.widget(
widget: (context, buildChildren) {
return const FlutterLogo(size: 36);
}),
networkSourceMatcher(domains: ["flutter.dev"]):
CustomRender.widget(widget: (context, buildChildren) {
return const FlutterLogo(size: 36);
}),
networkSourceMatcher(domains: ["mydomain.com"]): networkImageRender(
headers: {"Custom-Header": "some-value"},
altWidget: (alt) => Text(alt ?? ""),
loadingWidget: () => const Text("Loading..."),
),
// On relative paths starting with /wiki, prefix with a base url
(context) => context.tree.element?.attributes["src"] != null
&& context.tree.element!.attributes["src"]!.startsWith("/wiki"):
networkImageRender(mapUrl: (url) => "https://upload.wikimedia.org${url!}"),
(context) =>
context.tree.element?.attributes["src"] != null &&
context.tree.element!.attributes["src"]!
.startsWith("/wiki"): networkImageRender(
mapUrl: (url) => "https://upload.wikimedia.org${url!}"),
// Custom placeholder image for broken links
networkSourceMatcher(): networkImageRender(altWidget: (_) => const FlutterLogo()),
networkSourceMatcher():
networkImageRender(altWidget: (_) => const FlutterLogo()),
videoMatcher(): videoRender(),
},
onLinkTap: (url, _, __, ___) {
Expand All @@ -364,8 +378,11 @@ class MyHomePageState extends State<MyHomePage> {
}
}

CustomRenderMatcher texMatcher() => (context) => context.tree.element?.localName == 'tex';
CustomRenderMatcher texMatcher() =>
(context) => context.tree.element?.localName == 'tex';

CustomRenderMatcher birdMatcher() => (context) => context.tree.element?.localName == 'bird';
CustomRenderMatcher birdMatcher() =>
(context) => context.tree.element?.localName == 'bird';

CustomRenderMatcher flutterMatcher() => (context) => context.tree.element?.localName == 'flutter';
CustomRenderMatcher flutterMatcher() =>
(context) => context.tree.element?.localName == 'flutter';
8 changes: 6 additions & 2 deletions lib/custom_render.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,16 @@ CustomRender networkImageRender({
if (!completer.isCompleted) {
context.parser.cachedImageSizes[src] = size;
completer.complete(size);
image.image.resolve(const ImageConfiguration()).removeListener(listener!);
image.image
.resolve(const ImageConfiguration())
.removeListener(listener!);
}
}, onError: (object, stacktrace) {
if (!completer.isCompleted) {
completer.completeError(object);
image.image.resolve(const ImageConfiguration()).removeListener(listener!);
image.image
.resolve(const ImageConfiguration())
.removeListener(listener!);
}
});

Expand Down
3 changes: 2 additions & 1 deletion lib/flutter_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ class SelectableHtml extends StatefulWidget {
/// fallback to the default rendering.
final Map<CustomRenderMatcher, SelectableCustomRender> customRenders;

static List<String> get tags => List<String>.from(HtmlElements.selectableElements);
static List<String> get tags =>
List<String>.from(HtmlElements.selectableElements);

@override
State<StatefulWidget> createState() => _SelectableHtmlState();
Expand Down
16 changes: 7 additions & 9 deletions lib/html_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ class HtmlParser extends StatelessWidget {
this.root,
this.selectionControls,
this.scrollPhysics,
}) : internalOnAnchorTap = onAnchorTap ?? (key != null
? _handleAnchorTap(key, onLinkTap)
: onLinkTap);
}) : internalOnAnchorTap = onAnchorTap ??
(key != null ? _handleAnchorTap(key, onLinkTap) : onLinkTap);

/// As the widget [build]s, the HTML data is processed into a tree of [StyledElement]s,
/// which are then parsed into an [InlineSpan] tree that is then rendered to the screen by Flutter
Expand Down Expand Up @@ -184,7 +183,8 @@ class HtmlParser extends StatelessWidget {
return parseLayoutElement(node, children);
} else if (HtmlElements.tableCellElements.contains(node.localName)) {
return parseTableCellElement(node, children);
} else if (HtmlElements.tableDefinitionElements.contains(node.localName)) {
} else if (HtmlElements.tableDefinitionElements
.contains(node.localName)) {
return parseTableDefinitionElement(node, children);
} else {
final StyledElement tree = parseStyledElement(node, children);
Expand Down Expand Up @@ -285,7 +285,6 @@ class HtmlParser extends StatelessWidget {
/// child that doesn't specify a different style.
static StyledElement _cascadeStyles(
Map<String, Style> style, StyledElement tree) {

for (var child in tree.children) {
child.style = tree.style.copyOnlyInherited(child.style);
_cascadeStyles(style, child);
Expand Down Expand Up @@ -372,7 +371,7 @@ class HtmlParser extends StatelessWidget {
shrinkWrap: newContext.parser.shrinkWrap,
childIsReplaced: true, //TODO is this true?
child:
customRenders[entry]!.widget!.call(newContext, buildChildren),
customRenders[entry]!.widget!.call(newContext, buildChildren),
),
);
}
Expand Down Expand Up @@ -655,9 +654,8 @@ class HtmlParser extends StatelessWidget {
if (olStack.last.data <= 0) {
marker = '${olStack.last.data}.';
} else {
marker = "${(olStack.last.data as int)
.toRomanNumeralString()!
.toLowerCase()}.";
marker =
"${(olStack.last.data as int).toRomanNumeralString()!.toLowerCase()}.";
}
break;
case ListStyleType.upperRoman:
Expand Down
8 changes: 4 additions & 4 deletions lib/src/css_box_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CssBoxWidget extends StatelessWidget {
bool selectable = false,
TextSelectionControls? selectionControls,
ScrollPhysics? scrollPhysics,
}) : child = selectable
}) : child = selectable
? _generateSelectableWidgetChild(
children,
style,
Expand Down Expand Up @@ -676,9 +676,9 @@ extension Normalize on Dimension {
void normalize(double emValue) {
switch (unit) {
case Unit.rem:
// Because CSSBoxWidget doesn't have any information about any
// sort of tree structure, treat rem the same as em. The HtmlParser
// widget handles rem/em values before they get to CSSBoxWidget.
// Because CSSBoxWidget doesn't have any information about any
// sort of tree structure, treat rem the same as em. The HtmlParser
// widget handles rem/em values before they get to CSSBoxWidget.
case Unit.em:
value *= emValue;
unit = Unit.px;
Expand Down
32 changes: 12 additions & 20 deletions lib/src/css_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1083,8 +1083,8 @@ class ExpressionMapping {
// return LengthOrPercent(double.parse(value.text), Unit.rem);
// TODO there are several other available terms processed by the CSS parser
} else if (value is css.LengthTerm) {
double number = double.parse(
value.text.replaceAll(RegExp(r'\s+(\d+\.\d+)\s+'), ''));
double number =
double.parse(value.text.replaceAll(RegExp(r'\s+(\d+\.\d+)\s+'), ''));
Unit unit = _unitMap(value.unit);
return LengthOrPercent(number, unit);
}
Expand Down Expand Up @@ -1204,31 +1204,23 @@ class ExpressionMapping {
shadow.add(Shadow(
color: expressionToColor(color)!,
offset: Offset(
double.tryParse((offsetX)
.text
.replaceAll(nonNumberRegex, ''))!,
double.tryParse((offsetY)
.text
.replaceAll(nonNumberRegex, ''))!),
double.tryParse((offsetX).text.replaceAll(nonNumberRegex, ''))!,
double.tryParse(
(offsetY).text.replaceAll(nonNumberRegex, ''))!),
blurRadius: (blurRadius is css.LiteralTerm)
? double.tryParse((blurRadius)
.text
.replaceAll(nonNumberRegex, ''))!
? double.tryParse(
(blurRadius).text.replaceAll(nonNumberRegex, ''))!
: 0.0,
));
} else {
shadow.add(Shadow(
offset: Offset(
double.tryParse((offsetX)
.text
.replaceAll(nonNumberRegex, ''))!,
double.tryParse((offsetY)
.text
.replaceAll(nonNumberRegex, ''))!),
double.tryParse((offsetX).text.replaceAll(nonNumberRegex, ''))!,
double.tryParse(
(offsetY).text.replaceAll(nonNumberRegex, ''))!),
blurRadius: (blurRadius is css.LiteralTerm)
? double.tryParse((blurRadius)
.text
.replaceAll(nonNumberRegex, ''))!
? double.tryParse(
(blurRadius).text.replaceAll(nonNumberRegex, ''))!
: 0.0,
));
}
Expand Down
4 changes: 1 addition & 3 deletions lib/src/html_elements.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export 'interactable_element.dart';
export 'replaced_element.dart';

class HtmlElements {

static const styledElements = [
"abbr",
"acronym",
Expand Down Expand Up @@ -205,5 +204,4 @@ class HtmlElements {
"var",
"wbr",
];

}
}
1 change: 0 additions & 1 deletion lib/src/replaced_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class EmptyContentElement extends ReplacedElement {
}

class RubyElement extends ReplacedElement {

@override
dom.Element element;

Expand Down
6 changes: 4 additions & 2 deletions lib/src/style/length.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ enum UnitType {
length,
auto,
lengthPercent(children: [UnitType.length, UnitType.percent]),
lengthPercentAuto(children: [UnitType.length, UnitType.percent, UnitType.auto]);
lengthPercentAuto(
children: [UnitType.length, UnitType.percent, UnitType.auto]);

final List<UnitType> children;

Expand Down Expand Up @@ -45,7 +46,8 @@ abstract class Dimension {
/// This dimension takes a value with a length unit such as px or em. Note that
/// these can be fixed or relative (but they must not be a percent)
class Length extends Dimension {
Length(double value, [Unit unit = Unit.px]) : super(value, unit, UnitType.length);
Length(double value, [Unit unit = Unit.px])
: super(value, unit, UnitType.length);
}

/// This dimension takes a value with a length-percent unit such as px or em
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter_html_audio/lib/flutter_html_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class AudioWidget extends StatefulWidget {
final RenderContext context;
final AudioControllerCallback? callback;

const AudioWidget({Key? key,
const AudioWidget({
Key? key,
required this.context,
this.callback,
}) : super(key: key);
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter_html_iframe/lib/iframe_unsupported.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import 'package:webview_flutter/webview_flutter.dart';

CustomRender iframeRender({NavigationDelegate? navigationDelegate}) =>
CustomRender.widget(widget: (context, buildChildren) {
return const Text("Iframes are currently not supported in this environment");
return const Text(
"Iframes are currently not supported in this environment");
});
5 changes: 2 additions & 3 deletions packages/flutter_html_math/lib/flutter_html_math.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ String _parseMathRecursive(dom.Node node, String parsed) {
node.localName == "mover") &&
nodeList.length == 2) {
parsed = _parseMathRecursive(nodeList[0], parsed);
parsed = "${_parseMathRecursive(
nodeList[1],
"$parsed${node.localName == "msup" || node.localName == "mover" ? "^" : "_"}{")}}";
parsed =
"${_parseMathRecursive(nodeList[1], "$parsed${node.localName == "msup" || node.localName == "mover" ? "^" : "_"}{")}}";
}
if ((node.localName == "msubsup" || node.localName == "munderover") &&
nodeList.length == 3) {
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_html_table/lib/flutter_html_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CustomRender tableRender() =>
key: context.key,
style: context.style,
child: LayoutBuilder(
builder: (_, constraints) => _layoutCells(context, constraints),
builder: (_, constraints) => _layoutCells(context, constraints),
),
);
});
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter_html_video/lib/flutter_html_video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class VideoWidget extends StatefulWidget {
final RenderContext context;
final VideoControllerCallback? callback;

const VideoWidget({Key? key,
const VideoWidget({
Key? key,
required this.context,
this.callback,
}) : super(key: key);
Expand Down
10 changes: 7 additions & 3 deletions test/flutter_html_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void main() {

testWidgets(
"Check that widget displays given text",
(tester) async {
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: Html(
Expand All @@ -56,7 +56,9 @@ void main() {
expect(find.text('Text', findRichText: true), findsOneWidget);
});

testWidgets('Check that a simple element is hidden when tagsList does not contain it', (tester) async {
testWidgets(
'Check that a simple element is hidden when tagsList does not contain it',
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: Html(
Expand All @@ -68,7 +70,9 @@ void main() {
expect(find.text('Text', findRichText: true), findsNothing);
});

testWidgets('Check that a simple element is displayed when it is included in tagsList', (tester) async {
testWidgets(
'Check that a simple element is displayed when it is included in tagsList',
(tester) async {
await tester.pumpWidget(
MaterialApp(
home: Html(
Expand Down
Loading

0 comments on commit c53cb80

Please sign in to comment.