mini-url
Version:
Lightweight isomorphic url parser.
72 lines • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var get_loc_1 = require("get-loc");
var get_win_1 = require("get-win");
var parts_1 = require("./parts");
var vendors = ["ms", "moz", "webkit", "o"];
var URL = tryVendors(get_win_1.get(), "URL");
exports.URL = URL;
// Check if browser supports native url parser.
try {
Boolean(new URL("", "http://a"));
}
catch (e) {
var document_1 = get_win_1.get().document;
// istanbul ignore next
if (!document_1) {
throw new Error("URL parser not supported.");
}
// Load up a fake document to handle url resolution and parsing.
var doc = document_1.implementation.createHTMLDocument("parser");
var $base_1 = doc.head.appendChild(doc.createElement("base"));
var $a_1 = doc.createElement("a");
exports.URL = URL = /** @class */ (function () {
function class_1(path, base) {
// istanbul ignore next
$base_1.href = base || get_loc_1.get().href;
$a_1.href = path;
// Copies parsed parts from the link.
for (var _i = 0, parts_2 = parts_1.default; _i < parts_2.length; _i++) {
var part = parts_2[_i];
this[part] = $a_1[part] || "";
}
// Patch for ie9 which excludes leading slash.
// istanbul ignore next
if (this.pathname[0] !== "/") {
this.pathname = "/" + this.pathname;
}
// Patch for browsers automatically adding default ports.
// istanbul ignore next
if (this.port !== "") {
var _a = this, href = _a.href, hostname = _a.hostname;
var hostIndex = href.indexOf(hostname) + hostname.length + 1;
var expectedPort = href.slice(hostIndex, hostIndex + this.port.length);
if (expectedPort !== this.port) {
this.port = "";
this.host = this.hostname;
}
}
}
class_1.prototype.toString = function () {
return this.href;
};
return class_1;
}());
}
/**
* Check for vendored versions of function
*/
// istanbul ignore next
function tryVendors(obj, field) {
if (obj[field]) {
return obj[field];
}
for (var _i = 0, vendors_1 = vendors; _i < vendors_1.length; _i++) {
var vendor = vendors_1[_i];
var alias = obj[vendor + field];
if (alias) {
return alias;
}
}
}
//# sourceMappingURL=browser.js.map