Bug 1637473 [wpt PR 23557] - De-flaky pointerevents/pointerevent_capture_mouse.html...
[gecko.git] / intl / uconv / nsTextToSubURI.cpp
blobef84502aaa200c8be979e801bee186cabeb3358e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsString.h"
6 #include "nsITextToSubURI.h"
7 #include "nsEscape.h"
8 #include "nsTextToSubURI.h"
9 #include "nsCRT.h"
10 #include "mozilla/ArrayUtils.h"
11 #include "mozilla/Encoding.h"
12 #include "mozilla/Preferences.h"
13 #include "mozilla/TextUtils.h"
14 #include "mozilla/Utf8.h"
16 using namespace mozilla;
18 nsTextToSubURI::~nsTextToSubURI() = default;
20 NS_IMPL_ISUPPORTS(nsTextToSubURI, nsITextToSubURI)
22 NS_IMETHODIMP
23 nsTextToSubURI::ConvertAndEscape(const nsACString& aCharset,
24 const nsAString& aText, nsACString& aOut) {
25 auto encoding = Encoding::ForLabelNoReplacement(aCharset);
26 if (!encoding) {
27 aOut.Truncate();
28 return NS_ERROR_UCONV_NOCONV;
30 nsresult rv;
31 const Encoding* actualEncoding;
32 nsAutoCString intermediate;
33 Tie(rv, actualEncoding) = encoding->Encode(aText, intermediate);
34 Unused << actualEncoding;
35 if (NS_FAILED(rv)) {
36 aOut.Truncate();
37 return rv;
39 bool ok = NS_Escape(intermediate, aOut, url_XPAlphas);
40 if (!ok) {
41 aOut.Truncate();
42 return NS_ERROR_OUT_OF_MEMORY;
44 return NS_OK;
47 NS_IMETHODIMP
48 nsTextToSubURI::UnEscapeAndConvert(const nsACString& aCharset,
49 const nsACString& aText, nsAString& aOut) {
50 auto encoding = Encoding::ForLabelNoReplacement(aCharset);
51 if (!encoding) {
52 aOut.Truncate();
53 return NS_ERROR_UCONV_NOCONV;
55 nsAutoCString unescaped(aText);
56 NS_UnescapeURL(unescaped);
57 auto rv = encoding->DecodeWithoutBOMHandling(unescaped, aOut);
58 if (NS_SUCCEEDED(rv)) {
59 return NS_OK;
61 return rv;
64 static bool statefulCharset(const char* charset) {
65 // HZ, UTF-7 and the CN and KR ISO-2022 variants are no longer in
66 // mozilla-central but keeping them here just in case for the benefit of
67 // comm-central.
68 if (!nsCRT::strncasecmp(charset, "ISO-2022-", sizeof("ISO-2022-") - 1) ||
69 !nsCRT::strcasecmp(charset, "UTF-7") ||
70 !nsCRT::strcasecmp(charset, "HZ-GB-2312"))
71 return true;
73 return false;
76 nsresult nsTextToSubURI::convertURItoUnicode(const nsCString& aCharset,
77 const nsCString& aURI,
78 nsAString& aOut) {
79 // check for 7bit encoding the data may not be ASCII after we decode
80 bool isStatefulCharset = statefulCharset(aCharset.get());
82 if (!isStatefulCharset) {
83 if (IsAscii(aURI)) {
84 CopyASCIItoUTF16(aURI, aOut);
85 return NS_OK;
87 if (IsUtf8(aURI)) {
88 CopyUTF8toUTF16(aURI, aOut);
89 return NS_OK;
93 // empty charset could indicate UTF-8, but aURI turns out not to be UTF-8.
94 NS_ENSURE_FALSE(aCharset.IsEmpty(), NS_ERROR_INVALID_ARG);
96 auto encoding = Encoding::ForLabelNoReplacement(aCharset);
97 if (!encoding) {
98 aOut.Truncate();
99 return NS_ERROR_UCONV_NOCONV;
101 return encoding->DecodeWithoutBOMHandlingAndWithoutReplacement(aURI, aOut);
104 NS_IMETHODIMP nsTextToSubURI::UnEscapeURIForUI(const nsACString& aURIFragment,
105 nsAString& _retval) {
106 nsAutoCString unescapedSpec;
107 // skip control octets (0x00 - 0x1f and 0x7f) when unescaping
108 NS_UnescapeURL(PromiseFlatCString(aURIFragment),
109 esc_SkipControl | esc_AlwaysCopy, unescapedSpec);
111 // in case of failure, return escaped URI
112 // Test for != NS_OK rather than NS_FAILED, because incomplete multi-byte
113 // sequences are also considered failure in this context
114 if (convertURItoUnicode(NS_LITERAL_CSTRING("UTF-8"), unescapedSpec,
115 _retval) != NS_OK) {
116 // assume UTF-8 instead of ASCII because hostname (IDN) may be in UTF-8
117 CopyUTF8toUTF16(aURIFragment, _retval);
120 // If there are any characters that are unsafe for URIs, reescape those.
121 if (mIDNBlocklist.IsEmpty()) {
122 mozilla::net::InitializeBlocklist(mIDNBlocklist);
123 // we allow SPACE and IDEOGRAPHIC SPACE in this method
124 mozilla::net::RemoveCharFromBlocklist(u' ', mIDNBlocklist);
125 mozilla::net::RemoveCharFromBlocklist(0x3000, mIDNBlocklist);
128 MOZ_ASSERT(!mIDNBlocklist.IsEmpty());
129 const nsPromiseFlatString& unescapedResult = PromiseFlatString(_retval);
130 nsString reescapedSpec;
131 _retval = NS_EscapeURL(
132 unescapedResult,
133 [&](char16_t aChar) -> bool {
134 return mozilla::net::CharInBlocklist(aChar, mIDNBlocklist);
136 reescapedSpec);
138 return NS_OK;
141 NS_IMETHODIMP
142 nsTextToSubURI::UnEscapeNonAsciiURI(const nsACString& aCharset,
143 const nsACString& aURIFragment,
144 nsAString& _retval) {
145 nsAutoCString unescapedSpec;
146 NS_UnescapeURL(PromiseFlatCString(aURIFragment),
147 esc_AlwaysCopy | esc_OnlyNonASCII, unescapedSpec);
148 // leave the URI as it is if it's not UTF-8 and aCharset is not a ASCII
149 // superset since converting "http:" with such an encoding is always a bad
150 // idea.
151 if (!IsUtf8(unescapedSpec) &&
152 (aCharset.LowerCaseEqualsLiteral("utf-16") ||
153 aCharset.LowerCaseEqualsLiteral("utf-16be") ||
154 aCharset.LowerCaseEqualsLiteral("utf-16le") ||
155 aCharset.LowerCaseEqualsLiteral("utf-7") ||
156 aCharset.LowerCaseEqualsLiteral("x-imap4-modified-utf7"))) {
157 CopyASCIItoUTF16(aURIFragment, _retval);
158 return NS_OK;
161 nsresult rv =
162 convertURItoUnicode(PromiseFlatCString(aCharset), unescapedSpec, _retval);
163 // NS_OK_UDEC_MOREINPUT is a success code, so caller can't catch the error
164 // if the string ends with a valid (but incomplete) sequence.
165 return rv == NS_OK_UDEC_MOREINPUT ? NS_ERROR_UDEC_ILLEGALINPUT : rv;
168 //----------------------------------------------------------------------