OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 | |
6 #library('bidi_utils_test'); | |
7 | |
8 #import('../../../pkg/i18n/intl.dart'); | |
9 #import('../../../lib/unittest/unittest.dart'); | |
10 | |
11 /** | |
12 * Tests the bidi utilities library. | |
13 */ | |
14 main() { | |
15 var LRE = '\u202A'; | |
16 var RLE = '\u202B'; | |
17 var PDF = '\u202C'; | |
18 var LRM = '\u200E'; | |
19 var RLM = '\u200F'; | |
20 | |
21 test('isRtlLang', () { | |
22 expect(isRtlLanguage('en'), isFalse); | |
23 expect(isRtlLanguage('fr'), isFalse); | |
24 expect(isRtlLanguage('zh-CN'), isFalse); | |
25 expect(isRtlLanguage('fil'), isFalse); | |
26 expect(isRtlLanguage('az'), isFalse); | |
27 expect(isRtlLanguage('iw-Latn'), isFalse); | |
28 expect(isRtlLanguage('iw-LATN'), isFalse); | |
29 expect(isRtlLanguage('iw_latn'), isFalse); | |
30 expect(isRtlLanguage('ar'), isTrue); | |
31 expect(isRtlLanguage('AR'), isTrue); | |
32 expect(isRtlLanguage('iw'), isTrue); | |
33 expect(isRtlLanguage('he'), isTrue); | |
34 expect(isRtlLanguage('fa'), isTrue); | |
35 expect(isRtlLanguage('ar-EG'), isTrue); | |
36 expect(isRtlLanguage('az-Arab'), isTrue); | |
37 expect(isRtlLanguage('az-ARAB-IR'), isTrue); | |
38 expect(isRtlLanguage('az_arab_IR'), isTrue); | |
39 }); | |
40 | |
41 test('hasAnyLtr', () { | |
42 expect(hasAnyLtr(''), isFalse); | |
43 expect(hasAnyLtr('\u05e0\u05e1\u05e2'), isFalse); | |
44 expect(hasAnyLtr('\u05e0\u05e1z\u05e2'), isTrue); | |
45 expect(hasAnyLtr('123\t... \n'), isFalse); | |
46 expect(hasAnyLtr('<br>123<', false), isTrue); | |
47 expect(hasAnyLtr('<br>123<', true), isFalse); | |
48 }); | |
49 | |
50 test('hasAnyRtl', () { | |
51 expect(hasAnyRtl(''), isFalse); | |
52 expect(hasAnyRtl('abc'), isFalse); | |
53 expect(hasAnyRtl('ab\u05e0c'), isTrue); | |
54 expect(hasAnyRtl('123\t... \n'), isFalse); | |
55 expect(hasAnyRtl('<input value=\u05e0>123', false), isTrue); | |
56 expect(hasAnyRtl('<input value=\u05e0>123', true), isFalse); | |
57 }); | |
58 | |
59 | |
60 test('endsWithLtr', () { | |
61 expect(endsWithLtr('a'), isTrue); | |
62 expect(endsWithLtr('abc'), isTrue); | |
63 expect(endsWithLtr('a (!)'), isTrue); | |
64 expect(endsWithLtr('a.1'), isTrue); | |
65 expect(endsWithLtr('http://www.google.com '), isTrue); | |
66 expect(endsWithLtr('\u05e0a'), isTrue); | |
67 expect(endsWithLtr(' \u05e0\u05e1a\u05e2\u05e3 a (!)'), isTrue); | |
68 expect(endsWithLtr(''), isFalse); | |
69 expect(endsWithLtr(' '), isFalse); | |
70 expect(endsWithLtr('1'), isFalse); | |
71 expect(endsWithLtr('\u05e0'), isFalse); | |
72 expect(endsWithLtr('\u05e0 1(!)'), isFalse); | |
73 expect(endsWithLtr('a\u05e0'), isFalse); | |
74 expect(endsWithLtr('a abc\u05e0\u05e1def\u05e2. 1'), isFalse); | |
75 expect(endsWithLtr(' \u05e0\u05e1a\u05e2 <', true), isFalse); | |
76 expect(endsWithLtr(' \u05e0\u05e1a\u05e2 <', false), isTrue); | |
77 }); | |
78 | |
79 test('endsWithRtl', () { | |
80 expect(endsWithRtl('\u05e0'), isTrue); | |
81 expect(endsWithRtl('\u05e0\u05e1\u05e2'), isTrue); | |
82 expect(endsWithRtl('\u05e0 (!)'), isTrue); | |
83 expect(endsWithRtl('\u05e0.1'), isTrue); | |
84 expect(endsWithRtl('http://www.google.com/\u05e0 '), isTrue); | |
85 expect(endsWithRtl('a\u05e0'), isTrue); | |
86 expect(endsWithRtl(' a abc\u05e0def\u05e3. 1'), isTrue); | |
87 expect(endsWithRtl(''), isFalse); | |
88 expect(endsWithRtl(' '), isFalse); | |
89 expect(endsWithRtl('1'), isFalse); | |
90 expect(endsWithRtl('a'), isFalse); | |
91 expect(endsWithRtl('a 1(!)'), isFalse); | |
92 expect(endsWithRtl('\u05e0a'), isFalse); | |
93 expect(endsWithRtl('\u05e0 \u05e0\u05e1ab\u05e2 a (!)'), isFalse); | |
94 expect(endsWithRtl(' \u05e0\u05e1a\u05e2 <', true), isTrue); | |
95 expect(endsWithRtl(' \u05e0\u05e1a\u05e2 <', false), isFalse); | |
96 }); | |
97 | |
98 test('guardBracketInHtml', () { | |
99 var strWithRtl = "asc \u05d0 (\u05d0\u05d0\u05d0)"; | |
100 expect(guardBracketInHtml(strWithRtl), | |
101 equals("asc \u05d0 <span dir=rtl>(\u05d0\u05d0\u05d0)</span>")); | |
102 expect(guardBracketInHtml(strWithRtl, true), | |
103 equals("asc \u05d0 <span dir=rtl>(\u05d0\u05d0\u05d0)</span>")); | |
104 expect(guardBracketInHtml(strWithRtl, false), | |
105 equals("asc \u05d0 <span dir=ltr>(\u05d0\u05d0\u05d0)</span>")); | |
106 | |
107 var strWithRtl2 = "\u05d0 a (asc:))"; | |
108 expect(guardBracketInHtml(strWithRtl2), | |
109 equals("\u05d0 a <span dir=rtl>(asc:))</span>")); | |
110 expect(guardBracketInHtml(strWithRtl2, true), | |
111 equals("\u05d0 a <span dir=rtl>(asc:))</span>")); | |
112 expect(guardBracketInHtml(strWithRtl2, false), | |
113 equals("\u05d0 a <span dir=ltr>(asc:))</span>")); | |
114 | |
115 var strWithoutRtl = "a (asc) {{123}}"; | |
116 expect(guardBracketInHtml(strWithoutRtl), | |
117 equals("a <span dir=ltr>(asc)</span> <span dir=ltr>{{123}}</span>")); | |
118 expect(guardBracketInHtml(strWithoutRtl, true), | |
119 equals("a <span dir=rtl>(asc)</span> <span dir=rtl>{{123}}</span>")); | |
120 expect(guardBracketInHtml(strWithoutRtl, false), | |
121 equals("a <span dir=ltr>(asc)</span> <span dir=ltr>{{123}}</span>")); | |
122 | |
123 }); | |
124 | |
125 test('guardBracketInText', () { | |
126 var strWithRtl = "asc \u05d0 (\u05d0\u05d0\u05d0)"; | |
127 expect(guardBracketInText(strWithRtl), | |
128 equals("asc \u05d0 \u200f(\u05d0\u05d0\u05d0)\u200f")); | |
129 expect(guardBracketInText(strWithRtl, true), | |
130 equals("asc \u05d0 \u200f(\u05d0\u05d0\u05d0)\u200f")); | |
131 expect(guardBracketInText(strWithRtl, false), | |
132 equals("asc \u05d0 \u200e(\u05d0\u05d0\u05d0)\u200e")); | |
133 | |
134 var strWithRtl2 = "\u05d0 a (asc:))"; | |
135 expect(guardBracketInText(strWithRtl2), | |
136 equals("\u05d0 a \u200f(asc:))\u200f")); | |
137 expect(guardBracketInText(strWithRtl2, true), | |
138 equals("\u05d0 a \u200f(asc:))\u200f")); | |
139 expect(guardBracketInText(strWithRtl2, false), | |
140 equals("\u05d0 a \u200e(asc:))\u200e")); | |
141 | |
142 var strWithoutRtl = "a (asc) {{123}}"; | |
143 expect(guardBracketInText(strWithoutRtl), | |
144 equals("a \u200e(asc)\u200e \u200e{{123}}\u200e")); | |
145 expect(guardBracketInText(strWithoutRtl, true), | |
146 equals("a \u200f(asc)\u200f \u200f{{123}}\u200f")); | |
147 expect(guardBracketInText(strWithoutRtl, false), | |
148 equals("a \u200e(asc)\u200e \u200e{{123}}\u200e")); | |
149 | |
150 }); | |
151 | |
152 test('enforceRtlInHtml', () { | |
153 var str = '<div> first <br> second </div>'; | |
154 expect(enforceRtlInHtml(str), | |
155 equals('<div dir=rtl> first <br> second </div>')); | |
156 str = 'first second'; | |
157 expect(enforceRtlInHtml(str), | |
158 equals('\n<span dir=rtl>first second</span>')); | |
159 }); | |
160 | |
161 test('enforceRtlInText', () { | |
162 var str = 'first second'; | |
163 expect(enforceRtlInText(str), equals('${RLE}first second$PDF')); | |
164 }); | |
165 | |
166 test('enforceLtrInHtml', () { | |
167 var str = '<div> first <br> second </div>'; | |
168 expect(enforceLtrInHtml(str), | |
169 equals('<div dir=ltr> first <br> second </div>')); | |
170 str = 'first second'; | |
171 expect(enforceLtrInHtml(str), | |
172 equals('\n<span dir=ltr>first second</span>')); | |
173 }); | |
174 | |
175 test('enforceLtrInText', () { | |
176 var str = 'first second'; | |
177 expect(enforceLtrInText(str), equals('${LRE}first second$PDF')); | |
178 }); | |
179 | |
180 test('normalizeHebrewQuote', () { | |
181 expect(normalizeHebrewQuote('\u05d0"'), equals('\u05d0\u05f4')); | |
182 expect(normalizeHebrewQuote('\u05d0\''), equals('\u05d0\u05f3')); | |
183 expect(normalizeHebrewQuote('\u05d0"\u05d0\''), | |
184 equals('\u05d0\u05f4\u05d0\u05f3')); | |
185 }); | |
186 | |
187 test('estimateDirectionOfText', () { | |
188 expect(estimateDirectionOfText('', false).value, | |
189 equals(TextDirection.UNKNOWN.value)); | |
190 expect(estimateDirectionOfText(' ', false).value, | |
191 equals(TextDirection.UNKNOWN.value)); | |
192 expect(estimateDirectionOfText('! (...)', false).value, | |
193 equals(TextDirection.UNKNOWN.value)); | |
194 expect(estimateDirectionOfText('All-Ascii content', false).value, | |
195 equals(TextDirection.LTR.value)); | |
196 expect(estimateDirectionOfText('-17.0%', false).value, | |
197 equals(TextDirection.LTR.value)); | |
198 expect(estimateDirectionOfText('http://foo/bar/', false).value, | |
199 equals(TextDirection.LTR.value)); | |
200 expect(estimateDirectionOfText( | |
201 'http://foo/bar/?s=\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0' | |
202 '\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0' | |
203 '\u05d0\u05d0\u05d0\u05d0\u05d0').value, | |
204 equals(TextDirection.LTR.value)); | |
205 expect(estimateDirectionOfText('\u05d0', false).value, | |
206 equals(TextDirection.RTL.value)); | |
207 expect(estimateDirectionOfText( | |
208 '9 \u05d0 -> 17.5, 23, 45, 19', false).value, | |
209 equals(TextDirection.RTL.value)); | |
210 expect(estimateDirectionOfText( | |
211 'http://foo/bar/ \u05d0 http://foo2/bar2/ http://foo3/bar3/').value, | |
212 equals(TextDirection.RTL.value)); | |
213 expect(estimateDirectionOfText( | |
214 '\u05d0\u05d9\u05df \u05de\u05de\u05e9 \u05de\u05d4 \u05dc\u05e8\u05d0' | |
215 '\u05d5\u05ea: \u05dc\u05d0 \u05e6\u05d9\u05dc\u05de\u05ea\u05d9 \u05d4' | |
216 '\u05e8\u05d1\u05d4 \u05d5\u05d2\u05dd \u05d0\u05dd \u05d4\u05d9\u05d9' | |
217 '\u05ea\u05d9 \u05de\u05e6\u05dc\u05dd, \u05d4\u05d9\u05d4 \u05e9' | |
218 '\u05dd').value, | |
219 equals(TextDirection.RTL.value)); | |
220 expect(estimateDirectionOfText( | |
221 '\u05db\u05d0 - http://geek.co.il/gallery/v/2007-06 - \u05d0\u05d9' | |
222 '\u05df \u05de\u05de\u05e9 \u05de\u05d4 \u05dc\u05e8\u05d0\u05d5\u05ea:' | |
223 ' \u05dc\u05d0 \u05e6\u05d9\u05dc\u05de\u05ea\u05d9 \u05d4\u05e8\u05d1 ' | |
224 '\u05d5\u05d2\u05dd \u05d0\u05dd \u05d4\u05d9\u05d9\u05d9 \u05de\u05e6' | |
225 '\u05dc\u05dd, \u05d4\u05d9\u05d4 \u05e9\u05dd \u05d1\u05e2\u05d9\u05e7' | |
226 ' \u05d4\u05e8\u05d1\u05d4 \u05d0\u05e0\u05e9\u05d9\u05dd. \u05de\u05d4' | |
227 ' \u05e9\u05db\u05df - \u05d0\u05e4\u05e9\u05e8 \u05dc\u05e0\u05e6' | |
228 '\u05dc \u05d0\u05ea \u05d4\u05d4 \u05d3\u05d6\u05de\u05e0\u05d5 ' | |
229 '\u05dc\u05d4\u05e1\u05ea\u05db\u05dc \u05e2\u05dc \u05db\u05de\u05d4 ' | |
230 '\u05ea\u05de\u05d5\u05e0\u05d5\u05ea \u05de\u05e9\u05e9\u05e2\u05d5' | |
231 '\u05ea \u05d9\u05e9\u05e0\u05d5 \u05d9\u05d5\u05ea\u05e8 \u05e9\u05d9' | |
232 '\u05e9 \u05dc\u05d9 \u05d1\u05d0\u05ea\u05e8', false).value, | |
233 equals(TextDirection.RTL.value)); | |
234 expect(estimateDirectionOfText( | |
235 'CAPTCHA \u05de\u05e9\u05d5\u05db\u05dc\u05dc ' | |
236 '\u05de\u05d3\u05d9?').value, | |
237 equals(TextDirection.RTL.value)); | |
238 expect(estimateDirectionOfText( | |
239 'Yes Prime Minister \u05e2\u05d3\u05db\u05d5\u05df. \u05e9\u05d0\u05dc' | |
240 '\u05d5 \u05d0\u05d5\u05ea\u05d9 \u05de\u05d4 \u05d0\u05e0\u05d9 ' | |
241 '\u05e8\u05d5\u05e6\u05d4 \u05de\u05ea\u05e0\u05d4 \u05dc\u05d7' | |
242 '\u05d2').value, | |
243 equals(TextDirection.RTL.value)); | |
244 expect(estimateDirectionOfText( | |
245 '17.4.02 \u05e9\u05e2\u05d4:13-20 .15-00 .\u05dc\u05d0 \u05d4\u05d9' | |
246 '\u05d9\u05ea\u05d9 \u05db\u05d0\u05df.').value, | |
247 equals(TextDirection.RTL.value)); | |
248 expect(estimateDirectionOfText( | |
249 '5710 5720 5730. \u05d4\u05d3\u05dc\u05ea. \u05d4\u05e0\u05e9\u05d9' | |
250 '\u05e7\u05d4', false).value, | |
251 equals(TextDirection.RTL.value)); | |
252 expect(estimateDirectionOfText( | |
253 '\u05d4\u05d3\u05dc\u05ea http://www.google.com ' | |
254 'http://www.gmail.com').value, | |
255 equals(TextDirection.RTL.value)); | |
256 expect(estimateDirectionOfText( | |
257 '\u05d4\u05d3\u05dc <some quite nasty html mark up>').value, | |
258 equals(TextDirection.LTR.value)); | |
259 expect(estimateDirectionOfText( | |
260 '\u05d4\u05d3\u05dc <some quite nasty html mark up>').value, | |
261 equals(TextDirection.LTR.value)); | |
262 expect(estimateDirectionOfText( | |
263 '\u05d4\u05d3\u05dc\u05ea & < >').value, | |
264 equals(TextDirection.LTR.value)); | |
265 expect(estimateDirectionOfText( | |
266 '\u05d4\u05d3\u05dc\u05ea & < >', true).value, | |
267 equals(TextDirection.RTL.value)); | |
268 }); | |
269 | |
270 test('detectRtlDirectionality', () { | |
271 var bidiText = []; | |
272 var item = new SampleItem('Pure Ascii content'); | |
273 bidiText.add(item); | |
274 | |
275 item = new SampleItem('\u05d0\u05d9\u05df \u05de\u05de\u05e9 \u05de\u05d4' | |
276 ' \u05dc\u05e8\u05d0\u05d5\u05ea: \u05dc\u05d0 \u05e6\u05d9\u05dc' | |
277 '\u05de\u05ea\u05d9 \u05d4\u05e8\u05d1\u05d4 \u05d5\u05d2\u05dd ' | |
278 '\u05d0\u05dd \u05d4\u05d9\u05d9\u05ea\u05d9 \u05de\u05e6\u05dc\u05dd, ' | |
279 '\u05d4\u05d9\u05d4 \u05e9\u05dd', true); | |
280 bidiText.add(item); | |
281 | |
282 item = new SampleItem('\u05db\u05d0\u05df - http://geek.co.il/gallery/v/' | |
283 '2007-06 - \u05d0\u05d9\u05df \u05de\u05de\u05e9 \u05de\u05d4 \u05dc' | |
284 '\u05e8\u05d0\u05d5\u05ea: \u05dc\u05d0 \u05e6\u05d9\u05dc\u05de\u05ea' | |
285 '\u05d9 \u05d4\u05e8\u05d1\u05d4 \u05d5\u05d2\u05dd \u05d0\u05dd \u05d4' | |
286 '\u05d9\u05d9\u05ea\u05d9 \u05de\u05e6\u05dc\u05dd, \u05d4\u05d9\u05d4 ' | |
287 '\u05e9\u05dd \u05d1\u05e2\u05d9\u05e7\u05e8 \u05d4\u05e8\u05d1\u05d4 ' | |
288 '\u05d0\u05e0\u05e9\u05d9\u05dd. \u05de\u05d4 \u05e9\u05db\u05df - ' | |
289 '\u05d0\u05e4\u05e9\u05e8 \u05dc\u05e0\u05e6\u05dc \u05d0\u05ea \u05d4' | |
290 '\u05d4\u05d3\u05d6\u05de\u05e0\u05d5\u05ea \u05dc\u05d4\u05e1\u05ea' | |
291 '\u05db\u05dc \u05e2\u05dc \u05db\u05de\u05d4 \u05ea\u05de\u05d5\u05e0' | |
292 '\u05d5\u05ea \u05de\u05e9\u05e2\u05e9\u05e2\u05d5\u05ea \u05d9\u05e9' | |
293 '\u05e0\u05d5\u05ea \u05d9\u05d5\u05ea\u05e8 \u05e9\u05d9\u05e9 \u05dc' | |
294 '\u05d9 \u05d1\u05d0\u05ea\u05e8', true); | |
295 bidiText.add(item); | |
296 | |
297 item = new SampleItem('CAPTCHA \u05de\u05e9\u05d5\u05db\u05dc\u05dc ' | |
298 '\u05de\u05d3\u05d9?', true); | |
299 bidiText.add(item); | |
300 | |
301 | |
302 item = new SampleItem('Yes Prime Minister \u05e2\u05d3\u05db\u05d5\u05df. ' | |
303 '\u05e9\u05d0\u05dc\u05d5 \u05d0\u05d5\u05ea\u05d9 \u05de\u05d4 \u05d0' | |
304 '\u05e0\u05d9 \u05e8\u05d5\u05e6\u05d4 \u05de\u05ea\u05e0\u05d4 ' | |
305 '\u05dc\u05d7\u05d2', true); | |
306 bidiText.add(item); | |
307 | |
308 item = new SampleItem('17.4.02 \u05e9\u05e2\u05d4:13-20 .15-00 .\u05dc' | |
309 '\u05d0 \u05d4\u05d9\u05d9\u05ea\u05d9 \u05db\u05d0\u05df.', true); | |
310 bidiText.add(item); | |
311 | |
312 item = new SampleItem('5710 5720 5730. \u05d4\u05d3\u05dc\u05ea. \u05d4' | |
313 '\u05e0\u05e9\u05d9\u05e7\u05d4', true); | |
314 bidiText.add(item); | |
315 | |
316 item = new SampleItem('\u05d4\u05d3\u05dc\u05ea http://www.google.com ' | |
317 'http://www.gmail.com', true); | |
318 bidiText.add(item); | |
319 | |
320 item = new SampleItem('>\u05d4<', true, true); | |
321 bidiText.add(item); | |
322 | |
323 item = new SampleItem('>\u05d4<', false); | |
324 bidiText.add(item); | |
325 | |
326 for (var i = 0; i < bidiText.length; i++) { | |
327 var isRtlDir = detectRtlDirectionality(bidiText[i].text, | |
328 bidiText[i].isHtml); | |
329 if (isRtlDir != bidiText[i].isRtl) { | |
330 var str = '"${bidiText[i].text} " should be ' | |
331 '${bidiText[i].isRtl ? "rtl" : "ltr"} but detected as ' | |
332 '${isRtlDir ? "rtl" : "ltr"}'; | |
333 //alert(str); | |
334 } | |
335 expect(bidiText[i].isRtl, isRtlDir); | |
336 } | |
337 }); | |
338 } | |
339 | |
340 class SampleItem { | |
341 String text; | |
342 bool isRtl; | |
343 bool isHtml; | |
344 SampleItem([someText='', someIsRtl=false, isHtml=false]) : | |
345 this.text=someText, this.isRtl=someIsRtl, this.isHtml=isHtml; | |
346 } | |
OLD | NEW |