Path: blob/trunk/third_party/closure/goog/i18n/bidi.js
4341 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Utility functions for supporting Bidi issues.8*/91011/**12* Namespace for bidi supporting functions.13*/14goog.provide('goog.i18n.bidi');15goog.provide('goog.i18n.bidi.Dir');16goog.provide('goog.i18n.bidi.DirectionalString');17goog.provide('goog.i18n.bidi.Format');181920/**21* @define {boolean} FORCE_RTL forces the {@link goog.i18n.bidi.IS_RTL} constant22* to say that the current locale is a RTL locale. This should only be used23* if you want to override the default behavior for deciding whether the24* current locale is RTL or not.25*26* {@see goog.i18n.bidi.IS_RTL}27*/28goog.i18n.bidi.FORCE_RTL = goog.define('goog.i18n.bidi.FORCE_RTL', false);293031/**32* Constant that defines whether or not the current locale is a RTL locale.33* If {@link goog.i18n.bidi.FORCE_RTL} is not true, this constant will default34* to check that {@link goog.LOCALE} is one of a few major RTL locales.35*36* <p>This is designed to be a maximally efficient compile-time constant. For37* example, for the default goog.LOCALE, compiling38* "if (goog.i18n.bidi.IS_RTL) alert('rtl') else {}" should produce no code. It39* is this design consideration that limits the implementation to only40* supporting a few major RTL locales, as opposed to the broader repertoire of41* something like goog.i18n.bidi.isRtlLanguage.42*43* <p>Since this constant refers to the directionality of the locale, it is up44* to the caller to determine if this constant should also be used for the45* direction of the UI.46*47* {@see goog.LOCALE}48*49* @type {boolean}50*51* TODO(user): write a test that checks that this is a compile-time constant.52*/53// LINT.IfChange54goog.i18n.bidi.IS_RTL =55goog.i18n.bidi.FORCE_RTL ||56((goog.LOCALE.substring(0, 2).toLowerCase() == 'ar' ||57goog.LOCALE.substring(0, 2).toLowerCase() == 'fa' ||58goog.LOCALE.substring(0, 2).toLowerCase() == 'he' ||59goog.LOCALE.substring(0, 2).toLowerCase() == 'iw' ||60goog.LOCALE.substring(0, 2).toLowerCase() == 'ps' ||61goog.LOCALE.substring(0, 2).toLowerCase() == 'sd' ||62goog.LOCALE.substring(0, 2).toLowerCase() == 'ug' ||63goog.LOCALE.substring(0, 2).toLowerCase() == 'ur' ||64goog.LOCALE.substring(0, 2).toLowerCase() == 'yi') &&65(goog.LOCALE.length == 2 || goog.LOCALE.substring(2, 3) == '-' ||66goog.LOCALE.substring(2, 3) == '_')) ||67( // Specific to CKB (Central Kurdish)68goog.LOCALE.length >= 3 &&69goog.LOCALE.substring(0, 3).toLowerCase() == 'ckb' &&70(goog.LOCALE.length == 3 || goog.LOCALE.substring(3, 4) == '-' ||71goog.LOCALE.substring(3, 4) == '_')) ||72( // 2 letter language codes with RTL scripts73goog.LOCALE.length >= 7 &&74((goog.LOCALE.substring(2, 3) == '-' ||75goog.LOCALE.substring(2, 3) == '_') &&76(goog.LOCALE.substring(3, 7).toLowerCase() == 'adlm' ||77goog.LOCALE.substring(3, 7).toLowerCase() == 'arab' ||78goog.LOCALE.substring(3, 7).toLowerCase() == 'hebr' ||79goog.LOCALE.substring(3, 7).toLowerCase() == 'nkoo' ||80goog.LOCALE.substring(3, 7).toLowerCase() == 'rohg' ||81goog.LOCALE.substring(3, 7).toLowerCase() == 'thaa'))) ||82( // 3 letter languages codes with RTL scripts83goog.LOCALE.length >= 8 &&84((goog.LOCALE.substring(3, 4) == '-' ||85goog.LOCALE.substring(3, 4) == '_') &&86(goog.LOCALE.substring(4, 8).toLowerCase() == 'adlm' ||87goog.LOCALE.substring(4, 8).toLowerCase() == 'arab' ||88goog.LOCALE.substring(4, 8).toLowerCase() == 'hebr' ||89goog.LOCALE.substring(4, 8).toLowerCase() == 'nkoo' ||90goog.LOCALE.substring(4, 8).toLowerCase() == 'rohg' ||91goog.LOCALE.substring(4, 8).toLowerCase() == 'thaa')));92// closure/RtlLocalesTest.java)9394// TODO(user): Add additional scripts and languages that are RTL,95// e.g., mende, samaritan, etc.969798/**99* Unicode formatting characters and directionality string constants.100* @enum {string}101*/102goog.i18n.bidi.Format = {103/** Unicode "Left-To-Right Embedding" (LRE) character. */104LRE: '\u202A',105/** Unicode "Right-To-Left Embedding" (RLE) character. */106RLE: '\u202B',107/** Unicode "Pop Directional Formatting" (PDF) character. */108PDF: '\u202C',109/** Unicode "Left-To-Right Mark" (LRM) character. */110LRM: '\u200E',111/** Unicode "Right-To-Left Mark" (RLM) character. */112RLM: '\u200F'113};114115116/**117* Directionality enum.118* @enum {number}119*/120goog.i18n.bidi.Dir = {121/**122* Left-to-right.123*/124LTR: 1,125126/**127* Right-to-left.128*/129RTL: -1,130131/**132* Neither left-to-right nor right-to-left.133*/134NEUTRAL: 0135};136137138/**139* 'right' string constant.140* @type {string}141*/142goog.i18n.bidi.RIGHT = 'right';143144145/**146* 'left' string constant.147* @type {string}148*/149goog.i18n.bidi.LEFT = 'left';150151152/**153* 'left' if locale is RTL, 'right' if not.154* @type {string}155*/156goog.i18n.bidi.I18N_RIGHT =157goog.i18n.bidi.IS_RTL ? goog.i18n.bidi.LEFT : goog.i18n.bidi.RIGHT;158159160/**161* 'right' if locale is RTL, 'left' if not.162* @type {string}163*/164goog.i18n.bidi.I18N_LEFT =165goog.i18n.bidi.IS_RTL ? goog.i18n.bidi.RIGHT : goog.i18n.bidi.LEFT;166167168/**169* Convert a directionality given in various formats to a goog.i18n.bidi.Dir170* constant. Useful for interaction with different standards of directionality171* representation.172*173* @param {goog.i18n.bidi.Dir|number|boolean|null} givenDir Directionality given174* in one of the following formats:175* 1. A goog.i18n.bidi.Dir constant.176* 2. A number (positive = LTR, negative = RTL, 0 = neutral).177* 3. A boolean (true = RTL, false = LTR).178* 4. A null for unknown directionality.179* @param {boolean=} opt_noNeutral Whether a givenDir of zero or180* goog.i18n.bidi.Dir.NEUTRAL should be treated as null, i.e. unknown, in181* order to preserve legacy behavior.182* @return {?goog.i18n.bidi.Dir} A goog.i18n.bidi.Dir constant matching the183* given directionality. If given null, returns null (i.e. unknown).184*/185goog.i18n.bidi.toDir = function(givenDir, opt_noNeutral) {186'use strict';187if (typeof givenDir == 'number') {188// This includes the non-null goog.i18n.bidi.Dir case.189return givenDir > 0 ?190goog.i18n.bidi.Dir.LTR :191givenDir < 0 ? goog.i18n.bidi.Dir.RTL :192opt_noNeutral ? null : goog.i18n.bidi.Dir.NEUTRAL;193} else if (givenDir == null) {194return null;195} else {196// Must be typeof givenDir == 'boolean'.197return givenDir ? goog.i18n.bidi.Dir.RTL : goog.i18n.bidi.Dir.LTR;198}199};200201202/**203* A practical pattern to identify strong LTR character in the BMP.204* This pattern is not theoretically correct according to the Unicode205* standard. It is simplified for performance and small code size.206* It also partially supports LTR scripts beyond U+FFFF by including207* UTF-16 high surrogate values corresponding to mostly L-class code208* point ranges.209* However, low surrogate values and private-use regions are not included210* in this RegEx.211* @type {string}212* @private213*/214goog.i18n.bidi.ltrChars_ =215'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0900-\u1FFF' +216'\u200E\u2C00-\uD801\uD804-\uD839\uD83C-\uDBFF' +217'\uF900-\uFB1C\uFE00-\uFE6F\uFEFD-\uFFFF';218219/**220* A practical pattern to identify strong RTL character. This pattern is not221* theoretically correct according to the Unicode standard. It is simplified222* for performance and small code size.223* It also partially supports RTL scripts beyond U+FFFF by including224* UTF-16 high surrogate values corresponding to mostly R- or AL-class225* code point ranges.226* However, low surrogate values and private-use regions are not included227* in this RegEx.228* @type {string}229* @private230*/231goog.i18n.bidi.rtlChars_ =232'\u0591-\u06EF\u06FA-\u08FF\u200F\uD802-\uD803\uD83A-\uD83B' +233'\uFB1D-\uFDFF\uFE70-\uFEFC';234235/**236* Simplified regular expression for an HTML tag (opening or closing) or an HTML237* escape. We might want to skip over such expressions when estimating the text238* directionality.239* @type {RegExp}240* @private241*/242goog.i18n.bidi.htmlSkipReg_ = /<[^>]*>|&[^;]+;/g;243244245/**246* Returns the input text with spaces instead of HTML tags or HTML escapes, if247* opt_isStripNeeded is true. Else returns the input as is.248* Useful for text directionality estimation.249* Note: the function should not be used in other contexts; it is not 100%250* correct, but rather a good-enough implementation for directionality251* estimation purposes.252* @param {string} str The given string.253* @param {boolean=} opt_isStripNeeded Whether to perform the stripping.254* Default: false (to retain consistency with calling functions).255* @return {string} The given string cleaned of HTML tags / escapes.256* @private257*/258goog.i18n.bidi.stripHtmlIfNeeded_ = function(str, opt_isStripNeeded) {259'use strict';260return opt_isStripNeeded ? str.replace(goog.i18n.bidi.htmlSkipReg_, '') : str;261};262263264/**265* Regular expression to check for RTL characters, BMP and high surrogate.266* @type {RegExp}267* @private268*/269goog.i18n.bidi.rtlCharReg_ = new RegExp('[' + goog.i18n.bidi.rtlChars_ + ']');270271272/**273* Regular expression to check for LTR characters.274* @type {RegExp}275* @private276*/277goog.i18n.bidi.ltrCharReg_ = new RegExp('[' + goog.i18n.bidi.ltrChars_ + ']');278279280/**281* Test whether the given string has any RTL characters in it.282* @param {string} str The given string that need to be tested.283* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.284* Default: false.285* @return {boolean} Whether the string contains RTL characters.286*/287goog.i18n.bidi.hasAnyRtl = function(str, opt_isHtml) {288'use strict';289return goog.i18n.bidi.rtlCharReg_.test(290goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));291};292293294/**295* Test whether the given string has any RTL characters in it.296* @param {string} str The given string that need to be tested.297* @return {boolean} Whether the string contains RTL characters.298* @deprecated Use hasAnyRtl.299*/300goog.i18n.bidi.hasRtlChar = goog.i18n.bidi.hasAnyRtl;301302303/**304* Test whether the given string has any LTR characters in it.305* @param {string} str The given string that need to be tested.306* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.307* Default: false.308* @return {boolean} Whether the string contains LTR characters.309*/310goog.i18n.bidi.hasAnyLtr = function(str, opt_isHtml) {311'use strict';312return goog.i18n.bidi.ltrCharReg_.test(313goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));314};315316317/**318* Regular expression pattern to check if the first character in the string319* is LTR.320* @type {RegExp}321* @private322*/323goog.i18n.bidi.ltrRe_ = new RegExp('^[' + goog.i18n.bidi.ltrChars_ + ']');324325326/**327* Regular expression pattern to check if the first character in the string328* is RTL.329* @type {RegExp}330* @private331*/332goog.i18n.bidi.rtlRe_ = new RegExp('^[' + goog.i18n.bidi.rtlChars_ + ']');333334335/**336* Check if the first character in the string is RTL or not.337* @param {string} str The given string that need to be tested.338* @return {boolean} Whether the first character in str is an RTL char.339*/340goog.i18n.bidi.isRtlChar = function(str) {341'use strict';342return goog.i18n.bidi.rtlRe_.test(str);343};344345346/**347* Check if the first character in the string is LTR or not.348* @param {string} str The given string that need to be tested.349* @return {boolean} Whether the first character in str is an LTR char.350*/351goog.i18n.bidi.isLtrChar = function(str) {352'use strict';353return goog.i18n.bidi.ltrRe_.test(str);354};355356357/**358* Check if the first character in the string is neutral or not.359* @param {string} str The given string that need to be tested.360* @return {boolean} Whether the first character in str is a neutral char.361*/362goog.i18n.bidi.isNeutralChar = function(str) {363'use strict';364return !goog.i18n.bidi.isLtrChar(str) && !goog.i18n.bidi.isRtlChar(str);365};366367368/**369* Regular expressions to check if a piece of text is of LTR directionality370* on first character with strong directionality.371* @type {RegExp}372* @private373*/374goog.i18n.bidi.ltrDirCheckRe_ = new RegExp(375'^[^' + goog.i18n.bidi.rtlChars_ + ']*[' + goog.i18n.bidi.ltrChars_ + ']');376377378/**379* Regular expressions to check if a piece of text is of RTL directionality380* on first character with strong directionality.381* @type {RegExp}382* @private383*/384goog.i18n.bidi.rtlDirCheckRe_ = new RegExp(385'^[^' + goog.i18n.bidi.ltrChars_ + ']*[' + goog.i18n.bidi.rtlChars_ + ']');386387388/**389* Check whether the first strongly directional character (if any) is RTL.390* @param {string} str String being checked.391* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.392* Default: false.393* @return {boolean} Whether RTL directionality is detected using the first394* strongly-directional character method.395*/396goog.i18n.bidi.startsWithRtl = function(str, opt_isHtml) {397'use strict';398return goog.i18n.bidi.rtlDirCheckRe_.test(399goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));400};401402403/**404* Check whether the first strongly directional character (if any) is RTL.405* @param {string} str String being checked.406* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.407* Default: false.408* @return {boolean} Whether RTL directionality is detected using the first409* strongly-directional character method.410* @deprecated Use startsWithRtl.411*/412goog.i18n.bidi.isRtlText = goog.i18n.bidi.startsWithRtl;413414415/**416* Check whether the first strongly directional character (if any) is LTR.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 LTR directionality is detected using the first421* strongly-directional character method.422*/423goog.i18n.bidi.startsWithLtr = function(str, opt_isHtml) {424'use strict';425return goog.i18n.bidi.ltrDirCheckRe_.test(426goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));427};428429430/**431* Check whether the first strongly directional character (if any) is LTR.432* @param {string} str String being checked.433* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.434* Default: false.435* @return {boolean} Whether LTR directionality is detected using the first436* strongly-directional character method.437* @deprecated Use startsWithLtr.438*/439goog.i18n.bidi.isLtrText = goog.i18n.bidi.startsWithLtr;440441442/**443* Regular expression to check if a string looks like something that must444* always be LTR even in RTL text, e.g. a URL. When estimating the445* directionality of text containing these, we treat these as weakly LTR,446* like numbers.447* @type {RegExp}448* @private449*/450goog.i18n.bidi.isRequiredLtrRe_ = /^http:\/\/.*/;451452453/**454* Check whether the input string either contains no strongly directional455* characters or looks like a url.456* @param {string} str String being checked.457* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.458* Default: false.459* @return {boolean} Whether neutral directionality is detected.460*/461goog.i18n.bidi.isNeutralText = function(str, opt_isHtml) {462'use strict';463str = goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml);464return goog.i18n.bidi.isRequiredLtrRe_.test(str) ||465!goog.i18n.bidi.hasAnyLtr(str) && !goog.i18n.bidi.hasAnyRtl(str);466};467468469/**470* Regular expressions to check if the last strongly-directional character in a471* piece of text is LTR.472* @type {RegExp}473* @private474*/475goog.i18n.bidi.ltrExitDirCheckRe_ = new RegExp(476'[' + goog.i18n.bidi.ltrChars_ + ']' +477'[^' + goog.i18n.bidi.rtlChars_ + ']*$');478479480/**481* Regular expressions to check if the last strongly-directional character in a482* piece of text is RTL.483* @type {RegExp}484* @private485*/486goog.i18n.bidi.rtlExitDirCheckRe_ = new RegExp(487'[' + goog.i18n.bidi.rtlChars_ + ']' +488'[^' + goog.i18n.bidi.ltrChars_ + ']*$');489490491/**492* Check if the exit directionality a piece of text is LTR, i.e. if the last493* strongly-directional character in the string is LTR.494* @param {string} str String being checked.495* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.496* Default: false.497* @return {boolean} Whether LTR exit directionality was detected.498*/499goog.i18n.bidi.endsWithLtr = function(str, opt_isHtml) {500'use strict';501return goog.i18n.bidi.ltrExitDirCheckRe_.test(502goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));503};504505506/**507* Check if the exit directionality a piece of text is LTR, i.e. if the last508* strongly-directional character in the string is LTR.509* @param {string} str String being checked.510* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.511* Default: false.512* @return {boolean} Whether LTR exit directionality was detected.513* @deprecated Use endsWithLtr.514*/515goog.i18n.bidi.isLtrExitText = goog.i18n.bidi.endsWithLtr;516517518/**519* Check if the exit directionality a piece of text is RTL, i.e. if the last520* strongly-directional character in the string is RTL.521* @param {string} str String being checked.522* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.523* Default: false.524* @return {boolean} Whether RTL exit directionality was detected.525*/526goog.i18n.bidi.endsWithRtl = function(str, opt_isHtml) {527'use strict';528return goog.i18n.bidi.rtlExitDirCheckRe_.test(529goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));530};531532533/**534* Check if the exit directionality a piece of text is RTL, i.e. if the last535* strongly-directional character in the string is RTL.536* @param {string} str String being checked.537* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.538* Default: false.539* @return {boolean} Whether RTL exit directionality was detected.540* @deprecated Use endsWithRtl.541*/542goog.i18n.bidi.isRtlExitText = goog.i18n.bidi.endsWithRtl;543544545/**546* A regular expression for matching right-to-left language codes.547* See {@link #isRtlLanguage} for the design.548* Note that not all RTL scripts are included.549* @type {!RegExp}550* @private551*/552goog.i18n.bidi.rtlLocalesRe_ = new RegExp(553'^(ar|ckb|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|' +554'.*[-_](Adlm|Arab|Hebr|Nkoo|Rohg|Thaa))' +555'(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)',556'i');557558559/**560* Check if a BCP 47 / III language code indicates an RTL language, i.e. either:561* - a language code explicitly specifying one of the right-to-left scripts,562* e.g. "az-Arab", or<p>563* - a language code specifying one of the languages normally written in a564* right-to-left script, e.g. "fa" (Farsi), except ones explicitly specifying565* Latin or Cyrillic script (which are the usual LTR alternatives).<p>566* The list of right-to-left scripts appears in the 100-199 range in567* http://www.unicode.org/iso15924/iso15924-num.html, of which Arabic and568* Hebrew are by far the most widely used. We also recognize Thaana, and N'Ko,569* which also have significant modern usage. Adlam and Rohingya570* scripts are now included since they can be expected to be used in the571* future. The rest (Syriac, Samaritan, Mandaic, etc.) seem to have extremely572* limited or no modern usage and are not recognized to save on code size. The573* languages usually written in a right-to-left script are taken as those with574* Suppress-Script: Hebr|Arab|Thaa|Nkoo|Adlm|Rohg in575* http://www.iana.org/assignments/language-subtag-registry,576* as well as Central (or Sorani) Kurdish (ckb), Sindhi (sd) and Uyghur (ug).577* Other subtags of the language code, e.g. regions like EG (Egypt), are578* ignored.579* @param {string} lang BCP 47 (a.k.a III) language code.580* @return {boolean} Whether the language code is an RTL language.581*/582goog.i18n.bidi.isRtlLanguage = function(lang) {583'use strict';584return goog.i18n.bidi.rtlLocalesRe_.test(lang);585};586587588/**589* Regular expression for bracket guard replacement in text.590* @type {RegExp}591* @private592*/593goog.i18n.bidi.bracketGuardTextRe_ =594/(\(.*?\)+)|(\[.*?\]+)|(\{.*?\}+)|(<.*?>+)/g;595596597/**598* Apply bracket guard using LRM and RLM. This is to address the problem of599* messy bracket display frequently happens in RTL layout.600* This function works for plain text, not for HTML. In HTML, the opening601* bracket might be in a different context than the closing bracket (such as602* an attribute value).603* @param {string} s The string that need to be processed.604* @param {boolean=} opt_isRtlContext specifies default direction (usually605* direction of the UI).606* @return {string} The processed string, with all bracket guarded.607*/608goog.i18n.bidi.guardBracketInText = function(s, opt_isRtlContext) {609'use strict';610const useRtl = opt_isRtlContext === undefined ? goog.i18n.bidi.hasAnyRtl(s) :611opt_isRtlContext;612const mark = useRtl ? goog.i18n.bidi.Format.RLM : goog.i18n.bidi.Format.LRM;613return s.replace(goog.i18n.bidi.bracketGuardTextRe_, mark + '$&' + mark);614};615616617/**618* Enforce the html snippet in RTL directionality regardless of overall context.619* If the html piece was enclosed by tag, dir will be applied to existing620* tag, otherwise a span tag will be added as wrapper. For this reason, if621* html snippet starts with a tag, this tag must enclose the whole piece. If622* the tag already has a dir specified, this new one will override existing623* one in behavior (tested on FF and IE).624* @param {string} html The string that need to be processed.625* @return {string} The processed string, with directionality enforced to RTL.626*/627goog.i18n.bidi.enforceRtlInHtml = function(html) {628'use strict';629if (html.charAt(0) == '<') {630return html.replace(/<\w+/, '$& dir=rtl');631}632// '\n' is important for FF so that it won't incorrectly merge span groups633return '\n<span dir=rtl>' + html + '</span>';634};635636637/**638* Enforce RTL on both end of the given text piece using unicode BiDi formatting639* characters RLE and PDF.640* @param {string} text The piece of text that need to be wrapped.641* @return {string} The wrapped string after process.642*/643goog.i18n.bidi.enforceRtlInText = function(text) {644'use strict';645return goog.i18n.bidi.Format.RLE + text + goog.i18n.bidi.Format.PDF;646};647648649/**650* Enforce the html snippet in RTL directionality regardless or overall context.651* If the html piece was enclosed by tag, dir will be applied to existing652* tag, otherwise a span tag will be added as wrapper. For this reason, if653* html snippet starts with a tag, this tag must enclose the whole piece. If654* the tag already has a dir specified, this new one will override existing655* one in behavior (tested on FF and IE).656* @param {string} html The string that need to be processed.657* @return {string} The processed string, with directionality enforced to RTL.658*/659goog.i18n.bidi.enforceLtrInHtml = function(html) {660'use strict';661if (html.charAt(0) == '<') {662return html.replace(/<\w+/, '$& dir=ltr');663}664// '\n' is important for FF so that it won't incorrectly merge span groups665return '\n<span dir=ltr>' + html + '</span>';666};667668669/**670* Enforce LTR on both end of the given text piece using unicode BiDi formatting671* characters LRE and PDF.672* @param {string} text The piece of text that need to be wrapped.673* @return {string} The wrapped string after process.674*/675goog.i18n.bidi.enforceLtrInText = function(text) {676'use strict';677return goog.i18n.bidi.Format.LRE + text + goog.i18n.bidi.Format.PDF;678};679680681/**682* Regular expression to find dimensions such as "padding: .3 0.4ex 5px 6;"683* @type {RegExp}684* @private685*/686goog.i18n.bidi.dimensionsRe_ =687/:\s*([.\d][.\w]*)\s+([.\d][.\w]*)\s+([.\d][.\w]*)\s+([.\d][.\w]*)/g;688689690/**691* Regular expression for left.692* @type {RegExp}693* @private694*/695goog.i18n.bidi.leftRe_ = /left/gi;696697698/**699* Regular expression for right.700* @type {RegExp}701* @private702*/703goog.i18n.bidi.rightRe_ = /right/gi;704705706/**707* Placeholder regular expression for swapping.708* @type {RegExp}709* @private710*/711goog.i18n.bidi.tempRe_ = /%%%%/g;712713714/**715* Swap location parameters and 'left'/'right' in CSS specification. The716* processed string will be suited for RTL layout. Though this function can717* cover most cases, there are always exceptions. It is suggested to put718* those exceptions in separate group of CSS string.719* @param {string} cssStr CSS spefication string.720* @return {string} Processed CSS specification string.721*/722goog.i18n.bidi.mirrorCSS = function(cssStr) {723'use strict';724return cssStr725.726// reverse dimensions727replace(goog.i18n.bidi.dimensionsRe_, ':$1 $4 $3 $2')728.replace(goog.i18n.bidi.leftRe_, '%%%%')729. // swap left and right730replace(goog.i18n.bidi.rightRe_, goog.i18n.bidi.LEFT)731.replace(goog.i18n.bidi.tempRe_, goog.i18n.bidi.RIGHT);732};733734735/**736* Regular expression for hebrew double quote substitution, finding quote737* directly after hebrew characters.738* @type {RegExp}739* @private740*/741goog.i18n.bidi.doubleQuoteSubstituteRe_ = /([\u0591-\u05f2])"/g;742743744/**745* Regular expression for hebrew single quote substitution, finding quote746* directly after hebrew characters.747* @type {RegExp}748* @private749*/750goog.i18n.bidi.singleQuoteSubstituteRe_ = /([\u0591-\u05f2])'/g;751752753/**754* Replace the double and single quote directly after a Hebrew character with755* GERESH and GERSHAYIM. In such case, most likely that's user intention.756* @param {string} str String that need to be processed.757* @return {string} Processed string with double/single quote replaced.758*/759goog.i18n.bidi.normalizeHebrewQuote = function(str) {760'use strict';761return str.replace(goog.i18n.bidi.doubleQuoteSubstituteRe_, '$1\u05f4')762.replace(goog.i18n.bidi.singleQuoteSubstituteRe_, '$1\u05f3');763};764765766/**767* Regular expression to split a string into "words" for directionality768* estimation based on relative word counts.769* @type {RegExp}770* @private771*/772goog.i18n.bidi.wordSeparatorRe_ = /\s+/;773774775/**776* Regular expression to check if a string contains any numerals. Used to777* differentiate between completely neutral strings and those containing778* numbers, which are weakly LTR.779*780* Native Arabic digits (\u0660 - \u0669) are not included because although they781* do flow left-to-right inside a number, this is the case even if the overall782* directionality is RTL, and a mathematical expression using these digits is783* supposed to flow right-to-left overall, including unary plus and minus784* appearing to the right of a number, and this does depend on the overall785* directionality being RTL. The digits used in Farsi (\u06F0 - \u06F9), on the786* other hand, are included, since Farsi math (including unary plus and minus)787* does flow left-to-right.788* TODO: Consider other systems of digits, e.g., Adlam.789*790* @type {RegExp}791* @private792*/793goog.i18n.bidi.hasNumeralsRe_ = /[\d\u06f0-\u06f9]/;794795796/**797* This constant controls threshold of RTL directionality.798* @type {number}799* @private800*/801goog.i18n.bidi.rtlDetectionThreshold_ = 0.40;802803804/**805* Estimates the directionality of a string based on relative word counts.806* If the number of RTL words is above a certain percentage of the total number807* of strongly directional words, returns RTL.808* Otherwise, if any words are strongly or weakly LTR, returns LTR.809* Otherwise, returns UNKNOWN, which is used to mean "neutral".810* Numbers are counted as weakly LTR.811* @param {string} str The string to be checked.812* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.813* Default: false.814* @return {goog.i18n.bidi.Dir} Estimated overall directionality of `str`.815*/816goog.i18n.bidi.estimateDirection = function(str, opt_isHtml) {817'use strict';818let rtlCount = 0;819let totalCount = 0;820let hasWeaklyLtr = false;821const tokens = goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml)822.split(goog.i18n.bidi.wordSeparatorRe_);823for (let i = 0; i < tokens.length; i++) {824const token = tokens[i];825if (goog.i18n.bidi.startsWithRtl(token)) {826rtlCount++;827totalCount++;828} else if (goog.i18n.bidi.isRequiredLtrRe_.test(token)) {829hasWeaklyLtr = true;830} else if (goog.i18n.bidi.hasAnyLtr(token)) {831totalCount++;832} else if (goog.i18n.bidi.hasNumeralsRe_.test(token)) {833hasWeaklyLtr = true;834}835}836837return totalCount == 0 ?838(hasWeaklyLtr ? goog.i18n.bidi.Dir.LTR : goog.i18n.bidi.Dir.NEUTRAL) :839(rtlCount / totalCount > goog.i18n.bidi.rtlDetectionThreshold_ ?840goog.i18n.bidi.Dir.RTL :841goog.i18n.bidi.Dir.LTR);842};843844845/**846* Check the directionality of a piece of text, return true if the piece of847* text should be laid out in RTL direction.848* @param {string} str The piece of text that need to be detected.849* @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.850* Default: false.851* @return {boolean} Whether this piece of text should be laid out in RTL.852*/853goog.i18n.bidi.detectRtlDirectionality = function(str, opt_isHtml) {854'use strict';855return goog.i18n.bidi.estimateDirection(str, opt_isHtml) ==856goog.i18n.bidi.Dir.RTL;857};858859860/**861* Sets text input element's directionality and text alignment based on a862* given directionality. Does nothing if the given directionality is unknown or863* neutral.864* @param {Element} element Input field element to set directionality to.865* @param {goog.i18n.bidi.Dir|number|boolean|null} dir Desired directionality,866* given in one of the following formats:867* 1. A goog.i18n.bidi.Dir constant.868* 2. A number (positive = LRT, negative = RTL, 0 = neutral).869* 3. A boolean (true = RTL, false = LTR).870* 4. A null for unknown directionality.871* @return {void}872*/873goog.i18n.bidi.setElementDirAndAlign = function(element, dir) {874'use strict';875if (element) {876const htmlElement = /** @type {!HTMLElement} */ (element);877dir = goog.i18n.bidi.toDir(dir);878if (dir) {879htmlElement.style.textAlign = dir == goog.i18n.bidi.Dir.RTL ?880goog.i18n.bidi.RIGHT :881goog.i18n.bidi.LEFT;882htmlElement.dir = dir == goog.i18n.bidi.Dir.RTL ? 'rtl' : 'ltr';883}884}885};886887888/**889* Sets element dir based on estimated directionality of the given text.890* @param {!Element} element891* @param {string} text892* @return {void}893*/894goog.i18n.bidi.setElementDirByTextDirectionality = function(element, text) {895'use strict';896const htmlElement = /** @type {!HTMLElement} */ (element);897switch (goog.i18n.bidi.estimateDirection(text)) {898case (goog.i18n.bidi.Dir.LTR):899if (htmlElement.dir !== 'ltr') {900htmlElement.dir = 'ltr';901}902break;903case (goog.i18n.bidi.Dir.RTL):904if (htmlElement.dir !== 'rtl') {905htmlElement.dir = 'rtl';906}907break;908default:909// Default for no direction, inherit from document.910htmlElement.removeAttribute('dir');911}912};913914915916/**917* Strings that have an (optional) known direction.918*919* Implementations of this interface are string-like objects that carry an920* attached direction, if known.921* @interface922*/923goog.i18n.bidi.DirectionalString = function() {};924925926/**927* Interface marker of the DirectionalString interface.928*929* This property can be used to determine at runtime whether or not an object930* implements this interface. All implementations of this interface set this931* property to `true`.932* @type {boolean}933*/934goog.i18n.bidi.DirectionalString.prototype935.implementsGoogI18nBidiDirectionalString;936937938/**939* Retrieves this object's known direction (if any).940* @return {?goog.i18n.bidi.Dir} The known direction. Null if unknown.941*/942goog.i18n.bidi.DirectionalString.prototype.getDirection;943944945