1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_dom_encodingutils_h_
6 #define mozilla_dom_encodingutils_h_
8 #include "nsDataHashtable.h"
11 class nsIUnicodeDecoder
;
12 class nsIUnicodeEncoder
;
22 * Implements get an encoding algorithm from Encoding spec.
23 * http://encoding.spec.whatwg.org/#concept-encoding-get
24 * Given a label, this function returns the corresponding encoding or a
26 * The returned name may not be lowercased due to compatibility with
27 * our internal implementations.
29 * @param aLabel, incoming label describing charset to be decoded.
30 * @param aOutEncoding, returning corresponding encoding for label.
31 * @return false if no encoding was found for label.
32 * true if valid encoding found.
34 static bool FindEncodingForLabel(const nsACString
& aLabel
,
35 nsACString
& aOutEncoding
);
37 static bool FindEncodingForLabel(const nsAString
& aLabel
,
38 nsACString
& aOutEncoding
)
40 return FindEncodingForLabel(NS_ConvertUTF16toUTF8(aLabel
), aOutEncoding
);
44 * Like FindEncodingForLabel() except labels that map to "replacement"
45 * are treated as unknown.
47 * @param aLabel, incoming label describing charset to be decoded.
48 * @param aOutEncoding, returning corresponding encoding for label.
49 * @return false if no encoding was found for label.
50 * true if valid encoding found.
52 static bool FindEncodingForLabelNoReplacement(const nsACString
& aLabel
,
53 nsACString
& aOutEncoding
);
55 static bool FindEncodingForLabelNoReplacement(const nsAString
& aLabel
,
56 nsACString
& aOutEncoding
)
58 return FindEncodingForLabelNoReplacement(NS_ConvertUTF16toUTF8(aLabel
),
63 * Remove any leading and trailing space characters, following the
64 * definition of space characters from Encoding spec.
65 * http://encoding.spec.whatwg.org/#terminology
66 * Note that nsAString::StripWhitespace() doesn't exactly match the
67 * definition. It also removes all matching chars in the string,
68 * not just leading and trailing.
70 * @param aString, string to be trimmed.
73 static void TrimSpaceCharacters(T
& aString
)
75 aString
.Trim(" \t\n\f\r");
79 * Check is the encoding is ASCII-compatible in the sense that Basic Latin
80 * encodes to ASCII bytes. (The reverse may not be true!)
82 * @param aPreferredName a preferred encoding label
83 * @return whether the encoding is ASCII-compatible
85 static bool IsAsciiCompatible(const nsACString
& aPreferredName
);
88 * Instantiates a decoder for an encoding. The input must be a
89 * Gecko-canonical encoding name.
90 * @param aEncoding a Gecko-canonical encoding name
93 static already_AddRefed
<nsIUnicodeDecoder
>
94 DecoderForEncoding(const char* aEncoding
)
96 nsDependentCString
encoding(aEncoding
);
97 return DecoderForEncoding(encoding
);
101 * Instantiates a decoder for an encoding. The input must be a
102 * Gecko-canonical encoding name
103 * @param aEncoding a Gecko-canonical encoding name
106 static already_AddRefed
<nsIUnicodeDecoder
>
107 DecoderForEncoding(const nsACString
& aEncoding
);
110 * Instantiates an encoder for an encoding. The input must be a
111 * Gecko-canonical encoding name.
112 * @param aEncoding a Gecko-canonical encoding name
115 static already_AddRefed
<nsIUnicodeEncoder
>
116 EncoderForEncoding(const char* aEncoding
)
118 nsDependentCString
encoding(aEncoding
);
119 return EncoderForEncoding(encoding
);
123 * Instantiates an encoder for an encoding. The input must be a
124 * Gecko-canonical encoding name.
125 * @param aEncoding a Gecko-canonical encoding name
128 static already_AddRefed
<nsIUnicodeEncoder
>
129 EncoderForEncoding(const nsACString
& aEncoding
);
132 * Finds a Gecko language group string (e.g. x-western) for a Gecko-canonical
135 * @param aEncoding, incoming label describing charset to be decoded.
136 * @param aOutGroup, returning corresponding language group.
138 static void LangGroupForEncoding(const nsACString
& aEncoding
,
139 nsACString
& aOutGroup
);
142 EncodingUtils() = delete;
148 #endif // mozilla_dom_encodingutils_h_