From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- dom/encoding/EncodingUtils.cpp | 105 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 dom/encoding/EncodingUtils.cpp (limited to 'dom/encoding/EncodingUtils.cpp') diff --git a/dom/encoding/EncodingUtils.cpp b/dom/encoding/EncodingUtils.cpp new file mode 100644 index 000000000..c9675bfcc --- /dev/null +++ b/dom/encoding/EncodingUtils.cpp @@ -0,0 +1,105 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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" + +#include "mozilla/ArrayUtils.h" // ArrayLength +#include "nsUConvPropertySearch.h" +#include "nsIUnicodeDecoder.h" +#include "nsIUnicodeEncoder.h" +#include "nsComponentManagerUtils.h" + +namespace mozilla { +namespace dom { + +static const nsUConvProp labelsEncodings[] = { +#include "labelsencodings.properties.h" +}; + +static const nsUConvProp encodingsGroups[] = { +#include "encodingsgroups.properties.h" +}; + +bool +EncodingUtils::FindEncodingForLabel(const nsACString& aLabel, + nsACString& aOutEncoding) +{ + // Save aLabel first because it may refer the same string as aOutEncoding. + nsCString label(aLabel); + + EncodingUtils::TrimSpaceCharacters(label); + if (label.IsEmpty()) { + aOutEncoding.Truncate(); + return false; + } + + ToLowerCase(label); + return NS_SUCCEEDED(nsUConvPropertySearch::SearchPropertyValue( + labelsEncodings, ArrayLength(labelsEncodings), label, aOutEncoding)); +} + +bool +EncodingUtils::FindEncodingForLabelNoReplacement(const nsACString& aLabel, + nsACString& aOutEncoding) +{ + if(!FindEncodingForLabel(aLabel, aOutEncoding)) { + return false; + } + if (aOutEncoding.EqualsLiteral("replacement")) { + aOutEncoding.Truncate(); + return false; + } + return true; +} + +bool +EncodingUtils::IsAsciiCompatible(const nsACString& aPreferredName) +{ + // HZ and UTF-7 are no longer in mozilla-central, but keeping them here + // just in case for the benefit of comm-central. + return !(aPreferredName.LowerCaseEqualsLiteral("utf-16") || + aPreferredName.LowerCaseEqualsLiteral("utf-16be") || + aPreferredName.LowerCaseEqualsLiteral("utf-16le") || + aPreferredName.LowerCaseEqualsLiteral("replacement") || + aPreferredName.LowerCaseEqualsLiteral("hz-gb-2312") || + aPreferredName.LowerCaseEqualsLiteral("utf-7") || + aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7")); +} + +already_AddRefed +EncodingUtils::DecoderForEncoding(const nsACString& aEncoding) +{ + nsAutoCString contractId(NS_UNICODEDECODER_CONTRACTID_BASE); + contractId.Append(aEncoding); + + nsCOMPtr decoder = do_CreateInstance(contractId.get()); + MOZ_ASSERT(decoder, "Tried to create decoder for unknown encoding."); + return decoder.forget(); +} + +already_AddRefed +EncodingUtils::EncoderForEncoding(const nsACString& aEncoding) +{ + nsAutoCString contractId(NS_UNICODEENCODER_CONTRACTID_BASE); + contractId.Append(aEncoding); + + nsCOMPtr encoder = do_CreateInstance(contractId.get()); + MOZ_ASSERT(encoder, "Tried to create encoder for unknown encoding."); + return encoder.forget(); +} + +void +EncodingUtils::LangGroupForEncoding(const nsACString& aEncoding, + nsACString& aOutGroup) +{ + if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue( + encodingsGroups, ArrayLength(encodingsGroups), aEncoding, aOutGroup))) { + aOutGroup.AssignLiteral("x-unicode"); + } +} + +} // namespace dom +} // namespace mozilla -- cgit v1.2.3