Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Adds location.port and location.searchParams #1193

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"ms": "^2.1.1",
"request": "^2.85.0",
"tough-cookie": "^2.3.4",
"whatwg-url": "^7.0.0",
"ws": "^6.1.2"
},
"devDependencies": {
Expand Down
35 changes: 2 additions & 33 deletions src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const WebSocket = require('ws');
const Window = require('jsdom/lib/jsdom/browser/Window');
const XMLHttpRequest = require('./xhr');
const { idlUtils } = require('./dom/impl');
const whatwgURL = require('whatwg-url');

// File access, not implemented yet
class File {
Expand Down Expand Up @@ -51,38 +52,6 @@ class Screen {
}


// DOM implementation of URL class
class DOMURL {

constructor(url, base) {
if (url == null)
throw new TypeError('Failed to construct \'URL\': Invalid URL');
if (base)
url = URL.resolve(base, url);
const parsed = URL.parse(url || 'about:blank');
const origin = parsed.protocol && parsed.hostname && `${parsed.protocol}//${parsed.hostname}`;
Object.defineProperties(this, {
hash: { value: parsed.hash, enumerable: true },
host: { value: parsed.host, enumerable: true },
hostname: { value: parsed.hostname, enumerable: true },
href: { value: URL.format(parsed), enumerable: true },
origin: { value: origin, enumerable: true },
password: { value: parsed.password, enumerable: true },
pathname: { value: parsed.pathname, enumerable: true },
port: { value: parsed.port, enumerable: true },
protocol: { value: parsed.protocol, enumerable: true },
search: { value: parsed.search, enumerable: true },
username: { value: parsed.username, enumerable: true }
});
}

toString() {
return this.href;
}

}


function setupWindow(window, args) {
const { document } = window;
const { browser, history } = args;
Expand Down Expand Up @@ -171,7 +140,7 @@ function setupWindow(window, args) {

// Constructor for XHLHttpRequest
window.XMLHttpRequest = XMLHttpRequest.bind(null, window);
window.URL = DOMURL;
window.URL = whatwgURL.URL;

// Web sockets
window._allWebSockets = [];
Expand Down
7 changes: 7 additions & 0 deletions src/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ class Location {
const url = Object.assign(URL.parse(this._url), { search: value });
this.assign(URL.format(url));
}

get searchParams() {
const url = URL.parse(this._url);

if (url.search == null) return null;
return new URL.URLSearchParams(url.search);
}
}


Expand Down
33 changes: 33 additions & 0 deletions test/document_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ describe('Document', function() {
});
});

describe('location/DOMURL', function() {
before(function() {
brains.get('/somepath', function(req, res) {
res.send('<html></html>');
});

return browser.visit('/somepath?foo=bar');
});

describe('port', function() {
it('should be a string', function() {
const location = browser.location;

// Browsers type this as ?string.
assert.equal(location.port, '');
})
});
describe('searchParams', function() {
it('should be present', function() {
const searchParams = browser.location.searchParams;

assert.strictEqual(searchParams.get('foo'), 'bar');
});

describe('when no query is specified', function() {
it('is undefined', function() {
browser.visit('/somepath');
assert.equal(browser.location.searchParams, null);
});
});
});
});


describe('activeElement', function() {
before(function() {
Expand Down