bidi_test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. goog.provide('goog.i18n.bidiTest');
  15. goog.setTestOnly('goog.i18n.bidiTest');
  16. goog.require('goog.i18n.bidi');
  17. goog.require('goog.i18n.bidi.Dir');
  18. goog.require('goog.testing.jsunit');
  19. var LRE = '\u202A';
  20. var RLE = '\u202B';
  21. var PDF = '\u202C';
  22. var LRM = '\u200E';
  23. var RLM = '\u200F';
  24. function testToDir() {
  25. assertEquals(null, goog.i18n.bidi.toDir(null));
  26. assertEquals(null, goog.i18n.bidi.toDir(null, true));
  27. assertEquals(
  28. goog.i18n.bidi.Dir.NEUTRAL,
  29. goog.i18n.bidi.toDir(goog.i18n.bidi.Dir.NEUTRAL));
  30. assertEquals(null, goog.i18n.bidi.toDir(0, true));
  31. assertEquals(
  32. goog.i18n.bidi.Dir.LTR, goog.i18n.bidi.toDir(goog.i18n.bidi.Dir.LTR));
  33. assertEquals(
  34. goog.i18n.bidi.Dir.LTR,
  35. goog.i18n.bidi.toDir(goog.i18n.bidi.Dir.LTR, true));
  36. assertEquals(goog.i18n.bidi.Dir.LTR, goog.i18n.bidi.toDir(100));
  37. assertEquals(goog.i18n.bidi.Dir.LTR, goog.i18n.bidi.toDir(100, true));
  38. assertEquals(goog.i18n.bidi.Dir.LTR, goog.i18n.bidi.toDir(false));
  39. assertEquals(goog.i18n.bidi.Dir.LTR, goog.i18n.bidi.toDir(false, true));
  40. assertEquals(
  41. goog.i18n.bidi.Dir.RTL, goog.i18n.bidi.toDir(goog.i18n.bidi.Dir.RTL));
  42. assertEquals(
  43. goog.i18n.bidi.Dir.RTL,
  44. goog.i18n.bidi.toDir(goog.i18n.bidi.Dir.RTL, true));
  45. assertEquals(goog.i18n.bidi.Dir.RTL, goog.i18n.bidi.toDir(-100));
  46. assertEquals(goog.i18n.bidi.Dir.RTL, goog.i18n.bidi.toDir(-100, true));
  47. assertEquals(goog.i18n.bidi.Dir.RTL, goog.i18n.bidi.toDir(true));
  48. assertEquals(goog.i18n.bidi.Dir.RTL, goog.i18n.bidi.toDir(true, true));
  49. }
  50. function testIsRtlLang() {
  51. assert(!goog.i18n.bidi.isRtlLanguage('en'));
  52. assert(!goog.i18n.bidi.isRtlLanguage('fr'));
  53. assert(!goog.i18n.bidi.isRtlLanguage('zh-CN'));
  54. assert(!goog.i18n.bidi.isRtlLanguage('fil'));
  55. assert(!goog.i18n.bidi.isRtlLanguage('az'));
  56. assert(!goog.i18n.bidi.isRtlLanguage('iw-Latn'));
  57. assert(!goog.i18n.bidi.isRtlLanguage('iw-LATN'));
  58. assert(!goog.i18n.bidi.isRtlLanguage('iw_latn'));
  59. assert(goog.i18n.bidi.isRtlLanguage('ar'));
  60. assert(goog.i18n.bidi.isRtlLanguage('AR'));
  61. assert(goog.i18n.bidi.isRtlLanguage('iw'));
  62. assert(goog.i18n.bidi.isRtlLanguage('he'));
  63. assert(goog.i18n.bidi.isRtlLanguage('fa'));
  64. assert(goog.i18n.bidi.isRtlLanguage('ckb'));
  65. assert(goog.i18n.bidi.isRtlLanguage('ar-EG'));
  66. assert(goog.i18n.bidi.isRtlLanguage('az-Arab'));
  67. assert(goog.i18n.bidi.isRtlLanguage('az-ARAB-IR'));
  68. assert(goog.i18n.bidi.isRtlLanguage('az_arab_IR'));
  69. }
  70. function testIsLtrChar() {
  71. assert(goog.i18n.bidi.isLtrChar('a'));
  72. assert(!goog.i18n.bidi.isLtrChar('\u05e0'));
  73. var str = 'a\u05e0z';
  74. assert(goog.i18n.bidi.isLtrChar(str.charAt(0)));
  75. assert(!goog.i18n.bidi.isLtrChar(str.charAt(1)));
  76. assert(goog.i18n.bidi.isLtrChar(str.charAt(2)));
  77. }
  78. function testIsRtlChar() {
  79. assert(!goog.i18n.bidi.isRtlChar('a'));
  80. assert(goog.i18n.bidi.isRtlChar('\u05e0'));
  81. var str = 'a\u05e0z';
  82. assert(!goog.i18n.bidi.isRtlChar(str.charAt(0)));
  83. assert(goog.i18n.bidi.isRtlChar(str.charAt(1)));
  84. assert(!goog.i18n.bidi.isRtlChar(str.charAt(2)));
  85. }
  86. function testIsNeutralChar() {
  87. assert(goog.i18n.bidi.isNeutralChar('\u0000'));
  88. assert(goog.i18n.bidi.isNeutralChar('\u0020'));
  89. assert(!goog.i18n.bidi.isNeutralChar('a'));
  90. assert(goog.i18n.bidi.isNeutralChar('!'));
  91. assert(goog.i18n.bidi.isNeutralChar('@'));
  92. assert(goog.i18n.bidi.isNeutralChar('['));
  93. assert(goog.i18n.bidi.isNeutralChar('`'));
  94. assert(goog.i18n.bidi.isNeutralChar('0'));
  95. assert(!goog.i18n.bidi.isNeutralChar('\u05e0'));
  96. }
  97. function testIsNeutralText() {
  98. assert(goog.i18n.bidi.isNeutralText('123'));
  99. assert(!goog.i18n.bidi.isNeutralText('abc'));
  100. assert(goog.i18n.bidi.isNeutralText('http://abc'));
  101. assert(goog.i18n.bidi.isNeutralText(' 123-()'));
  102. assert(!goog.i18n.bidi.isNeutralText('123a456'));
  103. assert(!goog.i18n.bidi.isNeutralText('123\u05e0456'));
  104. assert(!goog.i18n.bidi.isNeutralText('<input value=\u05e0>123&lt;', false));
  105. assert(goog.i18n.bidi.isNeutralText('<input value=\u05e0>123&lt;', true));
  106. }
  107. function testHasAnyLtr() {
  108. assert(!goog.i18n.bidi.hasAnyLtr(''));
  109. assert(!goog.i18n.bidi.hasAnyLtr('\u05e0\u05e1\u05e2'));
  110. assert(goog.i18n.bidi.hasAnyLtr('\u05e0\u05e1z\u05e2'));
  111. assert(!goog.i18n.bidi.hasAnyLtr('123\t... \n'));
  112. assert(goog.i18n.bidi.hasAnyLtr('<br>123&lt;', false));
  113. assert(!goog.i18n.bidi.hasAnyLtr('<br>123&lt;', true));
  114. }
  115. function testHasAnyRtl() {
  116. assert(!goog.i18n.bidi.hasAnyRtl(''));
  117. assert(!goog.i18n.bidi.hasAnyRtl('abc'));
  118. assert(goog.i18n.bidi.hasAnyRtl('ab\u05e0c'));
  119. assert(!goog.i18n.bidi.hasAnyRtl('123\t... \n'));
  120. assert(goog.i18n.bidi.hasAnyRtl('<input value=\u05e0>123', false));
  121. assert(!goog.i18n.bidi.hasAnyRtl('<input value=\u05e0>123', true));
  122. }
  123. function testEndsWithLtr() {
  124. assert(goog.i18n.bidi.endsWithLtr('a'));
  125. assert(goog.i18n.bidi.endsWithLtr('abc'));
  126. assert(goog.i18n.bidi.endsWithLtr('a (!)'));
  127. assert(goog.i18n.bidi.endsWithLtr('a.1'));
  128. assert(goog.i18n.bidi.endsWithLtr('http://www.google.com '));
  129. assert(goog.i18n.bidi.endsWithLtr('\u05e0a'));
  130. assert(goog.i18n.bidi.endsWithLtr(' \u05e0\u05e1a\u05e2\u05e3 a (!)'));
  131. assert(goog.i18n.bidi.endsWithLtr('\u202b\u05d0!\u202c\u200e'));
  132. assert(!goog.i18n.bidi.endsWithLtr(''));
  133. assert(!goog.i18n.bidi.endsWithLtr(' '));
  134. assert(!goog.i18n.bidi.endsWithLtr('1'));
  135. assert(!goog.i18n.bidi.endsWithLtr('\u05e0'));
  136. assert(!goog.i18n.bidi.endsWithLtr('\u05e0 1(!)'));
  137. assert(!goog.i18n.bidi.endsWithLtr('a\u05e0'));
  138. assert(!goog.i18n.bidi.endsWithLtr('a abc\u05e0\u05e1def\u05e2. 1'));
  139. assert(!goog.i18n.bidi.endsWithLtr('\u200f\u202eArtielish\u202c\u200f'));
  140. assert(!goog.i18n.bidi.endsWithLtr(' \u05e0\u05e1a\u05e2 &lt;', true));
  141. assert(goog.i18n.bidi.endsWithLtr(' \u05e0\u05e1a\u05e2 &lt;', false));
  142. }
  143. function testEndsWithRtl() {
  144. assert(goog.i18n.bidi.endsWithRtl('\u05e0'));
  145. assert(goog.i18n.bidi.endsWithRtl('\u05e0\u05e1\u05e2'));
  146. assert(goog.i18n.bidi.endsWithRtl('\u05e0 (!)'));
  147. assert(goog.i18n.bidi.endsWithRtl('\u05e0.1'));
  148. assert(goog.i18n.bidi.endsWithRtl('http://www.google.com/\u05e0 '));
  149. assert(goog.i18n.bidi.endsWithRtl('a\u05e0'));
  150. assert(goog.i18n.bidi.endsWithRtl(' a abc\u05e0def\u05e3. 1'));
  151. assert(goog.i18n.bidi.endsWithRtl('\u200f\u202eArtielish\u202c\u200f'));
  152. assert(!goog.i18n.bidi.endsWithRtl(''));
  153. assert(!goog.i18n.bidi.endsWithRtl(' '));
  154. assert(!goog.i18n.bidi.endsWithRtl('1'));
  155. assert(!goog.i18n.bidi.endsWithRtl('a'));
  156. assert(!goog.i18n.bidi.endsWithRtl('a 1(!)'));
  157. assert(!goog.i18n.bidi.endsWithRtl('\u05e0a'));
  158. assert(!goog.i18n.bidi.endsWithRtl('\u202b\u05d0!\u202c\u200e'));
  159. assert(!goog.i18n.bidi.endsWithRtl('\u05e0 \u05e0\u05e1ab\u05e2 a (!)'));
  160. assert(goog.i18n.bidi.endsWithRtl(' \u05e0\u05e1a\u05e2 &lt;', true));
  161. assert(!goog.i18n.bidi.endsWithRtl(' \u05e0\u05e1a\u05e2 &lt;', false));
  162. }
  163. function testGuardBracketInText() {
  164. var strWithRtl = 'asc \u05d0 (\u05d0\u05d0\u05d0)';
  165. assertEquals(
  166. 'asc \u05d0 \u200f(\u05d0\u05d0\u05d0)\u200f',
  167. goog.i18n.bidi.guardBracketInText(strWithRtl));
  168. assertEquals(
  169. 'asc \u05d0 \u200f(\u05d0\u05d0\u05d0)\u200f',
  170. goog.i18n.bidi.guardBracketInText(strWithRtl, true));
  171. assertEquals(
  172. 'asc \u05d0 \u200e(\u05d0\u05d0\u05d0)\u200e',
  173. goog.i18n.bidi.guardBracketInText(strWithRtl, false));
  174. var strWithRtl2 = '\u05d0 a (asc:))';
  175. assertEquals(
  176. '\u05d0 a \u200f(asc:))\u200f',
  177. goog.i18n.bidi.guardBracketInText(strWithRtl2));
  178. assertEquals(
  179. '\u05d0 a \u200f(asc:))\u200f',
  180. goog.i18n.bidi.guardBracketInText(strWithRtl2, true));
  181. assertEquals(
  182. '\u05d0 a \u200e(asc:))\u200e',
  183. goog.i18n.bidi.guardBracketInText(strWithRtl2, false));
  184. var strWithoutRtl = 'a (asc) {{123}}';
  185. assertEquals(
  186. 'a \u200e(asc)\u200e \u200e{{123}}\u200e',
  187. goog.i18n.bidi.guardBracketInText(strWithoutRtl));
  188. assertEquals(
  189. 'a \u200f(asc)\u200f \u200f{{123}}\u200f',
  190. goog.i18n.bidi.guardBracketInText(strWithoutRtl, true));
  191. assertEquals(
  192. 'a \u200e(asc)\u200e \u200e{{123}}\u200e',
  193. goog.i18n.bidi.guardBracketInText(strWithoutRtl, false));
  194. }
  195. function testEnforceRtlInHtml() {
  196. var str = '<div> first <br> second </div>';
  197. assertEquals(
  198. '<div dir=rtl> first <br> second </div>',
  199. goog.i18n.bidi.enforceRtlInHtml(str));
  200. str = 'first second';
  201. assertEquals(
  202. '\n<span dir=rtl>first second</span>',
  203. goog.i18n.bidi.enforceRtlInHtml(str));
  204. }
  205. function testEnforceRtlInText() {
  206. var str = 'first second';
  207. assertEquals(
  208. RLE + 'first second' + PDF, goog.i18n.bidi.enforceRtlInText(str));
  209. }
  210. function testEnforceLtrInHtml() {
  211. var str = '<div> first <br> second </div>';
  212. assertEquals(
  213. '<div dir=ltr> first <br> second </div>',
  214. goog.i18n.bidi.enforceLtrInHtml(str));
  215. str = 'first second';
  216. assertEquals(
  217. '\n<span dir=ltr>first second</span>',
  218. goog.i18n.bidi.enforceLtrInHtml(str));
  219. }
  220. function testEnforceLtrInText() {
  221. var str = 'first second';
  222. assertEquals(
  223. LRE + 'first second' + PDF, goog.i18n.bidi.enforceLtrInText(str));
  224. }
  225. function testNormalizeHebrewQuote() {
  226. assertEquals('\u05d0\u05f4', goog.i18n.bidi.normalizeHebrewQuote('\u05d0"'));
  227. assertEquals('\u05d0\u05f3', goog.i18n.bidi.normalizeHebrewQuote('\u05d0\''));
  228. assertEquals(
  229. '\u05d0\u05f4\u05d0\u05f3',
  230. goog.i18n.bidi.normalizeHebrewQuote('\u05d0"\u05d0\''));
  231. }
  232. function testMirrorCSS() {
  233. var str = 'left:10px;right:20px';
  234. assertEquals('right:10px;left:20px', goog.i18n.bidi.mirrorCSS(str));
  235. str = 'border:10px 20px 30px 40px';
  236. assertEquals('border:10px 40px 30px 20px', goog.i18n.bidi.mirrorCSS(str));
  237. }
  238. function testEstimateDirection() {
  239. assertEquals(
  240. goog.i18n.bidi.Dir.NEUTRAL, goog.i18n.bidi.estimateDirection('', false));
  241. assertEquals(
  242. goog.i18n.bidi.Dir.NEUTRAL, goog.i18n.bidi.estimateDirection(' ', false));
  243. assertEquals(
  244. goog.i18n.bidi.Dir.NEUTRAL,
  245. goog.i18n.bidi.estimateDirection('! (...)', false));
  246. assertEquals(
  247. goog.i18n.bidi.Dir.LTR,
  248. goog.i18n.bidi.estimateDirection('All-Ascii content', false));
  249. assertEquals(
  250. goog.i18n.bidi.Dir.LTR,
  251. goog.i18n.bidi.estimateDirection('-17.0%', false));
  252. assertEquals(
  253. 'Farsi digits should count as weakly LTR', goog.i18n.bidi.Dir.LTR,
  254. goog.i18n.bidi.estimateDirection('\u06f0', false));
  255. assertEquals(
  256. 'Farsi digits should count as weakly LTR', goog.i18n.bidi.Dir.LTR,
  257. goog.i18n.bidi.estimateDirection('\u06f9', false));
  258. assertEquals(
  259. goog.i18n.bidi.Dir.LTR,
  260. goog.i18n.bidi.estimateDirection('http://foo/bar/', false));
  261. assertEquals(
  262. goog.i18n.bidi.Dir.LTR,
  263. goog.i18n.bidi.estimateDirection(
  264. 'http://foo/bar/?s=\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0' +
  265. '\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0' +
  266. '\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0\u05d0',
  267. false));
  268. assertEquals(
  269. goog.i18n.bidi.Dir.RTL,
  270. goog.i18n.bidi.estimateDirection('\u05d0', false));
  271. assertEquals(
  272. goog.i18n.bidi.Dir.RTL,
  273. goog.i18n.bidi.estimateDirection('9 \u05d0 -> 17.5, 23, 45, 19', false));
  274. assertEquals(
  275. 'Native arabic numbers should count as RTL', goog.i18n.bidi.Dir.RTL,
  276. goog.i18n.bidi.estimateDirection('\u0660', false));
  277. assertEquals(
  278. 'Both Farsi letters and digits should count as RTL',
  279. goog.i18n.bidi.Dir.RTL,
  280. goog.i18n.bidi.estimateDirection('\u06CC \u06F1 \u06F2\u06F3', false));
  281. assertEquals(
  282. goog.i18n.bidi.Dir.RTL, goog.i18n.bidi.estimateDirection(
  283. 'http://foo/bar/ \u05d0 http://foo2/bar2/ ' +
  284. 'http://foo3/bar3/',
  285. false));
  286. assertEquals(
  287. goog.i18n.bidi.Dir.RTL,
  288. goog.i18n.bidi.estimateDirection(
  289. '\u05d0\u05d9\u05df \u05de\u05de\u05e9 ' +
  290. '\u05de\u05d4 \u05dc\u05e8\u05d0\u05d5\u05ea: ' +
  291. '\u05dc\u05d0 \u05e6\u05d9\u05dc\u05de\u05ea\u05d9 ' +
  292. '\u05d4\u05e8\u05d1\u05d4 \u05d5\u05d2\u05dd \u05d0' +
  293. '\u05dd \u05d4\u05d9\u05d9\u05ea\u05d9 \u05de\u05e6' +
  294. '\u05dc\u05dd, \u05d4\u05d9\u05d4 \u05e9\u05dd',
  295. false));
  296. assertEquals(
  297. goog.i18n.bidi.Dir.RTL,
  298. goog.i18n.bidi.estimateDirection(
  299. '\u05db\u05d0 - http://geek.co.il/gallery/v/2007-06' +
  300. ' - \u05d0\u05d9\u05df \u05de\u05de\u05e9 \u05de\u05d4 ' +
  301. '\u05dc\u05e8\u05d0\u05d5\u05ea: \u05dc\u05d0 \u05e6' +
  302. '\u05d9\u05dc\u05de\u05ea\u05d9 \u05d4\u05e8\u05d1 ' +
  303. '\u05d5\u05d2\u05dd \u05d0\u05dd \u05d4\u05d9\u05d9' +
  304. '\u05d9 \u05de\u05e6\u05dc\u05dd, \u05d4\u05d9\u05d4 ' +
  305. '\u05e9\u05dd \u05d1\u05e2\u05d9\u05e7 \u05d4\u05e8' +
  306. '\u05d1\u05d4 \u05d0\u05e0\u05e9\u05d9\u05dd. \u05de' +
  307. '\u05d4 \u05e9\u05db\u05df - \u05d0\u05e4\u05e9\u05e8 ' +
  308. '\u05dc\u05e0\u05e6\u05dc \u05d0\u05ea \u05d4\u05d4 ' +
  309. '\u05d3\u05d6\u05de\u05e0\u05d5 \u05dc\u05d4\u05e1' +
  310. '\u05ea\u05db\u05dc \u05e2\u05dc \u05db\u05de\u05d4 ' +
  311. '\u05ea\u05de\u05d5\u05e0\u05d5\u05ea \u05de\u05e9' +
  312. '\u05e9\u05e2\u05d5\u05ea \u05d9\u05e9\u05e0\u05d5 ' +
  313. '\u05d9\u05d5\u05ea\u05e8 \u05e9\u05d9\u05e9 \u05dc' +
  314. '\u05d9 \u05d1\u05d0\u05ea\u05e8',
  315. false));
  316. assertEquals(
  317. goog.i18n.bidi.Dir.RTL,
  318. goog.i18n.bidi.estimateDirection(
  319. 'CAPTCHA \u05de\u05e9\u05d5\u05db\u05dc\u05dc ' +
  320. '\u05de\u05d3\u05d9?',
  321. false));
  322. assertEquals(
  323. goog.i18n.bidi.Dir.RTL,
  324. goog.i18n.bidi.estimateDirection(
  325. 'Yes Prime Minister \u05e2\u05d3\u05db\u05d5\u05df. ' +
  326. '\u05e9\u05d0\u05dc\u05d5 \u05d0\u05d5\u05ea\u05d9 ' +
  327. '\u05de\u05d4 \u05d0\u05e0\u05d9 \u05e8\u05d5\u05e6' +
  328. '\u05d4 \u05de\u05ea\u05e0\u05d4 \u05dc\u05d7\u05d2',
  329. false));
  330. assertEquals(
  331. goog.i18n.bidi.Dir.RTL,
  332. goog.i18n.bidi.estimateDirection(
  333. '17.4.02 \u05e9\u05e2\u05d4:13-20 .15-00 .\u05dc\u05d0 ' +
  334. '\u05d4\u05d9\u05d9\u05ea\u05d9 \u05db\u05d0\u05df.',
  335. false));
  336. assertEquals(
  337. goog.i18n.bidi.Dir.RTL, goog.i18n.bidi.estimateDirection(
  338. '5710 5720 5730. \u05d4\u05d3\u05dc\u05ea. ' +
  339. '\u05d4\u05e0\u05e9\u05d9\u05e7\u05d4',
  340. false));
  341. assertEquals(
  342. goog.i18n.bidi.Dir.RTL,
  343. goog.i18n.bidi.estimateDirection(
  344. '\u05d4\u05d3\u05dc\u05ea http://www.google.com ' +
  345. 'http://www.gmail.com',
  346. false));
  347. assertEquals(
  348. goog.i18n.bidi.Dir.RTL,
  349. goog.i18n.bidi.estimateDirection('\u200f\u202eArtielish\u202c\u200f'));
  350. assertEquals(
  351. goog.i18n.bidi.Dir.LTR,
  352. goog.i18n.bidi.estimateDirection(
  353. '\u05d4\u05d3\u05dc <some quite nasty html mark up>', false));
  354. assertEquals(
  355. goog.i18n.bidi.Dir.RTL,
  356. goog.i18n.bidi.estimateDirection(
  357. '\u05d4\u05d3\u05dc <some quite nasty html mark up>', true));
  358. assertEquals(
  359. goog.i18n.bidi.Dir.LTR,
  360. goog.i18n.bidi.estimateDirection(
  361. '\u05d4\u05d3\u05dc\u05ea &amp; &lt; &gt;', false));
  362. assertEquals(
  363. goog.i18n.bidi.Dir.RTL,
  364. goog.i18n.bidi.estimateDirection(
  365. '\u05d4\u05d3\u05dc\u05ea &amp; &lt; &gt;', true));
  366. assertEquals(
  367. goog.i18n.bidi.Dir.LTR,
  368. goog.i18n.bidi.estimateDirection('foo/<b>\u05d0</b>', true));
  369. }
  370. function testDetectRtlDirectionality() {
  371. var bidiText = getBidiTextSamples();
  372. for (var i = 0; i < bidiText.length; i++) {
  373. // alert(bidiText[i].text);
  374. var is_rtl = goog.i18n.bidi.detectRtlDirectionality(
  375. bidiText[i].text, bidiText[i].isHtml);
  376. if (is_rtl != bidiText[i].isRtl) {
  377. var str = '"' + bidiText[i].text + '" should be ' +
  378. (bidiText[i].isRtl ? 'rtl' : 'ltr') + ' but detected as ' +
  379. (is_rtl ? 'rtl' : 'ltr');
  380. alert(str);
  381. }
  382. assertEquals(bidiText[i].isRtl, is_rtl);
  383. }
  384. }
  385. function testSetElementDirByTextDirectionality() {
  386. var el = document.createElement('DIV');
  387. var text = '';
  388. goog.i18n.bidi.setElementDirByTextDirectionality(el, text);
  389. assertEquals('Expected no/empty dir value for empty text.', '', el.dir);
  390. text = ' ';
  391. goog.i18n.bidi.setElementDirByTextDirectionality(el, text);
  392. assertEquals(
  393. 'Expected no/empty dir value for neutral text:"' + text + '"', '',
  394. el.dir);
  395. text = 'a';
  396. goog.i18n.bidi.setElementDirByTextDirectionality(el, text);
  397. assertEquals(
  398. 'Expected dir="ltr" value for LTR text:"' + text + '"', 'ltr', el.dir);
  399. text = '\u05d0';
  400. goog.i18n.bidi.setElementDirByTextDirectionality(el, text);
  401. assertEquals(
  402. 'Expected dir="rtl" value for RTL text:"' + text + '"', 'rtl', el.dir);
  403. }
  404. function SampleItem() {
  405. this.text = '';
  406. this.isRtl = false;
  407. this.isHtml = false;
  408. }
  409. function getBidiTextSamples() {
  410. var bidiText = [];
  411. var item = new SampleItem;
  412. item.text = 'Pure Ascii content';
  413. item.isRtl = false;
  414. bidiText.push(item);
  415. item = new SampleItem;
  416. item.text = '\u05d0\u05d9\u05df \u05de\u05de\u05e9 \u05de\u05d4 ' +
  417. '\u05dc\u05e8\u05d0\u05d5\u05ea: \u05dc\u05d0 ' +
  418. '\u05e6\u05d9\u05dc\u05de\u05ea\u05d9 \u05d4\u05e8\u05d1\u05d4 ' +
  419. '\u05d5\u05d2\u05dd \u05d0\u05dd \u05d4\u05d9\u05d9\u05ea\u05d9 ' +
  420. '\u05de\u05e6\u05dc\u05dd, \u05d4\u05d9\u05d4 \u05e9\u05dd';
  421. item.isRtl = true;
  422. bidiText.push(item);
  423. item = new SampleItem;
  424. item.text = '\u05db\u05d0\u05df - http://geek.co.il/gallery/v/2007-06 - ' +
  425. '\u05d0\u05d9\u05df \u05de\u05de\u05e9 \u05de\u05d4 ' +
  426. '\u05dc\u05e8\u05d0\u05d5\u05ea: ' +
  427. '\u05dc\u05d0 \u05e6\u05d9\u05dc\u05de\u05ea\u05d9 ' +
  428. '\u05d4\u05e8\u05d1\u05d4 \u05d5\u05d2\u05dd \u05d0\u05dd ' +
  429. '\u05d4\u05d9\u05d9\u05ea\u05d9 \u05de\u05e6\u05dc\u05dd, ' +
  430. '\u05d4\u05d9\u05d4 \u05e9\u05dd \u05d1\u05e2\u05d9\u05e7\u05e8 ' +
  431. '\u05d4\u05e8\u05d1\u05d4 \u05d0\u05e0\u05e9\u05d9\u05dd. ' +
  432. '\u05de\u05d4 \u05e9\u05db\u05df - \u05d0\u05e4\u05e9\u05e8 ' +
  433. '\u05dc\u05e0\u05e6\u05dc \u05d0\u05ea ' +
  434. '\u05d4\u05d4\u05d3\u05d6\u05de\u05e0\u05d5\u05ea ' +
  435. '\u05dc\u05d4\u05e1\u05ea\u05db\u05dc \u05e2\u05dc \u05db\u05de\u05d4 ' +
  436. '\u05ea\u05de\u05d5\u05e0\u05d5\u05ea ' +
  437. '\u05de\u05e9\u05e2\u05e9\u05e2\u05d5\u05ea ' +
  438. '\u05d9\u05e9\u05e0\u05d5\u05ea \u05d9\u05d5\u05ea\u05e8 ' +
  439. '\u05e9\u05d9\u05e9 \u05dc\u05d9 \u05d1\u05d0\u05ea\u05e8';
  440. item.isRtl = true;
  441. bidiText.push(item);
  442. item = new SampleItem;
  443. item.text =
  444. 'CAPTCHA \u05de\u05e9\u05d5\u05db\u05dc\u05dc \u05de\u05d3\u05d9?';
  445. item.isRtl = true;
  446. bidiText.push(item);
  447. item = new SampleItem;
  448. item.text = 'Yes Prime Minister \u05e2\u05d3\u05db\u05d5\u05df. ' +
  449. '\u05e9\u05d0\u05dc\u05d5 \u05d0\u05d5\u05ea\u05d9 \u05de\u05d4 ' +
  450. '\u05d0\u05e0\u05d9 \u05e8\u05d5\u05e6\u05d4 \u05de\u05ea\u05e0\u05d4 ' +
  451. '\u05dc\u05d7\u05d2';
  452. item.isRtl = true;
  453. bidiText.push(item);
  454. item = new SampleItem;
  455. item.text = '17.4.02 \u05e9\u05e2\u05d4:13-20 .15-00 .\u05dc\u05d0 ' +
  456. '\u05d4\u05d9\u05d9\u05ea\u05d9 \u05db\u05d0\u05df.';
  457. item.isRtl = true;
  458. bidiText.push(item);
  459. item = new SampleItem;
  460. item.text = '5710 5720 5730. \u05d4\u05d3\u05dc\u05ea. ' +
  461. '\u05d4\u05e0\u05e9\u05d9\u05e7\u05d4';
  462. item.isRtl = true;
  463. bidiText.push(item);
  464. item = new SampleItem;
  465. item.text =
  466. '\u05d4\u05d3\u05dc\u05ea http://www.google.com http://www.gmail.com';
  467. item.isRtl = true;
  468. bidiText.push(item);
  469. item = new SampleItem;
  470. item.text = '&gt;\u05d4&lt;';
  471. item.isHtml = true;
  472. item.isRtl = true;
  473. bidiText.push(item);
  474. item = new SampleItem;
  475. item.text = '&gt;\u05d4&lt;';
  476. item.isHtml = false;
  477. item.isRtl = false;
  478. bidiText.push(item);
  479. return bidiText;
  480. }