Chromium Code Reviews| Index: pkg/intl/lib/bidi_utils.dart |
| =================================================================== |
| --- pkg/intl/lib/bidi_utils.dart (revision 13189) |
| +++ pkg/intl/lib/bidi_utils.dart (working copy) |
| @@ -28,8 +28,8 @@ |
| // text falls back on the more common ltr direction. |
| static const UNKNOWN = const TextDirection._('UNKNOWN', 'ltr'); |
| - /** |
| - * Textual representation of the directionality constant. One of |
| + /** |
| + * Textual representation of the directionality constant. One of |
| * 'LTR', 'RTL', or 'UNKNOWN'. |
| */ |
| final String value; |
| @@ -38,7 +38,7 @@ |
| final String spanText; |
| const TextDirection._(this.value, this.spanText); |
| - |
| + |
| /** |
| * Returns true if [otherDirection] is known to be different from this |
| * direction. |
| @@ -48,33 +48,39 @@ |
| } |
| } |
| +/** |
| + * This provides a number of utility methods for working with bidirectional |
| + * text. ####### |
|
Emily Fortuna
2012/10/04 18:37:55
is this a marker for more documentation suggested?
Alan Knight
2012/10/04 19:34:22
Oops. That was a temporary marker that I should ha
|
| + */ |
| +class Bidi { |
| + |
| /** Unicode "Left-To-Right Embedding" (LRE) character. */ |
|
Emily Fortuna
2012/10/04 18:37:55
can you fix the indentation of all of these lines
Alan Knight
2012/10/04 19:34:22
Done.
|
| -const LRE = '\u202A'; |
| +static const LRE = '\u202A'; |
| /** Unicode "Right-To-Left Embedding" (RLE) character. */ |
| -const RLE = '\u202B'; |
| +static const RLE = '\u202B'; |
| /** Unicode "Pop Directional Formatting" (PDF) character. */ |
| -const PDF = '\u202C'; |
| +static const PDF = '\u202C'; |
| /** Unicode "Left-To-Right Mark" (LRM) character. */ |
| -const LRM = '\u200E'; |
| +static const LRM = '\u200E'; |
| /** Unicode "Right-To-Left Mark" (RLM) character. */ |
| -const RLM = '\u200F'; |
| +static const RLM = '\u200F'; |
| /** Constant to define the threshold of RTL directionality. */ |
| -num _RTL_DETECTION_THRESHOLD = 0.40; |
| +static num _RTL_DETECTION_THRESHOLD = 0.40; |
| /** |
| * Practical patterns to identify strong LTR and RTL characters, respectively. |
| * These patterns are not completely correct according to the Unicode |
| * standard. They are simplified for performance and small code size. |
| */ |
| -const String _LTR_CHARS = |
| +static const String _LTR_CHARS = |
| r'A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02B8\u0300-\u0590' |
| r'\u0800-\u1FFF\u2C00-\uFB1C\uFDFE-\uFE6F\uFEFD-\uFFFF'; |
| -const String _RTL_CHARS = r'\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'; |
| +static const String _RTL_CHARS = r'\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'; |
| /** |
| * Returns the input [text] with spaces instead of HTML tags or HTML escapes, |
| @@ -87,8 +93,8 @@ |
| * the wrong directionality will be estimated, we have not invested in |
| * improving this. |
| */ |
| -String stripHtmlIfNeeded(String text) { |
| - // The regular expression is simplified for an HTML tag (opening or |
| +static String stripHtmlIfNeeded(String text) { |
| + // The regular expression is simplified for an HTML tag (opening or |
| // closing) or an HTML escape. We might want to skip over such expressions |
| // when estimating the text directionality. |
| return text.replaceAll(const RegExp(r'<[^>]*>|&[^;]+;'), ' '); |
| @@ -98,7 +104,7 @@ |
| * Determines if the first character in [text] with strong directionality is |
| * LTR. If [isHtml] is true, the text is HTML or HTML-escaped. |
| */ |
| -bool startsWithLtr(String text, [isHtml=false]) { |
| +static bool startsWithLtr(String text, [isHtml=false]) { |
| return const RegExp('^[^$_RTL_CHARS]*[$_LTR_CHARS]').hasMatch( |
| isHtml? stripHtmlIfNeeded(text) : text); |
| } |
| @@ -107,7 +113,7 @@ |
| * Determines if the first character in [text] with strong directionality is |
| * RTL. If [isHtml] is true, the text is HTML or HTML-escaped. |
| */ |
| -bool startsWithRtl(String text, [isHtml=false]) { |
| +static bool startsWithRtl(String text, [isHtml=false]) { |
| return const RegExp('^[^$_LTR_CHARS]*[$_RTL_CHARS]').hasMatch( |
| isHtml? stripHtmlIfNeeded(text) : text); |
| } |
| @@ -117,7 +123,7 @@ |
| * character in [text] is LTR. If [isHtml] is true, the text is HTML or |
| * HTML-escaped. |
| */ |
| -bool endsWithLtr(String text, [isHtml=false]) { |
| +static bool endsWithLtr(String text, [isHtml=false]) { |
| return const RegExp('[$_LTR_CHARS][^$_RTL_CHARS]*\$').hasMatch( |
| isHtml? stripHtmlIfNeeded(text) : text); |
| } |
| @@ -127,7 +133,7 @@ |
| * character in [text] is RTL. If [isHtml] is true, the text is HTML or |
| * HTML-escaped. |
| */ |
| -bool endsWithRtl(String text, [isHtml=false]) { |
| +static bool endsWithRtl(String text, [isHtml=false]) { |
| return const RegExp('[$_RTL_CHARS][^$_LTR_CHARS]*\$').hasMatch( |
| isHtml? stripHtmlIfNeeded(text) : text); |
| } |
| @@ -136,7 +142,7 @@ |
| * Determines if the given [text] has any LTR characters in it. |
| * If [isHtml] is true, the text is HTML or HTML-escaped. |
| */ |
| -bool hasAnyLtr(String text, [isHtml=false]) { |
| +static bool hasAnyLtr(String text, [isHtml=false]) { |
| return const RegExp(r'[' '$_LTR_CHARS' r']').hasMatch( |
| isHtml? stripHtmlIfNeeded(text) : text); |
| } |
| @@ -145,7 +151,7 @@ |
| * Determines if the given [text] has any RTL characters in it. |
| * If [isHtml] is true, the text is HTML or HTML-escaped. |
| */ |
| -bool hasAnyRtl(String text, [isHtml=false]) { |
| +static bool hasAnyRtl(String text, [isHtml=false]) { |
| return const RegExp(r'[' '$_RTL_CHARS' r']').hasMatch( |
| isHtml? stripHtmlIfNeeded(text) : text); |
| } |
| @@ -174,7 +180,7 @@ |
| * The presence of other subtags of the language code, e.g. regions like EG |
| * (Egypt), is ignored. |
| */ |
| -bool isRtlLanguage(String languageString) { |
| +static bool isRtlLanguage(String languageString) { |
| return const RegExp(r'^(ar|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|.*[-_]' |
| r'(Arab|Hebr|Thaa|Nkoo|Tfng))(?!.*[-_](Latn|Cyrl)($|-|_))' |
| r'($|-|_)', ignoreCase : true).hasMatch(languageString); |
| @@ -182,14 +188,14 @@ |
| /** |
| * Enforce the [html] snippet in RTL directionality regardless of overall |
| - * context. If the html piece was enclosed by a tag, the direction will be |
| + * context. If the html piece was enclosed by a tag, the direction will be |
| * applied to existing tag, otherwise a span tag will be added as wrapper. |
| * For this reason, if html snippet start with with tag, this tag must enclose |
| * the whole piece. If the tag already has a direction specified, this new one |
| * will override existing one in behavior (should work on Chrome, FF, and IE |
| * since this was ported directly from the Closure version). |
| */ |
| -String enforceRtlInHtml(String html) { |
| +static String enforceRtlInHtml(String html) { |
| return _enforceInHtmlHelper(html, 'rtl'); |
| } |
| @@ -197,19 +203,19 @@ |
| * Enforce RTL on both end of the given [text] using unicode BiDi formatting |
| * characters RLE and PDF. |
| */ |
| -String enforceRtlInText(String text) { |
| +static String enforceRtlInText(String text) { |
| return '$RLE$text$PDF'; |
| } |
| /** |
| * Enforce the [html] snippet in LTR directionality regardless of overall |
| - * context. If the html piece was enclosed by a tag, the direction will be |
| + * context. If the html piece was enclosed by a tag, the direction will be |
| * applied to existing tag, otherwise a span tag will be added as wrapper. |
| * For this reason, if html snippet start with with tag, this tag must enclose |
| * the whole piece. If the tag already has a direction specified, this new one |
| * will override existing one in behavior (tested on FF and IE). |
| */ |
| -String enforceLtrInHtml(String html) { |
| +static String enforceLtrInHtml(String html) { |
| return _enforceInHtmlHelper(html, 'ltr'); |
| } |
| @@ -217,19 +223,19 @@ |
| * Enforce LTR on both end of the given [text] using unicode BiDi formatting |
| * characters LRE and PDF. |
| */ |
| -String enforceLtrInText(String text) { |
| +static String enforceLtrInText(String text) { |
| return '$LRE$text$PDF'; |
| } |
| /** |
| * Enforce the [html] snippet in the desired [direction] regardless of overall |
| - * context. If the html piece was enclosed by a tag, the direction will be |
| + * context. If the html piece was enclosed by a tag, the direction will be |
| * applied to existing tag, otherwise a span tag will be added as wrapper. |
| * For this reason, if html snippet start with with tag, this tag must enclose |
| * the whole piece. If the tag already has a direction specified, this new one |
| * will override existing one in behavior (tested on FF and IE). |
| */ |
| -String _enforceInHtmlHelper(String html, String direction) { |
| +static String _enforceInHtmlHelper(String html, String direction) { |
| if (html.startsWith('<')) { |
| StringBuffer buffer = new StringBuffer(); |
| var startIndex = 0; |
| @@ -246,28 +252,28 @@ |
| } |
| /** |
| - * Apply bracket guard to [str] using html span tag. This is to address the |
| + * Apply bracket guard to [str] using html span tag. This is to address the |
| * problem of messy bracket display that frequently happens in RTL layout. |
| * If [isRtlContext] is true, then we explicitly want to wrap in a span of RTL |
| * directionality, regardless of the estimated directionality. |
| */ |
| -String guardBracketInHtml(String str, [bool isRtlContext]) { |
| +static String guardBracketInHtml(String str, [bool isRtlContext]) { |
| var useRtl = isRtlContext == null ? hasAnyRtl(str) : isRtlContext; |
| - RegExp matchingBrackets = |
| + RegExp matchingBrackets = |
| const RegExp(r'(\(.*?\)+)|(\[.*?\]+)|(\{.*?\}+)|(<.*?(>)+)'); |
| return _guardBracketHelper(str, matchingBrackets, |
| '<span dir=${useRtl? "rtl" : "ltr"}>', '</span>'); |
| } |
| /** |
| - * Apply bracket guard to [str] using LRM and RLM. This is to address the |
| + * Apply bracket guard to [str] using LRM and RLM. This is to address the |
| * problem of messy bracket display that frequently happens in RTL layout. |
| * This version works for both plain text and html, but in some cases is not |
| * as good as guardBracketInHtml. |
| * If [isRtlContext] is true, then we explicitly want to wrap in a span of RTL |
| * directionality, regardless of the estimated directionality. |
| */ |
| -String guardBracketInText(String str, [bool isRtlContext]) { |
| +static String guardBracketInText(String str, [bool isRtlContext]) { |
| var useRtl = isRtlContext == null ? hasAnyRtl(str) : isRtlContext; |
| var mark = useRtl ? RLM : LRM; |
| return _guardBracketHelper(str, |
| @@ -277,13 +283,13 @@ |
| /** |
| * (Mostly) reimplements the $& functionality of "replace" in JavaScript. |
| * Given a [str] and the [regexp] to match with, optionally supply a string to |
| - * be inserted [before] the match and/or [after]. For example, |
| + * be inserted [before] the match and/or [after]. For example, |
| * `_guardBracketHelper('firetruck', const RegExp('truck'), 'hydrant', '!')` |
| * would return 'firehydrant!'. |
| */ |
| // TODO(efortuna): Get rid of this once this is implemented in Dart. |
| // See Issue 2979. |
| -String _guardBracketHelper(String str, RegExp regexp, [String before, |
| +static String _guardBracketHelper(String str, RegExp regexp, [String before, |
| String after]) { |
| StringBuffer buffer = new StringBuffer(); |
| var startIndex = 0; |
| @@ -298,7 +304,7 @@ |
| /** |
| * Estimates the directionality of [text] using the best known |
| - * general-purpose method (using relative word counts). A |
| + * general-purpose method (using relative word counts). A |
| * TextDirection.UNKNOWN return value indicates completely neutral input. |
| * [isHtml] is true if [text] HTML or HTML-escaped. |
| * |
| @@ -308,7 +314,7 @@ |
| * Otherwise, returns UNKNOWN, which is used to mean `neutral`. |
| * Numbers and URLs are counted as weakly LTR. |
| */ |
| -TextDirection estimateDirectionOfText(String text, [bool isHtml=false]) { |
| +static TextDirection estimateDirectionOfText(String text, [bool isHtml=false]) { |
| text = isHtml? stripHtmlIfNeeded(text) : text; |
| var rtlCount = 0; |
| var total = 0; |
| @@ -344,7 +350,7 @@ |
| * Find the first index in [str] of the first closing parenthesis that does |
| * not match an opening parenthesis. |
| */ |
| -int _unmatchedParenIndex(String str) { |
| +static int _unmatchedParenIndex(String str) { |
| int sum = 0; |
| int index = 0; |
| while (sum >= 0 || index > str.length) { |
| @@ -360,7 +366,7 @@ |
| * Replace the double and single quote directly after a Hebrew character in |
| * [str] with GERESH and GERSHAYIM. This is most likely the user's intention. |
| */ |
| -String normalizeHebrewQuote(String str) { |
| +static String normalizeHebrewQuote(String str) { |
| StringBuffer buf = new StringBuffer(); |
| if (str.length > 0) { |
| buf.add(str.substring(0, 1)); |
| @@ -386,6 +392,8 @@ |
| * text should be laid out in RTL direction. If [isHtml] is true, the string |
| * is HTML or HTML-escaped. |
| */ |
| -bool detectRtlDirectionality(String str, [bool isHtml]) { |
| +static bool detectRtlDirectionality(String str, [bool isHtml]) { |
| return estimateDirectionOfText(str, isHtml) == TextDirection.RTL; |
| } |
| + |
|
Emily Fortuna
2012/10/04 18:37:55
You're going to love this... delete this extra lin
Alan Knight
2012/10/04 19:34:22
Done.
|
| +} |