| 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 20 matching lines...) Expand all Loading... |
| 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/v8/ExceptionState.h" | 35 #include "bindings/v8/ExceptionState.h" |
| 36 #include "core/dom/ExceptionCode.h" | 36 #include "core/dom/ExceptionCode.h" |
| 37 #include "wtf/text/TextEncodingRegistry.h" | 37 #include "wtf/text/TextEncodingRegistry.h" |
| 38 | 38 |
| 39 namespace WebCore { | 39 namespace WebCore { |
| 40 | 40 |
| 41 PassRefPtr<TextDecoder> TextDecoder::create(const String& label, const Dictionar
y& options, ExceptionState& exceptionState) | 41 PassRefPtr<TextDecoder> TextDecoder::create(const String& utfLabel, const Dictio
nary& options, ExceptionState& exceptionState) |
| 42 { | 42 { |
| 43 const String& encodingLabel = label.isNull() ? String("utf-8") : label; | 43 const String& encodingLabel = utfLabel.isNull() ? String("utf-8") : utfLabel
; |
| 44 | 44 |
| 45 WTF::TextEncoding encoding(encodingLabel); | 45 WTF::TextEncoding encoding(encodingLabel); |
| 46 if (!encoding.isValid()) { | 46 if (!encoding.isValid()) { |
| 47 exceptionState.throwTypeError("The encoding label provided ('" + encodin
gLabel + "') is invalid."); | 47 exceptionState.throwTypeError("The encoding label provided ('" + encodin
gLabel + "') is invalid."); |
| 48 return 0; | 48 return 0; |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool fatal = false; | 51 bool fatal = false; |
| 52 options.get("fatal", fatal); | 52 options.get("fatal", fatal); |
| 53 | 53 |
| 54 return adoptRef(new TextDecoder(encoding.name(), fatal)); | 54 return adoptRef(new TextDecoder(encodingLabel, encoding.name(), fatal)); |
| 55 } | 55 } |
| 56 | 56 |
| 57 | 57 TextDecoder::TextDecoder(const String& label, const String& name, bool fatal) |
| 58 TextDecoder::TextDecoder(const String& encoding, bool fatal) | 58 : m_encoding(name) |
| 59 : m_encoding(encoding) | 59 , m_codec(newTextCodec(label)) |
| 60 , m_codec(newTextCodec(m_encoding)) | |
| 61 , m_fatal(fatal) | 60 , m_fatal(fatal) |
| 62 , m_bomSeen(false) | 61 , m_bomSeen(false) |
| 63 { | 62 { |
| 64 } | 63 } |
| 65 | 64 |
| 66 TextDecoder::~TextDecoder() | 65 TextDecoder::~TextDecoder() |
| 67 { | 66 { |
| 68 } | 67 } |
| 69 | 68 |
| 70 String TextDecoder::encoding() const | 69 String TextDecoder::encoding() const |
| 71 { | 70 { |
| 72 String name = String(m_encoding.name()).lower(); | 71 String name = m_encoding.lower(); |
| 73 // 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. |
| 74 // 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. |
| 75 if (name == "iso-8859-1" || name == "us-ascii") | 74 if (name == "iso-8859-1" || name == "us-ascii") |
| 76 return "windows-1252"; | 75 return "windows-1252"; |
| 77 return name; | 76 return name; |
| 78 } | 77 } |
| 79 | 78 |
| 80 String TextDecoder::decode(ArrayBufferView* input, const Dictionary& options, Ex
ceptionState& exceptionState) | 79 String TextDecoder::decode(ArrayBufferView* input, const Dictionary& options, Ex
ceptionState& exceptionState) |
| 81 { | 80 { |
| 82 bool stream = false; | 81 bool stream = false; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 93 bool sawError = false; | 92 bool sawError = false; |
| 94 String s = m_codec->decode(start, length, flush, m_fatal, sawError); | 93 String s = m_codec->decode(start, length, flush, m_fatal, sawError); |
| 95 | 94 |
| 96 if (m_fatal && sawError) { | 95 if (m_fatal && sawError) { |
| 97 exceptionState.throwDOMException(EncodingError, "The encoded data was no
t valid."); | 96 exceptionState.throwDOMException(EncodingError, "The encoded data was no
t valid."); |
| 98 return String(); | 97 return String(); |
| 99 } | 98 } |
| 100 | 99 |
| 101 if (!m_bomSeen && !s.isEmpty()) { | 100 if (!m_bomSeen && !s.isEmpty()) { |
| 102 m_bomSeen = true; | 101 m_bomSeen = true; |
| 103 String name(m_encoding.name()); | 102 if ((m_encoding == "UTF-8" || m_encoding == "UTF-16LE" || m_encoding ==
"UTF-16BE") && s[0] == 0xFEFF) |
| 104 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0
] == 0xFEFF) | |
| 105 s.remove(0); | 103 s.remove(0); |
| 106 } | 104 } |
| 107 | 105 |
| 108 if (flush) | 106 if (flush) |
| 109 m_bomSeen = false; | 107 m_bomSeen = false; |
| 110 | 108 |
| 111 return s; | 109 return s; |
| 112 } | 110 } |
| 113 | 111 |
| 114 } // namespace WebCore | 112 } // namespace WebCore |
| OLD | NEW |