| 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 [Wikipedia](http://en.wikipedia.org/wiki/Bi-directional_text): | 7 * According to [Wikipedia](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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 * directionality, we wrap it with a `span` tag and add a `dir` attribute | 106 * directionality, we wrap it with a `span` tag and add a `dir` attribute |
| 107 * (either "dir=rtl" or "dir=ltr"). | 107 * (either "dir=rtl" or "dir=ltr"). |
| 108 * If alwaysSpan was true when constructing the formatter, the input is always | 108 * If alwaysSpan was true when constructing the formatter, the input is always |
| 109 * wrapped with `span` tag, skipping the dir attribute when it's not needed. | 109 * wrapped with `span` tag, skipping the dir attribute when it's not needed. |
| 110 * | 110 * |
| 111 * If [resetDir] is true and the overall directionality or the exit | 111 * If [resetDir] is true and the overall directionality or the exit |
| 112 * directionality of [text] is opposite to the context directionality, | 112 * directionality of [text] is opposite to the context directionality, |
| 113 * a trailing unicode BiDi mark matching the context directionality is | 113 * a trailing unicode BiDi mark matching the context directionality is |
| 114 * appended (LRM or RLM). If [isHtml] is false, we HTML-escape the [text]. | 114 * appended (LRM or RLM). If [isHtml] is false, we HTML-escape the [text]. |
| 115 */ | 115 */ |
| 116 String wrapWithSpan(String text, [bool isHtml=false, bool resetDir=true, | 116 String wrapWithSpan(String text, {bool isHtml: false, bool resetDir: true, |
| 117 TextDirection direction]) { | 117 TextDirection direction}) { |
| 118 if (direction == null) direction = estimateDirection(text, isHtml); | 118 if (direction == null) direction = estimateDirection(text, isHtml: isHtml); |
| 119 var result; | 119 var result; |
| 120 if (!isHtml) text = htmlEscape(text); | 120 if (!isHtml) text = htmlEscape(text); |
| 121 var directionChange = contextDirection.isDirectionChange(direction); | 121 var directionChange = contextDirection.isDirectionChange(direction); |
| 122 if (_alwaysSpan || directionChange) { | 122 if (_alwaysSpan || directionChange) { |
| 123 var spanDirection = ''; | 123 var spanDirection = ''; |
| 124 if (directionChange) { | 124 if (directionChange) { |
| 125 spanDirection = ' dir=${direction.spanText}'; | 125 spanDirection = ' dir=${direction.spanText}'; |
| 126 } | 126 } |
| 127 result= '<span$spanDirection>$text</span>'; | 127 result= '<span$spanDirection>$text</span>'; |
| 128 } else { | 128 } else { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 143 * directionality of text are opposite to the context directionality, | 143 * directionality of text are opposite to the context directionality, |
| 144 * a trailing unicode BiDi mark matching the context directionality is | 144 * a trailing unicode BiDi mark matching the context directionality is |
| 145 * appended (LRM or RLM). | 145 * appended (LRM or RLM). |
| 146 * | 146 * |
| 147 * In HTML, the *only* valid use of this function is inside of elements that | 147 * In HTML, the *only* valid use of this function is inside of elements that |
| 148 * do not allow markup, e.g. an 'option' tag. | 148 * do not allow markup, e.g. an 'option' tag. |
| 149 * This function does *not* do HTML-escaping regardless of the value of | 149 * This function does *not* do HTML-escaping regardless of the value of |
| 150 * [isHtml]. [isHtml] is used to designate if the text contains HTML (escaped | 150 * [isHtml]. [isHtml] is used to designate if the text contains HTML (escaped |
| 151 * or unescaped). | 151 * or unescaped). |
| 152 */ | 152 */ |
| 153 String wrapWithUnicode(String text, [bool isHtml=false, bool resetDir=true, | 153 String wrapWithUnicode(String text, {bool isHtml: false, bool resetDir: true, |
| 154 TextDirection direction]) { | 154 TextDirection direction}) { |
| 155 if (direction == null) direction = estimateDirection(text, isHtml); | 155 if (direction == null) direction = estimateDirection(text, isHtml: isHtml); |
| 156 var result = text; | 156 var result = text; |
| 157 if (contextDirection.isDirectionChange(direction)) { | 157 if (contextDirection.isDirectionChange(direction)) { |
| 158 var marker = direction == TextDirection.RTL ? Bidi.RLE : Bidi.LRE; | 158 var marker = direction == TextDirection.RTL ? Bidi.RLE : Bidi.LRE; |
| 159 result = "${marker}$text${Bidi.PDF}"; | 159 result = "${marker}$text${Bidi.PDF}"; |
| 160 | 160 |
| 161 } | 161 } |
| 162 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); | 162 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); |
| 163 } | 163 } |
| 164 | 164 |
| 165 /** | 165 /** |
| 166 * Estimates the directionality of [text] using the best known | 166 * Estimates the directionality of [text] using the best known |
| 167 * general-purpose method (using relative word counts). A | 167 * general-purpose method (using relative word counts). A |
| 168 * TextDirection.UNKNOWN return value indicates completely neutral input. | 168 * TextDirection.UNKNOWN return value indicates completely neutral input. |
| 169 * [isHtml] is true if [text] HTML or HTML-escaped. | 169 * [isHtml] is true if [text] HTML or HTML-escaped. |
| 170 */ | 170 */ |
| 171 TextDirection estimateDirection(String text, [bool isHtml=false]) { | 171 TextDirection estimateDirection(String text, {bool isHtml: false}) { |
| 172 return Bidi.estimateDirectionOfText(text, isHtml); //TODO~!!! | 172 return Bidi.estimateDirectionOfText(text, isHtml: isHtml); //TODO~!!! |
| 173 } | 173 } |
| 174 | 174 |
| 175 /** | 175 /** |
| 176 * Returns a unicode BiDi mark matching the surrounding context's [direction] | 176 * Returns a unicode BiDi mark matching the surrounding context's [direction] |
| 177 * (not necessarily the direction of [text]). The function returns an LRM or | 177 * (not necessarily the direction of [text]). The function returns an LRM or |
| 178 * RLM if the overall directionality or the exit directionality of [text] is | 178 * RLM if the overall directionality or the exit directionality of [text] is |
| 179 * opposite the context directionality. Otherwise | 179 * opposite the context directionality. Otherwise |
| 180 * return the empty string. [isHtml] is true if [text] is HTML or | 180 * return the empty string. [isHtml] is true if [text] is HTML or |
| 181 * HTML-escaped. | 181 * HTML-escaped. |
| 182 */ | 182 */ |
| 183 String _resetDir(String text, TextDirection direction, bool isHtml) { | 183 String _resetDir(String text, TextDirection direction, bool isHtml) { |
| 184 // endsWithRtl and endsWithLtr are called only if needed (short-circuit). | 184 // endsWithRtl and endsWithLtr are called only if needed (short-circuit). |
| 185 if ((contextDirection == TextDirection.LTR && | 185 if ((contextDirection == TextDirection.LTR && |
| 186 (direction == TextDirection.RTL || | 186 (direction == TextDirection.RTL || |
| 187 Bidi.endsWithRtl(text, isHtml))) || | 187 Bidi.endsWithRtl(text, isHtml))) || |
| 188 (contextDirection == TextDirection.RTL && | 188 (contextDirection == TextDirection.RTL && |
| 189 (direction == TextDirection.LTR || | 189 (direction == TextDirection.LTR || |
| 190 Bidi.endsWithLtr(text, isHtml)))) { | 190 Bidi.endsWithLtr(text, isHtml)))) { |
| 191 if (contextDirection == TextDirection.LTR) { | 191 if (contextDirection == TextDirection.LTR) { |
| 192 return Bidi.LRM; | 192 return Bidi.LRM; |
| 193 } else { | 193 } else { |
| 194 return Bidi.RLM; | 194 return Bidi.RLM; |
| 195 } | 195 } |
| 196 } else { | 196 } else { |
| 197 return ''; | 197 return ''; |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 } | 200 } |
| OLD | NEW |