#include "modules/encoding/TextDecoder.h"
-#include "bindings/v8/ExceptionState.h"
+#include "bindings/core/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
+#include "wtf/StringExtras.h"
#include "wtf/text/TextEncodingRegistry.h"
-namespace WebCore {
+namespace blink {
-PassRefPtrWillBeRawPtr<TextDecoder> TextDecoder::create(const String& label, const Dictionary& options, ExceptionState& exceptionState)
+TextDecoder* TextDecoder::create(const String& label, const TextDecoderOptions& options, ExceptionState& exceptionState)
{
- const String& encodingLabel = label.isNull() ? String("utf-8") : label;
-
- WTF::TextEncoding encoding(encodingLabel);
- if (!encoding.isValid()) {
- exceptionState.throwTypeError("The encoding label provided ('" + encodingLabel + "') is invalid.");
- return nullptr;
+ WTF::TextEncoding encoding(label);
+ // The replacement encoding is not valid, but the Encoding API also
+ // rejects aliases of the replacement encoding.
+ if (!encoding.isValid() || !strcasecmp(encoding.name(), "replacement")) {
+ exceptionState.throwTypeError("The encoding label provided ('" + label + "') is invalid.");
+ return 0;
}
- bool fatal = false;
- options.get("fatal", fatal);
-
- return adoptRefWillBeNoop(new TextDecoder(encoding.name(), fatal));
+ return new TextDecoder(encoding, options.fatal(), options.ignoreBOM());
}
-TextDecoder::TextDecoder(const String& encoding, bool fatal)
+TextDecoder::TextDecoder(const WTF::TextEncoding& encoding, bool fatal, bool ignoreBOM)
: m_encoding(encoding)
- , m_codec(newTextCodec(m_encoding))
+ , m_codec(newTextCodec(encoding))
, m_fatal(fatal)
+ , m_ignoreBOM(ignoreBOM)
, m_bomSeen(false)
{
}
return name;
}
-String TextDecoder::decode(ArrayBufferView* input, const Dictionary& options, ExceptionState& exceptionState)
+String TextDecoder::decode(ArrayBufferView* input, const TextDecodeOptions& options, ExceptionState& exceptionState)
{
- bool stream = false;
- options.get("stream", stream);
-
const char* start = input ? static_cast<const char*>(input->baseAddress()) : 0;
size_t length = input ? input->byteLength() : 0;
- WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF;
+ WTF::FlushBehavior flush = options.stream() ? WTF::DoNotFlush : WTF::DataEOF;
bool sawError = false;
String s = m_codec->decode(start, length, flush, m_fatal, sawError);
return String();
}
- if (!m_bomSeen && !s.isEmpty()) {
+ if (!m_ignoreBOM && !m_bomSeen && !s.isEmpty()) {
m_bomSeen = true;
String name(m_encoding.name());
if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0] == 0xFEFF)
return s;
}
-} // namespace WebCore
+String TextDecoder::decode(ExceptionState& exceptionState)
+{
+ TextDecodeOptions* options = TextDecodeOptions::create();
+ return decode(0, *options, exceptionState);
+}
+
+} // namespace blink