guardBracketInHtml method Null safety

String guardBracketInHtml(
  1. String str,
  2. [bool? isRtlContext]
)

Apply bracket guard to str using html span tag. This is to address the problem of messy bracket display that frequently happens in RTL layout. If isRtlContext is true, then we explicitly want to wrap in a span of RTL directionality, regardless of the estimated directionality.

Implementation

static String guardBracketInHtml(String str, [bool? isRtlContext]) {
  var useRtl = isRtlContext == null ? hasAnyRtl(str) : isRtlContext;
  var matchingBrackets =
      RegExp(r'(\(.*?\)+)|(\[.*?\]+)|(\{.*?\}+)|(<.*?(>)+)');
  return _guardBracketHelper(str, matchingBrackets,
      '<span dir=${useRtl ? "rtl" : "ltr"}>', '</span>');
}