unescape static method

  1. @useResult
String? unescape(
  1. String? htmlText
)

Converts HTML entities to their corresponding characters.

Uses a single-pass scan with O(1) Map lookup for named entities and inline decoding for numeric entities (decimal A and hex A). Recognizes legacy entities without trailing semicolons per HTML5 spec.

Returns null if the input is null or empty.

Example:

HtmlUtils.unescape('&lt;div&gt;'); // '<div>'
HtmlUtils.unescape('&#65;'); // 'A'
HtmlUtils.unescape('&#x41;'); // 'A'
HtmlUtils.unescape('Hello&nbsp;World'); // 'Hello\u00A0World'

Implementation

@useResult
static String? unescape(String? htmlText) {
  if (htmlText == null || htmlText.isEmpty) return null;

  // Fast path: no ampersand means no entities to decode
  if (!htmlText.contains('&')) return htmlText;

  return _scanAndDecode(htmlText);
}