Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: pkg/intl/lib/bidi_utils.dart

Issue 140843002: [Intl] Cleanup some code (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 27 matching lines...) Expand all
38 38
39 /** Textual representation of the directionality when used in span tag. */ 39 /** Textual representation of the directionality when used in span tag. */
40 final String spanText; 40 final String spanText;
41 41
42 const TextDirection._(this.value, this.spanText); 42 const TextDirection._(this.value, this.spanText);
43 43
44 /** 44 /**
45 * Returns true if [otherDirection] is known to be different from this 45 * Returns true if [otherDirection] is known to be different from this
46 * direction. 46 * direction.
47 */ 47 */
48 bool isDirectionChange(TextDirection otherDirection) { 48 bool isDirectionChange(TextDirection otherDirection) =>
49 return otherDirection != TextDirection.UNKNOWN && this != otherDirection; 49 otherDirection != TextDirection.UNKNOWN && this != otherDirection;
50 }
51 } 50 }
52 51
53 /** 52 /**
54 * This provides utility methods for working with bidirectional text. All 53 * This provides utility methods for working with bidirectional text. All
55 * of the methods are static, and are organized into a class primarily to 54 * of the methods are static, and are organized into a class primarily to
56 * group them together for documentation and discoverability. 55 * group them together for documentation and discoverability.
57 */ 56 */
58 class Bidi { 57 class Bidi {
59 58
60 /** Unicode "Left-To-Right Embedding" (LRE) character. */ 59 /** Unicode "Left-To-Right Embedding" (LRE) character. */
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 190
192 /** 191 /**
193 * Enforce the [html] snippet in RTL directionality regardless of overall 192 * Enforce the [html] snippet in RTL directionality regardless of overall
194 * context. If the html piece was enclosed by a tag, the direction will be 193 * context. If the html piece was enclosed by a tag, the direction will be
195 * applied to existing tag, otherwise a span tag will be added as wrapper. 194 * applied to existing tag, otherwise a span tag will be added as wrapper.
196 * For this reason, if html snippet start with with tag, this tag must enclose 195 * For this reason, if html snippet start with with tag, this tag must enclose
197 * the whole piece. If the tag already has a direction specified, this new one 196 * the whole piece. If the tag already has a direction specified, this new one
198 * will override existing one in behavior (should work on Chrome, FF, and IE 197 * will override existing one in behavior (should work on Chrome, FF, and IE
199 * since this was ported directly from the Closure version). 198 * since this was ported directly from the Closure version).
200 */ 199 */
201 static String enforceRtlInHtml(String html) { 200 static String enforceRtlInHtml(String html) =>
202 return _enforceInHtmlHelper(html, 'rtl'); 201 _enforceInHtmlHelper(html, 'rtl');
203 }
204 202
205 /** 203 /**
206 * Enforce RTL on both end of the given [text] using unicode BiDi formatting 204 * Enforce RTL on both end of the given [text] using unicode BiDi formatting
207 * characters RLE and PDF. 205 * characters RLE and PDF.
208 */ 206 */
209 static String enforceRtlInText(String text) { 207 static String enforceRtlInText(String text) => '$RLE$text$PDF';
210 return '$RLE$text$PDF';
211 }
212 208
213 /** 209 /**
214 * Enforce the [html] snippet in LTR directionality regardless of overall 210 * Enforce the [html] snippet in LTR directionality regardless of overall
215 * context. If the html piece was enclosed by a tag, the direction will be 211 * context. If the html piece was enclosed by a tag, the direction will be
216 * applied to existing tag, otherwise a span tag will be added as wrapper. 212 * applied to existing tag, otherwise a span tag will be added as wrapper.
217 * For this reason, if html snippet start with with tag, this tag must enclose 213 * For this reason, if html snippet start with with tag, this tag must enclose
218 * the whole piece. If the tag already has a direction specified, this new one 214 * the whole piece. If the tag already has a direction specified, this new one
219 * will override existing one in behavior (tested on FF and IE). 215 * will override existing one in behavior (tested on FF and IE).
220 */ 216 */
221 static String enforceLtrInHtml(String html) { 217 static String enforceLtrInHtml(String html) =>
222 return _enforceInHtmlHelper(html, 'ltr'); 218 _enforceInHtmlHelper(html, 'ltr');
223 }
224 219
225 /** 220 /**
226 * Enforce LTR on both end of the given [text] using unicode BiDi formatting 221 * Enforce LTR on both end of the given [text] using unicode BiDi formatting
227 * characters LRE and PDF. 222 * characters LRE and PDF.
228 */ 223 */
229 static String enforceLtrInText(String text) { 224 static String enforceLtrInText(String text) => '$LRE$text$PDF';
230 return '$LRE$text$PDF';
231 }
232 225
233 /** 226 /**
234 * Enforce the [html] snippet in the desired [direction] regardless of overall 227 * Enforce the [html] snippet in the desired [direction] regardless of overall
235 * context. If the html piece was enclosed by a tag, the direction will be 228 * context. If the html piece was enclosed by a tag, the direction will be
236 * applied to existing tag, otherwise a span tag will be added as wrapper. 229 * applied to existing tag, otherwise a span tag will be added as wrapper.
237 * For this reason, if html snippet start with with tag, this tag must enclose 230 * 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 231 * 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). 232 * will override existing one in behavior (tested on FF and IE).
240 */ 233 */
241 static String _enforceInHtmlHelper(String html, String direction) { 234 static String _enforceInHtmlHelper(String html, String direction) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 * (Mostly) reimplements the $& functionality of "replace" in JavaScript. 280 * (Mostly) reimplements the $& functionality of "replace" in JavaScript.
288 * Given a [str] and the [regexp] to match with, optionally supply a string to 281 * Given a [str] and the [regexp] to match with, optionally supply a string to
289 * be inserted [before] the match and/or [after]. For example, 282 * be inserted [before] the match and/or [after]. For example,
290 * `_guardBracketHelper('firetruck', new RegExp('truck'), 'hydrant', '!')` 283 * `_guardBracketHelper('firetruck', new RegExp('truck'), 'hydrant', '!')`
291 * would return 'firehydrant!'. 284 * would return 'firehydrant!'.
292 */ 285 */
293 // TODO(efortuna): Get rid of this once this is implemented in Dart. 286 // TODO(efortuna): Get rid of this once this is implemented in Dart.
294 // See Issue 2979. 287 // See Issue 2979.
295 static String _guardBracketHelper(String str, RegExp regexp, [String before, 288 static String _guardBracketHelper(String str, RegExp regexp, [String before,
296 String after]) { 289 String after]) {
297 StringBuffer buffer = new StringBuffer(); 290 var buffer = new StringBuffer();
298 var startIndex = 0; 291 var startIndex = 0;
299 Iterable matches = regexp.allMatches(str); 292 regexp.allMatches(str).forEach((match) {
300 for (Match match in matches) {
301 buffer..write(str.substring(startIndex, match.start)) 293 buffer..write(str.substring(startIndex, match.start))
302 ..write(before) 294 ..write(before)
303 ..write(str.substring(match.start, match.end)) 295 ..write(str.substring(match.start, match.end))
304 ..write(after); 296 ..write(after);
305 startIndex = match.end; 297 startIndex = match.end;
306 } 298 });
307 return (buffer..write(str.substring(startIndex))).toString(); 299 return (buffer..write(str.substring(startIndex))).toString();
308 } 300 }
309 301
310 /** 302 /**
311 * Estimates the directionality of [text] using the best known 303 * Estimates the directionality of [text] using the best known
312 * general-purpose method (using relative word counts). A 304 * general-purpose method (using relative word counts). A
313 * TextDirection.UNKNOWN return value indicates completely neutral input. 305 * TextDirection.UNKNOWN return value indicates completely neutral input.
314 * [isHtml] is true if [text] HTML or HTML-escaped. 306 * [isHtml] is true if [text] HTML or HTML-escaped.
315 * 307 *
316 * If the number of RTL words is above a certain percentage of the total 308 * If the number of RTL words is above a certain percentage of the total
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 } 367 }
376 } 368 }
377 return buf.toString(); 369 return buf.toString();
378 } 370 }
379 371
380 /** 372 /**
381 * Check the estimated directionality of [str], return true if the piece of 373 * 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 374 * text should be laid out in RTL direction. If [isHtml] is true, the string
383 * is HTML or HTML-escaped. 375 * is HTML or HTML-escaped.
384 */ 376 */
385 static bool detectRtlDirectionality(String str, {bool isHtml: false}) { 377 static bool detectRtlDirectionality(String str, {bool isHtml: false}) =>
386 return estimateDirectionOfText(str, isHtml: isHtml) == TextDirection.RTL; 378 estimateDirectionOfText(str, isHtml: isHtml) == TextDirection.RTL;
387 }
388 } 379 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698