Skip to content

Instantly share code, notes, and snippets.

@craftzdog
Created December 20, 2014 01:05
Show Gist options
  • Save craftzdog/12cbaf8a1674e3b8c8e6 to your computer and use it in GitHub Desktop.
Save craftzdog/12cbaf8a1674e3b8c8e6 to your computer and use it in GitHub Desktop.
PhantomJS shims for supporting HTML5
module.exports = function() {
var Ap = Array.prototype;
var slice = Ap.slice;
var Fp = Function.prototype;
if (!Fp.bind) {
// PhantomJS doesn't support Function.prototype.bind natively, so
// polyfill it whenever this module is required.
Fp.bind = function(context) {
var func = this;
var args = slice.call(arguments, 1);
function bound() {
var invokedAsConstructor = func.prototype && (this instanceof func);
return func.apply(
// Ignore the context parameter when invoking the bound function
// as a constructor. Note that this includes not only constructor
// invocations using the new keyword but also calls to base class
// constructors such as BaseClass.call(this, ...) or super(...).
!invokedAsConstructor && context || this,
args.concat(slice.call(arguments))
);
}
// The bound function must share the .prototype of the unbound
// function so that any object created by one constructor will count
// as an instance of both constructors.
bound.prototype = func.prototype;
return bound;
};
}
// history API shim
var history = window.history;
var oldPushState = history.pushState.bind(history);
var oldReplaceState = history.replaceState.bind(history);
history.pushState = function(state, title, url) {
history.state = state;
return oldPushState(state, title, url);
};
history.replaceState = function(state, title, url) {
history.state = state;
return oldReplaceState(state, title, url);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment