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( | 247 buffer..add(html.substring(startIndex, match.end)) |
248 startIndex, match.end)).add(' dir=$direction'); | 248 ..add(' dir=$direction'); |
249 startIndex = match.end; | 249 startIndex = match.end; |
250 } | 250 } |
251 return buffer.add(html.substring(startIndex)).toString(); | 251 return (buffer..add(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)).add(before); | 301 buffer..add(str.substring(startIndex, match.start)) |
302 buffer.add(str.substring(match.start, match.end)).add(after); | 302 ..add(before) |
| 303 ..add(str.substring(match.start, match.end)) |
| 304 ..add(after); |
303 startIndex = match.end; | 305 startIndex = match.end; |
304 } | 306 } |
305 return buffer.add(str.substring(startIndex)).toString(); | 307 return (buffer..add(str.substring(startIndex))).toString(); |
306 } | 308 } |
307 | 309 |
308 /** | 310 /** |
309 * Estimates the directionality of [text] using the best known | 311 * Estimates the directionality of [text] using the best known |
310 * general-purpose method (using relative word counts). A | 312 * general-purpose method (using relative word counts). A |
311 * TextDirection.UNKNOWN return value indicates completely neutral input. | 313 * TextDirection.UNKNOWN return value indicates completely neutral input. |
312 * [isHtml] is true if [text] HTML or HTML-escaped. | 314 * [isHtml] is true if [text] HTML or HTML-escaped. |
313 * | 315 * |
314 * 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 |
315 * number of strongly directional words, returns RTL. | 317 * number of strongly directional words, returns RTL. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 | 395 |
394 /** | 396 /** |
395 * Check the estimated directionality of [str], return true if the piece of | 397 * Check the estimated directionality of [str], return true if the piece of |
396 * text should be laid out in RTL direction. If [isHtml] is true, the string | 398 * text should be laid out in RTL direction. If [isHtml] is true, the string |
397 * is HTML or HTML-escaped. | 399 * is HTML or HTML-escaped. |
398 */ | 400 */ |
399 static bool detectRtlDirectionality(String str, {bool isHtml: false}) { | 401 static bool detectRtlDirectionality(String str, {bool isHtml: false}) { |
400 return estimateDirectionOfText(str, isHtml: isHtml) == TextDirection.RTL; | 402 return estimateDirectionOfText(str, isHtml: isHtml) == TextDirection.RTL; |
401 } | 403 } |
402 } | 404 } |
OLD | NEW |