aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/i18n/bidiformatter_test.html
blob: f756910c1492b7a377bb8900d86fdc3ad4f65305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<!DOCTYPE html>
<html>
<!--
Copyright 2009 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.i18n.BidiFormatter</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.i18n.BidiFormatter');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
  var LRM = goog.i18n.bidi.Format.LRM;
  var RLM = goog.i18n.bidi.Format.RLM;
  var LRE = goog.i18n.bidi.Format.LRE;
  var RLE = goog.i18n.bidi.Format.RLE;
  var PDF = goog.i18n.bidi.Format.PDF;
  var LTR = goog.i18n.bidi.Dir.LTR;
  var RTL = goog.i18n.bidi.Dir.RTL;
  var UNKNOWN = goog.i18n.bidi.Dir.UNKNOWN;
  var he = '\u05e0\u05e1';
  var en = 'abba';
  var html = '&lt;';
  var longEn = 'abba sabba gabba ';
  var longHe = '\u05e0 \u05e1 \u05e0 ';
  var ltrFmt = new goog.i18n.BidiFormatter(LTR,  false);  // LTR context
  var rtlFmt = new goog.i18n.BidiFormatter(RTL, false);  // RTL context
  var unkFmt = new goog.i18n.BidiFormatter(UNKNOWN,  false);  // unknown context

  function testEstimateDirection() {
    assertEquals(UNKNOWN, ltrFmt.estimateDirection(''));
    assertEquals(UNKNOWN, rtlFmt.estimateDirection(''));
    assertEquals(UNKNOWN, unkFmt.estimateDirection(''));
    assertEquals(LTR, ltrFmt.estimateDirection(en));
    assertEquals(LTR, rtlFmt.estimateDirection(en));
    assertEquals(LTR, unkFmt.estimateDirection(en));
    assertEquals(RTL, ltrFmt.estimateDirection(he));
    assertEquals(RTL, rtlFmt.estimateDirection(he));
    assertEquals(RTL, unkFmt.estimateDirection(he));

    // Text contains HTML or HTML-escaping.
    assertEquals(LTR, ltrFmt.estimateDirection(
        '<some sort of tag/>' + he + ' &amp;', false));
    assertEquals(RTL, ltrFmt.estimateDirection(
        '<some sort of tag/>' + he + ' &amp;', true));
  }

  function testDirAttrValue() {
    assertEquals('overall dir is RTL, context dir is LTR',
        'rtl', ltrFmt.dirAttrValue(he, true));
    assertEquals('overall dir and context dir are RTL',
        'rtl', rtlFmt.dirAttrValue(he, true));
    assertEquals('overall dir is LTR, context dir is RTL',
        'ltr', rtlFmt.dirAttrValue(en, true));
    assertEquals('overall dir and context dir are LTR',
        'ltr', ltrFmt.dirAttrValue(en, true));

    // Input's directionality is unknown.
    assertEquals('ltr', ltrFmt.dirAttrValue('', true));
    assertEquals('rtl', rtlFmt.dirAttrValue('', true));
    assertEquals('ltr', unkFmt.dirAttrValue('', true));

    // Text contains HTML or HTML-escaping:
    assertEquals('rtl',
        ltrFmt.dirAttrValue(he + "<some sort of an HTML tag>", true));
    assertEquals('ltr',
        ltrFmt.dirAttrValue(he + "<some sort of an HTML tag>", false));
  }

  function testKnownDirAttrValue() {
    assertEquals('rtl', ltrFmt.knownDirAttrValue(RTL));
    assertEquals('rtl', rtlFmt.knownDirAttrValue(RTL));
    assertEquals('ltr', rtlFmt.knownDirAttrValue(LTR));
    assertEquals('ltr', ltrFmt.knownDirAttrValue(LTR));

    // Input directionality is unknown.
    assertEquals('ltr', ltrFmt.knownDirAttrValue(UNKNOWN));
    assertEquals('rtl', rtlFmt.knownDirAttrValue(UNKNOWN));
    assertEquals('ltr', unkFmt.knownDirAttrValue(UNKNOWN));
  }

  function testDirAttr() {
    assertEquals('overall dir (RTL) doesnt match context dir (LTR)',
        'dir="rtl"', ltrFmt.dirAttr(he, true));
    assertEquals('overall dir matches context dir (RTL)',
        '', rtlFmt.dirAttr(he, true));
    assertEquals('overall dir (LTR) doesnt match context dir (RTL)',
        'dir="ltr"', rtlFmt.dirAttr(en, true));
    assertEquals('overall dir matches context dir (LTR)',
        '', ltrFmt.dirAttr(en, true));

    // Text contains HTML or HTML-escaping:
    assertEquals('dir="rtl"',
        ltrFmt.dirAttr(he + "<some sort of an HTML tag>", true));
    assertEquals('',
        ltrFmt.dirAttr(he + "<some sort of an HTML tag>", false));
  }

  function testKnownDirAttr() {
    assertEquals('overall dir (RTL) doesnt match context dir (LTR)',
        'dir="rtl"', ltrFmt.knownDirAttr(goog.i18n.bidi.Dir.RTL));
    assertEquals('overall dir matches context dir (RTL)',
        '', rtlFmt.knownDirAttr(goog.i18n.bidi.Dir.RTL));
    assertEquals('overall dir (LTR) doesnt match context dir (RTL)',
        'dir="ltr"', rtlFmt.knownDirAttr(goog.i18n.bidi.Dir.LTR));
    assertEquals('overall dir matches context dir (LTR)',
        '', ltrFmt.knownDirAttr(goog.i18n.bidi.Dir.LTR));
  }

  function testSpanWrap() {
    // alwaysSpan is false and opt_isHtml is true, unless specified otherwise.
    assertEquals('overall dir matches context dir (LTR), no dirReset',
        en, ltrFmt.spanWrap(en, true, false));
    assertEquals('overall dir matches context dir (LTR), dirReset',
        en, ltrFmt.spanWrap(en, true, true));
    assertEquals('overall dir matches context dir (RTL), no dirReset',
        he, rtlFmt.spanWrap(he, true, false));
    assertEquals('overall dir matches context dir (RTL), dirReset',
        he, rtlFmt.spanWrap(he, true, true));

    assertEquals('overall dir (RTL) doesnt match context dir (LTR), ' +
        'no dirReset',
        '<span dir="rtl">' + he + '<\/span>', ltrFmt.spanWrap(he, true, false));
    assertEquals('overall dir (RTL) doesnt match context dir (LTR), dirReset',
        '<span dir="rtl">' + he + '<\/span>' + LRM,
        ltrFmt.spanWrap(he, true, true));
    assertEquals('overall dir (LTR) doesnt match context dir (RTL), ' +
        'no dirReset',
        '<span dir="ltr">' + en + '<\/span>', rtlFmt.spanWrap(en, true, false));
    assertEquals('overall dir (LTR) doesnt match context dir (RTL), dirReset',
        '<span dir="ltr">' + en + '<\/span>' + RLM,
        rtlFmt.spanWrap(en, true, true));
    assertEquals('overall dir (LTR) doesnt match context dir (unknown), ' +
        'no dirReset',
        '<span dir="ltr">' + en + '<\/span>', unkFmt.spanWrap(en, true, false));
    assertEquals('overall dir (RTL) doesnt match context dir (unknown), ' +
        'dirReset',
        '<span dir="rtl">' + he + '<\/span>', unkFmt.spanWrap(he, true, true));
    assertEquals('overall dir (neutral) doesnt match context dir (LTR), ' +
        'dirReset',
        '',
        ltrFmt.spanWrap('', true, true));

    assertEquals('exit dir (but not overall dir) is opposite to context dir, ' +
        'dirReset',
        longEn + he + html + LRM,
        ltrFmt.spanWrap(longEn + he + html, true, true));
    assertEquals('overall dir (but not exit dir) is opposite to context dir, ' +
        'dirReset',
        '<span dir="ltr">' + longEn + he + '<\/span>' + RLM,
        rtlFmt.spanWrap(longEn + he, true, true));

    assertEquals('input is plain text (not escaped)',
        '&lt;br&gt;' + en,
        ltrFmt.spanWrap('<br>' + en, false, false));

    var ltrAlwaysSpanFmt = new goog.i18n.BidiFormatter(LTR,  true);
    var rtlAlwaysSpanFmt = new goog.i18n.BidiFormatter(RTL, true);
    var unkAlwaysSpanFmt = new goog.i18n.BidiFormatter(UNKNOWN,  true);

    assertEquals('alwaysSpan, overall dir matches context dir (LTR), ' +
        'no dirReset',
        '<span>' + en + '<\/span>', ltrAlwaysSpanFmt.spanWrap(en, true, false));
    assertEquals('alwaysSpan, overall dir matches context dir (LTR), dirReset',
        '<span>' + en + '<\/span>', ltrAlwaysSpanFmt.spanWrap(en, true, true));
    assertEquals('alwaysSpan, overall dir matches context dir (RTL), ' +
        'no dirReset',
        '<span>' + he + '<\/span>', rtlAlwaysSpanFmt.spanWrap(he, true, false));
    assertEquals('alwaysSpan, overall dir matches context dir (RTL), dirReset',
        '<span>' + he + '<\/span>', rtlAlwaysSpanFmt.spanWrap(he, true, true));

    assertEquals('alwaysSpan, overall dir (RTL) doesnt match ' +
        'context dir (LTR), no dirReset',
        '<span dir="rtl">' + he + '<\/span>',
        ltrAlwaysSpanFmt.spanWrap(he, true, false));
    assertEquals('alwaysSpan, overall dir (RTL) doesnt match ' +
        'context dir (LTR), dirReset',
        '<span dir="rtl">' + he + '<\/span>' + LRM,
        ltrAlwaysSpanFmt.spanWrap(he, true, true));
    assertEquals('alwaysSpan, overall dir (neutral) doesnt match ' +
        'context dir (LTR), dirReset',
        '<span></span>',
        ltrAlwaysSpanFmt.spanWrap('', true, true));
  }

  function testSpanWrapWithKnownDir() {
    assertEquals('overall dir matches context dir (LTR)',
        en, ltrFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, en));
    assertEquals(
        'overall dir (but not exit dir) supposedly matches context dir (LTR)',
        he + LRM, ltrFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he));
    assertEquals('overall dir matches context dir (RTL)',
        he, rtlFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, he));
    assertEquals(
        'overall dir (but not exit dir) supposedly matches context dir (RTL)',
        en + RLM, rtlFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, en));

    assertEquals('overall dir (RTL) doesnt match context dir (LTR)',
        '<span dir="rtl">' + he + '<\/span>' + LRM,
        ltrFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, he));
    assertEquals('supposed overall dir (RTL) doesnt match context dir (LTR)',
        '<span dir="rtl">' + en + '<\/span>' + LRM,
        ltrFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, en));
    assertEquals('overall dir (LTR) doesnt match context dir (RTL)',
        '<span dir="ltr">' + en + '<\/span>' + RLM,
        rtlFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, en));
    assertEquals('supposed overall dir (LTR) doesnt match context dir (RTL)',
        '<span dir="ltr">' + he + '<\/span>' + RLM,
        rtlFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he));
    assertEquals(
        'supposed overall dir (LTR) doesnt match context dir (unknown)',
        '<span dir="ltr">' + he + '<\/span>',
        unkFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he));
    assertEquals(
        'supposed overall dir (neutral) doesnt match context dir (LTR)',
        he + LRM,
        ltrFmt.spanWrapWithKnownDir(goog.i18n.bidi.Dir.UNKNOWN, he));
  }

  function testUnicodeWrap() {
    // opt_isHtml is true, unless specified otherwise.
    assertEquals('overall dir matches context dir (LTR), no dirReset',
        en, ltrFmt.unicodeWrap(en, true, false));
    assertEquals('overall dir matches context dir (LTR), dirReset',
        en, ltrFmt.unicodeWrap(en, true, true));
    assertEquals('overall dir matches context dir (RTL), no dirReset',
        he, rtlFmt.unicodeWrap(he, true, false));
    assertEquals('overall dir matches context dir (RTL), dirReset',
        he, rtlFmt.unicodeWrap(he, true, true));

    assertEquals('overall dir (RTL) doesnt match context dir (LTR), ' +
        'no dirReset',
        RLE + he + PDF, ltrFmt.unicodeWrap(he, true, false));
    assertEquals('overall dir (RTL) doesnt match context dir (LTR), dirReset',
        RLE + he + PDF + LRM, ltrFmt.unicodeWrap(he, true, true));
    assertEquals('overall dir (LTR) doesnt match context dir (RTL), ' +
        'no dirReset',
        LRE + en + PDF, rtlFmt.unicodeWrap(en, true, false));
    assertEquals('overall dir (LTR) doesnt match context dir (RTL), dirReset',
        LRE + en + PDF + RLM, rtlFmt.unicodeWrap(en, true, true));
    assertEquals('overall dir (LTR) doesnt match context dir (unknown), ' +
        'no dirReset',
        LRE + en + PDF, unkFmt.unicodeWrap(en, true, false));
    assertEquals('overall dir (RTL) doesnt match context dir (unknown), ' +
        'dirReset',
        RLE + he + PDF, unkFmt.unicodeWrap(he, true, true));
    assertEquals('overall dir (neutral) doesnt match context dir (LTR), ' +
        'dirReset',
        '',
        ltrFmt.unicodeWrap('', true, true));

    assertEquals('exit dir (but not overall dir) is opposite to context dir, ' +
        'dirReset',
        longEn + he + html + LRM,
        ltrFmt.unicodeWrap(longEn + he + html, true, true));
    assertEquals('overall dir (but not exit dir) is opposite to context dir, ' +
        'dirReset',
        LRE + longEn + he + PDF + RLM,
        rtlFmt.unicodeWrap(longEn + he, true, true));
  }

  function testUnicodeWrapWithKnownDir() {
    assertEquals('overall dir matches context dir (LTR)',
        en, ltrFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, en));
    assertEquals(
        'overall dir (but not exit dir) supposedly matches context dir (LTR)',
        he + LRM, ltrFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he));
    assertEquals('overall dir matches context dir (RTL)',
        he, rtlFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, he));
    assertEquals(
        'overall dir (but not exit dir) supposedly matches context dir (RTL)',
        en + RLM, rtlFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, en));

    assertEquals('overall dir (RTL) doesnt match context dir (LTR)',
        RLE + he + PDF + LRM,
        ltrFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, he));
    assertEquals('supposed overall dir (RTL) doesnt match context dir (LTR)',
        RLE + en + PDF + LRM,
        ltrFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.RTL, en));
    assertEquals('overall dir (LTR) doesnt match context dir (RTL)',
        LRE + en + PDF + RLM,
        rtlFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, en));
    assertEquals('supposed overall dir (LTR) doesnt match context dir (RTL)',
        LRE + he + PDF + RLM,
        rtlFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he));
    assertEquals(
        'supposed overall dir (LTR) doesnt match context dir (unknown)',
        LRE + he + PDF,
        unkFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.LTR, he));
    assertEquals(
        'supposed overall dir (neutral) doesnt match context dir (LTR)',
        he + LRM,
        ltrFmt.unicodeWrapWithKnownDir(goog.i18n.bidi.Dir.UNKNOWN, he));
  }

  function testMarkAfter() {
    assertEquals('exit dir (RTL) is opposite to context dir (LTR)',
        LRM, ltrFmt.markAfter(longEn + he + html, true));
    assertEquals('exit dir (LTR) is opposite to context dir (RTL)',
        RLM, rtlFmt.markAfter(longHe + en, true));
    assertEquals('exit dir (LTR) doesnt match context dir (unknown)',
        '', unkFmt.markAfter(longEn + en, true));
    assertEquals('overall dir (RTL) is opposite to context dir (LTR)',
        LRM, ltrFmt.markAfter(longHe + en, true));
    assertEquals('overall dir (LTR) is opposite to context dir (RTL)',
        RLM, rtlFmt.markAfter(longEn + he, true));
    assertEquals('exit dir and overall dir match context dir (LTR)',
        '', ltrFmt.markAfter(longEn + he + html, false));
    assertEquals('exit dir and overall dir matches context dir (RTL)',
        '', rtlFmt.markAfter(longHe + he, true));
  }

  function testMark() {
    // Implicitly, also tests the constructor.
    assertEquals(LRM, (new goog.i18n.BidiFormatter(LTR)).mark());
    assertEquals('',  (new goog.i18n.BidiFormatter(UNKNOWN)).mark());
    assertEquals(RLM, (new goog.i18n.BidiFormatter(RTL)).mark());
    assertEquals(RLM, (new goog.i18n.BidiFormatter(true)).mark());
    assertEquals(LRM, (new goog.i18n.BidiFormatter(false)).mark());
  }

  function testStartEdge() {
    assertEquals('left', ltrFmt.startEdge());
    assertEquals('left',  unkFmt.startEdge());
    assertEquals('right', rtlFmt.startEdge());
  }

  function testEndEdge() {
    assertEquals('right', ltrFmt.endEdge());
    assertEquals('right',  unkFmt.endEdge());
    assertEquals('left', rtlFmt.endEdge());
  }

</script>
</body>
</html>