inlineHtmlOf static method

String inlineHtmlOf(
  1. String text,
  2. List<UStyleSpan> spans
)

Serializes text with spans to inline HTML.

Implementation

static String inlineHtmlOf(String text, List<UStyleSpan> spans) {
  if (text.isEmpty) return "";
  final StringBuffer out = StringBuffer();
  final List<_Tag> stack = <_Tag>[];

  for (int i = 0; i < text.length; i++) {
    final List<_Tag> active = _tagsAt(spans, i);
    int common = 0;
    while (common < stack.length && common < active.length && stack[common] == active[common]) {
      common++;
    }
    for (int k = stack.length - 1; k >= common; k--) {
      out.write(stack[k].close());
    }
    stack.removeRange(common, stack.length);
    for (int k = common; k < active.length; k++) {
      out.write(active[k].open());
      stack.add(active[k]);
    }
    final String ch = text[i];
    out.write(ch == "\n" ? "<br>" : escapeHtml(ch));
  }
  for (int k = stack.length - 1; k >= 0; k--) {
    out.write(stack[k].close());
  }
  return out.toString();
}