react / wstein / node_modules / jest-cli / node_modules / jsdom / lib / jsdom / browser / history.js
80684 views"use strict";12var URL = require('url');34function StateEntry(data, title, url) {5this.data = data;6this.title = title;7this.url = url;8}910module.exports = History;1112function History(window) {13this._states = [];14this._index = -1;15this._window = window;16this._location = window.location;17}1819History.prototype = {20constructor: History,2122get length() {23return this._states.length;24},2526get state() {27var state = this._states[this._index];28return state ? state.data : null;29},3031back: function () {32this.go(-1);33},3435forward: function () {36this.go(1);37},3839go: function (delta) {40if (typeof delta === "undefined" || delta === 0) {41this._location.reload();42return;43}4445var newIndex = this._index + delta;4647if (newIndex < 0 || newIndex >= this.length) {48return;49}5051this._index = newIndex;52this._applyState(this._states[this._index]);53},5455pushState: function (data, title, url) {56var state = new StateEntry(data, title, url);57if (this._index + 1 !== this._states.length) {58this._states = this._states.slice(0, this._index + 1);59}60this._states.push(state);61this._applyState(state);62this._index++;63},6465replaceState: function (data, title, url) {66var state = new StateEntry(data, title, url);67this._states[this._index] = state;68this._applyState(state);69},7071_applyState: function(state) {72this._location._url = URL.parse(URL.resolve(this._location._url.href, state.url));7374this._signalPopstate(state);75},7677_signalPopstate: function(state) {78if (this._window.document) {79var ev = this._window.document.createEvent("HTMLEvents");80ev.initEvent("popstate", false, false);81ev.state = state.data;82process.nextTick(function () {83this._window.dispatchEvent(ev);84}.bind(this));85}86},8788toString: function () {89return "[object History]";90}91};929394