htmlSerializeEscape function
Escapes text
for use in the
HTML fragment serialization algorithm. In particular, as described
in the specification:
- Replace any occurrence of the
&
character by the string&
. - Replace any occurrences of the U+00A0 NO-BREAK SPACE character by the
string
. - If the algorithm was invoked in
attributeMode
, replace any occurrences of the"
character by the string"
. - If the algorithm was not invoked in
attributeMode
, replace any occurrences of the<
character by the string<
, and any occurrences of the>
character by the string>
.
Implementation
String htmlSerializeEscape(String text, {bool attributeMode = false}) {
// TODO(jmesserly): is it faster to build up a list of codepoints?
// StringBuffer seems cleaner assuming Dart can unbox 1-char strings.
StringBuffer? result;
for (var i = 0; i < text.length; i++) {
final ch = text[i];
String? replace;
switch (ch) {
case '&':
replace = '&';
break;
case '\u00A0' /*NO-BREAK SPACE*/ :
replace = ' ';
break;
case '"':
if (attributeMode) replace = '"';
break;
case '<':
if (!attributeMode) replace = '<';
break;
case '>':
if (!attributeMode) replace = '>';
break;
}
if (replace != null) {
result ??= StringBuffer(text.substring(0, i));
result.write(replace);
} else if (result != null) {
result.write(ch);
}
}
return result != null ? result.toString() : text;
}