Debian Codegraph @

closure-library/closure/goog/i18n/bidiformatter_test.js

raw
1 // Copyright 2009 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 goog.provide('goog.i18n.BidiFormatterTest');
16 goog.setTestOnly('goog.i18n.BidiFormatterTest');
17
18 goog.require('goog.html.SafeHtml');
19 goog.require('goog.html.testing');
20 goog.require('goog.i18n.BidiFormatter');
21 goog.require('goog.i18n.bidi.Dir');
22 goog.require('goog.i18n.bidi.Format');
23 goog.require('goog.testing.jsunit');
24
25 var LRM = goog.i18n.bidi.Format.LRM;
26 var RLM = goog.i18n.bidi.Format.RLM;
27 var LRE = goog.i18n.bidi.Format.LRE;
28 var RLE = goog.i18n.bidi.Format.RLE;
29 var PDF = goog.i18n.bidi.Format.PDF;
30 var LTR = goog.i18n.bidi.Dir.LTR;
31 var RTL = goog.i18n.bidi.Dir.RTL;
32 var NEUTRAL = goog.i18n.bidi.Dir.NEUTRAL;
33 var he = '\u05e0\u05e1';
34 var en = 'abba';
35 var html = '<';
36 var longEn = 'abba sabba gabba ';
37 var longHe = '\u05e0 \u05e1 \u05e0 ';
38 var ltrFmt = new goog.i18n.BidiFormatter(LTR, false); // LTR context
39 var rtlFmt = new goog.i18n.BidiFormatter(RTL, false); // RTL context
40 var unkFmt = new goog.i18n.BidiFormatter(null, false); // unknown context
41
42 function testGetContextDir() {
43 assertEquals(null, unkFmt.getContextDir());
44 assertEquals(null, new goog.i18n.BidiFormatter(NEUTRAL).getContextDir());
45 assertEquals(LTR, ltrFmt.getContextDir());
46 assertEquals(RTL, rtlFmt.getContextDir());
47 }
48
49 function testEstimateDirection() {
50 assertEquals(NEUTRAL, ltrFmt.estimateDirection(''));
51 assertEquals(NEUTRAL, rtlFmt.estimateDirection(''));
52 assertEquals(NEUTRAL, unkFmt.estimateDirection(''));
53 assertEquals(LTR, ltrFmt.estimateDirection(en));
54 assertEquals(LTR, rtlFmt.estimateDirection(en));
55 assertEquals(LTR, unkFmt.estimateDirection(en));
56 assertEquals(RTL, ltrFmt.estimateDirection(he));
57 assertEquals(RTL, rtlFmt.estimateDirection(he));
58 assertEquals(RTL, unkFmt.estimateDirection(he));
59
60 // Text contains HTML or HTML-escaping.
61 assertEquals(
62 LTR,
63 ltrFmt.estimateDirection('<some sort of tag/>' + he + ' &amp;', false));
64 assertEquals(
65 RTL,
66 ltrFmt.estimateDirection('<some sort of tag/>' + he + ' &amp;', true));
67 }
68
69 function testDirAttrValue() {
70 assertEquals(
71 'overall dir is RTL, context dir is LTR', 'rtl',
72 ltrFmt.dirAttrValue(he, true));
73 assertEquals(
74 'overall dir and context dir are RTL', 'rtl',
75 rtlFmt.dirAttrValue(he, true));
76 assertEquals(
77 'overall dir is LTR, context dir is RTL', 'ltr',
78 rtlFmt.dirAttrValue(en, true));
79 assertEquals(
80 'overall dir and context dir are LTR', 'ltr',
81 ltrFmt.dirAttrValue(en, true));
82
83 // Input's directionality is neutral.
84 assertEquals('ltr', ltrFmt.dirAttrValue('', true));
85 assertEquals('rtl', rtlFmt.dirAttrValue('', true));
86 assertEquals('ltr', unkFmt.dirAttrValue('', true));
87
88 // Text contains HTML or HTML-escaping:
89 assertEquals(
90 'rtl', ltrFmt.dirAttrValue(he + '<some sort of an HTML tag>', true));
91 assertEquals(
92 'ltr', ltrFmt.dirAttrValue(he + '<some sort of an HTML tag>', false));
93 }
94
95 function testKnownDirAttrValue() {
96 assertEquals('rtl', ltrFmt.knownDirAttrValue(RTL));
97 assertEquals('rtl', rtlFmt.knownDirAttrValue(RTL));
98 assertEquals('rtl', unkFmt.knownDirAttrValue(RTL));
99 assertEquals('ltr', rtlFmt.knownDirAttrValue(LTR));
100 assertEquals('ltr', ltrFmt.knownDirAttrValue(LTR));
101 assertEquals('ltr', unkFmt.knownDirAttrValue(LTR));
102
103 // Input directionality is neutral.
104 assertEquals('ltr', ltrFmt.knownDirAttrValue(NEUTRAL));
105 assertEquals('rtl', rtlFmt.knownDirAttrValue(NEUTRAL));
106 assertEquals('ltr', unkFmt.knownDirAttrValue(NEUTRAL));
107 }
108
109 function testDirAttr() {
110 assertEquals(
111 'overall dir (RTL) doesnt match context dir (LTR)', 'dir="rtl"',
112 ltrFmt.dirAttr(he, true));
113 assertEquals(
114 'overall dir (RTL) doesnt match context dir (unknown)', 'dir="rtl"',
115 unkFmt.dirAttr(he, true));
116 assertEquals(
117 'overall dir matches context dir (RTL)', '', rtlFmt.dirAttr(he, true));
118
119 assertEquals(
120 'overall dir (LTR) doesnt match context dir (RTL)', 'dir="ltr"',
121 rtlFmt.dirAttr(en, true));
122 assertEquals(
123 'overall dir (LTR) doesnt match context dir (unknown)', 'dir="ltr"',
124 unkFmt.dirAttr(en, true));
125 assertEquals(
126 'overall dir matches context dir (LTR)', '', ltrFmt.dirAttr(en, true));
127
128 assertEquals('neutral in RTL context', '', rtlFmt.dirAttr('.', true));
129 assertEquals('neutral in LTR context', '', ltrFmt.dirAttr('.', true));
130 assertEquals('neutral in unknown context', '', unkFmt.dirAttr('.', true));
131
132 // Text contains HTML or HTML-escaping:
133 assertEquals(
134 'dir="rtl"', ltrFmt.dirAttr(he + '<some sort of an HTML tag>', true));
135 assertEquals('', ltrFmt.dirAttr(he + '<some sort of an HTML tag>', false));
136 }
137
138 function testKnownDirAttr() {
139 assertEquals(
140 'overall dir (RTL) doesnt match context dir (LTR)', 'dir="rtl"',
141 ltrFmt.knownDirAttr(RTL));
142 assertEquals(
143 'overall dir matches context dir (RTL)', '', rtlFmt.knownDirAttr(RTL));
144 assertEquals(
145 'overall dir (LTR) doesnt match context dir (RTL)', 'dir="ltr"',
146 rtlFmt.knownDirAttr(LTR));
147 assertEquals(
148 'overall dir matches context dir (LTR)', '', ltrFmt.knownDirAttr(LTR));
149 }
150
151 /**
152 * @param {!goog.i18n.BidiFormatter} formatter
153 * @param {string} html
154 * @param {boolean=} opt_dirReset
155 * @return {string}
156 */
157 function spanWrap(formatter, html, opt_dirReset) {
158 return goog.html.SafeHtml.unwrap(formatter.spanWrapSafeHtml(
159 goog.html.testing.newSafeHtmlForTest(html),
160 opt_dirReset));
161 }
162
163 function testSpanWrap() {
164 // alwaysSpan is false and opt_isHtml is true, unless specified otherwise.
165 assertEquals(
166 'overall dir matches context dir (LTR), no dirReset', en,
167 spanWrap(ltrFmt, en, false));
168 assertEquals(
169 'overall dir matches context dir (LTR), dirReset', en,
170 spanWrap(ltrFmt, en, true));
171 assertEquals(
172 'overall dir matches context dir (RTL), no dirReset', he,
173 spanWrap(rtlFmt, he, false));
174 assertEquals(
175 'overall dir matches context dir (RTL), dirReset', he,
176 spanWrap(rtlFmt, he, true));
177
178 assertEquals(
179 'overall dir (RTL) doesnt match context dir (LTR), ' +
180 'no dirReset',
181 '<span dir="rtl">' + he + '<\/span>', spanWrap(ltrFmt, he, false));
182 assertEquals(
183 'overall dir (RTL) doesnt match context dir (LTR), dirReset',
184 '<span dir="rtl">' + he + '<\/span>' + LRM,
185 spanWrap(ltrFmt, he, true));
186 assertEquals(
187 'overall dir (LTR) doesnt match context dir (RTL), ' +
188 'no dirReset',
189 '<span dir="ltr">' + en + '<\/span>', spanWrap(rtlFmt, en, false));
190 assertEquals(
191 'overall dir (LTR) doesnt match context dir (RTL), dirReset',
192 '<span dir="ltr">' + en + '<\/span>' + RLM,
193 spanWrap(rtlFmt, en, true));
194 assertEquals(
195 'overall dir (LTR) doesnt match context dir (unknown), ' +
196 'no dirReset',
197 '<span dir="ltr">' + en + '<\/span>', spanWrap(unkFmt, en, false));
198 assertEquals(
199 'overall dir (RTL) doesnt match context dir (unknown), ' +
200 'dirReset',
201 '<span dir="rtl">' + he + '<\/span>', spanWrap(unkFmt, he, true));
202 assertEquals(
203 'overall dir (neutral) doesnt match context dir (LTR), ' +
204 'dirReset',
205 '', spanWrap(ltrFmt, '', true));
206
207 assertEquals(
208 'exit dir (but not overall dir) is opposite to context dir, ' +
209 'dirReset',
210 longEn + he + html + LRM,
211 spanWrap(ltrFmt, longEn + he + html, true));
212 assertEquals(
213 'overall dir (but not exit dir) is opposite to context dir, ' +
214 'dirReset',
215 '<span dir="ltr">' + longEn + he + '<\/span>' + RLM,
216 spanWrap(rtlFmt, longEn + he, true));
217
218 var ltrAlwaysSpanFmt = new goog.i18n.BidiFormatter(LTR, true);
219 var rtlAlwaysSpanFmt = new goog.i18n.BidiFormatter(RTL, true);
220 var unkAlwaysSpanFmt = new goog.i18n.BidiFormatter(null, true);
221
222 assertEquals(
223 'alwaysSpan, overall dir matches context dir (LTR), ' +
224 'no dirReset',
225 '<span>' + en + '<\/span>', spanWrap(ltrAlwaysSpanFmt, en, false));
226 assertEquals(
227 'alwaysSpan, overall dir matches context dir (LTR), dirReset',
228 '<span>' + en + '<\/span>', spanWrap(ltrAlwaysSpanFmt, en, true));
229 assertEquals(
230 'alwaysSpan, overall dir matches context dir (RTL), ' +
231 'no dirReset',
232 '<span>' + he + '<\/span>', spanWrap(rtlAlwaysSpanFmt, he, false));
233 assertEquals(
234 'alwaysSpan, overall dir matches context dir (RTL), dirReset',
235 '<span>' + he + '<\/span>', spanWrap(rtlAlwaysSpanFmt, he, true));
236
237 assertEquals(
238 'alwaysSpan, overall dir (RTL) doesnt match ' +
239 'context dir (LTR), no dirReset',
240 '<span dir="rtl">' + he + '<\/span>',
241 spanWrap(ltrAlwaysSpanFmt, he, false));
242 assertEquals(
243 'alwaysSpan, overall dir (RTL) doesnt match ' +
244 'context dir (LTR), dirReset',
245 '<span dir="rtl">' + he + '<\/span>' + LRM,
246 spanWrap(ltrAlwaysSpanFmt, he, true));
247 assertEquals(
248 'alwaysSpan, overall dir (neutral) doesnt match ' +
249 'context dir (LTR), dirReset',
250 '<span></span>', spanWrap(ltrAlwaysSpanFmt, '', true));
251 }
252
253 function testSpanWrapSafeHtml() {
254 var html = goog.html.SafeHtml.htmlEscape('a');
255 var wrapped = rtlFmt.spanWrapSafeHtml(html, false);
256 assertHtmlEquals('<span dir="ltr">a</span>', wrapped);
257 assertEquals(NEUTRAL, wrapped.getDirection());
258 }
259
260 /**
261 * @param {!goog.i18n.BidiFormatter} formatter
262 * @param {?goog.i18n.bidi.Dir} dir
263 * @param {string} html
264 * @return {string}
265 */
266 function spanWrapWithKnownDir(formatter, dir, html) {
267 return goog.html.SafeHtml.unwrap(formatter.spanWrapSafeHtmlWithKnownDir(dir,
268 goog.html.testing.newSafeHtmlForTest(html)));
269 }
270
271 function testSpanWrapWithKnownDir() {
272 assertEquals(
273 'known LTR in LTR context', en, spanWrapWithKnownDir(ltrFmt, LTR, en));
274 assertEquals(
275 'unknown LTR in LTR context', en, spanWrapWithKnownDir(ltrFmt, null, en));
276 assertEquals(
277 'overall LTR but exit RTL in LTR context', he + LRM,
278 spanWrapWithKnownDir(ltrFmt, LTR, he));
279 assertEquals(
280 'known RTL in LTR context', '<span dir="rtl">' + he + '<\/span>' + LRM,
281 spanWrapWithKnownDir(ltrFmt, RTL, he));
282 assertEquals(
283 'unknown RTL in LTR context', '<span dir="rtl">' + he + '<\/span>' + LRM,
284 spanWrapWithKnownDir(ltrFmt, null, he));
285 assertEquals(
286 'overall RTL but exit LTR in LTR context',
287 '<span dir="rtl">' + en + '<\/span>' + LRM,
288 spanWrapWithKnownDir(ltrFmt, RTL, en));
289 assertEquals(
290 'known neutral in LTR context', '.',
291 spanWrapWithKnownDir(ltrFmt, NEUTRAL, '.'));
292 assertEquals(
293 'unknown neutral in LTR context', '.',
294 spanWrapWithKnownDir(ltrFmt, null, '.'));
295 assertEquals(
296 'overall neutral but exit LTR in LTR context', en,
297 spanWrapWithKnownDir(ltrFmt, NEUTRAL, en));
298 assertEquals(
299 'overall neutral but exit RTL in LTR context', he + LRM,
300 spanWrapWithKnownDir(ltrFmt, NEUTRAL, he));
301
302 assertEquals(
303 'known RTL in RTL context', he, spanWrapWithKnownDir(rtlFmt, RTL, he));
304 assertEquals(
305 'unknown RTL in RTL context', he, spanWrapWithKnownDir(rtlFmt, null, he));
306 assertEquals(
307 'overall RTL but exit LTR in RTL context', en + RLM,
308 spanWrapWithKnownDir(rtlFmt, RTL, en));
309 assertEquals(
310 'known LTR in RTL context', '<span dir="ltr">' + en + '<\/span>' + RLM,
311 spanWrapWithKnownDir(rtlFmt, LTR, en));
312 assertEquals(
313 'unknown LTR in RTL context', '<span dir="ltr">' + en + '<\/span>' + RLM,
314 spanWrapWithKnownDir(rtlFmt, null, en));
315 assertEquals(
316 'LTR but exit RTL in RTL context',
317 '<span dir="ltr">' + he + '<\/span>' + RLM,
318 spanWrapWithKnownDir(rtlFmt, LTR, he));
319 assertEquals(
320 'known neutral in RTL context', '.',
321 spanWrapWithKnownDir(rtlFmt, NEUTRAL, '.'));
322 assertEquals(
323 'unknown neutral in RTL context', '.',
324 spanWrapWithKnownDir(rtlFmt, null, '.'));
325 assertEquals(
326 'overall neutral but exit LTR in LTR context', he,
327 spanWrapWithKnownDir(rtlFmt, NEUTRAL, he));
328 assertEquals(
329 'overall neutral but exit RTL in LTR context', en + RLM,
330 spanWrapWithKnownDir(rtlFmt, NEUTRAL, en));
331
332 assertEquals(
333 'known RTL in unknown context', '<span dir="rtl">' + he + '<\/span>',
334 spanWrapWithKnownDir(unkFmt, RTL, he));
335 assertEquals(
336 'unknown RTL in unknown context', '<span dir="rtl">' + he + '<\/span>',
337 spanWrapWithKnownDir(unkFmt, null, he));
338 assertEquals(
339 'overall RTL but exit LTR in unknown context',
340 '<span dir="rtl">' + en + '<\/span>',
341 spanWrapWithKnownDir(unkFmt, RTL, en));
342 assertEquals(
343 'known LTR in unknown context', '<span dir="ltr">' + en + '<\/span>',
344 spanWrapWithKnownDir(unkFmt, LTR, en));
345 assertEquals(
346 'unknown LTR in unknown context', '<span dir="ltr">' + en + '<\/span>',
347 spanWrapWithKnownDir(unkFmt, null, en));
348 assertEquals(
349 'LTR but exit RTL in unknown context',
350 '<span dir="ltr">' + he + '<\/span>',
351 spanWrapWithKnownDir(unkFmt, LTR, he));
352 assertEquals(
353 'known neutral in unknown context', '.',
354 spanWrapWithKnownDir(unkFmt, NEUTRAL, '.'));
355 assertEquals(
356 'unknown neutral in unknown context', '.',
357 spanWrapWithKnownDir(unkFmt, null, '.'));
358 assertEquals(
359 'overall neutral but exit LTR in unknown context', he,
360 spanWrapWithKnownDir(unkFmt, NEUTRAL, he));
361 assertEquals(
362 'overall neutral but exit RTL in unknown context', en,
363 spanWrapWithKnownDir(unkFmt, NEUTRAL, en));
364 }
365
366 function testSpanWrapSafeHtmlWithKnownDir() {
367 var html = goog.html.SafeHtml.htmlEscape('a');
368 assertHtmlEquals(
369 '<span dir="ltr">a</span>',
370 rtlFmt.spanWrapSafeHtmlWithKnownDir(LTR, html, false));
371 }
372
373 function testUnicodeWrap() {
374 // opt_isHtml is true, unless specified otherwise.
375 assertEquals(
376 'overall dir matches context dir (LTR), no dirReset', en,
377 ltrFmt.unicodeWrap(en, true, false));
378 assertEquals(
379 'overall dir matches context dir (LTR), dirReset', en,
380 ltrFmt.unicodeWrap(en, true, true));
381 assertEquals(
382 'overall dir matches context dir (RTL), no dirReset', he,
383 rtlFmt.unicodeWrap(he, true, false));
384 assertEquals(
385 'overall dir matches context dir (RTL), dirReset', he,
386 rtlFmt.unicodeWrap(he, true, true));
387
388 assertEquals(
389 'overall dir (RTL) doesnt match context dir (LTR), ' +
390 'no dirReset',
391 RLE + he + PDF, ltrFmt.unicodeWrap(he, true, false));
392 assertEquals(
393 'overall dir (RTL) doesnt match context dir (LTR), dirReset',
394 RLE + he + PDF + LRM, ltrFmt.unicodeWrap(he, true, true));
395 assertEquals(
396 'overall dir (LTR) doesnt match context dir (RTL), ' +
397 'no dirReset',
398 LRE + en + PDF, rtlFmt.unicodeWrap(en, true, false));
399 assertEquals(
400 'overall dir (LTR) doesnt match context dir (RTL), dirReset',
401 LRE + en + PDF + RLM, rtlFmt.unicodeWrap(en, true, true));
402 assertEquals(
403 'overall dir (LTR) doesnt match context dir (unknown), ' +
404 'no dirReset',
405 LRE + en + PDF, unkFmt.unicodeWrap(en, true, false));
406 assertEquals(
407 'overall dir (RTL) doesnt match context dir (unknown), ' +
408 'dirReset',
409 RLE + he + PDF, unkFmt.unicodeWrap(he, true, true));
410 assertEquals(
411 'overall dir (neutral) doesnt match context dir (LTR), ' +
412 'dirReset',
413 '', ltrFmt.unicodeWrap('', true, true));
414
415 assertEquals(
416 'exit dir (but not overall dir) is opposite to context dir, ' +
417 'dirReset',
418 longEn + he + html + LRM,
419 ltrFmt.unicodeWrap(longEn + he + html, true, true));
420 assertEquals(
421 'overall dir (but not exit dir) is opposite to context dir, ' +
422 'dirReset',
423 LRE + longEn + he + PDF + RLM,
424 rtlFmt.unicodeWrap(longEn + he, true, true));
425 }
426
427 function testUnicodeWrapWithKnownDir() {
428 assertEquals(
429 'known LTR in LTR context', en, ltrFmt.unicodeWrapWithKnownDir(LTR, en));
430 assertEquals(
431 'unknown LTR in LTR context', en,
432 ltrFmt.unicodeWrapWithKnownDir(null, en));
433 assertEquals(
434 'overall LTR but exit RTL in LTR context', he + LRM,
435 ltrFmt.unicodeWrapWithKnownDir(LTR, he));
436 assertEquals(
437 'known RTL in LTR context', RLE + he + PDF + LRM,
438 ltrFmt.unicodeWrapWithKnownDir(RTL, he));
439 assertEquals(
440 'unknown RTL in LTR context', RLE + he + PDF + LRM,
441 ltrFmt.unicodeWrapWithKnownDir(null, he));
442 assertEquals(
443 'overall RTL but exit LTR in LTR context', RLE + en + PDF + LRM,
444 ltrFmt.unicodeWrapWithKnownDir(RTL, en));
445 assertEquals(
446 'known neutral in LTR context', '.',
447 ltrFmt.unicodeWrapWithKnownDir(NEUTRAL, '.'));
448 assertEquals(
449 'unknown neutral in LTR context', '.',
450 ltrFmt.unicodeWrapWithKnownDir(null, '.'));
451 assertEquals(
452 'overall neutral but exit LTR in LTR context', en,
453 ltrFmt.unicodeWrapWithKnownDir(NEUTRAL, en));
454 assertEquals(
455 'overall neutral but exit RTL in LTR context', he + LRM,
456 ltrFmt.unicodeWrapWithKnownDir(NEUTRAL, he));
457
458 assertEquals(
459 'known RTL in RTL context', he, rtlFmt.unicodeWrapWithKnownDir(RTL, he));
460 assertEquals(
461 'unknown RTL in RTL context', he,
462 rtlFmt.unicodeWrapWithKnownDir(null, he));
463 assertEquals(
464 'overall RTL but exit LTR in RTL context', en + RLM,
465 rtlFmt.unicodeWrapWithKnownDir(RTL, en));
466 assertEquals(
467 'known LTR in RTL context', LRE + en + PDF + RLM,
468 rtlFmt.unicodeWrapWithKnownDir(LTR, en));
469 assertEquals(
470 'unknown LTR in RTL context', LRE + en + PDF + RLM,
471 rtlFmt.unicodeWrapWithKnownDir(null, en));
472 assertEquals(
473 'LTR but exit RTL in RTL context', LRE + he + PDF + RLM,
474 rtlFmt.unicodeWrapWithKnownDir(LTR, he));
475 assertEquals(
476 'known neutral in RTL context', '.',
477 rtlFmt.unicodeWrapWithKnownDir(NEUTRAL, '.'));
478 assertEquals(
479 'unknown neutral in RTL context', '.',
480 rtlFmt.unicodeWrapWithKnownDir(null, '.'));
481 assertEquals(
482 'overall neutral but exit LTR in LTR context', he,
483 rtlFmt.unicodeWrapWithKnownDir(NEUTRAL, he));
484 assertEquals(
485 'overall neutral but exit RTL in LTR context', en + RLM,
486 rtlFmt.unicodeWrapWithKnownDir(NEUTRAL, en));
487
488 assertEquals(
489 'known RTL in unknown context', RLE + he + PDF,
490 unkFmt.unicodeWrapWithKnownDir(RTL, he));
491 assertEquals(
492 'unknown RTL in unknown context', RLE + he + PDF,
493 unkFmt.unicodeWrapWithKnownDir(null, he));
494 assertEquals(
495 'overall RTL but exit LTR in unknown context', RLE + en + PDF,
496 unkFmt.unicodeWrapWithKnownDir(RTL, en));
497 assertEquals(
498 'known LTR in unknown context', LRE + en + PDF,
499 unkFmt.unicodeWrapWithKnownDir(LTR, en));
500 assertEquals(
501 'unknown LTR in unknown context', LRE + en + PDF,
502 unkFmt.unicodeWrapWithKnownDir(null, en));
503 assertEquals(
504 'LTR but exit RTL in unknown context', LRE + he + PDF,
505 unkFmt.unicodeWrapWithKnownDir(LTR, he));
506 assertEquals(
507 'known neutral in unknown context', '.',
508 unkFmt.unicodeWrapWithKnownDir(NEUTRAL, '.'));
509 assertEquals(
510 'unknown neutral in unknown context', '.',
511 unkFmt.unicodeWrapWithKnownDir(null, '.'));
512 assertEquals(
513 'overall neutral but exit LTR in unknown context', he,
514 unkFmt.unicodeWrapWithKnownDir(NEUTRAL, he));
515 assertEquals(
516 'overall neutral but exit RTL in unknown context', en,
517 unkFmt.unicodeWrapWithKnownDir(NEUTRAL, en));
518 }
519
520 function testMarkAfter() {
521 assertEquals(
522 'exit dir (RTL) is opposite to context dir (LTR)', LRM,
523 ltrFmt.markAfter(longEn + he + html, true));
524 assertEquals(
525 'exit dir (LTR) is opposite to context dir (RTL)', RLM,
526 rtlFmt.markAfter(longHe + en, true));
527 assertEquals(
528 'exit dir (LTR) doesnt match context dir (unknown)', '',
529 unkFmt.markAfter(longEn + en, true));
530 assertEquals(
531 'overall dir (RTL) is opposite to context dir (LTR)', LRM,
532 ltrFmt.markAfter(longHe + en, true));
533 assertEquals(
534 'overall dir (LTR) is opposite to context dir (RTL)', RLM,
535 rtlFmt.markAfter(longEn + he, true));
536 assertEquals(
537 'exit dir and overall dir match context dir (LTR)', '',
538 ltrFmt.markAfter(longEn + he + html, false));
539 assertEquals(
540 'exit dir and overall dir matches context dir (RTL)', '',
541 rtlFmt.markAfter(longHe + he, true));
542 }
543
544 function testMarkAfterKnownDir() {
545 assertEquals(
546 'known LTR in LTR context', '', ltrFmt.markAfterKnownDir(LTR, en));
547 assertEquals(
548 'unknown LTR in LTR context', '', ltrFmt.markAfterKnownDir(null, en));
549 assertEquals(
550 'overall LTR but exit RTL in LTR context', LRM,
551 ltrFmt.markAfterKnownDir(LTR, he));
552 assertEquals(
553 'known RTL in LTR context', LRM, ltrFmt.markAfterKnownDir(RTL, he));
554 assertEquals(
555 'unknown RTL in LTR context', LRM, ltrFmt.markAfterKnownDir(null, he));
556 assertEquals(
557 'overall RTL but exit LTR in LTR context', LRM,
558 ltrFmt.markAfterKnownDir(RTL, en));
559 assertEquals(
560 'known neutral in LTR context', '',
561 ltrFmt.markAfterKnownDir(NEUTRAL, '.'));
562 assertEquals(
563 'unknown neutral in LTR context', '',
564 ltrFmt.markAfterKnownDir(null, '.'));
565 assertEquals(
566 'overall neutral but exit LTR in LTR context', '',
567 ltrFmt.markAfterKnownDir(NEUTRAL, en));
568 assertEquals(
569 'overall neutral but exit RTL in LTR context', LRM,
570 ltrFmt.markAfterKnownDir(NEUTRAL, he));
571
572 assertEquals(
573 'known RTL in RTL context', '', rtlFmt.markAfterKnownDir(RTL, he));
574 assertEquals(
575 'unknown RTL in RTL context', '', rtlFmt.markAfterKnownDir(null, he));
576 assertEquals(
577 'overall RTL but exit LTR in RTL context', RLM,
578 rtlFmt.markAfterKnownDir(RTL, en));
579 assertEquals(
580 'known LTR in RTL context', RLM, rtlFmt.markAfterKnownDir(LTR, en));
581 assertEquals(
582 'unknown LTR in RTL context', RLM, rtlFmt.markAfterKnownDir(null, en));
583 assertEquals(
584 'LTR but exit RTL in RTL context', RLM,
585 rtlFmt.markAfterKnownDir(LTR, he));
586 assertEquals(
587 'known neutral in RTL context', '',
588 rtlFmt.markAfterKnownDir(NEUTRAL, '.'));
589 assertEquals(
590 'unknown neutral in RTL context', '',
591 rtlFmt.markAfterKnownDir(null, '.'));
592 assertEquals(
593 'overall neutral but exit LTR in LTR context', '',
594 rtlFmt.markAfterKnownDir(NEUTRAL, he));
595 assertEquals(
596 'overall neutral but exit RTL in LTR context', RLM,
597 rtlFmt.markAfterKnownDir(NEUTRAL, en));
598
599 assertEquals(
600 'known RTL in unknown context', '', unkFmt.markAfterKnownDir(RTL, he));
601 assertEquals(
602 'unknown RTL in unknown context', '', unkFmt.markAfterKnownDir(null, he));
603 assertEquals(
604 'overall RTL but exit LTR in unknown context', '',
605 unkFmt.markAfterKnownDir(RTL, en));
606 assertEquals(
607 'known LTR in unknown context', '', unkFmt.markAfterKnownDir(LTR, en));
608 assertEquals(
609 'unknown LTR in unknown context', '', unkFmt.markAfterKnownDir(null, en));
610 assertEquals(
611 'LTR but exit RTL in unknown context', '',
612 unkFmt.markAfterKnownDir(LTR, he));
613 assertEquals(
614 'known neutral in unknown context', '',
615 unkFmt.markAfterKnownDir(NEUTRAL, '.'));
616 assertEquals(
617 'unknown neutral in unknown context', '',
618 unkFmt.markAfterKnownDir(null, '.'));
619 assertEquals(
620 'overall neutral but exit LTR in unknown context', '',
621 unkFmt.markAfterKnownDir(NEUTRAL, he));
622 assertEquals(
623 'overall neutral but exit RTL in unknown context', '',
624 unkFmt.markAfterKnownDir(NEUTRAL, en));
625 }
626
627 function testMark() {
628 // Implicitly, also tests the constructor.
629 assertEquals(LRM, (new goog.i18n.BidiFormatter(LTR)).mark());
630 assertEquals('', (new goog.i18n.BidiFormatter(null)).mark());
631 assertEquals('', (new goog.i18n.BidiFormatter(NEUTRAL)).mark());
632 assertEquals(RLM, (new goog.i18n.BidiFormatter(RTL)).mark());
633 assertEquals(RLM, (new goog.i18n.BidiFormatter(true)).mark());
634 assertEquals(LRM, (new goog.i18n.BidiFormatter(false)).mark());
635 }
636
637 function testStartEdge() {
638 assertEquals('left', ltrFmt.startEdge());
639 assertEquals('left', unkFmt.startEdge());
640 assertEquals('right', rtlFmt.startEdge());
641 }
642
643 function testEndEdge() {
644 assertEquals('right', ltrFmt.endEdge());
645 assertEquals('right', unkFmt.endEdge());
646 assertEquals('left', rtlFmt.endEdge());
647 }
648
649 function assertHtmlEquals(expected, html) {
650 assertEquals(expected, goog.html.SafeHtml.unwrap(html));
651 }