guardRawText static method

String guardRawText(
  1. String text, {
  2. required String tag,
})

text made safe to write inside a tag element (see RawTextNode).

Implementation

static String guardRawText(String text, {required String tag}) {
  var safe = text.replaceAllMapped(
    RegExp('</($tag)', caseSensitive: false),
    (m) => '<\\/${m[1]}',
  );
  if (tag.toLowerCase() == 'script') {
    safe = safe.replaceAll('<!--', '<\\!--');
  }
  return safe;
}