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

Improve support for the file protocol #441

Merged
merged 2 commits into from
Jan 15, 2018
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
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BROWSERIFY=node_modules/.bin/browserify
INFOLOG := \033[34m ▸\033[0m

page.js: index.js
@echo "$(INFOLOG) Building page.js.."
@$(BROWSERIFY) index.js --standalone page -o page.js

watch:
find index.js | entr make page.js
.PHONY: watch
34 changes: 22 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

var isDocument = ('undefined' !== typeof document);
var isWindow = ('undefined' !== typeof window);
var isHistory = ('undefined' !== typeof history);
var hasHistory = ('undefined' !== typeof history);

/**
* Detect click event
Expand All @@ -36,8 +36,7 @@
* history.location generated polyfill in https://github.com/devote/HTML5-History-API
*/

var location = isWindow && (window.history.location || window.location);
var isLocation = !!location;
var isLocation = isWindow && !!(window.history.location || window.location);

/**
* Perform initial dispatch.
Expand Down Expand Up @@ -248,15 +247,15 @@
if (page.len > 0) {
// this may need more testing to see if all browsers
// wait for the next tick to go back in history
isHistory && history.back();
hasHistory && history.back();
page.len--;
} else if (path) {
setTimeout(function() {
page.show(path, state);
});
}else{
setTimeout(function() {
page.show(base, state);
page.show(getBase(), state);
});
}
};
Expand Down Expand Up @@ -359,7 +358,7 @@
var current;

if (hashbang) {
current = isLocation && base + location.hash.replace('#!', '');
current = isLocation && getBase() + location.hash.replace('#!', '');
} else {
current = isLocation && location.pathname + location.search;
}
Expand Down Expand Up @@ -410,11 +409,12 @@
*/

function Context(path, state) {
if ('/' === path[0] && 0 !== path.indexOf(base)) path = base + (hashbang ? '#!' : '') + path;
var pageBase = getBase();
if ('/' === path[0] && 0 !== path.indexOf(pageBase)) path = pageBase + (hashbang ? '#!' : '') + path;
var i = path.indexOf('?');

this.canonicalPath = path;
this.path = path.replace(base, '') || '/';
this.path = path.replace(pageBase, '') || '/';
if (hashbang) this.path = this.path.replace('#!', '') || '/';

this.title = (isDocument && document.title);
Expand Down Expand Up @@ -449,7 +449,7 @@

Context.prototype.pushState = function() {
page.len++;
if (isHistory) {
if (hasHistory) {
history.pushState(this.state, this.title, hashbang && this.path !== '/' ? '#!' + this.path : this.canonicalPath);
}
};
Expand All @@ -461,7 +461,7 @@
*/

Context.prototype.save = function() {
if (isHistory) {
if (hasHistory && location.protocol !== 'file:') {
history.replaceState(this.state, this.title, hashbang && this.path !== '/' ? '#!' + this.path : this.canonicalPath);
}
};
Expand Down Expand Up @@ -632,14 +632,15 @@

// same page
var orig = path;
var pageBase = getBase();

if (path.indexOf(base) === 0) {
if (path.indexOf(pageBase) === 0) {
path = path.substr(base.length);
}

if (hashbang) path = path.replace('#!', '');

if (base && orig === path) return;
if (pageBase && orig === path) return;

e.preventDefault();
page.show(orig);
Expand Down Expand Up @@ -680,4 +681,13 @@
location.port === url.port;
}

/**
* Gets the `base`, which depends on whether we are using History or
* hashbang routing.
*/
function getBase() {
if(!!base) return base;
return (hashbang && location.protocol === 'file:') ? location.pathname : base;
}

page.sameOrigin = sameOrigin;
17 changes: 14 additions & 3 deletions test/support/jsdom.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
before(function(next) {

function setupJsdom(options, next) {
options = options || {};

var jsdom = require('jsdom');
var html = '<!doctype html><html><head></head><body></body></html>';

function setupGlobals(window) {
window.console = console;
window.history.replaceState(null, '', '/');
if(window.location.protocol !== 'file:')
window.history.replaceState(null, '', '/');
global.window = window;
global.location = window.location;
global.document = window.document;
Expand All @@ -15,6 +19,7 @@ before(function(next) {
if(jsdom.env) {
jsdom.env({
html: html,
url: options.url || 'http://example.com',
done: function(errors, window) {
setupGlobals(window);
if (errors) {
Expand All @@ -26,9 +31,15 @@ before(function(next) {
});
} else {
var dom = new jsdom.JSDOM(html, {
url: 'http://example.com'
url: options.url || 'http://example.com'
});
setupGlobals(dom.window);
next();
}
}

exports.setup = setupJsdom;

before(function(next) {
setupJsdom({}, next);
});
45 changes: 35 additions & 10 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
called = false,
html = '',
base = '',
setbase = true,
hashbang = false,
decodeURLComponents = true,
chai = this.chai,
expect = this.expect,
page = this.page,
baseTag,
htmlWrapper,
$;
$,
jsdomSupport;

if (isNode) {
require('./support/jsdom');
jsdomSupport = require('./support/jsdom');
}

before(function() {
Expand Down Expand Up @@ -69,12 +71,14 @@
called = true;
});

if (!baseTag) {
baseTag = document.createElement('base');
$('head').appendChild(baseTag);
}
if(setbase) {
if (!baseTag) {
baseTag = document.createElement('base');
$('head').appendChild(baseTag);
}

baseTag.setAttribute('href', (base ? base + '/' : '/'));
baseTag.setAttribute('href', (base ? base + '/' : '/'));
}

htmlWrapper = document.createElement('div');

Expand All @@ -93,10 +97,7 @@
htmlWrapper.innerHTML = html;
document.body.appendChild(htmlWrapper);



page(options);

},
replaceable = function(route) {
function realCallback(ctx) {
Expand Down Expand Up @@ -521,6 +522,7 @@
page.strict(false);
page('/');
base = '';
setbase = true;

};

Expand Down Expand Up @@ -599,4 +601,27 @@
});
});

var describei = jsdomSupport ? describe : describe.skip;

describei('File protocol', function() {
before(function(){
jsdomSupport.setup({
url: 'file:///var/html/index.html'
}, Function.prototype);

setbase = false;
hashbang = true;
beforeTests({
hashbang: hashbang
});
});

it('test', function(){
page('/about', function(ctx){
expect(ctx.path).to.equal('/about');
});
page('/about');
});
});

}).call(this);