react / wstein / node_modules / jest-cli / node_modules / jsdom / lib / jsdom / browser / location.js
80684 views"use strict";12var URL = require("url");3var NOT_IMPLEMENTED = require("./utils").NOT_IMPLEMENTED;45module.exports = Location;67function Location(urlString, window) {8this._url = URL.parse(urlString);9this._window = window;10}1112Location.prototype = {13constructor: Location,14reload: function () {15NOT_IMPLEMENTED(this._window, "location.reload")();16},17get protocol() { return this._url.protocol || ":"; },18get host() { return this._url.host || ""; },19get auth() { return this._url.auth; },20get hostname() { return this._url.hostname || ""; },21get origin() { return ((this._url.protocol !== undefined && this._url.protocol !== null) ? this._url.protocol + "//" : this._url.protocol) + this._url.host || ""; },22get port() { return this._url.port || ""; },23get pathname() { return this._url.pathname || ""; },24get href() { return this._url.href; },25get hash() { return this._url.hash || ""; },26get search() { return this._url.search || ""; },2728set href(val) {29var oldUrl = this._url.href;30var oldProtocol = this._url.protocol;31var oldHost = this._url.host;32var oldPathname = this._url.pathname;33var oldHash = this._url.hash || "";3435this._url = URL.parse(URL.resolve(oldUrl, val));36var newUrl = this._url.href;37var newProtocol = this._url.protocol;38var newHost = this._url.host;39var newPathname = this._url.pathname;40var newHash = this._url.hash || "";4142if (oldProtocol === newProtocol && oldHost === newHost && oldPathname === newPathname && oldHash !== newHash) {43this._signalHashChange(oldUrl, newUrl);44} else {45NOT_IMPLEMENTED(this._window, "location.href (no reload)")();46}47},4849set hash(val) {50var oldUrl = this._url.href;51var oldHash = this._url.hash || "";5253if (val.lastIndexOf("#", 0) !== 0) {54val = "#" + val;55}5657this._url = URL.parse(URL.resolve(oldUrl, val));58var newUrl = this._url.href;59var newHash = this._url.hash || "";6061if (oldHash !== newHash) {62this._signalHashChange(oldUrl, newUrl);63}64},6566set search(val) {67var oldUrl = this._url.href;68var oldHash = this._url.hash || "";69if (val.length) {70if (val.lastIndexOf("?", 0) !== 0) {71val = "?" + val;72}73this._url = URL.parse(URL.resolve(oldUrl, val + oldHash));74} else {75this._url = URL.parse(oldUrl.replace(/\?([^#]+)/, ""));76}77},7879replace: function (val) {80this.href = val;81},8283toString: function () {84return this._url.href;85},8687_signalHashChange: function (oldUrl, newUrl) {88if (this._window.document) {89var ev = this._window.document.createEvent("HTMLEvents");90ev.initEvent("hashchange", false, false);91ev.oldUrl = oldUrl;92ev.newUrl = newUrl;93process.nextTick(function () {94this._window.dispatchEvent(ev);95}.bind(this));96}97}98};99100101