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