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 25 matching lines...) Expand all Loading... |
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 PassRefPtrWillBeRawPtr<TextDecoder> TextDecoder::create(const String& label, con
st Dictionary& options, ExceptionState& exceptionState) | 41 PassRefPtrWillBeRawPtr<TextDecoder> TextDecoder::create(const String& label, con
st Dictionary& options, ExceptionState& exceptionState) |
42 { | 42 { |
43 const String& encodingLabel = label.isNull() ? String("utf-8") : label; | 43 const String& encodingLabel = label.isNull() ? String("utf-8") : label; |
44 | 44 |
45 WTF::TextEncoding encoding(encodingLabel); | 45 WTF::TextEncoding encoding(encodingLabel); |
46 if (!encoding.isValid()) { | 46 if (!encoding.isValid() || !strcmp(encoding.name(), "replacement")) { |
47 exceptionState.throwTypeError("The encoding label provided ('" + encodin
gLabel + "') is invalid."); | 47 exceptionState.throwTypeError("The encoding label provided ('" + encodin
gLabel + "') is invalid."); |
48 return nullptr; | 48 return nullptr; |
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 adoptRefWillBeNoop(new TextDecoder(encoding.name(), fatal)); | 54 return adoptRefWillBeNoop(new TextDecoder(encodingLabel, encoding.name(), fa
tal)); |
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; |
83 options.get("stream", stream); | 82 options.get("stream", stream); |
84 | 83 |
85 const char* start = input ? static_cast<const char*>(input->baseAddress()) :
0; | 84 const char* start = input ? static_cast<const char*>(input->baseAddress()) :
0; |
86 size_t length = input ? input->byteLength() : 0; | 85 size_t length = input ? input->byteLength() : 0; |
87 | 86 |
88 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF; | 87 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF; |
89 | 88 |
90 bool sawError = false; | 89 bool sawError = false; |
91 String s = m_codec->decode(start, length, flush, m_fatal, sawError); | 90 String s = m_codec->decode(start, length, flush, m_fatal, sawError); |
92 | 91 |
93 if (m_fatal && sawError) { | 92 if (m_fatal && sawError) { |
94 exceptionState.throwDOMException(EncodingError, "The encoded data was no
t valid."); | 93 exceptionState.throwDOMException(EncodingError, "The encoded data was no
t valid."); |
95 return String(); | 94 return String(); |
96 } | 95 } |
97 | 96 |
98 if (!m_bomSeen && !s.isEmpty()) { | 97 if (!m_bomSeen && !s.isEmpty()) { |
99 m_bomSeen = true; | 98 m_bomSeen = true; |
100 String name(m_encoding.name()); | 99 if ((m_encoding == "UTF-8" || m_encoding == "UTF-16LE" || m_encoding ==
"UTF-16BE") && s[0] == 0xFEFF) |
101 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0
] == 0xFEFF) | |
102 s.remove(0); | 100 s.remove(0); |
103 } | 101 } |
104 | 102 |
105 if (flush) | 103 if (flush) |
106 m_bomSeen = false; | 104 m_bomSeen = false; |
107 | 105 |
108 return s; | 106 return s; |
109 } | 107 } |
110 | 108 |
111 } // namespace WebCore | 109 } // namespace WebCore |
OLD | NEW |