| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, 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 part of dart.convert; | |
| 6 | |
| 7 // TODO(floitsch) - Document - Issue 13097 | |
| 8 const HtmlEscape HTML_ESCAPE = const HtmlEscape(); | |
| 9 | |
| 10 class HtmlEscapeMode { | |
| 11 final String _name; | |
| 12 final bool escapeLtGt; | |
| 13 final bool escapeQuot; | |
| 14 final bool escapeApos; | |
| 15 final bool escapeSlash; | |
| 16 | |
| 17 // TODO(floitsch) - Document - Issue 13097 | |
| 18 static const HtmlEscapeMode UNKNOWN = | |
| 19 const HtmlEscapeMode._('unknown', true, true, true, true); | |
| 20 | |
| 21 // TODO(floitsch) - Document - Issue 13097 | |
| 22 static const HtmlEscapeMode ATTRIBUTE = | |
| 23 const HtmlEscapeMode._('attribute', false, true, false, false); | |
| 24 | |
| 25 // TODO(floitsch) - Document - Issue 13097 | |
| 26 static const HtmlEscapeMode ELEMENT = | |
| 27 const HtmlEscapeMode._('element', true, false, false, true); | |
| 28 | |
| 29 // TODO(floitsch) - Document - Issue 13097 | |
| 30 const HtmlEscapeMode._(this._name, this.escapeLtGt, this.escapeQuot, | |
| 31 this.escapeApos, this.escapeSlash); | |
| 32 | |
| 33 String toString() => _name; | |
| 34 } | |
| 35 | |
| 36 // TODO(floitsch) - Document - Issue 13097 | |
| 37 class HtmlEscape extends Converter<String, String> { | |
| 38 | |
| 39 // TODO(floitsch) - Document - Issue 13097 | |
| 40 final HtmlEscapeMode mode; | |
| 41 | |
| 42 // TODO(floitsch) - Document - Issue 13097 | |
| 43 const HtmlEscape([this.mode = HtmlEscapeMode.UNKNOWN]); | |
| 44 | |
| 45 String convert(String text) { | |
| 46 var val = _convert(text, 0, text.length); | |
| 47 return val == null ? text : val; | |
| 48 } | |
| 49 | |
| 50 String _convert(String text, int start, int end) { | |
| 51 StringBuffer result = null; | |
| 52 for (int i = start; i < end; i++) { | |
| 53 var ch = text[i]; | |
| 54 String replace = null; | |
| 55 switch (ch) { | |
| 56 case '&': replace = '&'; break; | |
| 57 case '\u00A0'/*NO-BREAK SPACE*/: replace = ' '; break; | |
| 58 case '"': if (mode.escapeQuot) replace = '"'; break; | |
| 59 case "'": if (mode.escapeApos) replace = '''; break; | |
| 60 case '<': if (mode.escapeLtGt) replace = '<'; break; | |
| 61 case '>': if (mode.escapeLtGt) replace = '>'; break; | |
| 62 case '/': if (mode.escapeSlash) replace = '/'; break; | |
| 63 } | |
| 64 if (replace != null) { | |
| 65 if (result == null) result = new StringBuffer(text.substring(start, i)); | |
| 66 result.write(replace); | |
| 67 } else if (result != null) { | |
| 68 result.write(ch); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 return result != null ? result.toString() : null; | |
| 73 } | |
| 74 | |
| 75 StringConversionSink startChunkedConversion(Sink<String> sink) { | |
| 76 if (sink is! StringConversionSink) { | |
| 77 sink = new StringConversionSink.from(sink); | |
| 78 } | |
| 79 return new _HtmlEscapeSink(this, sink); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 class _HtmlEscapeSink extends StringConversionSinkBase { | |
| 84 final HtmlEscape _escape; | |
| 85 final StringConversionSink _sink; | |
| 86 | |
| 87 _HtmlEscapeSink(this._escape, this._sink); | |
| 88 | |
| 89 void addSlice(String chunk, int start, int end, bool isLast) { | |
| 90 var val = _escape._convert(chunk, start, end); | |
| 91 if(val == null) { | |
| 92 _sink.addSlice(chunk, start, end, isLast); | |
| 93 } else { | |
| 94 _sink.add(val); | |
| 95 if (isLast) _sink.close(); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void close() => _sink.close(); | |
| 100 } | |
| OLD | NEW |