OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 | 32 |
33 #include "modules/encoding/TextDecoder.h" | 33 #include "modules/encoding/TextDecoder.h" |
34 | 34 |
| 35 #include "bindings/core/v8/DictionaryHelper.h" |
35 #include "bindings/core/v8/ExceptionState.h" | 36 #include "bindings/core/v8/ExceptionState.h" |
36 #include "core/dom/ExceptionCode.h" | 37 #include "core/dom/ExceptionCode.h" |
37 #include "wtf/StringExtras.h" | 38 #include "wtf/StringExtras.h" |
38 #include "wtf/text/TextEncodingRegistry.h" | 39 #include "wtf/text/TextEncodingRegistry.h" |
39 | 40 |
40 namespace WebCore { | 41 namespace WebCore { |
41 | 42 |
42 TextDecoder* TextDecoder::create(const String& label, const Dictionary& options,
ExceptionState& exceptionState) | 43 TextDecoder* TextDecoder::create(const String& label, const Dictionary& options,
ExceptionState& exceptionState) |
43 { | 44 { |
44 WTF::TextEncoding encoding(label); | 45 WTF::TextEncoding encoding(label); |
45 // The replacement encoding is not valid, but the Encoding API also | 46 // The replacement encoding is not valid, but the Encoding API also |
46 // rejects aliases of the replacement encoding. | 47 // rejects aliases of the replacement encoding. |
47 if (!encoding.isValid() || !strcasecmp(encoding.name(), "replacement")) { | 48 if (!encoding.isValid() || !strcasecmp(encoding.name(), "replacement")) { |
48 exceptionState.throwTypeError("The encoding label provided ('" + label +
"') is invalid."); | 49 exceptionState.throwTypeError("The encoding label provided ('" + label +
"') is invalid."); |
49 return 0; | 50 return 0; |
50 } | 51 } |
51 | 52 |
52 bool fatal = false; | 53 bool fatal = false; |
53 options.get("fatal", fatal); | 54 DictionaryHelper::get(options, "fatal", fatal); |
54 | 55 |
55 bool ignoreBOM = false; | 56 bool ignoreBOM = false; |
56 options.get("ignoreBOM", ignoreBOM); | 57 DictionaryHelper::get(options, "ignoreBOM", ignoreBOM); |
57 | 58 |
58 return new TextDecoder(encoding, fatal, ignoreBOM); | 59 return new TextDecoder(encoding, fatal, ignoreBOM); |
59 } | 60 } |
60 | 61 |
61 | 62 |
62 TextDecoder::TextDecoder(const WTF::TextEncoding& encoding, bool fatal, bool ign
oreBOM) | 63 TextDecoder::TextDecoder(const WTF::TextEncoding& encoding, bool fatal, bool ign
oreBOM) |
63 : m_encoding(encoding) | 64 : m_encoding(encoding) |
64 , m_codec(newTextCodec(encoding)) | 65 , m_codec(newTextCodec(encoding)) |
65 , m_fatal(fatal) | 66 , m_fatal(fatal) |
66 , m_ignoreBOM(ignoreBOM) | 67 , m_ignoreBOM(ignoreBOM) |
(...skipping 11 matching lines...) Expand all Loading... |
78 // Where possible, encoding aliases should be handled by changes to Chromium
's ICU or Blink's WTF. | 79 // Where possible, encoding aliases should be handled by changes to Chromium
's ICU or Blink's WTF. |
79 // The same codec is used, but WTF maintains a different name/identity for t
hese. | 80 // The same codec is used, but WTF maintains a different name/identity for t
hese. |
80 if (name == "iso-8859-1" || name == "us-ascii") | 81 if (name == "iso-8859-1" || name == "us-ascii") |
81 return "windows-1252"; | 82 return "windows-1252"; |
82 return name; | 83 return name; |
83 } | 84 } |
84 | 85 |
85 String TextDecoder::decode(ArrayBufferView* input, const Dictionary& options, Ex
ceptionState& exceptionState) | 86 String TextDecoder::decode(ArrayBufferView* input, const Dictionary& options, Ex
ceptionState& exceptionState) |
86 { | 87 { |
87 bool stream = false; | 88 bool stream = false; |
88 options.get("stream", stream); | 89 DictionaryHelper::get(options, "stream", stream); |
89 | 90 |
90 const char* start = input ? static_cast<const char*>(input->baseAddress()) :
0; | 91 const char* start = input ? static_cast<const char*>(input->baseAddress()) :
0; |
91 size_t length = input ? input->byteLength() : 0; | 92 size_t length = input ? input->byteLength() : 0; |
92 | 93 |
93 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF; | 94 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF; |
94 | 95 |
95 bool sawError = false; | 96 bool sawError = false; |
96 String s = m_codec->decode(start, length, flush, m_fatal, sawError); | 97 String s = m_codec->decode(start, length, flush, m_fatal, sawError); |
97 | 98 |
98 if (m_fatal && sawError) { | 99 if (m_fatal && sawError) { |
99 exceptionState.throwDOMException(EncodingError, "The encoded data was no
t valid."); | 100 exceptionState.throwDOMException(EncodingError, "The encoded data was no
t valid."); |
100 return String(); | 101 return String(); |
101 } | 102 } |
102 | 103 |
103 if (!m_ignoreBOM && !m_bomSeen && !s.isEmpty()) { | 104 if (!m_ignoreBOM && !m_bomSeen && !s.isEmpty()) { |
104 m_bomSeen = true; | 105 m_bomSeen = true; |
105 String name(m_encoding.name()); | 106 String name(m_encoding.name()); |
106 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0
] == 0xFEFF) | 107 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0
] == 0xFEFF) |
107 s.remove(0); | 108 s.remove(0); |
108 } | 109 } |
109 | 110 |
110 if (flush) | 111 if (flush) |
111 m_bomSeen = false; | 112 m_bomSeen = false; |
112 | 113 |
113 return s; | 114 return s; |
114 } | 115 } |
115 | 116 |
116 } // namespace WebCore | 117 } // namespace WebCore |
OLD | NEW |