Skip to content

Commit 059bfe0

Browse files
Merge branch '0.11.0' into shrink-to-fit
2 parents 431a08f + f38b56f commit 059bfe0

File tree

5 files changed

+58
-68
lines changed

5 files changed

+58
-68
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## [0.11.0] - September 6, 2019:
2+
3+
* Make it so `width=100%` doesn't throw error. Fixes [#118](https://github.com/Sub6Resources/flutter_html/issues/118).
4+
* You can now set width and/or height in `ImageProperties` to negative to ignore the `width` and/or `height` values from the html. Fixes [#97](https://github.com/Sub6Resources/flutter_html/issues/97)
5+
* The `img` `alt` property now renders correctly when the image fails to load and with the correct style. Fixes [#96](https://github.com/Sub6Resources/flutter_html/issues/96)
6+
* Add partial support for `sub` tag.
7+
18
## [0.10.4] - June 22, 2019:
29

310
* Add support for `customTextStyle` to block and specialty HTML elements.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ A Flutter widget for rendering static html tags as Flutter widgets. (Will render
1212
Add the following to your `pubspec.yaml` file:
1313

1414
dependencies:
15-
flutter_html: ^0.10.4
15+
flutter_html: ^0.11.0
1616

1717
## Currently Supported HTML Tags:
1818
`a`, `abbr`, `acronym`, `address`, `article`, `aside`, `b`, `bdi`, `bdo`, `big`, `blockquote`, `body`, `br`, `caption`, `cite`, `code`, `data`, `dd`, `del`, `dfn`, `div`, `dl`, `dt`, `em`, `figcaption`, `figure`, `footer`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `header`, `hr`, `i`, `img`, `ins`, `kbd`, `li`, `main`, `mark`, `nav`, `noscript`, `ol`, `p`, `pre`, `q`, `rp`, `rt`, `ruby`, `s`, `samp`, `section`, `small`, `span`, `strike`, `strong`, `sub`, `sup`, `table`, `tbody`, `td`, `template`, `tfoot`, `th`, `thead`, `time`, `tr`, `tt`, `u`, `ul`, `var`

lib/rich_text_parser.dart

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ class HtmlRichTextParser extends StatelessWidget {
217217
"time",
218218
"span",
219219
"big",
220+
"sub",
220221
];
221222

222223
// specialty elements require unique handling
@@ -519,6 +520,13 @@ class HtmlRichTextParser extends StatelessWidget {
519520
childStyle = childStyle.merge(
520521
TextStyle(backgroundColor: Colors.yellow, color: Colors.black));
521522
break;
523+
case "sub":
524+
childStyle = childStyle.merge(
525+
TextStyle(
526+
fontSize: childStyle.fontSize * OFFSET_TAGS_FONT_SIZE_FACTOR,
527+
),
528+
);
529+
break;
522530
case "del":
523531
case "s":
524532
case "strike":
@@ -730,6 +738,18 @@ class HtmlRichTextParser extends StatelessWidget {
730738
case "img":
731739
if (showImages) {
732740
if (node.attributes['src'] != null) {
741+
742+
final width = imageProperties?.width ??
743+
((node.attributes['width'] != null)
744+
? double.tryParse(node.attributes['width'])
745+
: null
746+
);
747+
final height = imageProperties?.height ??
748+
((node.attributes['height'] != null)
749+
? double.tryParse(node.attributes['height'])
750+
: null
751+
);
752+
733753
if (node.attributes['src'].startsWith("data:image") &&
734754
node.attributes['src'].contains("base64,")) {
735755
precacheImage(
@@ -739,20 +759,14 @@ class HtmlRichTextParser extends StatelessWidget {
739759
),
740760
),
741761
buildContext,
742-
onError: onImageError,
762+
onError: onImageError ?? (_,__) {},
743763
);
744764
parseContext.rootWidgetList.add(GestureDetector(
745765
child: Image.memory(
746766
base64.decode(
747767
node.attributes['src'].split("base64,")[1].trim()),
748-
width: imageProperties?.width ??
749-
((node.attributes['width'] != null)
750-
? double.tryParse(node.attributes['width'])
751-
: null),
752-
height: imageProperties?.height ??
753-
((node.attributes['height'] != null)
754-
? double.tryParse(node.attributes['height'])
755-
: null),
768+
width: (width ?? -1) > 0? width: null,
769+
height: (height ?? -1) > 0? width: null,
756770
scale: imageProperties?.scale ?? 1.0,
757771
matchTextDirection:
758772
imageProperties?.matchTextDirection ?? false,
@@ -780,19 +794,30 @@ class HtmlRichTextParser extends StatelessWidget {
780794
precacheImage(
781795
NetworkImage(node.attributes['src']),
782796
buildContext,
783-
onError: onImageError,
797+
onError: onImageError ?? (_,__) {},
784798
);
785799
parseContext.rootWidgetList.add(GestureDetector(
786800
child: Image.network(
787801
node.attributes['src'],
788-
width: imageProperties?.width ??
789-
((node.attributes['width'] != null)
790-
? double.parse(node.attributes['width'])
791-
: null),
792-
height: imageProperties?.height ??
793-
((node.attributes['height'] != null)
794-
? double.parse(node.attributes['height'])
795-
: null),
802+
frameBuilder: (context, child, frame, _) {
803+
if (node.attributes['alt'] != null && frame == null) {
804+
return BlockText(
805+
child: RichText(
806+
textAlign: TextAlign.center,
807+
text: TextSpan(
808+
text: node.attributes['alt'],
809+
style: nextContext.childStyle,
810+
),
811+
)
812+
);
813+
}
814+
if (frame != null) {
815+
return child;
816+
}
817+
return Container();
818+
},
819+
width: (width ?? -1) > 0? width: null,
820+
height: (height ?? -1) > 0? height: null,
796821
scale: imageProperties?.scale ?? 1.0,
797822
matchTextDirection:
798823
imageProperties?.matchTextDirection ?? false,
@@ -817,20 +842,6 @@ class HtmlRichTextParser extends StatelessWidget {
817842
},
818843
));
819844
}
820-
if (node.attributes['alt'] != null) {
821-
parseContext.rootWidgetList.add(BlockText(
822-
shrinkToFit: shrinkToFit,
823-
margin:
824-
EdgeInsets.symmetric(horizontal: 0.0, vertical: 10.0),
825-
padding: EdgeInsets.all(0.0),
826-
child: RichText(
827-
textAlign: TextAlign.center,
828-
text: TextSpan(
829-
text: node.attributes['alt'],
830-
style: nextContext.childStyle,
831-
children: <TextSpan>[],
832-
))));
833-
}
834845
}
835846
}
836847
break;
@@ -955,39 +966,11 @@ class HtmlRichTextParser extends StatelessWidget {
955966
}
956967
}
957968

958-
Paint _getPaint(Color color) {
959-
Paint paint = new Paint();
960-
paint.color = color;
961-
return paint;
962-
}
963-
964969
String condenseHtmlWhitespace(String stringToTrim) {
965970
stringToTrim = stringToTrim.replaceAll("\n", " ");
966971
while (stringToTrim.indexOf(" ") != -1) {
967972
stringToTrim = stringToTrim.replaceAll(" ", " ");
968973
}
969974
return stringToTrim;
970975
}
971-
972-
bool _isNotFirstBreakTag(dom.Node node) {
973-
int index = node.parentNode.nodes.indexOf(node);
974-
if (index == 0) {
975-
if (node.parentNode == null) {
976-
return false;
977-
}
978-
return _isNotFirstBreakTag(node.parentNode);
979-
} else if (node.parentNode.nodes[index - 1] is dom.Element) {
980-
if ((node.parentNode.nodes[index - 1] as dom.Element).localName == "br") {
981-
return true;
982-
}
983-
return false;
984-
} else if (node.parentNode.nodes[index - 1] is dom.Text) {
985-
if ((node.parentNode.nodes[index - 1] as dom.Text).text.trim() == "") {
986-
return _isNotFirstBreakTag(node.parentNode.nodes[index - 1]);
987-
} else {
988-
return false;
989-
}
990-
}
991-
return false;
992-
}
993976
}

pubspec.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Generated by pub
2-
# See https://www.dartlang.org/tools/pub/glossary#lockfile
2+
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
44
async:
55
dependency: transitive
66
description:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.1.0"
10+
version: "2.2.0"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -80,14 +80,14 @@ packages:
8080
name: pedantic
8181
url: "https://pub.dartlang.org"
8282
source: hosted
83-
version: "1.5.0"
83+
version: "1.7.0"
8484
quiver:
8585
dependency: transitive
8686
description:
8787
name: quiver
8888
url: "https://pub.dartlang.org"
8989
source: hosted
90-
version: "2.0.2"
90+
version: "2.0.3"
9191
sky_engine:
9292
dependency: transitive
9393
description: flutter
@@ -134,7 +134,7 @@ packages:
134134
name: test_api
135135
url: "https://pub.dartlang.org"
136136
source: hosted
137-
version: "0.2.4"
137+
version: "0.2.5"
138138
typed_data:
139139
dependency: transitive
140140
description:
@@ -150,5 +150,5 @@ packages:
150150
source: hosted
151151
version: "2.0.8"
152152
sdks:
153-
dart: ">=2.2.0 <3.0.0"
153+
dart: ">=2.2.2 <3.0.0"
154154
flutter: ">=0.5.0"

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_html
22
description: A Flutter widget for rendering static html tags as Flutter widgets. (Will render over 70 different html tags!)
3-
version: 0.10.4
3+
version: 0.11.0
44
author: Matthew Whitaker <sub6resources@gmail.com>
55
homepage: https://github.com/Sub6Resources/flutter_html
66

0 commit comments

Comments
 (0)