| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #library('bidi_formatter'); |
| 6 |
| 7 #import('bidi_utils.dart'); |
| 8 |
| 9 /** |
| 10 * Bidi stands for Bi-directional text. |
| 11 * According to http://en.wikipedia.org/wiki/Bi-directional_text: |
| 12 * Bi-directional text is text containing text in both text directionalities, |
| 13 * both right-to-left (RTL) and left-to-right (LTR). It generally involves text |
| 14 * containing different types of alphabets, but may also refer to boustrophedon, |
| 15 * which is changing text directionality in each row. |
| 16 * |
| 17 * Utility class for formatting display text in a potentially |
| 18 * opposite-directionality context without garbling layout issues. |
| 19 * Mostly a very "slimmed-down" and dart-ified port of the Closure Birectional |
| 20 * formatting libary. If there is a utility in the Closure library (or ICU, or |
| 21 * elsewhere) that you would like this formatter to make available, please |
| 22 * contact the Dart team. |
| 23 * |
| 24 * Provides the following functionality: |
| 25 * |
| 26 * 1. *BiDi Wrapping* |
| 27 * When text in one language is mixed into a document in another, opposite- |
| 28 * directionality language, e.g. when an English business name is embedded in a |
| 29 * Hebrew web page, both the inserted string and the text following it may be |
| 30 * displayed incorrectly unless the inserted string is explicitly separated |
| 31 * from the surrounding text in a "wrapper" that declares its directionality at |
| 32 * the start and then resets it back at the end. This wrapping can be done in |
| 33 * HTML mark-up (e.g. a 'span dir=rtl' tag) or - only in contexts where mark-up |
| 34 * can not be used - in Unicode BiDi formatting codes (LRE|RLE and PDF). |
| 35 * Providing such wrapping services is the basic purpose of the BiDi formatter. |
| 36 * |
| 37 * 2. *Directionality estimation* |
| 38 * How does one know whether a string about to be inserted into surrounding |
| 39 * text has the same directionality? Well, in many cases, one knows that this |
| 40 * must be the case when writing the code doing the insertion, e.g. when a |
| 41 * localized message is inserted into a localized page. In such cases there is |
| 42 * no need to involve the BiDi formatter at all. In the remaining cases, e.g. |
| 43 * when the string is user-entered or comes from a database, the language of |
| 44 * the string (and thus its directionality) is not known a priori, and must be |
| 45 * estimated at run-time. The BiDi formatter does this automatically. |
| 46 * |
| 47 * 3. *Escaping* |
| 48 * When wrapping plain text - i.e. text that is not already HTML or HTML- |
| 49 * escaped - in HTML mark-up, the text must first be HTML-escaped to prevent XSS |
| 50 * attacks and other nasty business. This of course is always true, but the |
| 51 * escaping cannot be done after the string has already been wrapped in |
| 52 * mark-up, so the BiDi formatter also serves as a last chance and includes |
| 53 * escaping services. |
| 54 * |
| 55 * Thus, in a single call, the formatter will escape the input string as |
| 56 * specified, determine its directionality, and wrap it as necessary. It is |
| 57 * then up to the caller to insert the return value in the output. |
| 58 */ |
| 59 |
| 60 #import('dart:web'); |
| 61 |
| 62 class BidiFormatter { |
| 63 |
| 64 /** The direction of the surrounding text (the context). */ |
| 65 TextDirection contextDirection; |
| 66 |
| 67 /** |
| 68 * Indicates if we should always wrap the formatted text in a <span<,. |
| 69 */ |
| 70 bool _alwaysSpan; |
| 71 |
| 72 /** |
| 73 * Create a formatting object with a direction. If [alwaysSpan] is true we |
| 74 * should always use a `span` tag, even when the input directionality is |
| 75 * neutral or matches the context, so that the DOM structure of the output |
| 76 * does not depend on the combination of directionalities. |
| 77 */ |
| 78 BidiFormatter.LTR([alwaysSpan=false]) : contextDirection = TextDirection.LTR, |
| 79 _alwaysSpan = alwaysSpan; |
| 80 BidiFormatter.RTL([alwaysSpan=false]) : contextDirection = TextDirection.RTL, |
| 81 _alwaysSpan = alwaysSpan; |
| 82 BidiFormatter.UNKNOWN([alwaysSpan=false]) : |
| 83 contextDirection = TextDirection.UNKNOWN, _alwaysSpan = alwaysSpan; |
| 84 |
| 85 /** Is true if the context direction for this formatter is RTL. */ |
| 86 bool get isRTL() => contextDirection == TextDirection.RTL; |
| 87 |
| 88 /** |
| 89 * Formats a string of a given (or estimated, if not provided) |
| 90 * [direction] for use in HTML output of the context directionality, so |
| 91 * an opposite-directionality string is neither garbled nor garbles what |
| 92 * follows it. |
| 93 * If the input string's directionality doesn't match the context |
| 94 * directionality, we wrap it with a `span` tag and add a `dir` attribute |
| 95 * (either "dir=rtl" or "dir=ltr"). |
| 96 * If alwaysSpan was true when constructing the formatter, the input is always |
| 97 * wrapped with `span` tag, skipping the dir attribute when it's not needed. |
| 98 * |
| 99 * If [resetDir] is true and the overall directionality or the exit |
| 100 * directionality of [text] is opposite to the context directionality, |
| 101 * a trailing unicode BiDi mark matching the context directionality is |
| 102 * appended (LRM or RLM). If [isHtml] is false, we HTML-escape the [text]. |
| 103 */ |
| 104 String wrapWithSpan(String text, [bool isHtml=false, bool resetDir=true, |
| 105 TextDirection direction]) { |
| 106 if (direction == null) direction = estimateDirection(text, isHtml); |
| 107 var result; |
| 108 if (!isHtml) text = htmlEscape(text); |
| 109 var directionChange = contextDirection.isDirectionChange(direction); |
| 110 if (_alwaysSpan || directionChange) { |
| 111 var spanDirection = ''; |
| 112 if (directionChange) { |
| 113 spanDirection = ' dir=${direction.spanText}'; |
| 114 } |
| 115 result= '<span$spanDirection>$text</span>'; |
| 116 } else { |
| 117 result = text; |
| 118 } |
| 119 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); |
| 120 } |
| 121 |
| 122 /** |
| 123 * Format [text] of a known (if specified) or estimated [direction] for use |
| 124 * in *plain-text* output of the context directionality, so an |
| 125 * opposite-directionality text is neither garbled nor garbles what follows |
| 126 * it. Unlike wrapWithSpan, this makes use of unicode BiDi formatting |
| 127 * characters instead of spans for wrapping. The returned string would be |
| 128 * RLE+text+PDF for RTL text, or LRE+text+PDF for LTR text. |
| 129 * |
| 130 * If [resetDir] is true, and if the overall directionality or the exit |
| 131 * directionality of text are opposite to the context directionality, |
| 132 * a trailing unicode BiDi mark matching the context directionality is |
| 133 * appended (LRM or RLM). |
| 134 * |
| 135 * In HTML, the *only* valid use of this function is inside of elements that |
| 136 * do not allow markup, e.g. an 'option' tag. |
| 137 * This function does *not* do HTML-escaping regardless of the value of |
| 138 * [isHtml]. [isHtml] is used to designate if the text contains HTML (escaped |
| 139 * or unescaped). |
| 140 */ |
| 141 String wrapWithUnicode(String text, [bool isHtml=false, bool resetDir=true, |
| 142 TextDirection direction]) { |
| 143 if (direction == null) direction = estimateDirection(text, isHtml); |
| 144 var result = text; |
| 145 if (contextDirection.isDirectionChange(direction)) { |
| 146 result = '''${direction == TextDirection.RTL ? |
| 147 BidiUtils.RLE : |
| 148 BidiUtils.LRE}$text${BidiUtils.PDF}'''; |
| 149 } |
| 150 return result.concat(resetDir? _resetDir(text, direction, isHtml) : ''); |
| 151 } |
| 152 |
| 153 /** |
| 154 * Estimates the directionality of [text] using the best known |
| 155 * general-purpose method (using relative word counts). A |
| 156 * TextDirection.UNKNOWN return value indicates completely neutral input. |
| 157 * [isHtml] is true if [text] HTML or HTML-escaped. |
| 158 */ |
| 159 TextDirection estimateDirection(String text, [bool isHtml=false]) { |
| 160 return BidiUtils.estimateDirection(text, isHtml); |
| 161 } |
| 162 |
| 163 /** |
| 164 * Returns a unicode BiDi mark matching the surrounding context's [direction] |
| 165 * (not necessarily the direction of [text]). The function returns an LRM or |
| 166 * RLM if the overall directionality or the exit directionality of [text] is |
| 167 * opposite the context directionality. Otherwise |
| 168 * return the empty string. [isHtml] is true if [text] is HTML or |
| 169 * HTML-escaped. |
| 170 */ |
| 171 String _resetDir(String text, TextDirection direction, bool isHtml) { |
| 172 // endsWithRtl and endsWithLtr are called only if needed (short-circuit). |
| 173 if ((contextDirection == TextDirection.LTR && |
| 174 (direction == TextDirection.RTL || |
| 175 BidiUtils.endsWithRtl(text, isHtml))) || |
| 176 (contextDirection == TextDirection.RTL && |
| 177 (direction == TextDirection.LTR || |
| 178 BidiUtils.endsWithLtr(text, isHtml)))) { |
| 179 if (contextDirection == TextDirection.LTR) { |
| 180 return BidiUtils.LRM; |
| 181 } else { |
| 182 return BidiUtils.RLM; |
| 183 } |
| 184 } else { |
| 185 return ''; |
| 186 } |
| 187 } |
| 188 } |
| OLD | NEW |