Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix same site urls #88

Merged
merged 3 commits into from
Sep 5, 2019
Merged
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
26 changes: 15 additions & 11 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,26 @@ class Options {

Options.extract = function (document) {
for (const element of Array.from(document.getElementsByTagName('script'))) {
var m, src;
if ((src = element.getAttribute('src')) && (m = src.match(new RegExp('^(?:[^:]+:)?//(.*)/z?livereload\\.js(?:\\?(.*))?$')))) {
var mm;
var m, mm,
src = element.src, srcAttr = element.getAttribute('src');
var lrUrlRegexp = /^([^:]+:\/\/([^\/:]+)(?::(\d+))?\/|\/\/|\/)?([^\/].*\/)?z?livereload\.js(?:\?(.*))?$/;
// ^proto:// ^host ^port ^// ^/ ^folder
if ((m = src.match(lrUrlRegexp)) && (mm = srcAttr.match(lrUrlRegexp))) {
const [, , host, port, , params] = m;
const [, , , portFromAttr] = mm;
const options = new Options();

options.https = element.src.indexOf('https') === 0;

if ((mm = m[1].match(new RegExp('^([^/:]+)(?::(\\d+))?(\\/+.*)?$')))) {
options.host = mm[1];
if (mm[2]) {
options.port = parseInt(mm[2], 10);
}
}
options.host = host;
options.port = port
? parseInt(port, 10)
: portFromAttr
? parseInt(portFromAttr, 10)
: options.port;

if (m[2]) {
for (const pair of m[2].split('&')) {
if (params) {
for (const pair of params.split('&')) {
var keyAndValue;

if ((keyAndValue = pair.split('=')).length > 1) {
Expand Down
24 changes: 24 additions & 0 deletions test/options_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ describe('Options', function () {
return assert.strictEqual(true, options.https);
});

it('should recognize same site URLs', function () {
let dom = new JSDOM('<script src="/somewhere.com/132324324/23243443/4343/livereload.js"></script>', {
url: 'https://somewhere.org/'
});
let options = Options.extract(dom.window.document);
assert.ok(options);
assert.strictEqual('somewhere.org', options.host);

dom = new JSDOM('<script src="livereload.js"></script>', {
url: 'https://somewhere.org/'
});
options = Options.extract(dom.window.document);
assert.ok(options);
assert.strictEqual('somewhere.org', options.host);

dom = new JSDOM('<script src="../livereload.js"></script>', {
url: 'https://somewhere.org/'
});
options = Options.extract(dom.window.document);
assert.ok(options);
return assert.strictEqual('somewhere.org', options.host);
});

return it('should recognize protocol-relative https URL', function () {
const dom = new JSDOM('<script src="//somewhere.com/132324324/23243443/4343/livereload.js"></script>', {
url: 'https://somewhere.org/'
Expand All @@ -85,4 +108,5 @@ describe('Options', function () {
assert.ok(options);
return assert.strictEqual(true, options.https);
});

});