The goog.i18n.BidiFormatter Class

goog.i18n.BidiFormatter(contextDir, opt_alwaysSpan)

Utility class for formatting text for display in a potentially opposite-directionality context without garbling. Provides the following functionality: 1. 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. 2. 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. 3. 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 can not 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. See http://wiki/Main/TemplatesAndBiDi for more information.

contextDir {goog.i18n.bidi.Dir|number|boolean}
The context directionality. May be supplied either as a goog.i18n.bidi.Dir constant, as a number (positive = LRT, negative = RTL, 0 = unknown) or as a boolean (true = RTL, false = LTR).
opt_alwaysSpan {boolean=}
Whether {@link #spanWrap} should always use a 'span' 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. Default: false.

.dirAttr(str, opt_isHtml)

Returns 'dir="ltr"' or 'dir="rtl"', depending on {@code str}'s estimated directionality, if it is not the same as the context directionality. Otherwise, returns the empty string.

str {string}
Text whose directionality is to be estimated.
opt_isHtml {boolean=}
Whether {@code str} is HTML / HTML-escaped. Default: false.
returns {string}
'dir="rtl"' for RTL text in non-RTL context; 'dir="ltr"' for LTR text in non-LTR context; else, the empty string.

.dirAttrValue(str, opt_isHtml)

Returns "rtl" if {@code str}'s estimated directionality is RTL, and "ltr" if it is LTR. In case it's UNKNOWN, returns "rtl" if the context directionality is RTL, and "ltr" otherwise. Needed for GXP, which can't handle dirAttr. Example use case:

str {string}
Text whose directionality is to be estimated.
opt_isHtml {boolean=}
Whether {@code str} is HTML / HTML-escaped. Default: false.
returns {string}
"rtl" or "ltr", according to the logic described above.

.endEdge()

Returns 'left' for RTL context directionality. Otherwise (LTR or neutral / unknown context directionality) returns 'right'.

returns {string}
'left' for RTL context directionality and 'right' for other context directionality.

.estimateDirection

Returns the directionality of input argument {@code str}. Identical to {@link goog.i18n.bidi.estimateDirection}.

str {string}
The input text.
opt_isHtml {boolean=}
Whether {@code str} is HTML / HTML-escaped. Default: false.
returns {goog.i18n.bidi.Dir}
Estimated overall directionality of {@code str}.

.getAlwaysSpan()

returns {boolean}
Whether alwaysSpan is set.

.getContextDir()

returns {goog.i18n.bidi.Dir}
The context directionality.

.knownDirAttr(dir)

Returns 'dir="ltr"' or 'dir="rtl"', depending on the given directionality, if it is not the same as the context directionality. Otherwise, returns the empty string.

dir {goog.i18n.bidi.Dir}
A directionality.
returns {string}
'dir="rtl"' for RTL text in non-RTL context; 'dir="ltr"' for LTR text in non-LTR context; else, the empty string.

.knownDirAttrValue(dir)

Returns "rtl" if the given directionality is RTL, and "ltr" if it is LTR. In case it's UNKNOWN, returns "rtl" if the context directionality is RTL, and "ltr" otherwise.

dir {goog.i18n.bidi.Dir}
A directionality.
returns {string}
"rtl" or "ltr", according to the logic described above.

.mark()

Returns the Unicode BiDi mark matching the context directionality (LRM for LTR context directionality, RLM for RTL context directionality), or the empty string for neutral / unknown context directionality.

returns {string}
LRM for LTR context directionality and RLM for RTL context directionality.

.markAfter(str, opt_isHtml)

Returns a Unicode BiDi mark matching the context directionality (LRM or RLM) if the directionality or the exit directionality of {@code str} are opposite to the context directionality. Otherwise returns the empty string.

str {string}
The input text.
opt_isHtml {boolean=}
Whether {@code str} is HTML / HTML-escaped. Default: false.
returns {string}
A Unicode bidi mark matching the global directionality or the empty string.

.setAlwaysSpan(alwaysSpan)

alwaysSpan {boolean}
Whether {@link #spanWrap} should always use a 'span' 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.

.setContextDir(contextDir)

contextDir {goog.i18n.bidi.Dir|number|boolean}
The context directionality. May be supplied either as a goog.i18n.bidi.Dir constant, as a number (positive = LRT, negative = RTL, 0 = unknown) or as a boolean (true = RTL, false = LTR).

.spanWrap(str, opt_isHtml, opt_dirReset)

Formats a string of unknown directionality for use in HTML output of the context directionality, so an opposite-directionality string is neither garbled nor garbles what follows it. The algorithm: estimates the directionality of input argument {@code str}. In case its directionality doesn't match the context directionality, wraps it with a 'span' tag and adds a "dir" attribute (either 'dir="rtl"' or 'dir="ltr"'). If setAlwaysSpan(true) was used, the input is always wrapped with 'span', skipping just the dir attribute when it's not needed. If {@code opt_dirReset}, and if the overall directionality or the exit directionality of {@code str} are opposite to the context directionality, a trailing unicode BiDi mark matching the context directionality is appened (LRM or RLM). If !{@code opt_isHtml}, HTML-escapes {@code str} regardless of wrapping.

str {string}
The input text.
opt_isHtml {boolean=}
Whether {@code str} is HTML / HTML-escaped. Default: false.
opt_dirReset {boolean=}
Whether to append a trailing unicode bidi mark matching the context directionality, when needed, to prevent the possible garbling of whatever may follow {@code str}. Default: true.
returns {string}
Input text after applying the above processing.

.spanWrapWithKnownDir(dir, str, opt_isHtml, opt_dirReset)

Formats a string of given directionality for use in HTML output of the context directionality, so an opposite-directionality string is neither garbled nor garbles what follows it. The algorithm: If {@code dir} doesn't match the context directionality, wraps {@code str} with a 'span' tag and adds a "dir" attribute (either 'dir="rtl"' or 'dir="ltr"'). If setAlwaysSpan(true) was used, the input is always wrapped with 'span', skipping just the dir attribute when it's not needed. If {@code opt_dirReset}, and if {@code dir} or the exit directionality of {@code str} are opposite to the context directionality, a trailing unicode BiDi mark matching the context directionality is appened (LRM or RLM). If !{@code opt_isHtml}, HTML-escapes {@code str} regardless of wrapping.

dir {goog.i18n.bidi.Dir}
{@code str}'s overall directionality.
str {string}
The input text.
opt_isHtml {boolean=}
Whether {@code str} is HTML / HTML-escaped. Default: false.
opt_dirReset {boolean=}
Whether to append a trailing unicode bidi mark matching the context directionality, when needed, to prevent the possible garbling of whatever may follow {@code str}. Default: true.
returns {string}
Input text after applying the above processing.

.startEdge()

Returns 'right' for RTL context directionality. Otherwise (LTR or neutral / unknown context directionality) returns 'left'.

returns {string}
'right' for RTL context directionality and 'left' for other context directionality.

.unicodeWrap(str, opt_isHtml, opt_dirReset)

Formats a string of unknown directionality for use in plain-text output of the context directionality, so an opposite-directionality string is neither garbled nor garbles what follows it. As opposed to {@link #spanWrap}, this makes use of unicode BiDi formatting characters. In HTML, its *only* valid use is inside of elements that do not allow mark-up, e.g. an 'option' tag. The algorithm: estimates the directionality of input argument {@code str}. In case it doesn't match the context directionality, wraps it with Unicode BiDi formatting characters: RLE{@code str}PDF for RTL text, and LRE{@code str}PDF for LTR text. If {@code opt_dirReset}, and if the overall directionality or the exit directionality of {@code str} are opposite to the context directionality, a trailing unicode BiDi mark matching the context directionality is appended (LRM or RLM). Does *not* do HTML-escaping regardless of the value of {@code opt_isHtml}. The return value can be HTML-escaped as necessary.

str {string}
The input text.
opt_isHtml {boolean=}
Whether {@code str} is HTML / HTML-escaped. Default: false.
opt_dirReset {boolean=}
Whether to append a trailing unicode bidi mark matching the context directionality, when needed, to prevent the possible garbling of whatever may follow {@code str}. Default: true.
returns {string}
Input text after applying the above processing.

.unicodeWrapWithKnownDir(dir, str, opt_isHtml, opt_dirReset)

Formats a string of given directionality for use in plain-text output of the context directionality, so an opposite-directionality string is neither garbled nor garbles what follows it. As opposed to {@link #spanWrapWithKnownDir}, makes use of unicode BiDi formatting characters. In HTML, its *only* valid use is inside of elements that do not allow mark-up, e.g. an 'option' tag. The algorithm: If {@code dir} doesn't match the context directionality, wraps {@code str} with Unicode BiDi formatting characters: RLE{@code str}PDF for RTL text, and LRE{@code str}PDF for LTR text. If {@code opt_dirReset}, and if the overall directionality or the exit directionality of {@code str} are opposite to the context directionality, a trailing unicode BiDi mark matching the context directionality is appended (LRM or RLM). Does *not* do HTML-escaping regardless of the value of {@code opt_isHtml}. The return value can be HTML-escaped as necessary.

dir {goog.i18n.bidi.Dir}
{@code str}'s overall directionality.
str {string}
The input text.
opt_isHtml {boolean=}
Whether {@code str} is HTML / HTML-escaped. Default: false.
opt_dirReset {boolean=}
Whether to append a trailing unicode bidi mark matching the context directionality, when needed, to prevent the possible garbling of whatever may follow {@code str}. Default: true.
returns {string}
Input text after applying the above processing.