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 const HTML_ESCAPE = const HtmlEscape(); | |
8 | |
9 class HtmlEscapeMode { | |
10 final String _name; | |
11 final bool escapeLtGt; | |
12 final bool escapeQuot; | |
13 final bool escapeApos; | |
14 | |
15 static const HtmlEscapeMode UNKNOWN = | |
16 const HtmlEscapeMode._('unknown', true, true, true); | |
17 | |
18 static const HtmlEscapeMode ATTRIBUTE = | |
19 const HtmlEscapeMode._('attribute', false, true, false); | |
20 | |
21 static const HtmlEscapeMode ELEMENT = | |
22 const HtmlEscapeMode._('element', true, false, false); | |
23 | |
24 const HtmlEscapeMode._(this._name, this.escapeLtGt, this.escapeQuot, | |
25 this.escapeApos); | |
26 | |
27 String toString() => _name; | |
28 } | |
29 | |
30 class HtmlEscape extends Converter<String, String> { | |
31 final HtmlEscapeMode mode; | |
32 | |
33 const HtmlEscape([this.mode = HtmlEscapeMode.UNKNOWN]); | |
34 | |
35 String convert(String text) { | |
36 StringBuffer result = null; | |
37 for (int i = 0; i < text.length; i++) { | |
38 var ch = text[i]; | |
floitsch
2013/08/30 15:52:00
Nit: we usually type everything in the dart: libra
Jennifer Messerly
2013/08/30 18:13:59
it is a shame styles in different parts of the cod
| |
39 String replace = null; | |
40 switch (ch) { | |
41 case '&': replace = '&'; break; | |
42 case '\u00A0'/*NO-BREAK SPACE*/: replace = ' '; break; | |
43 case '"': if (mode.escapeQuot) replace = '"'; break; | |
44 case "'": if (mode.escapeApos) replace = '''; break; | |
45 case '<': if (mode.escapeLtGt) replace = '<'; break; | |
46 case '>': if (mode.escapeLtGt) replace = '>'; break; | |
47 } | |
48 if (replace != null) { | |
49 if (result == null) result = new StringBuffer(text.substring(0, i)); | |
50 result.write(replace); | |
51 } else if (result != null) { | |
52 result.write(ch); | |
53 } | |
54 } | |
55 | |
56 return result != null ? result.toString() : text; | |
57 } | |
58 | |
59 StringConversionSink startChunkedConversion( | |
60 ChunkedConversionSink<String> sink) { | |
61 | |
62 if (sink is! StringConversionSink) { | |
63 sink = new StringConversionSink.from(sink); | |
64 } | |
65 return new _HtmlEscapeSink(this, sink); | |
66 } | |
67 } | |
68 | |
69 class _HtmlEscapeSink extends StringConversionSinkBase { | |
70 final HtmlEscape _escape; | |
71 final StringConversionSink _sink; | |
72 | |
73 _HtmlEscapeSink(this._escape, this._sink); | |
74 | |
75 void addSlice(String chunk, int start, int end, bool isLast) { | |
76 _sink.add(_escape.convert(chunk.substring(start, end))); | |
floitsch
2013/08/30 15:52:00
Add:
TODO(floitsch): Optimize.
kevmoo-old
2013/09/05 20:30:06
Does the last change handle your request to optimi
| |
77 if (isLast) _sink.close(); | |
78 } | |
79 | |
80 void close() => _sink.close(); | |
81 } | |
OLD | NEW |