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 /** | 5 /** |
6 * Bidi stands for Bi-directional text. | 6 * Bidi stands for Bi-directional text. |
7 * According to http://en.wikipedia.org/wiki/Bi-directional_text: | 7 * According to http://en.wikipedia.org/wiki/Bi-directional_text: |
8 * Bi-directional text is text containing text in both text directionalities, | 8 * Bi-directional text is text containing text in both text directionalities, |
9 * both right-to-left (RTL) and left-to-right (LTR). It generally involves text | 9 * both right-to-left (RTL) and left-to-right (LTR). It generally involves text |
10 * containing different types of alphabets, but may also refer to boustrophedon, | 10 * containing different types of alphabets, but may also refer to boustrophedon, |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 * the whole piece. If the tag already has a direction specified, this new one | 236 * the whole piece. If the tag already has a direction specified, this new one |
237 * will override existing one in behavior (tested on FF and IE). | 237 * will override existing one in behavior (tested on FF and IE). |
238 */ | 238 */ |
239 static String _enforceInHtmlHelper(String html, String direction) { | 239 static String _enforceInHtmlHelper(String html, String direction) { |
240 if (html.startsWith('<')) { | 240 if (html.startsWith('<')) { |
241 StringBuffer buffer = new StringBuffer(); | 241 StringBuffer buffer = new StringBuffer(); |
242 var startIndex = 0; | 242 var startIndex = 0; |
243 Match match = const RegExp('<\\w+').firstMatch(html); | 243 Match match = const RegExp('<\\w+').firstMatch(html); |
244 if (match != null) { | 244 if (match != null) { |
245 buffer.add(html.substring( | 245 buffer.add(html.substring( |
246 startIndex, match.end())).add(' dir=$direction'); | 246 startIndex, match.end)).add(' dir=$direction'); |
247 startIndex = match.end(); | 247 startIndex = match.end; |
248 } | 248 } |
249 return buffer.add(html.substring(startIndex)).toString(); | 249 return buffer.add(html.substring(startIndex)).toString(); |
250 } | 250 } |
251 // '\n' is important for FF so that it won't incorrectly merge span groups. | 251 // '\n' is important for FF so that it won't incorrectly merge span groups. |
252 return '\n<span dir=$direction>$html</span>'; | 252 return '\n<span dir=$direction>$html</span>'; |
253 } | 253 } |
254 | 254 |
255 /** | 255 /** |
256 * Apply bracket guard to [str] using html span tag. This is to address the | 256 * Apply bracket guard to [str] using html span tag. This is to address the |
257 * problem of messy bracket display that frequently happens in RTL layout. | 257 * problem of messy bracket display that frequently happens in RTL layout. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 * would return 'firehydrant!'. | 289 * would return 'firehydrant!'. |
290 */ | 290 */ |
291 // TODO(efortuna): Get rid of this once this is implemented in Dart. | 291 // TODO(efortuna): Get rid of this once this is implemented in Dart. |
292 // See Issue 2979. | 292 // See Issue 2979. |
293 static String _guardBracketHelper(String str, RegExp regexp, [String before, | 293 static String _guardBracketHelper(String str, RegExp regexp, [String before, |
294 String after]) { | 294 String after]) { |
295 StringBuffer buffer = new StringBuffer(); | 295 StringBuffer buffer = new StringBuffer(); |
296 var startIndex = 0; | 296 var startIndex = 0; |
297 Iterable matches = regexp.allMatches(str); | 297 Iterable matches = regexp.allMatches(str); |
298 for (Match match in matches) { | 298 for (Match match in matches) { |
299 buffer.add(str.substring(startIndex, match.start())).add(before); | 299 buffer.add(str.substring(startIndex, match.start)).add(before); |
300 buffer.add(str.substring(match.start(), match.end())).add(after); | 300 buffer.add(str.substring(match.start, match.end)).add(after); |
301 startIndex = match.end(); | 301 startIndex = match.end; |
302 } | 302 } |
303 return buffer.add(str.substring(startIndex)).toString(); | 303 return buffer.add(str.substring(startIndex)).toString(); |
304 } | 304 } |
305 | 305 |
306 /** | 306 /** |
307 * Estimates the directionality of [text] using the best known | 307 * Estimates the directionality of [text] using the best known |
308 * general-purpose method (using relative word counts). A | 308 * general-purpose method (using relative word counts). A |
309 * TextDirection.UNKNOWN return value indicates completely neutral input. | 309 * TextDirection.UNKNOWN return value indicates completely neutral input. |
310 * [isHtml] is true if [text] HTML or HTML-escaped. | 310 * [isHtml] is true if [text] HTML or HTML-escaped. |
311 * | 311 * |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 | 391 |
392 /** | 392 /** |
393 * Check the estimated directionality of [str], return true if the piece of | 393 * Check the estimated directionality of [str], return true if the piece of |
394 * text should be laid out in RTL direction. If [isHtml] is true, the string | 394 * text should be laid out in RTL direction. If [isHtml] is true, the string |
395 * is HTML or HTML-escaped. | 395 * is HTML or HTML-escaped. |
396 */ | 396 */ |
397 static bool detectRtlDirectionality(String str, {bool isHtml: false}) { | 397 static bool detectRtlDirectionality(String str, {bool isHtml: false}) { |
398 return estimateDirectionOfText(str, isHtml: isHtml) == TextDirection.RTL; | 398 return estimateDirectionOfText(str, isHtml: isHtml) == TextDirection.RTL; |
399 } | 399 } |
400 } | 400 } |
OLD | NEW |