Skip to content

Commit caa0f57

Browse files
authored
Allow bypassing HTML content unescaping (#29)
* Allow bypassing HTML content unescaping * Add minor code improvements * Bump version. Update changelog * Document unescapeContent flag
1 parent 631d6ad commit caa0f57

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.0.1
2+
* Allow bypassing HTML content unescaping
3+
14
## 4.0.0
25
* Breaking change, this package now needs a Flutter version >= 3
36
* Update dependencies

Diff for: lib/src/html_stylist.dart

+38-9
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,23 @@ class HTML {
5555
/// },
5656
/// );
5757
/// ```
58+
///
59+
/// HTML content is unescaped by default before parsing to render escape
60+
/// entities contained in the input. For example:
61+
/// ```
62+
/// <b>2 &times; 4 &#61; 8</b>
63+
/// ```
64+
///
65+
/// This may result in parsing errors if the input contains escaped angled
66+
/// brackets (`&lt;`, `&gt;`). In such cases, automatic unescaping may be
67+
/// disabled via the [unescapeContent] flag.
5868
59-
static TextSpan toTextSpan(BuildContext context, String htmlContent,
60-
{Function(dynamic)? linksCallback,
61-
Map<String, TextStyle>? overrideStyle,
62-
TextStyle? defaultTextStyle}) {
69+
static TextSpan toTextSpan(BuildContext context, String htmlContent, {
70+
Function(dynamic)? linksCallback,
71+
Map<String, TextStyle>? overrideStyle,
72+
TextStyle? defaultTextStyle,
73+
bool unescapeContent = true,
74+
}) {
6375
// Validating empty content
6476
if (htmlContent.isEmpty) {
6577
return const TextSpan();
@@ -73,7 +85,11 @@ class HTML {
7385
// to fix a known issue with non self closing <br> tags
7486
content = content.replaceAll('<br>', '<br />');
7587

76-
final Parser parser = Parser(context, HtmlUnescape().convert(content),
88+
if (unescapeContent) {
89+
content = HtmlUnescape().convert(content);
90+
}
91+
92+
final Parser parser = Parser(context, content,
7793
linksCallback: linksCallback,
7894
overrideStyleMap: overrideStyle ?? <String, TextStyle>{},
7995
defaultTextStyle: defaultTextStyle);
@@ -120,18 +136,31 @@ class HTML {
120136
/// },
121137
/// );
122138
/// ```
139+
///
140+
/// HTML content is unescaped by default before parsing to render escape
141+
/// entities contained in the input. For example:
142+
/// ```
143+
/// <b>2 &times; 4 &#61; 8</b>
144+
/// ```
145+
///
146+
/// This may result in parsing errors if the input contains escaped angled
147+
/// brackets (`&lt;`, `&gt;`). In such cases, automatic unescaping may be
148+
/// disabled via the [unescapeContent] flag.
123149
124-
static RichText toRichText(BuildContext context, String htmlContent,
125-
{Function(dynamic)? linksCallback,
126-
Map<String, TextStyle>? overrideStyle,
127-
TextStyle? defaultTextStyle}) {
150+
static RichText toRichText(BuildContext context, String htmlContent, {
151+
Function(dynamic)? linksCallback,
152+
Map<String, TextStyle>? overrideStyle,
153+
TextStyle? defaultTextStyle,
154+
bool unescapeContent = true,
155+
}) {
128156
return RichText(
129157
text: toTextSpan(
130158
context,
131159
htmlContent,
132160
linksCallback: linksCallback,
133161
overrideStyle: overrideStyle,
134162
defaultTextStyle: defaultTextStyle,
163+
unescapeContent: unescapeContent,
135164
),
136165
);
137166
}

Diff for: pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: simple_html_css
22
description: This package allows you to use simple HTML and inline CSS styles to style your text in flutter. A fork of css_text package.
3-
version: 4.0.0
3+
version: 4.0.1
44
homepage: https://github.com/ali-thowfeek/simple_html_css_flutter
55
repository: https://github.com/ali-thowfeek/simple_html_css_flutter
66

0 commit comments

Comments
 (0)