2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-09-28 14:19:18 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
2013-03-17 11:55:16 +04:00
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h" // ArrayLength
|
2012-11-08 03:04:22 +04:00
|
|
|
#include "nsUConvPropertySearch.h"
|
2013-11-26 11:31:52 +04:00
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2012-09-28 14:19:18 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-02-01 05:42:33 +03:00
|
|
|
static constexpr nsUConvProp labelsEncodings[] = {
|
2012-11-08 03:04:22 +04:00
|
|
|
#include "labelsencodings.properties.h"
|
2012-09-28 14:19:18 +04:00
|
|
|
};
|
|
|
|
|
2017-02-01 05:42:33 +03:00
|
|
|
static constexpr nsUConvProp encodingsGroups[] = {
|
2014-05-08 13:32:00 +04:00
|
|
|
#include "encodingsgroups.properties.h"
|
|
|
|
};
|
|
|
|
|
2012-09-28 14:19:18 +04:00
|
|
|
bool
|
2012-11-08 03:04:22 +04:00
|
|
|
EncodingUtils::FindEncodingForLabel(const nsACString& aLabel,
|
|
|
|
nsACString& aOutEncoding)
|
2012-09-28 14:19:18 +04:00
|
|
|
{
|
2012-11-08 03:04:22 +04:00
|
|
|
// Save aLabel first because it may refer the same string as aOutEncoding.
|
|
|
|
nsCString label(aLabel);
|
2012-09-28 14:19:18 +04:00
|
|
|
|
|
|
|
EncodingUtils::TrimSpaceCharacters(label);
|
|
|
|
if (label.IsEmpty()) {
|
2012-11-08 03:04:22 +04:00
|
|
|
aOutEncoding.Truncate();
|
2012-09-28 14:19:18 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-08 03:04:22 +04:00
|
|
|
ToLowerCase(label);
|
|
|
|
return NS_SUCCEEDED(nsUConvPropertySearch::SearchPropertyValue(
|
|
|
|
labelsEncodings, ArrayLength(labelsEncodings), label, aOutEncoding));
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
2014-05-08 13:32:00 +04:00
|
|
|
bool
|
|
|
|
EncodingUtils::FindEncodingForLabelNoReplacement(const nsACString& aLabel,
|
|
|
|
nsACString& aOutEncoding)
|
|
|
|
{
|
|
|
|
if(!FindEncodingForLabel(aLabel, aOutEncoding)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (aOutEncoding.EqualsLiteral("replacement")) {
|
|
|
|
aOutEncoding.Truncate();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-04 22:09:11 +04:00
|
|
|
bool
|
|
|
|
EncodingUtils::IsAsciiCompatible(const nsACString& aPreferredName)
|
|
|
|
{
|
2014-09-26 16:12:02 +04:00
|
|
|
// HZ and UTF-7 are no longer in mozilla-central, but keeping them here
|
|
|
|
// just in case for the benefit of comm-central.
|
2013-03-04 22:09:11 +04:00
|
|
|
return !(aPreferredName.LowerCaseEqualsLiteral("utf-16") ||
|
|
|
|
aPreferredName.LowerCaseEqualsLiteral("utf-16be") ||
|
|
|
|
aPreferredName.LowerCaseEqualsLiteral("utf-16le") ||
|
2013-11-25 12:06:56 +04:00
|
|
|
aPreferredName.LowerCaseEqualsLiteral("replacement") ||
|
2013-11-25 12:06:56 +04:00
|
|
|
aPreferredName.LowerCaseEqualsLiteral("hz-gb-2312") ||
|
2013-03-04 22:09:11 +04:00
|
|
|
aPreferredName.LowerCaseEqualsLiteral("utf-7") ||
|
|
|
|
aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7"));
|
|
|
|
}
|
|
|
|
|
2013-11-26 11:31:52 +04:00
|
|
|
already_AddRefed<nsIUnicodeDecoder>
|
|
|
|
EncodingUtils::DecoderForEncoding(const nsACString& aEncoding)
|
|
|
|
{
|
|
|
|
nsAutoCString contractId(NS_UNICODEDECODER_CONTRACTID_BASE);
|
|
|
|
contractId.Append(aEncoding);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeDecoder> decoder = do_CreateInstance(contractId.get());
|
|
|
|
MOZ_ASSERT(decoder, "Tried to create decoder for unknown encoding.");
|
|
|
|
return decoder.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIUnicodeEncoder>
|
|
|
|
EncodingUtils::EncoderForEncoding(const nsACString& aEncoding)
|
|
|
|
{
|
|
|
|
nsAutoCString contractId(NS_UNICODEENCODER_CONTRACTID_BASE);
|
|
|
|
contractId.Append(aEncoding);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIUnicodeEncoder> encoder = do_CreateInstance(contractId.get());
|
|
|
|
MOZ_ASSERT(encoder, "Tried to create encoder for unknown encoding.");
|
|
|
|
return encoder.forget();
|
|
|
|
}
|
|
|
|
|
2014-05-08 13:32:00 +04:00
|
|
|
void
|
|
|
|
EncodingUtils::LangGroupForEncoding(const nsACString& aEncoding,
|
|
|
|
nsACString& aOutGroup)
|
|
|
|
{
|
|
|
|
if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
|
|
|
|
encodingsGroups, ArrayLength(encodingsGroups), aEncoding, aOutGroup))) {
|
|
|
|
aOutGroup.AssignLiteral("x-unicode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-28 14:19:18 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|