| 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 /** | 66 /** |
| 67 * Create a formatting object with a direction. If [alwaysSpan] is true we | 67 * Create a formatting object with a direction. If [alwaysSpan] is true we |
| 68 * should always use a `span` tag, even when the input directionality is | 68 * should always use a `span` tag, even when the input directionality is |
| 69 * neutral or matches the context, so that the DOM structure of the output | 69 * neutral or matches the context, so that the DOM structure of the output |
| 70 * does not depend on the combination of directionalities. | 70 * does not depend on the combination of directionalities. |
| 71 */ | 71 */ |
| 72 BidiFormatter.LTR([alwaysSpan=false]) : contextDirection = TextDirection.LTR, | 72 BidiFormatter.LTR([alwaysSpan=false]) : contextDirection = TextDirection.LTR, |
| 73 _alwaysSpan = alwaysSpan; | 73 _alwaysSpan = alwaysSpan; |
| 74 BidiFormatter.RTL([alwaysSpan=false]) : contextDirection = TextDirection.RTL, | 74 BidiFormatter.RTL([alwaysSpan=false]) : contextDirection = TextDirection.RTL, |
| 75 _alwaysSpan = alwaysSpan; | 75 _alwaysSpan = alwaysSpan; |
| 76 BidiFormatter.UNKNOWN([alwaysSpan=false]) : | 76 BidiFormatter.UNKNOWN([alwaysSpan=false]) : |
| 77 contextDirection = TextDirection.UNKNOWN, _alwaysSpan = alwaysSpan; | 77 contextDirection = TextDirection.UNKNOWN, _alwaysSpan = alwaysSpan; |
| 78 | 78 |
| 79 /** Is true if the known context direction for this formatter is RTL. */ | 79 /** Is true if the known context direction for this formatter is RTL. */ |
| 80 bool get isRTL => contextDirection == TextDirection.RTL; | 80 bool get isRTL => contextDirection == TextDirection.RTL; |
| 81 | 81 |
| 82 /** | 82 /** |
| 83 * Escapes HTML-special characters of [text] so that the result can be |
| 84 * included verbatim in HTML source code, either in an element body or in an |
| 85 * attribute value. |
| 86 */ |
| 87 String htmlEscape(String text) { |
| 88 // TODO(alanknight): This is copied into here directly to avoid having a |
| 89 // dependency on the htmlescape library, which is difficult to do in a way |
| 90 // that's compatible with both package: links and direct links in the SDK. |
| 91 // Once pub is used in test.dart (Issue #4968) this should be removed. |
| 92 // TODO(efortuna): A more efficient implementation. |
| 93 return text.replaceAll("&", "&") |
| 94 .replaceAll("<", "<") |
| 95 .replaceAll(">", ">") |
| 96 .replaceAll('"', """) |
| 97 .replaceAll("'", "'"); |
| 98 } |
| 99 |
| 100 /** |
| 83 * Formats a string of a given (or estimated, if not provided) | 101 * Formats a string of a given (or estimated, if not provided) |
| 84 * [direction] for use in HTML output of the context directionality, so | 102 * [direction] for use in HTML output of the context directionality, so |
| 85 * an opposite-directionality string is neither garbled nor garbles what | 103 * an opposite-directionality string is neither garbled nor garbles what |
| 86 * follows it. | 104 * follows it. |
| 87 * If the input string's directionality doesn't match the context | 105 * If the input string's directionality doesn't match the context |
| 88 * 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 |
| 89 * (either "dir=rtl" or "dir=ltr"). | 107 * (either "dir=rtl" or "dir=ltr"). |
| 90 * 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 |
| 91 * 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. |
| 92 * | 110 * |
| (...skipping 14 matching lines...) Expand all Loading... |
| 107 spanDirection = ' dir=${direction.spanText}'; | 125 spanDirection = ' dir=${direction.spanText}'; |
| 108 } | 126 } |
| 109 result= '<span$spanDirection>$text</span>'; | 127 result= '<span$spanDirection>$text</span>'; |
| 110 } else { | 128 } else { |
| 111 result = text; | 129 result = text; |
| 112 } | 130 } |
| 113 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); | 131 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); |
| 114 } | 132 } |
| 115 | 133 |
| 116 /** | 134 /** |
| 117 * Format [text] of a known (if specified) or estimated [direction] for use | 135 * Format [text] of a known (if specified) or estimated [direction] for use |
| 118 * in *plain-text* output of the context directionality, so an | 136 * in *plain-text* output of the context directionality, so an |
| 119 * opposite-directionality text is neither garbled nor garbles what follows | 137 * opposite-directionality text is neither garbled nor garbles what follows |
| 120 * it. Unlike wrapWithSpan, this makes use of unicode BiDi formatting | 138 * it. Unlike wrapWithSpan, this makes use of unicode BiDi formatting |
| 121 * characters instead of spans for wrapping. The returned string would be | 139 * characters instead of spans for wrapping. The returned string would be |
| 122 * RLE+text+PDF for RTL text, or LRE+text+PDF for LTR text. | 140 * RLE+text+PDF for RTL text, or LRE+text+PDF for LTR text. |
| 123 * | 141 * |
| 124 * If [resetDir] is true, and if the overall directionality or the exit | 142 * If [resetDir] is true, and if the overall directionality or the exit |
| 125 * directionality of text are opposite to the context directionality, | 143 * directionality of text are opposite to the context directionality, |
| 126 * a trailing unicode BiDi mark matching the context directionality is | 144 * a trailing unicode BiDi mark matching the context directionality is |
| 127 * appended (LRM or RLM). | 145 * appended (LRM or RLM). |
| 128 * | 146 * |
| 129 * 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 |
| 130 * do not allow markup, e.g. an 'option' tag. | 148 * do not allow markup, e.g. an 'option' tag. |
| 131 * 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 |
| 132 * [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 |
| 133 * or unescaped). | 151 * or unescaped). |
| 134 */ | 152 */ |
| 135 String wrapWithUnicode(String text, [bool isHtml=false, bool resetDir=true, | 153 String wrapWithUnicode(String text, [bool isHtml=false, bool resetDir=true, |
| 136 TextDirection direction]) { | 154 TextDirection direction]) { |
| 137 if (direction == null) direction = estimateDirection(text, isHtml); | 155 if (direction == null) direction = estimateDirection(text, isHtml); |
| 138 var result = text; | 156 var result = text; |
| 139 if (contextDirection.isDirectionChange(direction)) { | 157 if (contextDirection.isDirectionChange(direction)) { |
| 140 result = '''${direction == TextDirection.RTL ? RLE : LRE}$text$PDF'''; | 158 result = '''${direction == TextDirection.RTL ? RLE : LRE}$text$PDF'''; |
| 141 } | 159 } |
| 142 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); | 160 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); |
| 143 } | 161 } |
| 144 | 162 |
| 145 /** | 163 /** |
| 146 * Estimates the directionality of [text] using the best known | 164 * Estimates the directionality of [text] using the best known |
| 147 * general-purpose method (using relative word counts). A | 165 * general-purpose method (using relative word counts). A |
| 148 * TextDirection.UNKNOWN return value indicates completely neutral input. | 166 * TextDirection.UNKNOWN return value indicates completely neutral input. |
| 149 * [isHtml] is true if [text] HTML or HTML-escaped. | 167 * [isHtml] is true if [text] HTML or HTML-escaped. |
| 150 */ | 168 */ |
| 151 TextDirection estimateDirection(String text, [bool isHtml=false]) { | 169 TextDirection estimateDirection(String text, [bool isHtml=false]) { |
| 152 return estimateDirectionOfText(text, isHtml); //TODO~!!! | 170 return estimateDirectionOfText(text, isHtml); //TODO~!!! |
| 153 } | 171 } |
| 154 | 172 |
| 155 /** | 173 /** |
| 156 * Returns a unicode BiDi mark matching the surrounding context's [direction] | 174 * Returns a unicode BiDi mark matching the surrounding context's [direction] |
| 157 * (not necessarily the direction of [text]). The function returns an LRM or | 175 * (not necessarily the direction of [text]). The function returns an LRM or |
| 158 * RLM if the overall directionality or the exit directionality of [text] is | 176 * RLM if the overall directionality or the exit directionality of [text] is |
| 159 * opposite the context directionality. Otherwise | 177 * opposite the context directionality. Otherwise |
| 160 * return the empty string. [isHtml] is true if [text] is HTML or | 178 * return the empty string. [isHtml] is true if [text] is HTML or |
| 161 * HTML-escaped. | 179 * HTML-escaped. |
| 162 */ | 180 */ |
| 163 String _resetDir(String text, TextDirection direction, bool isHtml) { | 181 String _resetDir(String text, TextDirection direction, bool isHtml) { |
| 164 // endsWithRtl and endsWithLtr are called only if needed (short-circuit). | 182 // endsWithRtl and endsWithLtr are called only if needed (short-circuit). |
| 165 if ((contextDirection == TextDirection.LTR && | 183 if ((contextDirection == TextDirection.LTR && |
| 166 (direction == TextDirection.RTL || | 184 (direction == TextDirection.RTL || |
| 167 endsWithRtl(text, isHtml))) || | 185 endsWithRtl(text, isHtml))) || |
| 168 (contextDirection == TextDirection.RTL && | 186 (contextDirection == TextDirection.RTL && |
| 169 (direction == TextDirection.LTR || | 187 (direction == TextDirection.LTR || |
| 170 endsWithLtr(text, isHtml)))) { | 188 endsWithLtr(text, isHtml)))) { |
| 171 if (contextDirection == TextDirection.LTR) { | 189 if (contextDirection == TextDirection.LTR) { |
| 172 return LRM; | 190 return LRM; |
| 173 } else { | 191 } else { |
| 174 return RLM; | 192 return RLM; |
| 175 } | 193 } |
| 176 } else { | 194 } else { |
| 177 return ''; | 195 return ''; |
| 178 } | 196 } |
| 179 } | 197 } |
| 180 } | 198 } |
| OLD | NEW |