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 21 matching lines...) Expand all Loading... |
32 | 32 |
33 #include "modules/encoding/TextDecoder.h" | 33 #include "modules/encoding/TextDecoder.h" |
34 | 34 |
35 #include "bindings/core/v8/ExceptionState.h" | 35 #include "bindings/core/v8/ExceptionState.h" |
36 #include "core/dom/ExceptionCode.h" | 36 #include "core/dom/ExceptionCode.h" |
37 #include "wtf/StringExtras.h" | 37 #include "wtf/StringExtras.h" |
38 #include "wtf/text/TextEncodingRegistry.h" | 38 #include "wtf/text/TextEncodingRegistry.h" |
39 | 39 |
40 namespace blink { | 40 namespace blink { |
41 | 41 |
42 TextDecoder* TextDecoder::create(const String& label, const Dictionary& options,
ExceptionState& exceptionState) | 42 TextDecoder* TextDecoder::create(const String& label, const TextDecoderOptions&
options, ExceptionState& exceptionState) |
43 { | 43 { |
44 WTF::TextEncoding encoding(label); | 44 WTF::TextEncoding encoding(label); |
45 // The replacement encoding is not valid, but the Encoding API also | 45 // The replacement encoding is not valid, but the Encoding API also |
46 // rejects aliases of the replacement encoding. | 46 // rejects aliases of the replacement encoding. |
47 if (!encoding.isValid() || !strcasecmp(encoding.name(), "replacement")) { | 47 if (!encoding.isValid() || !strcasecmp(encoding.name(), "replacement")) { |
48 exceptionState.throwTypeError("The encoding label provided ('" + label +
"') is invalid."); | 48 exceptionState.throwTypeError("The encoding label provided ('" + label +
"') is invalid."); |
49 return 0; | 49 return 0; |
50 } | 50 } |
51 | 51 |
52 bool fatal = false; | 52 return new TextDecoder(encoding, options.fatal(), options.ignoreBOM()); |
53 DictionaryHelper::get(options, "fatal", fatal); | |
54 | |
55 bool ignoreBOM = false; | |
56 DictionaryHelper::get(options, "ignoreBOM", ignoreBOM); | |
57 | |
58 return new TextDecoder(encoding, fatal, ignoreBOM); | |
59 } | 53 } |
60 | 54 |
61 | 55 |
62 TextDecoder::TextDecoder(const WTF::TextEncoding& encoding, bool fatal, bool ign
oreBOM) | 56 TextDecoder::TextDecoder(const WTF::TextEncoding& encoding, bool fatal, bool ign
oreBOM) |
63 : m_encoding(encoding) | 57 : m_encoding(encoding) |
64 , m_codec(newTextCodec(encoding)) | 58 , m_codec(newTextCodec(encoding)) |
65 , m_fatal(fatal) | 59 , m_fatal(fatal) |
66 , m_ignoreBOM(ignoreBOM) | 60 , m_ignoreBOM(ignoreBOM) |
67 , m_bomSeen(false) | 61 , m_bomSeen(false) |
68 { | 62 { |
69 } | 63 } |
70 | 64 |
71 TextDecoder::~TextDecoder() | 65 TextDecoder::~TextDecoder() |
72 { | 66 { |
73 } | 67 } |
74 | 68 |
75 String TextDecoder::encoding() const | 69 String TextDecoder::encoding() const |
76 { | 70 { |
77 String name = String(m_encoding.name()).lower(); | 71 String name = String(m_encoding.name()).lower(); |
78 // Where possible, encoding aliases should be handled by changes to Chromium
's ICU or Blink's WTF. | 72 // 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. | 73 // The same codec is used, but WTF maintains a different name/identity for t
hese. |
80 if (name == "iso-8859-1" || name == "us-ascii") | 74 if (name == "iso-8859-1" || name == "us-ascii") |
81 return "windows-1252"; | 75 return "windows-1252"; |
82 return name; | 76 return name; |
83 } | 77 } |
84 | 78 |
85 String TextDecoder::decode(ArrayBufferView* input, const Dictionary& options, Ex
ceptionState& exceptionState) | 79 String TextDecoder::decode(ArrayBufferView* input, const TextDecodeOptions& opti
ons, ExceptionState& exceptionState) |
86 { | 80 { |
87 bool stream = false; | |
88 DictionaryHelper::get(options, "stream", stream); | |
89 | |
90 const char* start = input ? static_cast<const char*>(input->baseAddress()) :
0; | 81 const char* start = input ? static_cast<const char*>(input->baseAddress()) :
0; |
91 size_t length = input ? input->byteLength() : 0; | 82 size_t length = input ? input->byteLength() : 0; |
92 | 83 |
93 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF; | 84 WTF::FlushBehavior flush = options.stream() ? WTF::DoNotFlush : WTF::DataEOF
; |
94 | 85 |
95 bool sawError = false; | 86 bool sawError = false; |
96 String s = m_codec->decode(start, length, flush, m_fatal, sawError); | 87 String s = m_codec->decode(start, length, flush, m_fatal, sawError); |
97 | 88 |
98 if (m_fatal && sawError) { | 89 if (m_fatal && sawError) { |
99 exceptionState.throwDOMException(EncodingError, "The encoded data was no
t valid."); | 90 exceptionState.throwDOMException(EncodingError, "The encoded data was no
t valid."); |
100 return String(); | 91 return String(); |
101 } | 92 } |
102 | 93 |
103 if (!m_ignoreBOM && !m_bomSeen && !s.isEmpty()) { | 94 if (!m_ignoreBOM && !m_bomSeen && !s.isEmpty()) { |
104 m_bomSeen = true; | 95 m_bomSeen = true; |
105 String name(m_encoding.name()); | 96 String name(m_encoding.name()); |
106 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0
] == 0xFEFF) | 97 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0
] == 0xFEFF) |
107 s.remove(0); | 98 s.remove(0); |
108 } | 99 } |
109 | 100 |
110 if (flush) | 101 if (flush) |
111 m_bomSeen = false; | 102 m_bomSeen = false; |
112 | 103 |
113 return s; | 104 return s; |
114 } | 105 } |
115 | 106 |
| 107 String TextDecoder::decode(ExceptionState& exceptionState) |
| 108 { |
| 109 TextDecodeOptions* options = TextDecodeOptions::create(); |
| 110 return decode(0, *options, exceptionState); |
| 111 } |
| 112 |
116 } // namespace blink | 113 } // namespace blink |
OLD | NEW |