Path: blob/trunk/third_party/closure/goog/i18n/bidi.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Utility functions for supporting Bidi issues.16*/171819/**20* Namespace for bidi supporting functions.21*/22goog.provide('goog.i18n.bidi');23goog.provide('goog.i18n.bidi.Dir');24goog.provide('goog.i18n.bidi.DirectionalString');25goog.provide('goog.i18n.bidi.Format');262728/**29* @define {boolean} FORCE_RTL forces the {@link goog.i18n.bidi.IS_RTL} constant30* to say that the current locale is a RTL locale. This should only be used31* if you want to override the default behavior for deciding whether the32* current locale is RTL or not.33*34* {@see goog.i18n.bidi.IS_RTL}35*/36goog.define('goog.i18n.bidi.FORCE_RTL', false);373839/**40* Constant that defines whether or not the current locale is a RTL locale.41* If {@link goog.i18n.bidi.FORCE_RTL} is not true, this constant will default42* to check that {@link goog.LOCALE} is one of a few major RTL locales.43*44* <p>This is designed to be a maximally efficient compile-time constant. For45* example, for the default goog.LOCALE, compiling46* "if (goog.i18n.bidi.IS_RTL) alert('rtl') else {}" should produce no code. It47* is this design consideration that limits the implementation to only48* supporting a few major RTL locales, as opposed to the broader repertoire of49* something like goog.i18n.bidi.isRtlLanguage.50*51* <p>Since this constant refers to the directionality of the locale, it is up52* to the caller to determine if this constant should also be used for the53* direction of the UI.54*55* {@see goog.LOCALE}56*57* @type {boolean}58*59* TODO(user): write a test that checks that this is a compile-time constant.60*/61goog.i18n.bidi.IS_RTL = goog.i18n.bidi.FORCE_RTL ||62((goog.LOCALE.substring(0, 2).toLowerCase() == 'ar' ||63goog.LOCALE.substring(0, 2).toLowerCase() == 'fa' ||64goog.LOCALE.substring(0, 2).toLowerCase() == 'he' ||65goog.LOCALE.substring(0, 2).toLowerCase() == 'iw' ||66goog.LOCALE.substring(0, 2).toLowerCase() == 'ps' ||67goog.LOCALE.substring(0, 2).toLowerCase() == 'sd' ||68goog.LOCALE.substring(0, 2).toLowerCase() == 'ug' ||69goog.LOCALE.substring(0, 2).toLowerCase() == 'ur' ||70goog.LOCALE.substring(0, 2).toLowerCase() == 'yi') &&71(goog.LOCALE.length == 2 || goog.LOCALE.substring(2, 3) == '-' ||72goog.LOCALE.substring(2, 3) == '_')) ||73(goog.LOCALE.length >= 3 &&74goog.LOCALE.substring(0, 3).toLowerCase() == 'ckb' &&75(goog.LOCALE.length == 3 || goog.LOCALE.substring(3, 4) == '-' ||76goog.LOCALE.substring(3, 4) == '_'));777879/**80* Unicode formatting characters and directionality string constants.81* @enum {string}82*/83goog.i18n.bidi.Format = {84/** Unicode "Left-To-Right Embedding" (LRE) character. */85LRE: '\u202A',86/** Unicode "Right-To-Left Embedding" (RLE) character. */87RLE: '\u202B',88/** Unicode "Pop Directional Formatting" (PDF) character. */89PDF: '\u202C',90/** Unicode "Left-To-Right Mark" (LRM) character. */91LRM: '\u200E',92/** Unicode "Right-To-Left Mark" (RLM) character. */93RLM: '\u200F'94};959697/**98* Directionality enum.99* @enum {number}100*/101goog.i18n.bidi.Dir = {102/**103* Left-to-right.104*/105LTR: 1,106107/**108* Right-to-left.109*/110RTL: -1,111112/**113* Neither left-to-right nor right-to-left.114*/115NEUTRAL: 0116};117118119/**120* 'right' string constant.121* @type {string}122*/123goog.i18n.bidi.RIGHT = 'right';124125126/**127* 'left' string constant.128* @type {string}129*/130goog.i18n.bidi.LEFT = 'left';131132133/**134* 'left' if locale is RTL, 'right' if not.135* @type {string}136*/137goog.i18n.bidi.I18N_RIGHT =138goog.i18n.bidi.IS_RTL ? goog.i18n.bidi.LEFT : goog.i18n.bidi.RIGHT;139140141/**142* 'right' if locale is RTL, 'left' if not.143* @type {string}144*/145goog.i18n.bidi.I18N_LEFT =146goog.i18n.bidi.IS_RTL ? goog.i18n.bidi.RIGHT : goog.i18n.bidi.LEFT;147148149/**150* Convert a directionality given in various formats to a goog.i18n.bidi.Dir151* constant. Useful for interaction with different standards of directionality152* representation.153*154* @param {goog.i18n.bidi.Dir|number|boolean|null} givenDir Directionality given155* in one of the following formats:156* 1. A goog.i18n.bidi.Dir constant.157* 2. A number (positive = LTR, negative = RTL, 0 = neutral).158* 3. A boolean (true = RTL, false = LTR).159* 4. A null for unknown directionality.160* @param {boolean=} opt_noNeutral Whether a givenDir of zero or161* goog.i18n.bidi.Dir.NEUTRAL should be treated as null, i.e. unknown, in162* order to preserve legacy behavior.163* @return {?goog.i18n.bidi.Dir} A goog.i18n.bidi.Dir constant matching the164* given directionality. If given null, returns null (i.e. unknown).165*/166goog.i18n.bidi.toDir = function(givenDir, opt_noNeutral) {167if (typeof givenDir == 'number') {168// This includes the non-null goog.i18n.bidi.Dir case.169return givenDir > 0 ? goog.i18n.bidi.Dir.LTR : givenDir < 0 ?170goog.i18n.bidi.Dir.RTL :171opt_noNeutral ? null : goog.i18n.bidi.Dir.NEUTRAL;172} else if (givenDir == null) {173return null;174} else {175// Must be typeof givenDir == 'boolean'.176return givenDir ? goog.i18n.bidi.Dir.RTL : goog.i18n.bidi.Dir.LTR;177}178};179180181/**182* A practical pattern to identify strong LTR characters. This pattern is not183* theoretically correct according to the Unicode standard. It is simplified for184* performance and small code size.185* @type {string}186* @private187*/188goog.i18n.bidi.ltrChars_ =189'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF' +190'\u200E\u2C00-\uFB1C\uFE00-\uFE6F\uFEFD-\uFFFF';191192193/**194* A practical pattern to identify strong RTL character. This pattern is not195* theoretically correct according to the Unicode standard. It is simplified196* for performance and small code size.197* @type {string}198* @private199*/200goog.i18n.bidi.rtlChars_ =201'\u0591-\u06EF\u06FA-\u07FF\u200F\uFB1D-\uFDFF\uFE70-\uFEFC';202203204/**205* Simplified regular expression for an HTML tag (opening or closing) or an HTML206* escape. We might want to skip over such expressions when estimating the text207* directionality.208* @type {RegExp}209* @private210*/211goog.i18n.bidi.htmlSkipReg_ = /<[^>]*>|&[^;]+;/g;212213214/**215* Returns the input text with spaces instead of HTML tags or HTML escapes, if216* opt_isStripNeeded is true. Else returns the input as is.217* Useful for text directionality estimation.218* Note: the function should not be used in other contexts; it is not 100%219* correct, but rather a good-enough implementation for directionality220* estimation purposes.221* @param {string} str The given string.222* @param {boolean=} opt_isStripNeeded Whether to perform the stripping.223* Default: false (to retain consistency with calling functions).224* @return {string} The given string cleaned of HTML tags / escapes.225* @private226*/227goog.i18n.bidi.stripHtmlIfNeeded_ = function(str, opt_isStripNeeded) {228return opt_isStripNeeded ? str.replace(goog.i18n.bidi.htmlSkipReg_, '') : str;229};230231232/**233* Regular expression to check for RTL characters.234* @type {RegExp}235* @private236*/237goog.i18n.bidi.rtlCharReg_ = new RegExp('[' + goog.i18n.bidi.rtlChars_ + ']');238239240/**241* Regular expression to check for LTR characters.242* @type {RegExp}243* @private244*/245goog.i18n.bidi.ltrCharReg_ = new RegExp('[' + goog.i18n.bidi.ltrChars_ + ']');246247248/**249* Test whether the given string has any RTL characters in it.250* @param {string} str The given string that need to be tested.251* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.252* Default: false.253* @return {boolean} Whether the string contains RTL characters.254*/255goog.i18n.bidi.hasAnyRtl = function(str, opt_isHtml) {256return goog.i18n.bidi.rtlCharReg_.test(257goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));258};259260261/**262* Test whether the given string has any RTL characters in it.263* @param {string} str The given string that need to be tested.264* @return {boolean} Whether the string contains RTL characters.265* @deprecated Use hasAnyRtl.266*/267goog.i18n.bidi.hasRtlChar = goog.i18n.bidi.hasAnyRtl;268269270/**271* Test whether the given string has any LTR characters in it.272* @param {string} str The given string that need to be tested.273* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.274* Default: false.275* @return {boolean} Whether the string contains LTR characters.276*/277goog.i18n.bidi.hasAnyLtr = function(str, opt_isHtml) {278return goog.i18n.bidi.ltrCharReg_.test(279goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));280};281282283/**284* Regular expression pattern to check if the first character in the string285* is LTR.286* @type {RegExp}287* @private288*/289goog.i18n.bidi.ltrRe_ = new RegExp('^[' + goog.i18n.bidi.ltrChars_ + ']');290291292/**293* Regular expression pattern to check if the first character in the string294* is RTL.295* @type {RegExp}296* @private297*/298goog.i18n.bidi.rtlRe_ = new RegExp('^[' + goog.i18n.bidi.rtlChars_ + ']');299300301/**302* Check if the first character in the string is RTL or not.303* @param {string} str The given string that need to be tested.304* @return {boolean} Whether the first character in str is an RTL char.305*/306goog.i18n.bidi.isRtlChar = function(str) {307return goog.i18n.bidi.rtlRe_.test(str);308};309310311/**312* Check if the first character in the string is LTR or not.313* @param {string} str The given string that need to be tested.314* @return {boolean} Whether the first character in str is an LTR char.315*/316goog.i18n.bidi.isLtrChar = function(str) {317return goog.i18n.bidi.ltrRe_.test(str);318};319320321/**322* Check if the first character in the string is neutral or not.323* @param {string} str The given string that need to be tested.324* @return {boolean} Whether the first character in str is a neutral char.325*/326goog.i18n.bidi.isNeutralChar = function(str) {327return !goog.i18n.bidi.isLtrChar(str) && !goog.i18n.bidi.isRtlChar(str);328};329330331/**332* Regular expressions to check if a piece of text is of LTR directionality333* on first character with strong directionality.334* @type {RegExp}335* @private336*/337goog.i18n.bidi.ltrDirCheckRe_ = new RegExp(338'^[^' + goog.i18n.bidi.rtlChars_ + ']*[' + goog.i18n.bidi.ltrChars_ + ']');339340341/**342* Regular expressions to check if a piece of text is of RTL directionality343* on first character with strong directionality.344* @type {RegExp}345* @private346*/347goog.i18n.bidi.rtlDirCheckRe_ = new RegExp(348'^[^' + goog.i18n.bidi.ltrChars_ + ']*[' + goog.i18n.bidi.rtlChars_ + ']');349350351/**352* Check whether the first strongly directional character (if any) is RTL.353* @param {string} str String being checked.354* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.355* Default: false.356* @return {boolean} Whether RTL directionality is detected using the first357* strongly-directional character method.358*/359goog.i18n.bidi.startsWithRtl = function(str, opt_isHtml) {360return goog.i18n.bidi.rtlDirCheckRe_.test(361goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));362};363364365/**366* Check whether the first strongly directional character (if any) is RTL.367* @param {string} str String being checked.368* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.369* Default: false.370* @return {boolean} Whether RTL directionality is detected using the first371* strongly-directional character method.372* @deprecated Use startsWithRtl.373*/374goog.i18n.bidi.isRtlText = goog.i18n.bidi.startsWithRtl;375376377/**378* Check whether the first strongly directional character (if any) is LTR.379* @param {string} str String being checked.380* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.381* Default: false.382* @return {boolean} Whether LTR directionality is detected using the first383* strongly-directional character method.384*/385goog.i18n.bidi.startsWithLtr = function(str, opt_isHtml) {386return goog.i18n.bidi.ltrDirCheckRe_.test(387goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));388};389390391/**392* Check whether the first strongly directional character (if any) is LTR.393* @param {string} str String being checked.394* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.395* Default: false.396* @return {boolean} Whether LTR directionality is detected using the first397* strongly-directional character method.398* @deprecated Use startsWithLtr.399*/400goog.i18n.bidi.isLtrText = goog.i18n.bidi.startsWithLtr;401402403/**404* Regular expression to check if a string looks like something that must405* always be LTR even in RTL text, e.g. a URL. When estimating the406* directionality of text containing these, we treat these as weakly LTR,407* like numbers.408* @type {RegExp}409* @private410*/411goog.i18n.bidi.isRequiredLtrRe_ = /^http:\/\/.*/;412413414/**415* Check whether the input string either contains no strongly directional416* characters or looks like a url.417* @param {string} str String being checked.418* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.419* Default: false.420* @return {boolean} Whether neutral directionality is detected.421*/422goog.i18n.bidi.isNeutralText = function(str, opt_isHtml) {423str = goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml);424return goog.i18n.bidi.isRequiredLtrRe_.test(str) ||425!goog.i18n.bidi.hasAnyLtr(str) && !goog.i18n.bidi.hasAnyRtl(str);426};427428429/**430* Regular expressions to check if the last strongly-directional character in a431* piece of text is LTR.432* @type {RegExp}433* @private434*/435goog.i18n.bidi.ltrExitDirCheckRe_ = new RegExp(436'[' + goog.i18n.bidi.ltrChars_ + '][^' + goog.i18n.bidi.rtlChars_ + ']*$');437438439/**440* Regular expressions to check if the last strongly-directional character in a441* piece of text is RTL.442* @type {RegExp}443* @private444*/445goog.i18n.bidi.rtlExitDirCheckRe_ = new RegExp(446'[' + goog.i18n.bidi.rtlChars_ + '][^' + goog.i18n.bidi.ltrChars_ + ']*$');447448449/**450* Check if the exit directionality a piece of text is LTR, i.e. if the last451* strongly-directional character in the string is LTR.452* @param {string} str String being checked.453* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.454* Default: false.455* @return {boolean} Whether LTR exit directionality was detected.456*/457goog.i18n.bidi.endsWithLtr = function(str, opt_isHtml) {458return goog.i18n.bidi.ltrExitDirCheckRe_.test(459goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));460};461462463/**464* Check if the exit directionality a piece of text is LTR, i.e. if the last465* strongly-directional character in the string is LTR.466* @param {string} str String being checked.467* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.468* Default: false.469* @return {boolean} Whether LTR exit directionality was detected.470* @deprecated Use endsWithLtr.471*/472goog.i18n.bidi.isLtrExitText = goog.i18n.bidi.endsWithLtr;473474475/**476* Check if the exit directionality a piece of text is RTL, i.e. if the last477* strongly-directional character in the string is RTL.478* @param {string} str String being checked.479* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.480* Default: false.481* @return {boolean} Whether RTL exit directionality was detected.482*/483goog.i18n.bidi.endsWithRtl = function(str, opt_isHtml) {484return goog.i18n.bidi.rtlExitDirCheckRe_.test(485goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));486};487488489/**490* Check if the exit directionality a piece of text is RTL, i.e. if the last491* strongly-directional character in the string is RTL.492* @param {string} str String being checked.493* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.494* Default: false.495* @return {boolean} Whether RTL exit directionality was detected.496* @deprecated Use endsWithRtl.497*/498goog.i18n.bidi.isRtlExitText = goog.i18n.bidi.endsWithRtl;499500501/**502* A regular expression for matching right-to-left language codes.503* See {@link #isRtlLanguage} for the design.504* @type {RegExp}505* @private506*/507goog.i18n.bidi.rtlLocalesRe_ = new RegExp(508'^(ar|ckb|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|' +509'.*[-_](Arab|Hebr|Thaa|Nkoo|Tfng))' +510'(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)',511'i');512513514/**515* Check if a BCP 47 / III language code indicates an RTL language, i.e. either:516* - a language code explicitly specifying one of the right-to-left scripts,517* e.g. "az-Arab", or<p>518* - a language code specifying one of the languages normally written in a519* right-to-left script, e.g. "fa" (Farsi), except ones explicitly specifying520* Latin or Cyrillic script (which are the usual LTR alternatives).<p>521* The list of right-to-left scripts appears in the 100-199 range in522* http://www.unicode.org/iso15924/iso15924-num.html, of which Arabic and523* Hebrew are by far the most widely used. We also recognize Thaana, N'Ko, and524* Tifinagh, which also have significant modern usage. The rest (Syriac,525* Samaritan, Mandaic, etc.) seem to have extremely limited or no modern usage526* and are not recognized to save on code size.527* The languages usually written in a right-to-left script are taken as those528* with Suppress-Script: Hebr|Arab|Thaa|Nkoo|Tfng in529* http://www.iana.org/assignments/language-subtag-registry,530* as well as Central (or Sorani) Kurdish (ckb), Sindhi (sd) and Uyghur (ug).531* Other subtags of the language code, e.g. regions like EG (Egypt), are532* ignored.533* @param {string} lang BCP 47 (a.k.a III) language code.534* @return {boolean} Whether the language code is an RTL language.535*/536goog.i18n.bidi.isRtlLanguage = function(lang) {537return goog.i18n.bidi.rtlLocalesRe_.test(lang);538};539540541/**542* Regular expression for bracket guard replacement in text.543* @type {RegExp}544* @private545*/546goog.i18n.bidi.bracketGuardTextRe_ =547/(\(.*?\)+)|(\[.*?\]+)|(\{.*?\}+)|(<.*?>+)/g;548549550/**551* Apply bracket guard using LRM and RLM. This is to address the problem of552* messy bracket display frequently happens in RTL layout.553* This function works for plain text, not for HTML. In HTML, the opening554* bracket might be in a different context than the closing bracket (such as555* an attribute value).556* @param {string} s The string that need to be processed.557* @param {boolean=} opt_isRtlContext specifies default direction (usually558* direction of the UI).559* @return {string} The processed string, with all bracket guarded.560*/561goog.i18n.bidi.guardBracketInText = function(s, opt_isRtlContext) {562var useRtl = opt_isRtlContext === undefined ? goog.i18n.bidi.hasAnyRtl(s) :563opt_isRtlContext;564var mark = useRtl ? goog.i18n.bidi.Format.RLM : goog.i18n.bidi.Format.LRM;565return s.replace(goog.i18n.bidi.bracketGuardTextRe_, mark + '$&' + mark);566};567568569/**570* Enforce the html snippet in RTL directionality regardless overall context.571* If the html piece was enclosed by tag, dir will be applied to existing572* tag, otherwise a span tag will be added as wrapper. For this reason, if573* html snippet start with with tag, this tag must enclose the whole piece. If574* the tag already has a dir specified, this new one will override existing575* one in behavior (tested on FF and IE).576* @param {string} html The string that need to be processed.577* @return {string} The processed string, with directionality enforced to RTL.578*/579goog.i18n.bidi.enforceRtlInHtml = function(html) {580if (html.charAt(0) == '<') {581return html.replace(/<\w+/, '$& dir=rtl');582}583// '\n' is important for FF so that it won't incorrectly merge span groups584return '\n<span dir=rtl>' + html + '</span>';585};586587588/**589* Enforce RTL on both end of the given text piece using unicode BiDi formatting590* characters RLE and PDF.591* @param {string} text The piece of text that need to be wrapped.592* @return {string} The wrapped string after process.593*/594goog.i18n.bidi.enforceRtlInText = function(text) {595return goog.i18n.bidi.Format.RLE + text + goog.i18n.bidi.Format.PDF;596};597598599/**600* Enforce the html snippet in RTL directionality regardless overall context.601* If the html piece was enclosed by tag, dir will be applied to existing602* tag, otherwise a span tag will be added as wrapper. For this reason, if603* html snippet start with with tag, this tag must enclose the whole piece. If604* the tag already has a dir specified, this new one will override existing605* one in behavior (tested on FF and IE).606* @param {string} html The string that need to be processed.607* @return {string} The processed string, with directionality enforced to RTL.608*/609goog.i18n.bidi.enforceLtrInHtml = function(html) {610if (html.charAt(0) == '<') {611return html.replace(/<\w+/, '$& dir=ltr');612}613// '\n' is important for FF so that it won't incorrectly merge span groups614return '\n<span dir=ltr>' + html + '</span>';615};616617618/**619* Enforce LTR on both end of the given text piece using unicode BiDi formatting620* characters LRE and PDF.621* @param {string} text The piece of text that need to be wrapped.622* @return {string} The wrapped string after process.623*/624goog.i18n.bidi.enforceLtrInText = function(text) {625return goog.i18n.bidi.Format.LRE + text + goog.i18n.bidi.Format.PDF;626};627628629/**630* Regular expression to find dimensions such as "padding: .3 0.4ex 5px 6;"631* @type {RegExp}632* @private633*/634goog.i18n.bidi.dimensionsRe_ =635/:\s*([.\d][.\w]*)\s+([.\d][.\w]*)\s+([.\d][.\w]*)\s+([.\d][.\w]*)/g;636637638/**639* Regular expression for left.640* @type {RegExp}641* @private642*/643goog.i18n.bidi.leftRe_ = /left/gi;644645646/**647* Regular expression for right.648* @type {RegExp}649* @private650*/651goog.i18n.bidi.rightRe_ = /right/gi;652653654/**655* Placeholder regular expression for swapping.656* @type {RegExp}657* @private658*/659goog.i18n.bidi.tempRe_ = /%%%%/g;660661662/**663* Swap location parameters and 'left'/'right' in CSS specification. The664* processed string will be suited for RTL layout. Though this function can665* cover most cases, there are always exceptions. It is suggested to put666* those exceptions in separate group of CSS string.667* @param {string} cssStr CSS spefication string.668* @return {string} Processed CSS specification string.669*/670goog.i18n.bidi.mirrorCSS = function(cssStr) {671return cssStr672.673// reverse dimensions674replace(goog.i18n.bidi.dimensionsRe_, ':$1 $4 $3 $2')675.replace(goog.i18n.bidi.leftRe_, '%%%%')676. // swap left and right677replace(goog.i18n.bidi.rightRe_, goog.i18n.bidi.LEFT)678.replace(goog.i18n.bidi.tempRe_, goog.i18n.bidi.RIGHT);679};680681682/**683* Regular expression for hebrew double quote substitution, finding quote684* directly after hebrew characters.685* @type {RegExp}686* @private687*/688goog.i18n.bidi.doubleQuoteSubstituteRe_ = /([\u0591-\u05f2])"/g;689690691/**692* Regular expression for hebrew single quote substitution, finding quote693* directly after hebrew characters.694* @type {RegExp}695* @private696*/697goog.i18n.bidi.singleQuoteSubstituteRe_ = /([\u0591-\u05f2])'/g;698699700/**701* Replace the double and single quote directly after a Hebrew character with702* GERESH and GERSHAYIM. In such case, most likely that's user intention.703* @param {string} str String that need to be processed.704* @return {string} Processed string with double/single quote replaced.705*/706goog.i18n.bidi.normalizeHebrewQuote = function(str) {707return str.replace(goog.i18n.bidi.doubleQuoteSubstituteRe_, '$1\u05f4')708.replace(goog.i18n.bidi.singleQuoteSubstituteRe_, '$1\u05f3');709};710711712/**713* Regular expression to split a string into "words" for directionality714* estimation based on relative word counts.715* @type {RegExp}716* @private717*/718goog.i18n.bidi.wordSeparatorRe_ = /\s+/;719720721/**722* Regular expression to check if a string contains any numerals. Used to723* differentiate between completely neutral strings and those containing724* numbers, which are weakly LTR.725*726* Native Arabic digits (\u0660 - \u0669) are not included because although they727* do flow left-to-right inside a number, this is the case even if the overall728* directionality is RTL, and a mathematical expression using these digits is729* supposed to flow right-to-left overall, including unary plus and minus730* appearing to the right of a number, and this does depend on the overall731* directionality being RTL. The digits used in Farsi (\u06F0 - \u06F9), on the732* other hand, are included, since Farsi math (including unary plus and minus)733* does flow left-to-right.734*735* @type {RegExp}736* @private737*/738goog.i18n.bidi.hasNumeralsRe_ = /[\d\u06f0-\u06f9]/;739740741/**742* This constant controls threshold of RTL directionality.743* @type {number}744* @private745*/746goog.i18n.bidi.rtlDetectionThreshold_ = 0.40;747748749/**750* Estimates the directionality of a string based on relative word counts.751* If the number of RTL words is above a certain percentage of the total number752* of strongly directional words, returns RTL.753* Otherwise, if any words are strongly or weakly LTR, returns LTR.754* Otherwise, returns UNKNOWN, which is used to mean "neutral".755* Numbers are counted as weakly LTR.756* @param {string} str The string to be checked.757* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.758* Default: false.759* @return {goog.i18n.bidi.Dir} Estimated overall directionality of {@code str}.760*/761goog.i18n.bidi.estimateDirection = function(str, opt_isHtml) {762var rtlCount = 0;763var totalCount = 0;764var hasWeaklyLtr = false;765var tokens = goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml)766.split(goog.i18n.bidi.wordSeparatorRe_);767for (var i = 0; i < tokens.length; i++) {768var token = tokens[i];769if (goog.i18n.bidi.startsWithRtl(token)) {770rtlCount++;771totalCount++;772} else if (goog.i18n.bidi.isRequiredLtrRe_.test(token)) {773hasWeaklyLtr = true;774} else if (goog.i18n.bidi.hasAnyLtr(token)) {775totalCount++;776} else if (goog.i18n.bidi.hasNumeralsRe_.test(token)) {777hasWeaklyLtr = true;778}779}780781return totalCount == 0 ?782(hasWeaklyLtr ? goog.i18n.bidi.Dir.LTR : goog.i18n.bidi.Dir.NEUTRAL) :783(rtlCount / totalCount > goog.i18n.bidi.rtlDetectionThreshold_ ?784goog.i18n.bidi.Dir.RTL :785goog.i18n.bidi.Dir.LTR);786};787788789/**790* Check the directionality of a piece of text, return true if the piece of791* text should be laid out in RTL direction.792* @param {string} str The piece of text that need to be detected.793* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.794* Default: false.795* @return {boolean} Whether this piece of text should be laid out in RTL.796*/797goog.i18n.bidi.detectRtlDirectionality = function(str, opt_isHtml) {798return goog.i18n.bidi.estimateDirection(str, opt_isHtml) ==799goog.i18n.bidi.Dir.RTL;800};801802803/**804* Sets text input element's directionality and text alignment based on a805* given directionality. Does nothing if the given directionality is unknown or806* neutral.807* @param {Element} element Input field element to set directionality to.808* @param {goog.i18n.bidi.Dir|number|boolean|null} dir Desired directionality,809* given in one of the following formats:810* 1. A goog.i18n.bidi.Dir constant.811* 2. A number (positive = LRT, negative = RTL, 0 = neutral).812* 3. A boolean (true = RTL, false = LTR).813* 4. A null for unknown directionality.814*/815goog.i18n.bidi.setElementDirAndAlign = function(element, dir) {816if (element) {817dir = goog.i18n.bidi.toDir(dir);818if (dir) {819element.style.textAlign = dir == goog.i18n.bidi.Dir.RTL ?820goog.i18n.bidi.RIGHT :821goog.i18n.bidi.LEFT;822element.dir = dir == goog.i18n.bidi.Dir.RTL ? 'rtl' : 'ltr';823}824}825};826827828/**829* Sets element dir based on estimated directionality of the given text.830* @param {!Element} element831* @param {string} text832*/833goog.i18n.bidi.setElementDirByTextDirectionality = function(element, text) {834switch (goog.i18n.bidi.estimateDirection(text)) {835case (goog.i18n.bidi.Dir.LTR):836element.dir = 'ltr';837break;838case (goog.i18n.bidi.Dir.RTL):839element.dir = 'rtl';840break;841default:842// Default for no direction, inherit from document.843element.removeAttribute('dir');844}845};846847848849/**850* Strings that have an (optional) known direction.851*852* Implementations of this interface are string-like objects that carry an853* attached direction, if known.854* @interface855*/856goog.i18n.bidi.DirectionalString = function() {};857858859/**860* Interface marker of the DirectionalString interface.861*862* This property can be used to determine at runtime whether or not an object863* implements this interface. All implementations of this interface set this864* property to {@code true}.865* @type {boolean}866*/867goog.i18n.bidi.DirectionalString.prototype868.implementsGoogI18nBidiDirectionalString;869870871/**872* Retrieves this object's known direction (if any).873* @return {?goog.i18n.bidi.Dir} The known direction. Null if unknown.874*/875goog.i18n.bidi.DirectionalString.prototype.getDirection;876877878