PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/packages/closure-library/src/main/resources/com/github/urmuzov/closuremaven/closurelibrarypackage/javascript/goog/i18n/bidi.js

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