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 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 [Wikipedia](http://en.wikipedia.org/wiki/Bi-directional_text): | 9 * According to [Wikipedia](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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 contextDirection = TextDirection.UNKNOWN, _alwaysSpan = alwaysSpan; | 79 contextDirection = TextDirection.UNKNOWN, _alwaysSpan = alwaysSpan; |
80 | 80 |
81 /** Is true if the known context direction for this formatter is RTL. */ | 81 /** Is true if the known context direction for this formatter is RTL. */ |
82 bool get isRTL => contextDirection == TextDirection.RTL; | 82 bool get isRTL => contextDirection == TextDirection.RTL; |
83 | 83 |
84 /** | 84 /** |
85 * Escapes HTML-special characters of [text] so that the result can be | 85 * Escapes HTML-special characters of [text] so that the result can be |
86 * included verbatim in HTML source code, either in an element body or in an | 86 * included verbatim in HTML source code, either in an element body or in an |
87 * attribute value. | 87 * attribute value. |
88 */ | 88 */ |
89 String htmlEscape(String text) { | 89 @deprecated |
90 // TODO(alanknight): This is copied into here directly to avoid having a | 90 String htmlEscape(String text) => HTML_ESCAPE.convert(text); |
91 // dependency on the htmlescape library, which is difficult to do in a way | |
92 // that's compatible with both package: links and direct links in the SDK. | |
93 // Once pub is used in test.dart (Issue #4968) this should be removed. | |
94 // TODO(efortuna): A more efficient implementation. | |
95 return text.replaceAll("&", "&") | |
96 .replaceAll("<", "<") | |
97 .replaceAll(">", ">") | |
98 .replaceAll('"', """) | |
99 .replaceAll("'", "'"); | |
100 } | |
101 | 91 |
102 /** | 92 /** |
103 * Formats a string of a given (or estimated, if not provided) | 93 * Formats a string of a given (or estimated, if not provided) |
104 * [direction] for use in HTML output of the context directionality, so | 94 * [direction] for use in HTML output of the context directionality, so |
105 * an opposite-directionality string is neither garbled nor garbles what | 95 * an opposite-directionality string is neither garbled nor garbles what |
106 * follows it. | 96 * follows it. |
107 * If the input string's directionality doesn't match the context | 97 * If the input string's directionality doesn't match the context |
108 * directionality, we wrap it with a `span` tag and add a `dir` attribute | 98 * directionality, we wrap it with a `span` tag and add a `dir` attribute |
109 * (either "dir=rtl" or "dir=ltr"). | 99 * (either "dir=rtl" or "dir=ltr"). |
110 * If alwaysSpan was true when constructing the formatter, the input is always | 100 * If alwaysSpan was true when constructing the formatter, the input is always |
111 * wrapped with `span` tag, skipping the dir attribute when it's not needed. | 101 * wrapped with `span` tag, skipping the dir attribute when it's not needed. |
112 * | 102 * |
113 * If [resetDir] is true and the overall directionality or the exit | 103 * If [resetDir] is true and the overall directionality or the exit |
114 * directionality of [text] is opposite to the context directionality, | 104 * directionality of [text] is opposite to the context directionality, |
115 * a trailing unicode BiDi mark matching the context directionality is | 105 * a trailing unicode BiDi mark matching the context directionality is |
116 * appended (LRM or RLM). If [isHtml] is false, we HTML-escape the [text]. | 106 * appended (LRM or RLM). If [isHtml] is false, we HTML-escape the [text]. |
117 */ | 107 */ |
118 String wrapWithSpan(String text, {bool isHtml: false, bool resetDir: true, | 108 String wrapWithSpan(String text, {bool isHtml: false, bool resetDir: true, |
119 TextDirection direction}) { | 109 TextDirection direction}) { |
120 if (direction == null) direction = estimateDirection(text, isHtml: isHtml); | 110 if (direction == null) direction = estimateDirection(text, isHtml: isHtml); |
121 var result; | 111 var result; |
122 if (!isHtml) text = htmlEscape(text); | 112 if (!isHtml) text = HTML_ESCAPE.convert(text); |
123 var directionChange = contextDirection.isDirectionChange(direction); | 113 var directionChange = contextDirection.isDirectionChange(direction); |
124 if (_alwaysSpan || directionChange) { | 114 if (_alwaysSpan || directionChange) { |
125 var spanDirection = ''; | 115 var spanDirection = ''; |
126 if (directionChange) { | 116 if (directionChange) { |
127 spanDirection = ' dir=${direction.spanText}'; | 117 spanDirection = ' dir=${direction.spanText}'; |
128 } | 118 } |
129 result= '<span$spanDirection>$text</span>'; | 119 result= '<span$spanDirection>$text</span>'; |
130 } else { | 120 } else { |
131 result = text; | 121 result = text; |
132 } | 122 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 if (contextDirection == TextDirection.LTR) { | 183 if (contextDirection == TextDirection.LTR) { |
194 return Bidi.LRM; | 184 return Bidi.LRM; |
195 } else { | 185 } else { |
196 return Bidi.RLM; | 186 return Bidi.RLM; |
197 } | 187 } |
198 } else { | 188 } else { |
199 return ''; | 189 return ''; |
200 } | 190 } |
201 } | 191 } |
202 } | 192 } |
OLD | NEW |