tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

url.js (2035B)


      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 
      5 const defaultUrl = {
      6  hash: "",
      7  host: "",
      8  hostname: "",
      9  href: "",
     10  origin: "null",
     11  password: "",
     12  path: "",
     13  pathname: "",
     14  port: "",
     15  protocol: "",
     16  search: "",
     17  // This should be a "URLSearchParams" object
     18  searchParams: {},
     19  username: "",
     20 };
     21 
     22 const parseCache = new Map();
     23 export function parse(url) {
     24  if (parseCache.has(url)) {
     25    return parseCache.get(url);
     26  }
     27 
     28  let urlObj;
     29  try {
     30    urlObj = new URL(url);
     31  } catch (err) {
     32    urlObj = { ...defaultUrl };
     33    // If we're given simply a filename...
     34    if (url) {
     35      const hashStart = url.indexOf("#");
     36      if (hashStart >= 0) {
     37        urlObj.hash = url.slice(hashStart);
     38        url = url.slice(0, hashStart);
     39 
     40        if (urlObj.hash === "#") {
     41          // The standard URL parser does not include the ? unless there are
     42          // parameters included in the search value.
     43          urlObj.hash = "";
     44        }
     45      }
     46 
     47      const queryStart = url.indexOf("?");
     48      if (queryStart >= 0) {
     49        urlObj.search = url.slice(queryStart);
     50        url = url.slice(0, queryStart);
     51 
     52        if (urlObj.search === "?") {
     53          // The standard URL parser does not include the ? unless there are
     54          // parameters included in the search value.
     55          urlObj.search = "";
     56        }
     57      }
     58 
     59      urlObj.pathname = url;
     60    }
     61  }
     62  // When provided a special URL like "webpack:///webpack/foo",
     63  // prevents passing the three slashes in the path, and pass only onea.
     64  // This will prevent displaying modules in empty-name sub folders.
     65  urlObj.pathname = urlObj.pathname.replace(/\/+/, "/");
     66  urlObj.path = urlObj.pathname + urlObj.search;
     67 
     68  // Cache the result
     69  parseCache.set(url, urlObj);
     70  return urlObj;
     71 }
     72 
     73 export function sameOrigin(firstUrl, secondUrl) {
     74  return parse(firstUrl).origin == parse(secondUrl).origin;
     75 }