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