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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 // The replacement encoding is not valid, but the Encoding API also | 47 // The replacement encoding is not valid, but the Encoding API also |
48 // rejects aliases of the replacement encoding. | 48 // rejects aliases of the replacement encoding. |
49 if (!encoding.isValid() || !strcasecmp(encoding.name(), "replacement")) { | 49 if (!encoding.isValid() || !strcasecmp(encoding.name(), "replacement")) { |
50 exceptionState.throwTypeError("The encoding label provided ('" + encodin
gLabel + "') is invalid."); | 50 exceptionState.throwTypeError("The encoding label provided ('" + encodin
gLabel + "') is invalid."); |
51 return 0; | 51 return 0; |
52 } | 52 } |
53 | 53 |
54 bool fatal = false; | 54 bool fatal = false; |
55 options.get("fatal", fatal); | 55 options.get("fatal", fatal); |
56 | 56 |
57 return new TextDecoder(encoding, fatal); | 57 bool ignoreBOM = false; |
| 58 options.get("ignoreBOM", ignoreBOM); |
| 59 |
| 60 return new TextDecoder(encoding, fatal, ignoreBOM); |
58 } | 61 } |
59 | 62 |
60 | 63 |
61 TextDecoder::TextDecoder(const WTF::TextEncoding& encoding, bool fatal) | 64 TextDecoder::TextDecoder(const WTF::TextEncoding& encoding, bool fatal, bool ign
oreBOM) |
62 : m_encoding(encoding) | 65 : m_encoding(encoding) |
63 , m_codec(newTextCodec(encoding)) | 66 , m_codec(newTextCodec(encoding)) |
64 , m_fatal(fatal) | 67 , m_fatal(fatal) |
| 68 , m_ignoreBOM(ignoreBOM) |
65 , m_bomSeen(false) | 69 , m_bomSeen(false) |
66 { | 70 { |
67 } | 71 } |
68 | 72 |
69 TextDecoder::~TextDecoder() | 73 TextDecoder::~TextDecoder() |
70 { | 74 { |
71 } | 75 } |
72 | 76 |
73 String TextDecoder::encoding() const | 77 String TextDecoder::encoding() const |
74 { | 78 { |
(...skipping 16 matching lines...) Expand all Loading... |
91 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF; | 95 WTF::FlushBehavior flush = stream ? WTF::DoNotFlush : WTF::DataEOF; |
92 | 96 |
93 bool sawError = false; | 97 bool sawError = false; |
94 String s = m_codec->decode(start, length, flush, m_fatal, sawError); | 98 String s = m_codec->decode(start, length, flush, m_fatal, sawError); |
95 | 99 |
96 if (m_fatal && sawError) { | 100 if (m_fatal && sawError) { |
97 exceptionState.throwDOMException(EncodingError, "The encoded data was no
t valid."); | 101 exceptionState.throwDOMException(EncodingError, "The encoded data was no
t valid."); |
98 return String(); | 102 return String(); |
99 } | 103 } |
100 | 104 |
101 if (!m_bomSeen && !s.isEmpty()) { | 105 if (!m_ignoreBOM && !m_bomSeen && !s.isEmpty()) { |
102 m_bomSeen = true; | 106 m_bomSeen = true; |
103 String name(m_encoding.name()); | 107 String name(m_encoding.name()); |
104 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0
] == 0xFEFF) | 108 if ((name == "UTF-8" || name == "UTF-16LE" || name == "UTF-16BE") && s[0
] == 0xFEFF) |
105 s.remove(0); | 109 s.remove(0); |
106 } | 110 } |
107 | 111 |
108 if (flush) | 112 if (flush) |
109 m_bomSeen = false; | 113 m_bomSeen = false; |
110 | 114 |
111 return s; | 115 return s; |
112 } | 116 } |
113 | 117 |
114 } // namespace WebCore | 118 } // namespace WebCore |
OLD | NEW |