added ECMAScript 4 compatibility

This commit is contained in:
cloudhead 2010-05-17 22:57:11 -04:00
parent a3f252b727
commit a9b47a026c
4 changed files with 162 additions and 18 deletions

View File

@ -21,7 +21,7 @@ VERSION = `cat VERSION`
less:
@@mkdir -p dist
@@cat build/compat.js\
@@cat build/ecma-5.js\
${SRC}/parser.js\
${SRC}/functions.js\
${SRC}/tree/*.js\

View File

@ -1,14 +0,0 @@
if (typeof(String.prototype.trim) !== "function") {
String.prototype.trim = function (str) {
return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};
}
if (typeof(Array.isArray) !== "function") {
Array.isArray = function (a) {
if (a instanceof Array) {
return true;
} else {
return false;
}
}
}

157
build/ecma-5.js Normal file
View File

@ -0,0 +1,157 @@
// -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
// -- tlrobinson Tom Robinson
// dantman Daniel Friesen
/*!
Copyright (c) 2009, 280 North Inc. http://280north.com/
MIT License. http://github.com/280north/narwhal/blob/master/README.md
*/
//
// Array
// =====
//
// ES5 15.4.3.2
if (!Array.isArray) {
Array.isArray = function(obj) {
return Object.prototype.toString.call(obj) == "[object Array]";
};
}
// ES5 15.4.4.18
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(block, thisObject) {
var len = this.length >>> 0;
for (var i = 0; i < len; i++) {
if (i in this) {
block.call(thisObject, this[i], i, this);
}
}
};
}
// ES5 15.4.4.19
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
if (!Array.prototype.map) {
Array.prototype.map = function(fun /*, thisp*/) {
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
// ES5 15.4.4.20
if (!Array.prototype.filter) {
Array.prototype.filter = function (block /*, thisp */) {
var values = [];
var thisp = arguments[1];
for (var i = 0; i < this.length; i++)
if (block.call(thisp, this[i]))
values.push(this[i]);
return values;
};
}
// ES5 15.4.4.21
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/reduce
if (!Array.prototype.reduce) {
Array.prototype.reduce = function(fun /*, initial*/) {
var len = this.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
// no value to return if no initial value and an empty array
if (len == 0 && arguments.length == 1)
throw new TypeError();
var i = 0;
if (arguments.length >= 2) {
var rv = arguments[1];
} else {
do {
if (i in this) {
rv = this[i++];
break;
}
// if array contains no values, no initial value to return
if (++i >= len)
throw new TypeError();
} while (true);
}
for (; i < len; i++) {
if (i in this)
rv = fun.call(null, rv, this[i], i, this);
}
return rv;
};
}
// ES5 15.4.4.14
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (value /*, fromIndex */ ) {
var length = this.length;
if (!length)
return -1;
var i = arguments[1] || 0;
if (i >= length)
return -1;
if (i < 0)
i += length;
for (; i < length; i++) {
if (!Object.prototype.hasOwnProperty.call(this, i))
continue;
if (value === this[i])
return i;
}
return -1;
};
}
//
// Object
// ======
//
// ES5 15.2.3.14
if (!Object.keys) {
Object.keys = function (object) {
var keys = [];
for (var name in object) {
if (Object.prototype.hasOwnProperty.call(object, name)) {
keys.push(name);
}
}
return keys;
};
}
//
// String
// ======
//
// ES5 15.5.4.20
if (!String.prototype.trim) {
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
var trimBeginRegexp = /^\s\s*/;
var trimEndRegexp = /\s\s*$/;
String.prototype.trim = function () {
return String(this).replace(trimBeginRegexp, '').replace(trimEndRegexp, '');
};
}

View File

@ -1,7 +1,8 @@
//
// Select all links with the 'rel' attribute set to "less"
//
var sheets = document.querySelectorAll("link[rel=less]");
var sheets = (document.querySelectorAll ? document.querySelectorAll
: jQuery)('link[rel="stylesheet/less"]');
less.env = location.hostname == '127.0.0.1' ||
location.hostname == '0.0.0.0' ||
@ -11,7 +12,7 @@ less.env = location.hostname == '127.0.0.1' ||
for (var i = 0; i < sheets.length; i++) {
(function (sheet) { // Because the functions here are async, we need to create a closure
var css = localStorage.getItem(sheet.href);
var css = localStorage && localStorage.getItem(sheet.href);
var styles = css && JSON.parse(css);
xhr(sheet.href, function (data, lastModified) {
@ -42,7 +43,7 @@ function createCSS(styles, sheet, lastModified) {
css.title = sheet.title || sheet.href.match(/\/([-\w]+)\.[a-z]+$/i)[1];
// Don't update the local store if the file wasn't modified
if (lastModified) {
if (lastModified && localStorage) {
localStorage.setItem(sheet.href, JSON.stringify({ timestamp: lastModified, css: styles }));
}
}