Bug 1890689 accumulate input in LargerReceiverBlockSizeThanDesiredBuffering GTest...
[gecko.git] / devtools / client / shared / unicode-url.js
blob36fae098eba63a70ec0d9b2e306c78dd4a6c3c1a
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
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 "use strict";
6 const idnService = Cc["@mozilla.org/network/idn-service;1"].getService(
7   Ci.nsIIDNService
8 );
10 /**
11  * Gets a readble Unicode hostname from a hostname.
12  *
13  * If the `hostname` is a readable ASCII hostname, such as example.org, then
14  * this function will simply return the original `hostname`.
15  *
16  * If the `hostname` is a Punycode hostname representing a Unicode domain name,
17  * such as xn--g6w.xn--8pv, then this function will return the readable Unicode
18  * domain name by decoding the Punycode hostname.
19  *
20  * @param {string}  hostname
21  *                  the hostname from which the Unicode hostname will be
22  *                  parsed, such as example.org, xn--g6w.xn--8pv.
23  * @return {string} The Unicode hostname. It may be the same as the `hostname`
24  *                  passed to this function if the `hostname` itself is
25  *                  a readable ASCII hostname or a Unicode hostname.
26  */
27 function getUnicodeHostname(hostname) {
28   return idnService.convertToDisplayIDN(hostname, {});
31 /**
32  * Gets a readble Unicode URL pathname from a URL pathname.
33  *
34  * If the `urlPath` is a readable ASCII URL pathname, such as /a/b/c.js, then
35  * this function will simply return the original `urlPath`.
36  *
37  * If the `urlPath` is a URI-encoded pathname, such as %E8%A9%A6/%E6%B8%AC.js,
38  * then this function will return the readable Unicode pathname.
39  *
40  * If the `urlPath` is a malformed URL pathname, then this function will simply
41  * return the original `urlPath`.
42  *
43  * @param {string}  urlPath
44  *                  the URL path from which the Unicode URL path will be parsed,
45  *                  such as /a/b/c.js, %E8%A9%A6/%E6%B8%AC.js.
46  * @return {string} The Unicode URL Path. It may be the same as the `urlPath`
47  *                  passed to this function if the `urlPath` itself is a readable
48  *                  ASCII url or a Unicode url.
49  */
50 function getUnicodeUrlPath(urlPath) {
51   try {
52     return decodeURIComponent(urlPath);
53   } catch (err) {}
54   return urlPath;
57 /**
58  * Gets a readable Unicode URL from a URL.
59  *
60  * If the `url` is a readable ASCII URL, such as http://example.org/a/b/c.js,
61  * then this function will simply return the original `url`.
62  *
63  * If the `url` includes either an unreadable Punycode domain name or an
64  * unreadable URI-encoded pathname, such as
65  * http://xn--g6w.xn--8pv/%E8%A9%A6/%E6%B8%AC.js, then this function will return
66  * the readable URL by decoding all its unreadable URL components to Unicode
67  * characters. The character `#` is not decoded from escape sequences.
68  *
69  * If the `url` is a malformed URL, then this function will return the original
70  * `url`.
71  *
72  * If the `url` is a data: URI, then this function will return the original
73  * `url`.
74  *
75  * @param {string}  url
76  *                  the full URL, or a data: URI. from which the readable URL
77  *                  will be parsed, such as, http://example.org/a/b/c.js,
78  *                  http://xn--g6w.xn--8pv/%E8%A9%A6/%E6%B8%AC.js
79  * @return {string} The readable URL. It may be the same as the `url` passed to
80  *                  this function if the `url` itself is readable.
81  */
82 function getUnicodeUrl(url) {
83   try {
84     const { protocol, hostname } = new URL(url);
85     if (protocol === "data:") {
86       // Never convert a data: URI.
87       return url;
88     }
89     const readableHostname = getUnicodeHostname(hostname);
91     /* We use `decodeURIComponent` instead of decodeURI as the
92      * later does not decode some characters, it only can decode characters
93      * previously encoded by the encodeURI. See
94      * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#Description
95      */
96     url = decodeURIComponent(url);
97     return url.replace(hostname, readableHostname);
98   } catch (err) {}
99   return url;
102 module.exports = {
103   getUnicodeHostname,
104   getUnicodeUrlPath,
105   getUnicodeUrl,