21

It seems that Dart does not provide a default mechanism (or at least I could not find it) to decode HTML escaped entities.

What I'd like to do is convert eg. Q&A to Q&A. (This is just an example)

As of version 1.11.1, Dart converts encodes these like so.

From there it is rather simple to create a custom converter implementation but that would not cover all the use-cases. Such as: what if < is expressed with the hex value of <?

Anyone got some pretty solution?

Daniel V.
  • 1,138
  • 1
  • 9
  • 15

5 Answers5

35

I just made a small but complete Dart library for that exact purpose: html_unescape.

It supports:

  • Named Character References ( )
    • 2099 of them
  • Decimal Character References (á)
  • Hexadecimal Character References (ã)

Sync use

import 'package:html_unescape/html_unescape.dart';

main() {
  var unescape = new HtmlUnescape();
  var text = unescape.convert("<strong>This "escaped" string");
  print(text);
}

Async use

You can also use the converter to transform a stream. For example, the code below will transform a POSIX stdin into an HTML-unencoded stdout.

await stdin
    .transform(new Utf8Decoder())
    .transform(new HtmlUnescape())
    .transform(new Utf8Encoder())
    .pipe(stdout);

More info + docs on pub.

filiph
  • 2,673
  • 2
  • 26
  • 33
9

Just use the HTML parser library:

import 'package:html/parser.dart';
main() {
  print(parseFragment('Q&A').text); // Q&A
}
Patrick
  • 3,578
  • 5
  • 31
  • 53
4

I think Dart/Flutter can do it for you by itself:

import 'dart:html' as html;

//  In production use library — universal_html: ^1.1.18
//  and — import 'package:universal_html/html.dart' as html;

void main() {
  String badString =
      'This &quot; string &quot; will be<strong> printed normally. &lt; &apos; &gt; </strong> &#62;';
      print(_parseHtmlString(badString));
}

String _parseHtmlString(String htmlString) {
  var text = html.Element.span()..appendHtml(htmlString);
  return text.innerText;
}

// It prints: This " string " will be printed normally. < ' > >

Dmitry_Kovalov
  • 1,924
  • 1
  • 14
  • 11
  • The linter says "Don't use web-only libraries outside Flutter web plugin packages. Try finding a different library for your needs." – Sergey Aug 22 '23 at 06:35
3

If you want to Extract the HTML content to Text String then Follow this step:

  1. Add this plugin to pubspec.yaml => HTML
  1. Then in your code add this block =>

    String htmlText = parse("String with HTML tags").body!.text

  2. Then assign this varible wherever you want

Nachiket Gohil
  • 955
  • 11
  • 17
2

1- Add Dependency Package to pubspec.yaml:

    dependencies:
    .....
    html_unescape: ^1.0.2

2- Run in terminal:

   flutter pub get

Example how to use:

import 'package:html_unescape/html_unescape.dart';

Text(HtmlUnescape().convert(title));

if you are going to use multiple times, follow this:

var unescape = HtmlUnescape();
var text = unescape.convert("&lt;strong&#62;This &quot;escaped&quot; string");
  print(text);

This "escaped" string

bara batta
  • 1,002
  • 8
  • 8