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

Commit

Permalink
Added classlist and bind polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Ferdinandi committed Aug 8, 2014
1 parent ceaf595 commit f7c5d80
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ Gulp Boilerplate is licensed under the [MIT License](http://gomakethings.com/mit

## Changelog

* v0.2.2 - August 8, 2014
* Added polyfills for `Functions.prototype.bind` and `element.classList`.
* v0.2.1 - July 21, 2014
* Updated `getDataOptions` method to use JSON.
* Removed auto-dating from minified files.
Expand Down
2 changes: 1 addition & 1 deletion dist/css/myplugin.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* gulp-boilerplate v0.2.1
* gulp-boilerplate v0.2.2
* My Gulp.js boilerplate for creating new web projects, by Chris Ferdinandi.
* http://github.com/cferdinandi/Plugin
*
Expand Down
2 changes: 1 addition & 1 deletion dist/css/myplugin.min.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/** gulp-boilerplate v0.2.1, by Chris Ferdinandi | http://github.com/cferdinandi/Plugin | Licensed under MIT: http://gomakethings.com/mit/ */
/** gulp-boilerplate v0.2.2, by Chris Ferdinandi | http://github.com/cferdinandi/Plugin | Licensed under MIT: http://gomakethings.com/mit/ */
35 changes: 35 additions & 0 deletions dist/js/bind-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* gulp-boilerplate v0.2.2
* My Gulp.js boilerplate for creating new web projects, by Chris Ferdinandi.
* http://github.com/cferdinandi/Plugin
*
* Free to use under the MIT License.
* http://gomakethings.com/mit/
*/

/*
* Polyfill Function.prototype.bind support for otherwise ECMA Script 5 compliant browsers
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Compatibility
*/

if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}

var aArgs = Array.prototype.slice.call(arguments, 1);
var fToBind = this;
fNOP = function () {};
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
};

fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();

return fBound;
};
}
2 changes: 2 additions & 0 deletions dist/js/bind-polyfill.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

176 changes: 176 additions & 0 deletions dist/js/classList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/**
* gulp-boilerplate v0.2.2
* My Gulp.js boilerplate for creating new web projects, by Chris Ferdinandi.
* http://github.com/cferdinandi/Plugin
*
* Free to use under the MIT License.
* http://gomakethings.com/mit/
*/

/*
* classList.js: Cross-browser full element.classList implementation.
* 2014-01-31
*
* By Eli Grey, http://eligrey.com
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/

/*global self, document, DOMException */

/*! @source http://purl.eligrey.com/github/classList.js/blob/master/classList.js*/

if ("document" in self && !("classList" in document.createElement("_"))) {

(function (view) {

"use strict";

if (!('Element' in view)) return;

var
classListProp = "classList",
protoProp = "prototype",
elemCtrProto = view.Element[protoProp],
objCtr = Object,
strTrim = String[protoProp].trim || function () {
return this.replace(/^\s+|\s+$/g, "");
},
arrIndexOf = Array[protoProp].indexOf || function (item) {
var
i = 0,
len = this.length;
for (; i < len; i++) {
if (i in this && this[i] === item) {
return i;
}
}
return -1;
},
// Vendors: please allow content code to instantiate DOMExceptions
DOMEx = function (type, message) {
this.name = type;
this.code = DOMException[type];
this.message = message;
},
checkTokenAndGetIndex = function (classList, token) {
if (token === "") {
throw new DOMEx(
"SYNTAX_ERR",
"An invalid or illegal string was specified"
);
}
if (/\s/.test(token)) {
throw new DOMEx(
"INVALID_CHARACTER_ERR",
"String contains an invalid character"
);
}
return arrIndexOf.call(classList, token);
},
ClassList = function (elem) {
var
trimmedClasses = strTrim.call(elem.getAttribute("class") || ""),
classes = trimmedClasses ? trimmedClasses.split(/\s+/) : [],
i = 0,
len = classes.length;
for (; i < len; i++) {
this.push(classes[i]);
}
this._updateClassName = function () {
elem.setAttribute("class", this.toString());
};
},
classListProto = ClassList[protoProp] = [],
classListGetter = function () {
return new ClassList(this);
};
// Most DOMException implementations don't allow calling DOMException's toString()
// on non-DOMExceptions. Error's toString() is sufficient here.
DOMEx[protoProp] = Error[protoProp];
classListProto.item = function (i) {
return this[i] || null;
};
classListProto.contains = function (token) {
token += "";
return checkTokenAndGetIndex(this, token) !== -1;
};
classListProto.add = function () {
var
tokens = arguments,
i = 0,
l = tokens.length,
token,
updated = false;
do {
token = tokens[i] + "";
if (checkTokenAndGetIndex(this, token) === -1) {
this.push(token);
updated = true;
}
}
while (++i < l);

if (updated) {
this._updateClassName();
}
};
classListProto.remove = function () {
var
tokens = arguments,
i = 0,
l = tokens.length,
token,
updated = false;
do {
token = tokens[i] + "";
var index = checkTokenAndGetIndex(this, token);
if (index !== -1) {
this.splice(index, 1);
updated = true;
}
}
while (++i < l);

if (updated) {
this._updateClassName();
}
};
classListProto.toggle = function (token, force) {
token += "";

var
result = this.contains(token),
method = result ? force !== true && "remove" : force !== false && "add";

if (method) {
this[method](token);
}

return !result;
};
classListProto.toString = function () {
return this.join(" ");
};

if (objCtr.defineProperty) {
var classListPropDesc = {
get: classListGetter,
enumerable: true,
configurable: true
};
try {
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
} catch (ex) { // IE 8 doesn't support enumerable:true
if (ex.number === -0x7FF5EC54) {
classListPropDesc.enumerable = false;
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
}
}
} else if (objCtr[protoProp].__defineGetter__) {
elemCtrProto.__defineGetter__(classListProp, classListGetter);
}

}(self));

}
2 changes: 2 additions & 0 deletions dist/js/classList.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/myplugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* gulp-boilerplate v0.2.1
* gulp-boilerplate v0.2.2
* My Gulp.js boilerplate for creating new web projects, by Chris Ferdinandi.
* http://github.com/cferdinandi/Plugin
*
Expand Down
2 changes: 1 addition & 1 deletion dist/js/myplugin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-boilerplate",
"version": "0.2.1",
"version": "0.2.2",
"description": "My Gulp.js boilerplate for creating new web projects",
"author": {
"name": "Chris Ferdinandi",
Expand Down
26 changes: 26 additions & 0 deletions src/js/bind-polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Polyfill Function.prototype.bind support for otherwise ECMA Script 5 compliant browsers
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Compatibility
*/

if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}

var aArgs = Array.prototype.slice.call(arguments, 1);
var fToBind = this;
fNOP = function () {};
fBound = function () {
return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
};

fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();

return fBound;
};
}
Loading

0 comments on commit f7c5d80

Please sign in to comment.