bidi.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Utility functions for supporting Bidi issues.
  16. */
  17. /**
  18. * Namespace for bidi supporting functions.
  19. */
  20. goog.provide('goog.i18n.bidi');
  21. goog.provide('goog.i18n.bidi.Dir');
  22. goog.provide('goog.i18n.bidi.DirectionalString');
  23. goog.provide('goog.i18n.bidi.Format');
  24. /**
  25. * @define {boolean} FORCE_RTL forces the {@link goog.i18n.bidi.IS_RTL} constant
  26. * to say that the current locale is a RTL locale. This should only be used
  27. * if you want to override the default behavior for deciding whether the
  28. * current locale is RTL or not.
  29. *
  30. * {@see goog.i18n.bidi.IS_RTL}
  31. */
  32. goog.define('goog.i18n.bidi.FORCE_RTL', false);
  33. /**
  34. * Constant that defines whether or not the current locale is a RTL locale.
  35. * If {@link goog.i18n.bidi.FORCE_RTL} is not true, this constant will default
  36. * to check that {@link goog.LOCALE} is one of a few major RTL locales.
  37. *
  38. * <p>This is designed to be a maximally efficient compile-time constant. For
  39. * example, for the default goog.LOCALE, compiling
  40. * "if (goog.i18n.bidi.IS_RTL) alert('rtl') else {}" should produce no code. It
  41. * is this design consideration that limits the implementation to only
  42. * supporting a few major RTL locales, as opposed to the broader repertoire of
  43. * something like goog.i18n.bidi.isRtlLanguage.
  44. *
  45. * <p>Since this constant refers to the directionality of the locale, it is up
  46. * to the caller to determine if this constant should also be used for the
  47. * direction of the UI.
  48. *
  49. * {@see goog.LOCALE}
  50. *
  51. * @type {boolean}
  52. *
  53. * TODO(user): write a test that checks that this is a compile-time constant.
  54. */
  55. goog.i18n.bidi.IS_RTL = goog.i18n.bidi.FORCE_RTL ||
  56. ((goog.LOCALE.substring(0, 2).toLowerCase() == 'ar' ||
  57. goog.LOCALE.substring(0, 2).toLowerCase() == 'fa' ||
  58. goog.LOCALE.substring(0, 2).toLowerCase() == 'he' ||
  59. goog.LOCALE.substring(0, 2).toLowerCase() == 'iw' ||
  60. goog.LOCALE.substring(0, 2).toLowerCase() == 'ps' ||
  61. goog.LOCALE.substring(0, 2).toLowerCase() == 'sd' ||
  62. goog.LOCALE.substring(0, 2).toLowerCase() == 'ug' ||
  63. goog.LOCALE.substring(0, 2).toLowerCase() == 'ur' ||
  64. goog.LOCALE.substring(0, 2).toLowerCase() == 'yi') &&
  65. (goog.LOCALE.length == 2 || goog.LOCALE.substring(2, 3) == '-' ||
  66. goog.LOCALE.substring(2, 3) == '_')) ||
  67. (goog.LOCALE.length >= 3 &&
  68. goog.LOCALE.substring(0, 3).toLowerCase() == 'ckb' &&
  69. (goog.LOCALE.length == 3 || goog.LOCALE.substring(3, 4) == '-' ||
  70. goog.LOCALE.substring(3, 4) == '_'));
  71. /**
  72. * Unicode formatting characters and directionality string constants.
  73. * @enum {string}
  74. */
  75. goog.i18n.bidi.Format = {
  76. /** Unicode "Left-To-Right Embedding" (LRE) character. */
  77. LRE: '\u202A',
  78. /** Unicode "Right-To-Left Embedding" (RLE) character. */
  79. RLE: '\u202B',
  80. /** Unicode "Pop Directional Formatting" (PDF) character. */
  81. PDF: '\u202C',
  82. /** Unicode "Left-To-Right Mark" (LRM) character. */
  83. LRM: '\u200E',
  84. /** Unicode "Right-To-Left Mark" (RLM) character. */
  85. RLM: '\u200F'
  86. };
  87. /**
  88. * Directionality enum.
  89. * @enum {number}
  90. */
  91. goog.i18n.bidi.Dir = {
  92. /**
  93. * Left-to-right.
  94. */
  95. LTR: 1,
  96. /**
  97. * Right-to-left.
  98. */
  99. RTL: -1,
  100. /**
  101. * Neither left-to-right nor right-to-left.
  102. */
  103. NEUTRAL: 0
  104. };
  105. /**
  106. * 'right' string constant.
  107. * @type {string}
  108. */
  109. goog.i18n.bidi.RIGHT = 'right';
  110. /**
  111. * 'left' string constant.
  112. * @type {string}
  113. */
  114. goog.i18n.bidi.LEFT = 'left';
  115. /**
  116. * 'left' if locale is RTL, 'right' if not.
  117. * @type {string}
  118. */
  119. goog.i18n.bidi.I18N_RIGHT =
  120. goog.i18n.bidi.IS_RTL ? goog.i18n.bidi.LEFT : goog.i18n.bidi.RIGHT;
  121. /**
  122. * 'right' if locale is RTL, 'left' if not.
  123. * @type {string}
  124. */
  125. goog.i18n.bidi.I18N_LEFT =
  126. goog.i18n.bidi.IS_RTL ? goog.i18n.bidi.RIGHT : goog.i18n.bidi.LEFT;
  127. /**
  128. * Convert a directionality given in various formats to a goog.i18n.bidi.Dir
  129. * constant. Useful for interaction with different standards of directionality
  130. * representation.
  131. *
  132. * @param {goog.i18n.bidi.Dir|number|boolean|null} givenDir Directionality given
  133. * in one of the following formats:
  134. * 1. A goog.i18n.bidi.Dir constant.
  135. * 2. A number (positive = LTR, negative = RTL, 0 = neutral).
  136. * 3. A boolean (true = RTL, false = LTR).
  137. * 4. A null for unknown directionality.
  138. * @param {boolean=} opt_noNeutral Whether a givenDir of zero or
  139. * goog.i18n.bidi.Dir.NEUTRAL should be treated as null, i.e. unknown, in
  140. * order to preserve legacy behavior.
  141. * @return {?goog.i18n.bidi.Dir} A goog.i18n.bidi.Dir constant matching the
  142. * given directionality. If given null, returns null (i.e. unknown).
  143. */
  144. goog.i18n.bidi.toDir = function(givenDir, opt_noNeutral) {
  145. if (typeof givenDir == 'number') {
  146. // This includes the non-null goog.i18n.bidi.Dir case.
  147. return givenDir > 0 ? goog.i18n.bidi.Dir.LTR : givenDir < 0 ?
  148. goog.i18n.bidi.Dir.RTL :
  149. opt_noNeutral ? null : goog.i18n.bidi.Dir.NEUTRAL;
  150. } else if (givenDir == null) {
  151. return null;
  152. } else {
  153. // Must be typeof givenDir == 'boolean'.
  154. return givenDir ? goog.i18n.bidi.Dir.RTL : goog.i18n.bidi.Dir.LTR;
  155. }
  156. };
  157. /**
  158. * A practical pattern to identify strong LTR characters. This pattern is not
  159. * theoretically correct according to the Unicode standard. It is simplified for
  160. * performance and small code size.
  161. * @type {string}
  162. * @private
  163. */
  164. goog.i18n.bidi.ltrChars_ =
  165. 'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF' +
  166. '\u200E\u2C00-\uFB1C\uFE00-\uFE6F\uFEFD-\uFFFF';
  167. /**
  168. * A practical pattern to identify strong RTL character. This pattern is not
  169. * theoretically correct according to the Unicode standard. It is simplified
  170. * for performance and small code size.
  171. * @type {string}
  172. * @private
  173. */
  174. goog.i18n.bidi.rtlChars_ =
  175. '\u0591-\u06EF\u06FA-\u07FF\u200F\uFB1D-\uFDFF\uFE70-\uFEFC';
  176. /**
  177. * Simplified regular expression for an HTML tag (opening or closing) or an HTML
  178. * escape. We might want to skip over such expressions when estimating the text
  179. * directionality.
  180. * @type {RegExp}
  181. * @private
  182. */
  183. goog.i18n.bidi.htmlSkipReg_ = /<[^>]*>|&[^;]+;/g;
  184. /**
  185. * Returns the input text with spaces instead of HTML tags or HTML escapes, if
  186. * opt_isStripNeeded is true. Else returns the input as is.
  187. * Useful for text directionality estimation.
  188. * Note: the function should not be used in other contexts; it is not 100%
  189. * correct, but rather a good-enough implementation for directionality
  190. * estimation purposes.
  191. * @param {string} str The given string.
  192. * @param {boolean=} opt_isStripNeeded Whether to perform the stripping.
  193. * Default: false (to retain consistency with calling functions).
  194. * @return {string} The given string cleaned of HTML tags / escapes.
  195. * @private
  196. */
  197. goog.i18n.bidi.stripHtmlIfNeeded_ = function(str, opt_isStripNeeded) {
  198. return opt_isStripNeeded ? str.replace(goog.i18n.bidi.htmlSkipReg_, '') : str;
  199. };
  200. /**
  201. * Regular expression to check for RTL characters.
  202. * @type {RegExp}
  203. * @private
  204. */
  205. goog.i18n.bidi.rtlCharReg_ = new RegExp('[' + goog.i18n.bidi.rtlChars_ + ']');
  206. /**
  207. * Regular expression to check for LTR characters.
  208. * @type {RegExp}
  209. * @private
  210. */
  211. goog.i18n.bidi.ltrCharReg_ = new RegExp('[' + goog.i18n.bidi.ltrChars_ + ']');
  212. /**
  213. * Test whether the given string has any RTL characters in it.
  214. * @param {string} str The given string that need to be tested.
  215. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  216. * Default: false.
  217. * @return {boolean} Whether the string contains RTL characters.
  218. */
  219. goog.i18n.bidi.hasAnyRtl = function(str, opt_isHtml) {
  220. return goog.i18n.bidi.rtlCharReg_.test(
  221. goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));
  222. };
  223. /**
  224. * Test whether the given string has any RTL characters in it.
  225. * @param {string} str The given string that need to be tested.
  226. * @return {boolean} Whether the string contains RTL characters.
  227. * @deprecated Use hasAnyRtl.
  228. */
  229. goog.i18n.bidi.hasRtlChar = goog.i18n.bidi.hasAnyRtl;
  230. /**
  231. * Test whether the given string has any LTR characters in it.
  232. * @param {string} str The given string that need to be tested.
  233. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  234. * Default: false.
  235. * @return {boolean} Whether the string contains LTR characters.
  236. */
  237. goog.i18n.bidi.hasAnyLtr = function(str, opt_isHtml) {
  238. return goog.i18n.bidi.ltrCharReg_.test(
  239. goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));
  240. };
  241. /**
  242. * Regular expression pattern to check if the first character in the string
  243. * is LTR.
  244. * @type {RegExp}
  245. * @private
  246. */
  247. goog.i18n.bidi.ltrRe_ = new RegExp('^[' + goog.i18n.bidi.ltrChars_ + ']');
  248. /**
  249. * Regular expression pattern to check if the first character in the string
  250. * is RTL.
  251. * @type {RegExp}
  252. * @private
  253. */
  254. goog.i18n.bidi.rtlRe_ = new RegExp('^[' + goog.i18n.bidi.rtlChars_ + ']');
  255. /**
  256. * Check if the first character in the string is RTL or not.
  257. * @param {string} str The given string that need to be tested.
  258. * @return {boolean} Whether the first character in str is an RTL char.
  259. */
  260. goog.i18n.bidi.isRtlChar = function(str) {
  261. return goog.i18n.bidi.rtlRe_.test(str);
  262. };
  263. /**
  264. * Check if the first character in the string is LTR or not.
  265. * @param {string} str The given string that need to be tested.
  266. * @return {boolean} Whether the first character in str is an LTR char.
  267. */
  268. goog.i18n.bidi.isLtrChar = function(str) {
  269. return goog.i18n.bidi.ltrRe_.test(str);
  270. };
  271. /**
  272. * Check if the first character in the string is neutral or not.
  273. * @param {string} str The given string that need to be tested.
  274. * @return {boolean} Whether the first character in str is a neutral char.
  275. */
  276. goog.i18n.bidi.isNeutralChar = function(str) {
  277. return !goog.i18n.bidi.isLtrChar(str) && !goog.i18n.bidi.isRtlChar(str);
  278. };
  279. /**
  280. * Regular expressions to check if a piece of text is of LTR directionality
  281. * on first character with strong directionality.
  282. * @type {RegExp}
  283. * @private
  284. */
  285. goog.i18n.bidi.ltrDirCheckRe_ = new RegExp(
  286. '^[^' + goog.i18n.bidi.rtlChars_ + ']*[' + goog.i18n.bidi.ltrChars_ + ']');
  287. /**
  288. * Regular expressions to check if a piece of text is of RTL directionality
  289. * on first character with strong directionality.
  290. * @type {RegExp}
  291. * @private
  292. */
  293. goog.i18n.bidi.rtlDirCheckRe_ = new RegExp(
  294. '^[^' + goog.i18n.bidi.ltrChars_ + ']*[' + goog.i18n.bidi.rtlChars_ + ']');
  295. /**
  296. * Check whether the first strongly directional character (if any) is RTL.
  297. * @param {string} str String being checked.
  298. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  299. * Default: false.
  300. * @return {boolean} Whether RTL directionality is detected using the first
  301. * strongly-directional character method.
  302. */
  303. goog.i18n.bidi.startsWithRtl = function(str, opt_isHtml) {
  304. return goog.i18n.bidi.rtlDirCheckRe_.test(
  305. goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));
  306. };
  307. /**
  308. * Check whether the first strongly directional character (if any) is RTL.
  309. * @param {string} str String being checked.
  310. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  311. * Default: false.
  312. * @return {boolean} Whether RTL directionality is detected using the first
  313. * strongly-directional character method.
  314. * @deprecated Use startsWithRtl.
  315. */
  316. goog.i18n.bidi.isRtlText = goog.i18n.bidi.startsWithRtl;
  317. /**
  318. * Check whether the first strongly directional character (if any) is LTR.
  319. * @param {string} str String being checked.
  320. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  321. * Default: false.
  322. * @return {boolean} Whether LTR directionality is detected using the first
  323. * strongly-directional character method.
  324. */
  325. goog.i18n.bidi.startsWithLtr = function(str, opt_isHtml) {
  326. return goog.i18n.bidi.ltrDirCheckRe_.test(
  327. goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));
  328. };
  329. /**
  330. * Check whether the first strongly directional character (if any) is LTR.
  331. * @param {string} str String being checked.
  332. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  333. * Default: false.
  334. * @return {boolean} Whether LTR directionality is detected using the first
  335. * strongly-directional character method.
  336. * @deprecated Use startsWithLtr.
  337. */
  338. goog.i18n.bidi.isLtrText = goog.i18n.bidi.startsWithLtr;
  339. /**
  340. * Regular expression to check if a string looks like something that must
  341. * always be LTR even in RTL text, e.g. a URL. When estimating the
  342. * directionality of text containing these, we treat these as weakly LTR,
  343. * like numbers.
  344. * @type {RegExp}
  345. * @private
  346. */
  347. goog.i18n.bidi.isRequiredLtrRe_ = /^http:\/\/.*/;
  348. /**
  349. * Check whether the input string either contains no strongly directional
  350. * characters or looks like a url.
  351. * @param {string} str String being checked.
  352. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  353. * Default: false.
  354. * @return {boolean} Whether neutral directionality is detected.
  355. */
  356. goog.i18n.bidi.isNeutralText = function(str, opt_isHtml) {
  357. str = goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml);
  358. return goog.i18n.bidi.isRequiredLtrRe_.test(str) ||
  359. !goog.i18n.bidi.hasAnyLtr(str) && !goog.i18n.bidi.hasAnyRtl(str);
  360. };
  361. /**
  362. * Regular expressions to check if the last strongly-directional character in a
  363. * piece of text is LTR.
  364. * @type {RegExp}
  365. * @private
  366. */
  367. goog.i18n.bidi.ltrExitDirCheckRe_ = new RegExp(
  368. '[' + goog.i18n.bidi.ltrChars_ + '][^' + goog.i18n.bidi.rtlChars_ + ']*$');
  369. /**
  370. * Regular expressions to check if the last strongly-directional character in a
  371. * piece of text is RTL.
  372. * @type {RegExp}
  373. * @private
  374. */
  375. goog.i18n.bidi.rtlExitDirCheckRe_ = new RegExp(
  376. '[' + goog.i18n.bidi.rtlChars_ + '][^' + goog.i18n.bidi.ltrChars_ + ']*$');
  377. /**
  378. * Check if the exit directionality a piece of text is LTR, i.e. if the last
  379. * strongly-directional character in the string is LTR.
  380. * @param {string} str String being checked.
  381. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  382. * Default: false.
  383. * @return {boolean} Whether LTR exit directionality was detected.
  384. */
  385. goog.i18n.bidi.endsWithLtr = function(str, opt_isHtml) {
  386. return goog.i18n.bidi.ltrExitDirCheckRe_.test(
  387. goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));
  388. };
  389. /**
  390. * Check if the exit directionality a piece of text is LTR, i.e. if the last
  391. * strongly-directional character in the string is LTR.
  392. * @param {string} str String being checked.
  393. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  394. * Default: false.
  395. * @return {boolean} Whether LTR exit directionality was detected.
  396. * @deprecated Use endsWithLtr.
  397. */
  398. goog.i18n.bidi.isLtrExitText = goog.i18n.bidi.endsWithLtr;
  399. /**
  400. * Check if the exit directionality a piece of text is RTL, i.e. if the last
  401. * strongly-directional character in the string is RTL.
  402. * @param {string} str String being checked.
  403. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  404. * Default: false.
  405. * @return {boolean} Whether RTL exit directionality was detected.
  406. */
  407. goog.i18n.bidi.endsWithRtl = function(str, opt_isHtml) {
  408. return goog.i18n.bidi.rtlExitDirCheckRe_.test(
  409. goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml));
  410. };
  411. /**
  412. * Check if the exit directionality a piece of text is RTL, i.e. if the last
  413. * strongly-directional character in the string is RTL.
  414. * @param {string} str String being checked.
  415. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  416. * Default: false.
  417. * @return {boolean} Whether RTL exit directionality was detected.
  418. * @deprecated Use endsWithRtl.
  419. */
  420. goog.i18n.bidi.isRtlExitText = goog.i18n.bidi.endsWithRtl;
  421. /**
  422. * A regular expression for matching right-to-left language codes.
  423. * See {@link #isRtlLanguage} for the design.
  424. * @type {RegExp}
  425. * @private
  426. */
  427. goog.i18n.bidi.rtlLocalesRe_ = new RegExp(
  428. '^(ar|ckb|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|' +
  429. '.*[-_](Arab|Hebr|Thaa|Nkoo|Tfng))' +
  430. '(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)',
  431. 'i');
  432. /**
  433. * Check if a BCP 47 / III language code indicates an RTL language, i.e. either:
  434. * - a language code explicitly specifying one of the right-to-left scripts,
  435. * e.g. "az-Arab", or<p>
  436. * - a language code specifying one of the languages normally written in a
  437. * right-to-left script, e.g. "fa" (Farsi), except ones explicitly specifying
  438. * Latin or Cyrillic script (which are the usual LTR alternatives).<p>
  439. * The list of right-to-left scripts appears in the 100-199 range in
  440. * http://www.unicode.org/iso15924/iso15924-num.html, of which Arabic and
  441. * Hebrew are by far the most widely used. We also recognize Thaana, N'Ko, and
  442. * Tifinagh, which also have significant modern usage. The rest (Syriac,
  443. * Samaritan, Mandaic, etc.) seem to have extremely limited or no modern usage
  444. * and are not recognized to save on code size.
  445. * The languages usually written in a right-to-left script are taken as those
  446. * with Suppress-Script: Hebr|Arab|Thaa|Nkoo|Tfng in
  447. * http://www.iana.org/assignments/language-subtag-registry,
  448. * as well as Central (or Sorani) Kurdish (ckb), Sindhi (sd) and Uyghur (ug).
  449. * Other subtags of the language code, e.g. regions like EG (Egypt), are
  450. * ignored.
  451. * @param {string} lang BCP 47 (a.k.a III) language code.
  452. * @return {boolean} Whether the language code is an RTL language.
  453. */
  454. goog.i18n.bidi.isRtlLanguage = function(lang) {
  455. return goog.i18n.bidi.rtlLocalesRe_.test(lang);
  456. };
  457. /**
  458. * Regular expression for bracket guard replacement in text.
  459. * @type {RegExp}
  460. * @private
  461. */
  462. goog.i18n.bidi.bracketGuardTextRe_ =
  463. /(\(.*?\)+)|(\[.*?\]+)|(\{.*?\}+)|(<.*?>+)/g;
  464. /**
  465. * Apply bracket guard using LRM and RLM. This is to address the problem of
  466. * messy bracket display frequently happens in RTL layout.
  467. * This function works for plain text, not for HTML. In HTML, the opening
  468. * bracket might be in a different context than the closing bracket (such as
  469. * an attribute value).
  470. * @param {string} s The string that need to be processed.
  471. * @param {boolean=} opt_isRtlContext specifies default direction (usually
  472. * direction of the UI).
  473. * @return {string} The processed string, with all bracket guarded.
  474. */
  475. goog.i18n.bidi.guardBracketInText = function(s, opt_isRtlContext) {
  476. var useRtl = opt_isRtlContext === undefined ? goog.i18n.bidi.hasAnyRtl(s) :
  477. opt_isRtlContext;
  478. var mark = useRtl ? goog.i18n.bidi.Format.RLM : goog.i18n.bidi.Format.LRM;
  479. return s.replace(goog.i18n.bidi.bracketGuardTextRe_, mark + '$&' + mark);
  480. };
  481. /**
  482. * Enforce the html snippet in RTL directionality regardless overall context.
  483. * If the html piece was enclosed by tag, dir will be applied to existing
  484. * tag, otherwise a span tag will be added as wrapper. For this reason, if
  485. * html snippet start with with tag, this tag must enclose the whole piece. If
  486. * the tag already has a dir specified, this new one will override existing
  487. * one in behavior (tested on FF and IE).
  488. * @param {string} html The string that need to be processed.
  489. * @return {string} The processed string, with directionality enforced to RTL.
  490. */
  491. goog.i18n.bidi.enforceRtlInHtml = function(html) {
  492. if (html.charAt(0) == '<') {
  493. return html.replace(/<\w+/, '$& dir=rtl');
  494. }
  495. // '\n' is important for FF so that it won't incorrectly merge span groups
  496. return '\n<span dir=rtl>' + html + '</span>';
  497. };
  498. /**
  499. * Enforce RTL on both end of the given text piece using unicode BiDi formatting
  500. * characters RLE and PDF.
  501. * @param {string} text The piece of text that need to be wrapped.
  502. * @return {string} The wrapped string after process.
  503. */
  504. goog.i18n.bidi.enforceRtlInText = function(text) {
  505. return goog.i18n.bidi.Format.RLE + text + goog.i18n.bidi.Format.PDF;
  506. };
  507. /**
  508. * Enforce the html snippet in RTL directionality regardless overall context.
  509. * If the html piece was enclosed by tag, dir will be applied to existing
  510. * tag, otherwise a span tag will be added as wrapper. For this reason, if
  511. * html snippet start with with tag, this tag must enclose the whole piece. If
  512. * the tag already has a dir specified, this new one will override existing
  513. * one in behavior (tested on FF and IE).
  514. * @param {string} html The string that need to be processed.
  515. * @return {string} The processed string, with directionality enforced to RTL.
  516. */
  517. goog.i18n.bidi.enforceLtrInHtml = function(html) {
  518. if (html.charAt(0) == '<') {
  519. return html.replace(/<\w+/, '$& dir=ltr');
  520. }
  521. // '\n' is important for FF so that it won't incorrectly merge span groups
  522. return '\n<span dir=ltr>' + html + '</span>';
  523. };
  524. /**
  525. * Enforce LTR on both end of the given text piece using unicode BiDi formatting
  526. * characters LRE and PDF.
  527. * @param {string} text The piece of text that need to be wrapped.
  528. * @return {string} The wrapped string after process.
  529. */
  530. goog.i18n.bidi.enforceLtrInText = function(text) {
  531. return goog.i18n.bidi.Format.LRE + text + goog.i18n.bidi.Format.PDF;
  532. };
  533. /**
  534. * Regular expression to find dimensions such as "padding: .3 0.4ex 5px 6;"
  535. * @type {RegExp}
  536. * @private
  537. */
  538. goog.i18n.bidi.dimensionsRe_ =
  539. /:\s*([.\d][.\w]*)\s+([.\d][.\w]*)\s+([.\d][.\w]*)\s+([.\d][.\w]*)/g;
  540. /**
  541. * Regular expression for left.
  542. * @type {RegExp}
  543. * @private
  544. */
  545. goog.i18n.bidi.leftRe_ = /left/gi;
  546. /**
  547. * Regular expression for right.
  548. * @type {RegExp}
  549. * @private
  550. */
  551. goog.i18n.bidi.rightRe_ = /right/gi;
  552. /**
  553. * Placeholder regular expression for swapping.
  554. * @type {RegExp}
  555. * @private
  556. */
  557. goog.i18n.bidi.tempRe_ = /%%%%/g;
  558. /**
  559. * Swap location parameters and 'left'/'right' in CSS specification. The
  560. * processed string will be suited for RTL layout. Though this function can
  561. * cover most cases, there are always exceptions. It is suggested to put
  562. * those exceptions in separate group of CSS string.
  563. * @param {string} cssStr CSS spefication string.
  564. * @return {string} Processed CSS specification string.
  565. */
  566. goog.i18n.bidi.mirrorCSS = function(cssStr) {
  567. return cssStr
  568. .
  569. // reverse dimensions
  570. replace(goog.i18n.bidi.dimensionsRe_, ':$1 $4 $3 $2')
  571. .replace(goog.i18n.bidi.leftRe_, '%%%%')
  572. . // swap left and right
  573. replace(goog.i18n.bidi.rightRe_, goog.i18n.bidi.LEFT)
  574. .replace(goog.i18n.bidi.tempRe_, goog.i18n.bidi.RIGHT);
  575. };
  576. /**
  577. * Regular expression for hebrew double quote substitution, finding quote
  578. * directly after hebrew characters.
  579. * @type {RegExp}
  580. * @private
  581. */
  582. goog.i18n.bidi.doubleQuoteSubstituteRe_ = /([\u0591-\u05f2])"/g;
  583. /**
  584. * Regular expression for hebrew single quote substitution, finding quote
  585. * directly after hebrew characters.
  586. * @type {RegExp}
  587. * @private
  588. */
  589. goog.i18n.bidi.singleQuoteSubstituteRe_ = /([\u0591-\u05f2])'/g;
  590. /**
  591. * Replace the double and single quote directly after a Hebrew character with
  592. * GERESH and GERSHAYIM. In such case, most likely that's user intention.
  593. * @param {string} str String that need to be processed.
  594. * @return {string} Processed string with double/single quote replaced.
  595. */
  596. goog.i18n.bidi.normalizeHebrewQuote = function(str) {
  597. return str.replace(goog.i18n.bidi.doubleQuoteSubstituteRe_, '$1\u05f4')
  598. .replace(goog.i18n.bidi.singleQuoteSubstituteRe_, '$1\u05f3');
  599. };
  600. /**
  601. * Regular expression to split a string into "words" for directionality
  602. * estimation based on relative word counts.
  603. * @type {RegExp}
  604. * @private
  605. */
  606. goog.i18n.bidi.wordSeparatorRe_ = /\s+/;
  607. /**
  608. * Regular expression to check if a string contains any numerals. Used to
  609. * differentiate between completely neutral strings and those containing
  610. * numbers, which are weakly LTR.
  611. *
  612. * Native Arabic digits (\u0660 - \u0669) are not included because although they
  613. * do flow left-to-right inside a number, this is the case even if the overall
  614. * directionality is RTL, and a mathematical expression using these digits is
  615. * supposed to flow right-to-left overall, including unary plus and minus
  616. * appearing to the right of a number, and this does depend on the overall
  617. * directionality being RTL. The digits used in Farsi (\u06F0 - \u06F9), on the
  618. * other hand, are included, since Farsi math (including unary plus and minus)
  619. * does flow left-to-right.
  620. *
  621. * @type {RegExp}
  622. * @private
  623. */
  624. goog.i18n.bidi.hasNumeralsRe_ = /[\d\u06f0-\u06f9]/;
  625. /**
  626. * This constant controls threshold of RTL directionality.
  627. * @type {number}
  628. * @private
  629. */
  630. goog.i18n.bidi.rtlDetectionThreshold_ = 0.40;
  631. /**
  632. * Estimates the directionality of a string based on relative word counts.
  633. * If the number of RTL words is above a certain percentage of the total number
  634. * of strongly directional words, returns RTL.
  635. * Otherwise, if any words are strongly or weakly LTR, returns LTR.
  636. * Otherwise, returns UNKNOWN, which is used to mean "neutral".
  637. * Numbers are counted as weakly LTR.
  638. * @param {string} str The string to be checked.
  639. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  640. * Default: false.
  641. * @return {goog.i18n.bidi.Dir} Estimated overall directionality of {@code str}.
  642. */
  643. goog.i18n.bidi.estimateDirection = function(str, opt_isHtml) {
  644. var rtlCount = 0;
  645. var totalCount = 0;
  646. var hasWeaklyLtr = false;
  647. var tokens = goog.i18n.bidi.stripHtmlIfNeeded_(str, opt_isHtml)
  648. .split(goog.i18n.bidi.wordSeparatorRe_);
  649. for (var i = 0; i < tokens.length; i++) {
  650. var token = tokens[i];
  651. if (goog.i18n.bidi.startsWithRtl(token)) {
  652. rtlCount++;
  653. totalCount++;
  654. } else if (goog.i18n.bidi.isRequiredLtrRe_.test(token)) {
  655. hasWeaklyLtr = true;
  656. } else if (goog.i18n.bidi.hasAnyLtr(token)) {
  657. totalCount++;
  658. } else if (goog.i18n.bidi.hasNumeralsRe_.test(token)) {
  659. hasWeaklyLtr = true;
  660. }
  661. }
  662. return totalCount == 0 ?
  663. (hasWeaklyLtr ? goog.i18n.bidi.Dir.LTR : goog.i18n.bidi.Dir.NEUTRAL) :
  664. (rtlCount / totalCount > goog.i18n.bidi.rtlDetectionThreshold_ ?
  665. goog.i18n.bidi.Dir.RTL :
  666. goog.i18n.bidi.Dir.LTR);
  667. };
  668. /**
  669. * Check the directionality of a piece of text, return true if the piece of
  670. * text should be laid out in RTL direction.
  671. * @param {string} str The piece of text that need to be detected.
  672. * @param {boolean=} opt_isHtml Whether str is HTML / HTML-escaped.
  673. * Default: false.
  674. * @return {boolean} Whether this piece of text should be laid out in RTL.
  675. */
  676. goog.i18n.bidi.detectRtlDirectionality = function(str, opt_isHtml) {
  677. return goog.i18n.bidi.estimateDirection(str, opt_isHtml) ==
  678. goog.i18n.bidi.Dir.RTL;
  679. };
  680. /**
  681. * Sets text input element's directionality and text alignment based on a
  682. * given directionality. Does nothing if the given directionality is unknown or
  683. * neutral.
  684. * @param {Element} element Input field element to set directionality to.
  685. * @param {goog.i18n.bidi.Dir|number|boolean|null} dir Desired directionality,
  686. * given in one of the following formats:
  687. * 1. A goog.i18n.bidi.Dir constant.
  688. * 2. A number (positive = LRT, negative = RTL, 0 = neutral).
  689. * 3. A boolean (true = RTL, false = LTR).
  690. * 4. A null for unknown directionality.
  691. */
  692. goog.i18n.bidi.setElementDirAndAlign = function(element, dir) {
  693. if (element) {
  694. dir = goog.i18n.bidi.toDir(dir);
  695. if (dir) {
  696. element.style.textAlign = dir == goog.i18n.bidi.Dir.RTL ?
  697. goog.i18n.bidi.RIGHT :
  698. goog.i18n.bidi.LEFT;
  699. element.dir = dir == goog.i18n.bidi.Dir.RTL ? 'rtl' : 'ltr';
  700. }
  701. }
  702. };
  703. /**
  704. * Sets element dir based on estimated directionality of the given text.
  705. * @param {!Element} element
  706. * @param {string} text
  707. */
  708. goog.i18n.bidi.setElementDirByTextDirectionality = function(element, text) {
  709. switch (goog.i18n.bidi.estimateDirection(text)) {
  710. case (goog.i18n.bidi.Dir.LTR):
  711. element.dir = 'ltr';
  712. break;
  713. case (goog.i18n.bidi.Dir.RTL):
  714. element.dir = 'rtl';
  715. break;
  716. default:
  717. // Default for no direction, inherit from document.
  718. element.removeAttribute('dir');
  719. }
  720. };
  721. /**
  722. * Strings that have an (optional) known direction.
  723. *
  724. * Implementations of this interface are string-like objects that carry an
  725. * attached direction, if known.
  726. * @interface
  727. */
  728. goog.i18n.bidi.DirectionalString = function() {};
  729. /**
  730. * Interface marker of the DirectionalString interface.
  731. *
  732. * This property can be used to determine at runtime whether or not an object
  733. * implements this interface. All implementations of this interface set this
  734. * property to {@code true}.
  735. * @type {boolean}
  736. */
  737. goog.i18n.bidi.DirectionalString.prototype
  738. .implementsGoogI18nBidiDirectionalString;
  739. /**
  740. * Retrieves this object's known direction (if any).
  741. * @return {?goog.i18n.bidi.Dir} The known direction. Null if unknown.
  742. */
  743. goog.i18n.bidi.DirectionalString.prototype.getDirection;