all files / lib/ URL-impl.js

84.09% Statements 74/88
86.89% Branches 53/61
87.5% Functions 21/24
84.09% Lines 74/88
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213      443× 443×   443× 443× 327× 327×       442× 442× 52×     390×                                           395×                         243×       281×       14×       276×                 276× 252×     24×                 323×   323× 68×     255× 202×     53×       27×     25×       313× 64×     249×       15×     13×       313× 260×     53×       14×       14×       275× 23×     252×                 277× 256×     21×           10×   10×           275× 249×     26×                      
"use strict";
const usm = require("./url-state-machine");
 
exports.implementation = class URLImpl {
  constructor(constructorArgs) {
    const url = constructorArgs[0];
    const base = constructorArgs[1];
 
    let parsedBase = null;
    if (base !== undefined) {
      parsedBase = usm.basicURLParse(base);
      if (parsedBase === "failure") {
        throw new TypeError("Invalid base URL");
      }
    }
 
    const parsedURL = usm.basicURLParse(url, { baseURL: parsedBase });
    if (parsedURL === "failure") {
      throw new TypeError("Invalid URL");
    }
 
    this._url = parsedURL;
 
    // TODO: query stuff
  }
 
  static domainToASCII(domain) {
    const asciiDomain = usm.parseHost(domain);
    if (typeof asciiDomain !== "string") {
      return "";
    }
    return asciiDomain;
  }
 
  static domainToUnicode(domain) {
    const unicodeDomain = usm.parseHost(domain, true);
    if (typeof unicodeDomain !== "string") {
      return "";
    }
    return unicodeDomain;
  }
 
  get href() {
    return usm.serializeURL(this._url);
  }
 
  set href(v) {
    const parsedURL = usm.basicURLParse(v);
    if (parsedURL === "failure") {
      throw new TypeError("Invalid URL");
    }
 
    this._url = parsedURL;
  }
 
  get origin() {
    return usm.serializeURLToUnicodeOrigin(this._url);
  }
 
  get protocol() {
    return this._url.scheme + ":";
  }
 
  set protocol(v) {
    usm.basicURLParse(v + ":", { url: this._url, stateOverride: "scheme start" });
  }
 
  get username() {
    return this._url.username;
  }
 
  set username(v) {
    if (this._url.host === null || this._url.cannotBeABaseURL) {
      return;
    }
 
    usm.setTheUsername(this._url, v);
  }
 
  get password() {
    if (this._url.password === null) {
      return "";
    }
 
    return this._url.password;
  }
 
  set password(v) {
    if (this._url.host === null || this._url.cannotBeABaseURL) {
      return;
    }
 
    usm.setThePassword(this._url, v);
  }
 
  get host() {
    const url = this._url;
 
    if (url.host === null) {
      return "";
    }
 
    if (url.port === null) {
      return usm.serializeHost(url.host);
    }
 
    return usm.serializeHost(url.host) + ":" + usm.serializeInteger(url.port);
  }
 
  set host(v) {
    if (this._url.cannotBeABaseURL) {
      return;
    }
 
    usm.basicURLParse(v, { url: this._url, stateOverride: "host" });
  }
 
  get hostname() {
    if (this._url.host === null) {
      return "";
    }
 
    return usm.serializeHost(this._url.host);
  }
 
  set hostname(v) {
    if (this._url.cannotBeABaseURL) {
      return;
    }
 
    usm.basicURLParse(v, { url: this._url, stateOverride: "hostname" });
  }
 
  get port() {
    if (this._url.port === null) {
      return "";
    }
 
    return usm.serializeInteger(this._url.port);
  }
 
  set port(v) {
    Iif (this._url.host === null || this._url.cannotBeABaseURL || this._url.scheme === "file") {
      return;
    }
 
    usm.basicURLParse(v, { url: this._url, stateOverride: "port" });
  }
 
  get pathname() {
    if (this._url.cannotBeABaseURL) {
      return this._url.path[0];
    }
 
    return "/" + this._url.path.join("/");
  }
 
  set pathname(v) {
    if (this._url.cannotBeABaseURL) {
      return;
    }
 
    this._url.path = [];
    usm.basicURLParse(v, { url: this._url, stateOverride: "path start" });
  }
 
  get search() {
    if (this._url.query === null || this._url.query === "") {
      return "";
    }
 
    return "?" + this._url.query;
  }
 
  set search(v) {
    // TODO: query stuff
 
    const url = this._url;
 
    if (v === "") {
      url.query = null;
      return;
    }
 
    const input = v[0] === "?" ? v.substring(1) : v;
    url.query = "";
    usm.basicURLParse(input, { url, stateOverride: "query" });
  }
 
  get hash() {
    if (this._url.fragment === null || this._url.fragment === "") {
      return "";
    }
 
    return "#" + this._url.fragment;
  }
 
  set hash(v) {
    Iif (this._url.scheme === "javascript") {
      return;
    }
 
    if (v === "") {
      this._url.fragment = null;
      return;
    }
 
    const input = v[0] === "#" ? v.substring(1) : v;
    this._url.fragment = "";
    usm.basicURLParse(input, { url: this._url, stateOverride: "fragment" });
  }
};