1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/encoding/EncodingUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef mozilla_dom_encodingutils_h_ 1.9 +#define mozilla_dom_encodingutils_h_ 1.10 + 1.11 +#include "nsDataHashtable.h" 1.12 +#include "nsString.h" 1.13 + 1.14 +class nsIUnicodeDecoder; 1.15 +class nsIUnicodeEncoder; 1.16 + 1.17 +namespace mozilla { 1.18 +namespace dom { 1.19 + 1.20 +class EncodingUtils 1.21 +{ 1.22 +public: 1.23 + 1.24 + /** 1.25 + * Implements get an encoding algorithm from Encoding spec. 1.26 + * http://encoding.spec.whatwg.org/#concept-encoding-get 1.27 + * Given a label, this function returns the corresponding encoding or a 1.28 + * false. 1.29 + * The returned name may not be lowercased due to compatibility with 1.30 + * our internal implementations. 1.31 + * 1.32 + * @param aLabel, incoming label describing charset to be decoded. 1.33 + * @param aRetEncoding, returning corresponding encoding for label. 1.34 + * @return false if no encoding was found for label. 1.35 + * true if valid encoding found. 1.36 + */ 1.37 + static bool FindEncodingForLabel(const nsACString& aLabel, 1.38 + nsACString& aOutEncoding); 1.39 + 1.40 + static bool FindEncodingForLabel(const nsAString& aLabel, 1.41 + nsACString& aOutEncoding) 1.42 + { 1.43 + return FindEncodingForLabel(NS_ConvertUTF16toUTF8(aLabel), aOutEncoding); 1.44 + } 1.45 + 1.46 + /** 1.47 + * Remove any leading and trailing space characters, following the 1.48 + * definition of space characters from Encoding spec. 1.49 + * http://encoding.spec.whatwg.org/#terminology 1.50 + * Note that nsAString::StripWhitespace() doesn't exactly match the 1.51 + * definition. It also removes all matching chars in the string, 1.52 + * not just leading and trailing. 1.53 + * 1.54 + * @param aString, string to be trimmed. 1.55 + */ 1.56 + template<class T> 1.57 + static void TrimSpaceCharacters(T& aString) 1.58 + { 1.59 + aString.Trim(" \t\n\f\r"); 1.60 + } 1.61 + 1.62 + /** 1.63 + * Check is the encoding is ASCII-compatible in the sense that Basic Latin 1.64 + * encodes to ASCII bytes. (The reverse may not be true!) 1.65 + * 1.66 + * @param aPreferredName a preferred encoding label 1.67 + * @return whether the encoding is ASCII-compatible 1.68 + */ 1.69 + static bool IsAsciiCompatible(const nsACString& aPreferredName); 1.70 + 1.71 + /** 1.72 + * Instantiates a decoder for an encoding. The input must be a 1.73 + * Gecko-canonical encoding name. 1.74 + * @param aEncoding a Gecko-canonical encoding name 1.75 + * @return a decoder 1.76 + */ 1.77 + static already_AddRefed<nsIUnicodeDecoder> 1.78 + DecoderForEncoding(const char* aEncoding) 1.79 + { 1.80 + nsDependentCString encoding(aEncoding); 1.81 + return DecoderForEncoding(encoding); 1.82 + } 1.83 + 1.84 + /** 1.85 + * Instantiates a decoder for an encoding. The input must be a 1.86 + * Gecko-canonical encoding name 1.87 + * @param aEncoding a Gecko-canonical encoding name 1.88 + * @return a decoder 1.89 + */ 1.90 + static already_AddRefed<nsIUnicodeDecoder> 1.91 + DecoderForEncoding(const nsACString& aEncoding); 1.92 + 1.93 + /** 1.94 + * Instantiates an encoder for an encoding. The input must be a 1.95 + * Gecko-canonical encoding name. 1.96 + * @param aEncoding a Gecko-canonical encoding name 1.97 + * @return an encoder 1.98 + */ 1.99 + static already_AddRefed<nsIUnicodeEncoder> 1.100 + EncoderForEncoding(const char* aEncoding) 1.101 + { 1.102 + nsDependentCString encoding(aEncoding); 1.103 + return EncoderForEncoding(encoding); 1.104 + } 1.105 + 1.106 + /** 1.107 + * Instantiates an encoder for an encoding. The input must be a 1.108 + * Gecko-canonical encoding name. 1.109 + * @param aEncoding a Gecko-canonical encoding name 1.110 + * @return an encoder 1.111 + */ 1.112 + static already_AddRefed<nsIUnicodeEncoder> 1.113 + EncoderForEncoding(const nsACString& aEncoding); 1.114 + 1.115 +private: 1.116 + EncodingUtils() MOZ_DELETE; 1.117 +}; 1.118 + 1.119 +} // dom 1.120 +} // mozilla 1.121 + 1.122 +#endif // mozilla_dom_encodingutils_h_