michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_dom_encodingutils_h_ michael@0: #define mozilla_dom_encodingutils_h_ michael@0: michael@0: #include "nsDataHashtable.h" michael@0: #include "nsString.h" michael@0: michael@0: class nsIUnicodeDecoder; michael@0: class nsIUnicodeEncoder; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: class EncodingUtils michael@0: { michael@0: public: michael@0: michael@0: /** michael@0: * Implements get an encoding algorithm from Encoding spec. michael@0: * http://encoding.spec.whatwg.org/#concept-encoding-get michael@0: * Given a label, this function returns the corresponding encoding or a michael@0: * false. michael@0: * The returned name may not be lowercased due to compatibility with michael@0: * our internal implementations. michael@0: * michael@0: * @param aLabel, incoming label describing charset to be decoded. michael@0: * @param aRetEncoding, returning corresponding encoding for label. michael@0: * @return false if no encoding was found for label. michael@0: * true if valid encoding found. michael@0: */ michael@0: static bool FindEncodingForLabel(const nsACString& aLabel, michael@0: nsACString& aOutEncoding); michael@0: michael@0: static bool FindEncodingForLabel(const nsAString& aLabel, michael@0: nsACString& aOutEncoding) michael@0: { michael@0: return FindEncodingForLabel(NS_ConvertUTF16toUTF8(aLabel), aOutEncoding); michael@0: } michael@0: michael@0: /** michael@0: * Remove any leading and trailing space characters, following the michael@0: * definition of space characters from Encoding spec. michael@0: * http://encoding.spec.whatwg.org/#terminology michael@0: * Note that nsAString::StripWhitespace() doesn't exactly match the michael@0: * definition. It also removes all matching chars in the string, michael@0: * not just leading and trailing. michael@0: * michael@0: * @param aString, string to be trimmed. michael@0: */ michael@0: template michael@0: static void TrimSpaceCharacters(T& aString) michael@0: { michael@0: aString.Trim(" \t\n\f\r"); michael@0: } michael@0: michael@0: /** michael@0: * Check is the encoding is ASCII-compatible in the sense that Basic Latin michael@0: * encodes to ASCII bytes. (The reverse may not be true!) michael@0: * michael@0: * @param aPreferredName a preferred encoding label michael@0: * @return whether the encoding is ASCII-compatible michael@0: */ michael@0: static bool IsAsciiCompatible(const nsACString& aPreferredName); michael@0: michael@0: /** michael@0: * Instantiates a decoder for an encoding. The input must be a michael@0: * Gecko-canonical encoding name. michael@0: * @param aEncoding a Gecko-canonical encoding name michael@0: * @return a decoder michael@0: */ michael@0: static already_AddRefed michael@0: DecoderForEncoding(const char* aEncoding) michael@0: { michael@0: nsDependentCString encoding(aEncoding); michael@0: return DecoderForEncoding(encoding); michael@0: } michael@0: michael@0: /** michael@0: * Instantiates a decoder for an encoding. The input must be a michael@0: * Gecko-canonical encoding name michael@0: * @param aEncoding a Gecko-canonical encoding name michael@0: * @return a decoder michael@0: */ michael@0: static already_AddRefed michael@0: DecoderForEncoding(const nsACString& aEncoding); michael@0: michael@0: /** michael@0: * Instantiates an encoder for an encoding. The input must be a michael@0: * Gecko-canonical encoding name. michael@0: * @param aEncoding a Gecko-canonical encoding name michael@0: * @return an encoder michael@0: */ michael@0: static already_AddRefed michael@0: EncoderForEncoding(const char* aEncoding) michael@0: { michael@0: nsDependentCString encoding(aEncoding); michael@0: return EncoderForEncoding(encoding); michael@0: } michael@0: michael@0: /** michael@0: * Instantiates an encoder for an encoding. The input must be a michael@0: * Gecko-canonical encoding name. michael@0: * @param aEncoding a Gecko-canonical encoding name michael@0: * @return an encoder michael@0: */ michael@0: static already_AddRefed michael@0: EncoderForEncoding(const nsACString& aEncoding); michael@0: michael@0: private: michael@0: EncodingUtils() MOZ_DELETE; michael@0: }; michael@0: michael@0: } // dom michael@0: } // mozilla michael@0: michael@0: #endif // mozilla_dom_encodingutils_h_