kis-js/src/polyfill.js

220 lines
4.8 KiB
JavaScript
Raw Normal View History

2011-11-02 12:10:58 -04:00
/**
* A module of various browser polyfills
* @file polyfill.js
*/
2011-11-03 16:04:35 -04:00
(function(){
2011-11-02 12:10:58 -04:00
2011-11-03 16:04:35 -04:00
"use strict";
// Console.log polyfill for IE 8 stupidity
if(typeof window.console === "undefined")
{
window.console = {
log:function(){}
};
}
// --------------------------------------------------------------------------
2011-11-02 12:10:58 -04:00
/**
2011-11-03 16:04:35 -04:00
* String trim function polyfill
2011-11-02 12:10:58 -04:00
*/
2011-11-03 16:04:35 -04:00
if(typeof String.prototype.trim === "undefined")
{
/**
* @private
*/
String.prototype.trim = function()
{
return this.replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, "");
};
}
2011-11-02 12:10:58 -04:00
2011-11-03 16:04:35 -04:00
// --------------------------------------------------------------------------
//This is used so IE 8 can use the classList api
/*
* classList.js: Cross-browser full element.classList implementation.
* 2011-06-15
*
* By Eli Grey, http://eligrey.com
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/
if (typeof document !== "undefined" && !("classList" in document.createElement("a")))
{
(function (view){
var classListProp = "classList",
protoProp = "prototype",
elemCtrProto = (view.HTMLElement || view.Element)[protoProp],
objCtr = Object,
strTrim = String[protoProp].trim ||
function ()
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
return this.replace(/^\s+|\s+$/g, "");
},
arrIndexOf = Array[protoProp].indexOf ||
function (item)
{
var
i = 0,
len = this.length;
for (; i < len; i++)
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
if (i in this && this[i] === item)
{
return i;
}
2011-11-02 12:10:58 -04:00
}
2011-11-03 16:04:35 -04:00
return -1;
2011-11-02 12:10:58 -04:00
}
2011-11-03 16:04:35 -04:00
// Vendors: please allow content code to instantiate DOMExceptions
,
/**
* @private
*/
DOMEx = function (type, message)
{
this.name = type;
this.code = DOMException[type];
this.message = message;
},
/**
* @private
*/
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);
},
/**
* @private
*/
ClassList = function (elem)
{
var
trimmedClasses = strTrim.call(elem.className),
classes = trimmedClasses ? trimmedClasses.split(/\s+/) : [],
i = 0,
len = classes.length;
for (; i < len; i++)
{
this.push(classes[i]);
}
this._updateClassName = function ()
{
elem.className = this.toString();
};
},
classListProto = ClassList[protoProp] = [],
/**
* @private
*/
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)
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
return this[i] || null;
};
classListProto.contains = function (token)
{
token += "";
return checkTokenAndGetIndex(this, token) !== -1;
};
classListProto.add = function (token)
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
token += "";
if (checkTokenAndGetIndex(this, token) === -1)
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
this.push(token);
this._updateClassName();
2011-11-02 12:10:58 -04:00
}
2011-11-03 16:04:35 -04:00
};
classListProto.remove = function (token)
{
token += "";
var index = checkTokenAndGetIndex(this, token);
if (index !== -1)
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
this.splice(index, 1);
this._updateClassName();
2011-11-02 12:10:58 -04:00
}
2011-11-03 16:04:35 -04:00
};
classListProto.toggle = function (token)
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
token += "";
if (checkTokenAndGetIndex(this, token) === -1)
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
this.add(token);
2011-11-02 12:10:58 -04:00
}
2011-11-03 16:04:35 -04:00
else
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
this.remove(token);
}
2011-11-02 12:10:58 -04:00
};
2011-11-03 16:04:35 -04:00
classListProto.toString = function ()
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
return this.join(" ");
2011-11-02 12:10:58 -04:00
};
2011-11-03 16:04:35 -04:00
if (objCtr.defineProperty)
2011-11-02 12:10:58 -04:00
{
2011-11-03 16:04:35 -04:00
var classListPropDesc = {
get: classListGetter,
enumerable: true,
configurable: true
};
try
2011-11-02 12:10:58 -04:00
{
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
}
2011-11-03 16:04:35 -04:00
catch (ex)
{ // IE 8 doesn't support enumerable:true
if (ex.number === -0x7FF5EC54)
{
classListPropDesc.enumerable = false;
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
}
}
2011-11-02 12:10:58 -04:00
}
2011-11-03 16:04:35 -04:00
else if (objCtr[protoProp].__defineGetter__)
{
elemCtrProto.__defineGetter__(classListProp, classListGetter);
}
}(self));
}
2011-11-06 17:11:34 -05:00
// --------------------------------------------------------------------------
/**
* event.preventDefault/e.stopPropagation polyfill
* @private
*/
if(typeof Event.preventDefault === "undefined" && typeof window.event !== "undefined")
{
Event.prototype.preventDefault = function()
{
window.event.stop();
},
Event.prototype.stopPropagation = function()
{
window.event.returnValue = false;
}
}
2011-11-03 16:04:35 -04:00
}());