Home

# Class goog.i18n.BidiFormatter

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.

Instance Method Summary
| `areDirectionalitiesOpposite_(?goog.i18n.bidi.Dir dir1, ?goog.i18n.bidi.Dir dir2) ⇒ boolean` Returns true if two given directionalities are opposite. Note: the implementation is based on the numeric values of the Dir enum.
| `dirAttr(string str, boolean= opt_isHtml) ⇒ string` 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.
| `dirAttrValue(string str, boolean= opt_isHtml) ⇒ string` 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: |
| `dirResetIfNeeded_(string str, ?goog.i18n.bidi.Dir dir, boolean= opt_isHtml, boolean= opt_dirReset) ⇒ string` Returns a unicode BiDi mark matching the context directionality (LRM or RLM) if {@code opt_dirReset}, and if either the directionality or the exit directionality of {@code str} is opposite to the context directionality. Otherwise returns the empty string.
| `endEdge() ⇒ string` Returns 'left' for RTL context directionality. Otherwise (LTR or neutral / unknown context directionality) returns 'right'.
| `getAlwaysSpan() ⇒ boolean`
| `getContextDir() ⇒ ?goog.i18n.bidi.Dir`
| `knownDirAttr(?goog.i18n.bidi.Dir dir) ⇒ string` 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.
| `knownDirAttrValue(?goog.i18n.bidi.Dir dir) ⇒ string` 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.
| `mark() ⇒ string` 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.
| `markAfter(string str, boolean= opt_isHtml) ⇒ string` 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.
| `setAlwaysSpan(boolean alwaysSpan)`
| `setContextDir((boolean|goog.i18n.bidi.Dir|null|number) contextDir)`

`spanWrap(string str, boolean= opt_isHtml, boolean= opt_dirReset) ⇒ string`

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.

`spanWrapWithKnownDir(?goog.i18n.bidi.Dir dir, string str, boolean= opt_isHtml, boolean= opt_dirReset) ⇒ string`

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.

| `startEdge() ⇒ string` Returns 'right' for RTL context directionality. Otherwise (LTR or neutral / unknown context directionality) returns 'left'.

`unicodeWrap(string str, boolean= opt_isHtml, boolean= opt_dirReset) ⇒ string`

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.

`unicodeWrapWithKnownDir(?goog.i18n.bidi.Dir dir, string str, boolean= opt_isHtml, boolean= opt_dirReset) ⇒ string`

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.