unescape static method
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('<div>'); // '<div>'
HtmlUtils.unescape('A'); // 'A'
HtmlUtils.unescape('A'); // 'A'
HtmlUtils.unescape('Hello 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);
}