BidiFormatter class
Bidi stands for Bi-directional text. According to Wikipedia: Bi-directional text is text containing text in both text directionalities, both right-to-left (RTL) and left-to-right (LTR). It generally involves text containing different types of alphabets, but may also refer to boustrophedon, which is changing text directionality in each row.
Utility class for formatting display text in a potentially opposite-directionality context without garbling layout issues. Mostly a very "slimmed-down" and dart-ified port of the Closure Birectional formatting libary. If there is a utility in the Closure library (or ICU, or elsewhere) that you would like this formatter to make available, please contact the Dart team.
Provides the following functionality:
-
BiDi Wrapping When text in one language is mixed into a document in another, opposite- directionality language, e.g. when an English business name is embedded in a Hebrew web page, both the inserted string and the text following it may be displayed incorrectly unless the inserted string is explicitly separated from the surrounding text in a "wrapper" that declares its directionality at the start and then resets it back at the end. This wrapping can be done in HTML mark-up (e.g. a 'span dir=rtl' tag) or - only in contexts where mark-up can not be used - in Unicode BiDi formatting codes (LRE|RLE and PDF). Providing such wrapping services is the basic purpose of the BiDi formatter.
-
Directionality estimation How does one know whether a string about to be inserted into surrounding text has the same directionality? Well, in many cases, one knows that this must be the case when writing the code doing the insertion, e.g. when a localized message is inserted into a localized page. In such cases there is no need to involve the BiDi formatter at all. In the remaining cases, e.g. when the string is user-entered or comes from a database, the language of the string (and thus its directionality) is not known a priori, and must be estimated at run-time. The BiDi formatter does this automatically.
-
Escaping When wrapping plain text - i.e. text that is not already HTML or HTML- escaped - in HTML mark-up, the text must first be HTML-escaped to prevent XSS attacks and other nasty business. This of course is always true, but the escaping cannot be done after the string has already been wrapped in mark-up, so the BiDi formatter also serves as a last chance and includes escaping services.
Thus, in a single call, the formatter will escape the input string as specified, determine its directionality, and wrap it as necessary. It is then up to the caller to insert the return value in the output.
Constructors
- BidiFormatter.LTR([bool alwaysSpan = false])
-
Create a formatting object with a direction. If
alwaysSpan
is true we should always use aspan
tag, even when the input directionality is neutral or matches the context, so that the DOM structure of the output does not depend on the combination of directionalities. - BidiFormatter.RTL([bool alwaysSpan = false])
- BidiFormatter.UNKNOWN([bool alwaysSpan = false])
Properties
- contextDirection ↔ TextDirection
-
The direction of the surrounding text (the context).
getter/setter pair
- hashCode → int
-
The hash code for this object.
no setterinherited
- isRTL → bool
-
Is true if the known context direction for this formatter is RTL.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
estimateDirection(
String text, {bool isHtml = false}) → TextDirection -
Estimates the directionality of
text
using the best known general-purpose method (using relative word counts). A TextDirection.UNKNOWN return value indicates completely neutral input.isHtml
is true iftext
HTML or HTML-escaped. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
wrapWithSpan(
String text, {bool isHtml = false, bool resetDir = true, TextDirection? direction}) → String -
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. -
wrapWithUnicode(
String text, {bool isHtml = false, bool resetDir = true, TextDirection? direction}) → String -
Format
text
of a known (if specified) or estimateddirection
for use in plain-text output of the context directionality, so an opposite-directionality text is neither garbled nor garbles what follows it. Unlike wrapWithSpan, this makes use of unicode BiDi formatting characters instead of spans for wrapping. The returned string would be RLE+text+PDF for RTL text, or LRE+text+PDF for LTR text.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited