@@ -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 × 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.
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 × 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.
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 }
0 commit comments