decodeHtmlEntities function
Decodes the small set of HTML entities seen in search results and page content (named, decimal, and hexadecimal forms).
Implementation
String decodeHtmlEntities(String text) {
if (!text.contains('&')) return text;
return text.replaceAllMapped(
RegExp(r'&(#x?[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);'),
(match) {
final body = match.group(1)!;
if (body.startsWith('#x') || body.startsWith('#X')) {
final code = int.tryParse(body.substring(2), radix: 16);
return code == null ? match.group(0)! : String.fromCharCode(code);
}
if (body.startsWith('#')) {
final code = int.tryParse(body.substring(1));
return code == null ? match.group(0)! : String.fromCharCode(code);
}
return _namedEntities[body.toLowerCase()] ?? match.group(0)!;
},
);
}