search

goog.i18n.BidiFormatter

Provided By

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.

new BidiFormatter( contextDir, opt_alwaysSpan )

Parameters
contextDir(number|boolean|null)

The context directionality, in one of the following formats: 1. A goog.i18n.bidi.Dir constant. NEUTRAL is treated the same as null, i.e. unknown, for backward compatibility with legacy calls. 2. A number (positive = LTR, negative = RTL, 0 = unknown). 3. A boolean (true = RTL, false = LTR). 4. A null for unknown directionality.

opt_alwaysSpanboolean=

Whether #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.

Instance Methods