Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef mozilla_dom_encodingutils_h_ |
michael@0 | 6 | #define mozilla_dom_encodingutils_h_ |
michael@0 | 7 | |
michael@0 | 8 | #include "nsDataHashtable.h" |
michael@0 | 9 | #include "nsString.h" |
michael@0 | 10 | |
michael@0 | 11 | class nsIUnicodeDecoder; |
michael@0 | 12 | class nsIUnicodeEncoder; |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | namespace dom { |
michael@0 | 16 | |
michael@0 | 17 | class EncodingUtils |
michael@0 | 18 | { |
michael@0 | 19 | public: |
michael@0 | 20 | |
michael@0 | 21 | /** |
michael@0 | 22 | * Implements get an encoding algorithm from Encoding spec. |
michael@0 | 23 | * http://encoding.spec.whatwg.org/#concept-encoding-get |
michael@0 | 24 | * Given a label, this function returns the corresponding encoding or a |
michael@0 | 25 | * false. |
michael@0 | 26 | * The returned name may not be lowercased due to compatibility with |
michael@0 | 27 | * our internal implementations. |
michael@0 | 28 | * |
michael@0 | 29 | * @param aLabel, incoming label describing charset to be decoded. |
michael@0 | 30 | * @param aRetEncoding, returning corresponding encoding for label. |
michael@0 | 31 | * @return false if no encoding was found for label. |
michael@0 | 32 | * true if valid encoding found. |
michael@0 | 33 | */ |
michael@0 | 34 | static bool FindEncodingForLabel(const nsACString& aLabel, |
michael@0 | 35 | nsACString& aOutEncoding); |
michael@0 | 36 | |
michael@0 | 37 | static bool FindEncodingForLabel(const nsAString& aLabel, |
michael@0 | 38 | nsACString& aOutEncoding) |
michael@0 | 39 | { |
michael@0 | 40 | return FindEncodingForLabel(NS_ConvertUTF16toUTF8(aLabel), aOutEncoding); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | /** |
michael@0 | 44 | * Remove any leading and trailing space characters, following the |
michael@0 | 45 | * definition of space characters from Encoding spec. |
michael@0 | 46 | * http://encoding.spec.whatwg.org/#terminology |
michael@0 | 47 | * Note that nsAString::StripWhitespace() doesn't exactly match the |
michael@0 | 48 | * definition. It also removes all matching chars in the string, |
michael@0 | 49 | * not just leading and trailing. |
michael@0 | 50 | * |
michael@0 | 51 | * @param aString, string to be trimmed. |
michael@0 | 52 | */ |
michael@0 | 53 | template<class T> |
michael@0 | 54 | static void TrimSpaceCharacters(T& aString) |
michael@0 | 55 | { |
michael@0 | 56 | aString.Trim(" \t\n\f\r"); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | /** |
michael@0 | 60 | * Check is the encoding is ASCII-compatible in the sense that Basic Latin |
michael@0 | 61 | * encodes to ASCII bytes. (The reverse may not be true!) |
michael@0 | 62 | * |
michael@0 | 63 | * @param aPreferredName a preferred encoding label |
michael@0 | 64 | * @return whether the encoding is ASCII-compatible |
michael@0 | 65 | */ |
michael@0 | 66 | static bool IsAsciiCompatible(const nsACString& aPreferredName); |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * Instantiates a decoder for an encoding. The input must be a |
michael@0 | 70 | * Gecko-canonical encoding name. |
michael@0 | 71 | * @param aEncoding a Gecko-canonical encoding name |
michael@0 | 72 | * @return a decoder |
michael@0 | 73 | */ |
michael@0 | 74 | static already_AddRefed<nsIUnicodeDecoder> |
michael@0 | 75 | DecoderForEncoding(const char* aEncoding) |
michael@0 | 76 | { |
michael@0 | 77 | nsDependentCString encoding(aEncoding); |
michael@0 | 78 | return DecoderForEncoding(encoding); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * Instantiates a decoder for an encoding. The input must be a |
michael@0 | 83 | * Gecko-canonical encoding name |
michael@0 | 84 | * @param aEncoding a Gecko-canonical encoding name |
michael@0 | 85 | * @return a decoder |
michael@0 | 86 | */ |
michael@0 | 87 | static already_AddRefed<nsIUnicodeDecoder> |
michael@0 | 88 | DecoderForEncoding(const nsACString& aEncoding); |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * Instantiates an encoder for an encoding. The input must be a |
michael@0 | 92 | * Gecko-canonical encoding name. |
michael@0 | 93 | * @param aEncoding a Gecko-canonical encoding name |
michael@0 | 94 | * @return an encoder |
michael@0 | 95 | */ |
michael@0 | 96 | static already_AddRefed<nsIUnicodeEncoder> |
michael@0 | 97 | EncoderForEncoding(const char* aEncoding) |
michael@0 | 98 | { |
michael@0 | 99 | nsDependentCString encoding(aEncoding); |
michael@0 | 100 | return EncoderForEncoding(encoding); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * Instantiates an encoder for an encoding. The input must be a |
michael@0 | 105 | * Gecko-canonical encoding name. |
michael@0 | 106 | * @param aEncoding a Gecko-canonical encoding name |
michael@0 | 107 | * @return an encoder |
michael@0 | 108 | */ |
michael@0 | 109 | static already_AddRefed<nsIUnicodeEncoder> |
michael@0 | 110 | EncoderForEncoding(const nsACString& aEncoding); |
michael@0 | 111 | |
michael@0 | 112 | private: |
michael@0 | 113 | EncodingUtils() MOZ_DELETE; |
michael@0 | 114 | }; |
michael@0 | 115 | |
michael@0 | 116 | } // dom |
michael@0 | 117 | } // mozilla |
michael@0 | 118 | |
michael@0 | 119 | #endif // mozilla_dom_encodingutils_h_ |