| 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 14 matching lines...) Expand all Loading... |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "modules/encoding/TextEncoder.h" | 31 #include "modules/encoding/TextEncoder.h" |
| 32 | 32 |
| 33 #include "bindings/core/v8/ExceptionState.h" | 33 #include "bindings/core/v8/ExceptionState.h" |
| 34 #include "core/dom/ExecutionContext.h" | 34 #include "core/dom/ExecutionContext.h" |
| 35 #include "core/frame/UseCounter.h" | |
| 36 #include "modules/encoding/Encoding.h" | 35 #include "modules/encoding/Encoding.h" |
| 37 #include "wtf/text/CString.h" | 36 #include "wtf/text/CString.h" |
| 38 #include "wtf/text/TextEncodingRegistry.h" | 37 #include "wtf/text/TextEncodingRegistry.h" |
| 39 | 38 |
| 40 namespace blink { | 39 namespace blink { |
| 41 | 40 |
| 42 TextEncoder* TextEncoder::create(ExecutionContext* context, const String& utfLab
el, ExceptionState& exceptionState) | 41 TextEncoder* TextEncoder::create(ExecutionContext* context, ExceptionState& exce
ptionState) |
| 43 { | 42 { |
| 44 WTF::TextEncoding encoding(utfLabel.stripWhiteSpace(&Encoding::isASCIIWhiteS
pace)); | 43 WTF::TextEncoding encoding("UTF-8"); |
| 45 if (!encoding.isValid()) { | |
| 46 exceptionState.throwRangeError("The encoding label provided ('" + utfLab
el + "') is invalid."); | |
| 47 return 0; | |
| 48 } | |
| 49 | |
| 50 String name(encoding.name()); | |
| 51 if (name != "UTF-8" && name != "UTF-16LE" && name != "UTF-16BE") { | |
| 52 exceptionState.throwRangeError("The encoding provided ('" + utfLabel + "
') is not one of 'utf-8', 'utf-16', or 'utf-16be'."); | |
| 53 return 0; | |
| 54 } | |
| 55 | |
| 56 if (name == "UTF-16LE" || name == "UTF-16BE") | |
| 57 UseCounter::count(context, UseCounter::TextEncoderUTF16); | |
| 58 | |
| 59 return new TextEncoder(encoding); | 44 return new TextEncoder(encoding); |
| 60 } | 45 } |
| 61 | 46 |
| 62 TextEncoder::TextEncoder(const WTF::TextEncoding& encoding) | 47 TextEncoder::TextEncoder(const WTF::TextEncoding& encoding) |
| 63 : m_encoding(encoding) | 48 : m_encoding(encoding) |
| 64 , m_codec(newTextCodec(encoding)) | 49 , m_codec(newTextCodec(encoding)) |
| 65 { | 50 { |
| 51 String name(m_encoding.name()); |
| 52 DCHECK_EQ(name, "UTF-8"); |
| 66 } | 53 } |
| 67 | 54 |
| 68 TextEncoder::~TextEncoder() | 55 TextEncoder::~TextEncoder() |
| 69 { | 56 { |
| 70 } | 57 } |
| 71 | 58 |
| 72 String TextEncoder::encoding() const | 59 String TextEncoder::encoding() const |
| 73 { | 60 { |
| 74 String name = String(m_encoding.name()).lower(); | 61 String name = String(m_encoding.name()).lower(); |
| 75 ASSERT(name == "utf-8" || name == "utf-16le" || name == "utf-16be"); | 62 DCHECK_EQ(name, "utf-8"); |
| 76 return name; | 63 return name; |
| 77 } | 64 } |
| 78 | 65 |
| 79 DOMUint8Array* TextEncoder::encode(const String& input) | 66 DOMUint8Array* TextEncoder::encode(const String& input) |
| 80 { | 67 { |
| 81 CString result; | 68 CString result; |
| 82 if (input.is8Bit()) | 69 if (input.is8Bit()) |
| 83 result = m_codec->encode(input.characters8(), input.length(), WTF::Quest
ionMarksForUnencodables); | 70 result = m_codec->encode(input.characters8(), input.length(), WTF::Quest
ionMarksForUnencodables); |
| 84 else | 71 else |
| 85 result = m_codec->encode(input.characters16(), input.length(), WTF::Ques
tionMarksForUnencodables); | 72 result = m_codec->encode(input.characters16(), input.length(), WTF::Ques
tionMarksForUnencodables); |
| 86 | 73 |
| 87 const char* buffer = result.data(); | 74 const char* buffer = result.data(); |
| 88 const unsigned char* unsignedBuffer = reinterpret_cast<const unsigned char*>
(buffer); | 75 const unsigned char* unsignedBuffer = reinterpret_cast<const unsigned char*>
(buffer); |
| 89 | 76 |
| 90 return DOMUint8Array::create(unsignedBuffer, result.length()); | 77 return DOMUint8Array::create(unsignedBuffer, result.length()); |
| 91 } | 78 } |
| 92 | 79 |
| 93 } // namespace blink | 80 } // namespace blink |
| OLD | NEW |