@@ -55,11 +55,23 @@ class HTML {
55
55
/// },
56
56
/// );
57
57
/// ```
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 × 4 = 8</b>
63
+ /// ```
64
+ ///
65
+ /// This may result in parsing errors if the input contains escaped angled
66
+ /// brackets (`<` , `>` ). In such cases, automatic unescaping may be
67
+ /// disabled via the [unescapeContent] flag.
58
68
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
+ }) {
63
75
// Validating empty content
64
76
if (htmlContent.isEmpty) {
65
77
return const TextSpan ();
@@ -73,7 +85,11 @@ class HTML {
73
85
// to fix a known issue with non self closing <br> tags
74
86
content = content.replaceAll ('<br>' , '<br />' );
75
87
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,
77
93
linksCallback: linksCallback,
78
94
overrideStyleMap: overrideStyle ?? < String , TextStyle > {},
79
95
defaultTextStyle: defaultTextStyle);
@@ -120,18 +136,31 @@ class HTML {
120
136
/// },
121
137
/// );
122
138
/// ```
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 × 4 = 8</b>
144
+ /// ```
145
+ ///
146
+ /// This may result in parsing errors if the input contains escaped angled
147
+ /// brackets (`<` , `>` ). In such cases, automatic unescaping may be
148
+ /// disabled via the [unescapeContent] flag.
123
149
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
+ }) {
128
156
return RichText (
129
157
text: toTextSpan (
130
158
context,
131
159
htmlContent,
132
160
linksCallback: linksCallback,
133
161
overrideStyle: overrideStyle,
134
162
defaultTextStyle: defaultTextStyle,
163
+ unescapeContent: unescapeContent,
135
164
),
136
165
);
137
166
}
0 commit comments