wrapWithSpan method

String wrapWithSpan(
  1. String text, {
  2. bool isHtml = false,
  3. bool resetDir = true,
  4. TextDirection? direction,
})

Formats a string of a given (or estimated, if not provided) direction for use in HTML output of the context directionality, so an opposite-directionality string is neither garbled nor garbles what follows it.

If the input string's directionality doesn't match the context directionality, we wrap it with a span tag and add a dir attribute (either "dir=rtl" or "dir=ltr"). If alwaysSpan was true when constructing the formatter, the input is always wrapped with span tag, skipping the dir attribute when it's not needed.

If resetDir is true and the overall directionality or the exit directionality of text is opposite to the context directionality, a trailing unicode BiDi mark matching the context directionality is appended (LRM or RLM). If isHtml is false, we HTML-escape the text.

Implementation

String wrapWithSpan(String text,
    {bool isHtml = false, bool resetDir = true, TextDirection? direction}) {
  direction ??= estimateDirection(text, isHtml: isHtml);
  String result;
  if (!isHtml) text = const HtmlEscape().convert(text);
  var directionChange = contextDirection.isDirectionChange(direction);
  if (_alwaysSpan || directionChange) {
    var spanDirection = '';
    if (directionChange) {
      spanDirection = ' dir=${direction.spanText}';
    }
    result = '<span$spanDirection>$text</span>';
  } else {
    result = text;
  }
  return result + (resetDir ? _resetDir(text, direction, isHtml) : '');
}