| 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 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 #include "bindings/v8/Dictionary.h" | 35 #include "bindings/v8/Dictionary.h" |
| 36 #include "bindings/v8/ExceptionState.h" | 36 #include "bindings/v8/ExceptionState.h" |
| 37 #include "wtf/text/CString.h" | 37 #include "wtf/text/CString.h" |
| 38 #include "wtf/text/TextEncodingRegistry.h" | 38 #include "wtf/text/TextEncodingRegistry.h" |
| 39 | 39 |
| 40 namespace WebCore { | 40 namespace WebCore { |
| 41 | 41 |
| 42 TextEncoder* TextEncoder::create(const String& utfLabel, ExceptionState& excepti
onState) | 42 TextEncoder* TextEncoder::create(const String& utfLabel, ExceptionState& excepti
onState) |
| 43 { | 43 { |
| 44 const String& encodingLabel = utfLabel.isNull() ? String("utf-8") : utfLabel
; | 44 WTF::TextEncoding encoding(utfLabel); |
| 45 | |
| 46 WTF::TextEncoding encoding(encodingLabel); | |
| 47 if (!encoding.isValid()) { | 45 if (!encoding.isValid()) { |
| 48 exceptionState.throwTypeError("The encoding label provided ('" + encodin
gLabel + "') is invalid."); | 46 exceptionState.throwTypeError("The encoding label provided ('" + utfLabe
l + "') is invalid."); |
| 49 return 0; | 47 return 0; |
| 50 } | 48 } |
| 51 | 49 |
| 52 String name(encoding.name()); | 50 String name(encoding.name()); |
| 53 if (name != "UTF-8" && name != "UTF-16LE" && name != "UTF-16BE") { | 51 if (name != "UTF-8" && name != "UTF-16LE" && name != "UTF-16BE") { |
| 54 exceptionState.throwTypeError("The encoding provided ('" + encodingLabel
+ "') is not one of 'utf-8', 'utf-16', or 'utf-16be'."); | 52 exceptionState.throwTypeError("The encoding provided ('" + utfLabel + "'
) is not one of 'utf-8', 'utf-16', or 'utf-16be'."); |
| 55 return 0; | 53 return 0; |
| 56 } | 54 } |
| 57 | 55 |
| 58 return new TextEncoder(encoding); | 56 return new TextEncoder(encoding); |
| 59 } | 57 } |
| 60 | 58 |
| 61 TextEncoder::TextEncoder(const WTF::TextEncoding& encoding) | 59 TextEncoder::TextEncoder(const WTF::TextEncoding& encoding) |
| 62 : m_encoding(encoding) | 60 : m_encoding(encoding) |
| 63 , m_codec(newTextCodec(encoding)) | 61 , m_codec(newTextCodec(encoding)) |
| 64 { | 62 { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 77 | 75 |
| 78 PassRefPtr<Uint8Array> TextEncoder::encode(const String& input, const Dictionary
& options) | 76 PassRefPtr<Uint8Array> TextEncoder::encode(const String& input, const Dictionary
& options) |
| 79 { | 77 { |
| 80 bool stream = false; | 78 bool stream = false; |
| 81 options.get("stream", stream); | 79 options.get("stream", stream); |
| 82 | 80 |
| 83 // FIXME: Not flushing is not supported by TextCodec for encode; add it or | 81 // FIXME: Not flushing is not supported by TextCodec for encode; add it or |
| 84 // handle split surrogates here. | 82 // handle split surrogates here. |
| 85 | 83 |
| 86 CString result; | 84 CString result; |
| 87 if (!input.isNull()) { | 85 if (input.is8Bit()) |
| 88 if (input.is8Bit()) | 86 result = m_codec->encode(input.characters8(), input.length(), WTF::Quest
ionMarksForUnencodables); |
| 89 result = m_codec->encode(input.characters8(), input.length(), WTF::Q
uestionMarksForUnencodables); | 87 else |
| 90 else | 88 result = m_codec->encode(input.characters16(), input.length(), WTF::Ques
tionMarksForUnencodables); |
| 91 result = m_codec->encode(input.characters16(), input.length(), WTF::
QuestionMarksForUnencodables); | |
| 92 } | |
| 93 | 89 |
| 94 const char* buffer = result.data(); | 90 const char* buffer = result.data(); |
| 95 const unsigned char* unsignedBuffer = reinterpret_cast<const unsigned char*>
(buffer); | 91 const unsigned char* unsignedBuffer = reinterpret_cast<const unsigned char*>
(buffer); |
| 96 | 92 |
| 97 return Uint8Array::create(unsignedBuffer, result.length()); | 93 return Uint8Array::create(unsignedBuffer, result.length()); |
| 98 } | 94 } |
| 99 | 95 |
| 100 } // namespace WebCore | 96 } // namespace WebCore |
| OLD | NEW |