writeConvertHtmlEntities static method
Writes the HTML entities such as &
from the input
into the target
StringBuffer.
Implementation
static void writeConvertHtmlEntities(String input, StringBuffer target) {
final matches = _htmlEntityRegex.allMatches(input);
var lastStartIndex = 0;
for (final match in matches) {
if (match.start > lastStartIndex) {
target.write(input.substring(lastStartIndex, match.start));
}
final entity = match.group(0)!;
final replacement = _characters[entity];
if (replacement != null) {
target.write(replacement);
} else {
int? charCode;
if (entity.startsWith('&#x')) {
// this is a hexadecimal number:
final hexText = entity.substring('&#x'.length, entity.length - 1);
charCode = int.tryParse(hexText, radix: 16);
} else if (entity.startsWith('&#')) {
final text = entity.substring('&#'.length, entity.length - 1);
charCode = int.tryParse(text);
}
if (charCode != null) {
final charText = String.fromCharCode(charCode);
target.write(charText);
} else {
print('Warning: unable to decode HTML entity "$entity"');
target.write(entity);
}
}
lastStartIndex = match.end;
}
if (lastStartIndex < input.length) {
target.write(input.substring(lastStartIndex));
}
}