Chromium Code Reviews| Index: sdk/lib/convert/html_escape.dart |
| diff --git a/sdk/lib/convert/html_escape.dart b/sdk/lib/convert/html_escape.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9383cfb78b4d46b8463db04f729894c46199c49e |
| --- /dev/null |
| +++ b/sdk/lib/convert/html_escape.dart |
| @@ -0,0 +1,46 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of dart.convert; |
| + |
| +const HTML_ESCAPE = const HtmlEscape(); |
| + |
| +class HtmlEscape extends Converter<String, String> { |
|
Bob Nystrom
2013/08/27 18:03:57
Document all of this.
|
| + |
| + const HtmlEscape(); |
| + |
| + String convert(String data) => _convert(data); |
| + |
| + StringConversionSink startChunkedConversion( |
| + ChunkedConversionSink<String> sink) { |
| + |
| + if (sink is! StringConversionSink) { |
| + sink = new StringConversionSink.from(sink); |
| + } |
| + return new _HtmlEscapeSink(sink); |
| + } |
| + |
| + static String _convert(String data) { |
| + return data.replaceAll("&", "&") |
| + .replaceAll("<", "<") |
| + .replaceAll(">", ">") |
| + .replaceAll('"', """) |
| + .replaceAll("'", "'") |
| + .replaceAll('\u00A0', ' '); |
| + } |
| + |
| +} |
| + |
| +class _HtmlEscapeSink extends StringConversionSinkBase { |
| + final StringConversionSink _sink; |
| + |
| + _HtmlEscapeSink(this._sink); |
| + |
| + void addSlice(String chunk, int start, int end, bool isLast) { |
| + _sink.add(HtmlEscape._convert(chunk.substring(start, end))); |
| + if (isLast) _sink.close(); |
| + } |
| + |
| + void close() => _sink.close(); |
| +} |