begin method

  1. @override
void begin(
  1. TagContext tc,
  2. String data
)
override

Called when the beginning of a tag is encountered.

Implementation

@override
void begin(TagContext tc, String data) {
  if (data.isEmpty)
    tc.error("Expect a variable name");

  final len = data.length;
  int i = 0;
  for (; i < len && isValidVarCharCode(data.codeUnitAt(i), i == 0); ++i)
    ;
  if (i == 0)
    tc.error("Expect a variable name, not '${data[0]}'");

  final nm = data.substring(0, i);
  for (; i < len && $whitespaces.contains(data.codeUnitAt(i)); ++i)
    ;
  if (i >= len || data.codeUnitAt(i) != $equal)
    tc.error("Expect '=', not '${data[i]}");

  final val = data.substring(i + 1).trim();
  if (val.isEmpty)
    tc.error("Expect an expression");

  tc.writeln("""${tc.pre}response..write('<script type="text/plain" id="')${tc.getLineNumberComment()}""");
  tc.writeln("""${tc.pre} ..write(${toEL(nm)})..write('">')""");
  tc.writeln("${tc.pre} ..write(Rsp.json($val))..writeln('</script>');");
}