| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of intl; | 5 part of intl; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Bidi stands for Bi-directional text. | 8 * Bidi stands for Bi-directional text. |
| 9 * According to http://en.wikipedia.org/wiki/Bi-directional_text: | 9 * According to http://en.wikipedia.org/wiki/Bi-directional_text: |
| 10 * Bi-directional text is text containing text in both text directionalities, | 10 * Bi-directional text is text containing text in both text directionalities, |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 * For this reason, if html snippet start with with tag, this tag must enclose | 237 * For this reason, if html snippet start with with tag, this tag must enclose |
| 238 * the whole piece. If the tag already has a direction specified, this new one | 238 * the whole piece. If the tag already has a direction specified, this new one |
| 239 * will override existing one in behavior (tested on FF and IE). | 239 * will override existing one in behavior (tested on FF and IE). |
| 240 */ | 240 */ |
| 241 static String _enforceInHtmlHelper(String html, String direction) { | 241 static String _enforceInHtmlHelper(String html, String direction) { |
| 242 if (html.startsWith('<')) { | 242 if (html.startsWith('<')) { |
| 243 StringBuffer buffer = new StringBuffer(); | 243 StringBuffer buffer = new StringBuffer(); |
| 244 var startIndex = 0; | 244 var startIndex = 0; |
| 245 Match match = new RegExp('<\\w+').firstMatch(html); | 245 Match match = new RegExp('<\\w+').firstMatch(html); |
| 246 if (match != null) { | 246 if (match != null) { |
| 247 buffer..add(html.substring(startIndex, match.end)) | 247 buffer..write(html.substring(startIndex, match.end)) |
| 248 ..add(' dir=$direction'); | 248 ..write(' dir=$direction'); |
| 249 startIndex = match.end; | 249 startIndex = match.end; |
| 250 } | 250 } |
| 251 return (buffer..add(html.substring(startIndex))).toString(); | 251 return (buffer..write(html.substring(startIndex))).toString(); |
| 252 } | 252 } |
| 253 // '\n' is important for FF so that it won't incorrectly merge span groups. | 253 // '\n' is important for FF so that it won't incorrectly merge span groups. |
| 254 return '\n<span dir=$direction>$html</span>'; | 254 return '\n<span dir=$direction>$html</span>'; |
| 255 } | 255 } |
| 256 | 256 |
| 257 /** | 257 /** |
| 258 * Apply bracket guard to [str] using html span tag. This is to address the | 258 * Apply bracket guard to [str] using html span tag. This is to address the |
| 259 * problem of messy bracket display that frequently happens in RTL layout. | 259 * problem of messy bracket display that frequently happens in RTL layout. |
| 260 * If [isRtlContext] is true, then we explicitly want to wrap in a span of RTL | 260 * If [isRtlContext] is true, then we explicitly want to wrap in a span of RTL |
| 261 * directionality, regardless of the estimated directionality. | 261 * directionality, regardless of the estimated directionality. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 291 * would return 'firehydrant!'. | 291 * would return 'firehydrant!'. |
| 292 */ | 292 */ |
| 293 // TODO(efortuna): Get rid of this once this is implemented in Dart. | 293 // TODO(efortuna): Get rid of this once this is implemented in Dart. |
| 294 // See Issue 2979. | 294 // See Issue 2979. |
| 295 static String _guardBracketHelper(String str, RegExp regexp, [String before, | 295 static String _guardBracketHelper(String str, RegExp regexp, [String before, |
| 296 String after]) { | 296 String after]) { |
| 297 StringBuffer buffer = new StringBuffer(); | 297 StringBuffer buffer = new StringBuffer(); |
| 298 var startIndex = 0; | 298 var startIndex = 0; |
| 299 Iterable matches = regexp.allMatches(str); | 299 Iterable matches = regexp.allMatches(str); |
| 300 for (Match match in matches) { | 300 for (Match match in matches) { |
| 301 buffer..add(str.substring(startIndex, match.start)) | 301 buffer..write(str.substring(startIndex, match.start)) |
| 302 ..add(before) | 302 ..write(before) |
| 303 ..add(str.substring(match.start, match.end)) | 303 ..write(str.substring(match.start, match.end)) |
| 304 ..add(after); | 304 ..write(after); |
| 305 startIndex = match.end; | 305 startIndex = match.end; |
| 306 } | 306 } |
| 307 return (buffer..add(str.substring(startIndex))).toString(); | 307 return (buffer..write(str.substring(startIndex))).toString(); |
| 308 } | 308 } |
| 309 | 309 |
| 310 /** | 310 /** |
| 311 * Estimates the directionality of [text] using the best known | 311 * Estimates the directionality of [text] using the best known |
| 312 * general-purpose method (using relative word counts). A | 312 * general-purpose method (using relative word counts). A |
| 313 * TextDirection.UNKNOWN return value indicates completely neutral input. | 313 * TextDirection.UNKNOWN return value indicates completely neutral input. |
| 314 * [isHtml] is true if [text] HTML or HTML-escaped. | 314 * [isHtml] is true if [text] HTML or HTML-escaped. |
| 315 * | 315 * |
| 316 * If the number of RTL words is above a certain percentage of the total | 316 * If the number of RTL words is above a certain percentage of the total |
| 317 * number of strongly directional words, returns RTL. | 317 * number of strongly directional words, returns RTL. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 } | 352 } |
| 353 } | 353 } |
| 354 | 354 |
| 355 /** | 355 /** |
| 356 * Replace the double and single quote directly after a Hebrew character in | 356 * Replace the double and single quote directly after a Hebrew character in |
| 357 * [str] with GERESH and GERSHAYIM. This is most likely the user's intention. | 357 * [str] with GERESH and GERSHAYIM. This is most likely the user's intention. |
| 358 */ | 358 */ |
| 359 static String normalizeHebrewQuote(String str) { | 359 static String normalizeHebrewQuote(String str) { |
| 360 StringBuffer buf = new StringBuffer(); | 360 StringBuffer buf = new StringBuffer(); |
| 361 if (str.length > 0) { | 361 if (str.length > 0) { |
| 362 buf.add(str.substring(0, 1)); | 362 buf.write(str.substring(0, 1)); |
| 363 } | 363 } |
| 364 // Start at 1 because we're looking for the patterns [\u0591-\u05f2])" or | 364 // Start at 1 because we're looking for the patterns [\u0591-\u05f2])" or |
| 365 // [\u0591-\u05f2]'. | 365 // [\u0591-\u05f2]'. |
| 366 for (int i = 1; i < str.length; i++) { | 366 for (int i = 1; i < str.length; i++) { |
| 367 if (str.substring(i, i+1) == '"' | 367 if (str.substring(i, i+1) == '"' |
| 368 && new RegExp('[\u0591-\u05f2]').hasMatch(str.substring(i-1, i))) { | 368 && new RegExp('[\u0591-\u05f2]').hasMatch(str.substring(i-1, i))) { |
| 369 buf.add('\u05f4'); | 369 buf.write('\u05f4'); |
| 370 } else if (str.substring(i, i+1) == "'" | 370 } else if (str.substring(i, i+1) == "'" |
| 371 && new RegExp('[\u0591-\u05f2]').hasMatch(str.substring(i-1, i))) { | 371 && new RegExp('[\u0591-\u05f2]').hasMatch(str.substring(i-1, i))) { |
| 372 buf.add('\u05f3'); | 372 buf.write('\u05f3'); |
| 373 } else { | 373 } else { |
| 374 buf.add(str.substring(i, i+1)); | 374 buf.write(str.substring(i, i+1)); |
| 375 } | 375 } |
| 376 } | 376 } |
| 377 return buf.toString(); | 377 return buf.toString(); |
| 378 } | 378 } |
| 379 | 379 |
| 380 /** | 380 /** |
| 381 * Check the estimated directionality of [str], return true if the piece of | 381 * Check the estimated directionality of [str], return true if the piece of |
| 382 * text should be laid out in RTL direction. If [isHtml] is true, the string | 382 * text should be laid out in RTL direction. If [isHtml] is true, the string |
| 383 * is HTML or HTML-escaped. | 383 * is HTML or HTML-escaped. |
| 384 */ | 384 */ |
| 385 static bool detectRtlDirectionality(String str, {bool isHtml: false}) { | 385 static bool detectRtlDirectionality(String str, {bool isHtml: false}) { |
| 386 return estimateDirectionOfText(str, isHtml: isHtml) == TextDirection.RTL; | 386 return estimateDirectionOfText(str, isHtml: isHtml) == TextDirection.RTL; |
| 387 } | 387 } |
| 388 } | 388 } |
| OLD | NEW |