From a9b47a026ced4a6bb067917da09190e8be6cc632 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Mon, 17 May 2010 22:57:11 -0400 Subject: [PATCH] added ECMAScript 4 compatibility --- Makefile | 2 +- build/compat.js | 14 ---- build/ecma-5.js | 157 ++++++++++++++++++++++++++++++++++++++++++++ lib/less/browser.js | 7 +- 4 files changed, 162 insertions(+), 18 deletions(-) delete mode 100644 build/compat.js create mode 100644 build/ecma-5.js diff --git a/Makefile b/Makefile index b6e458f..55d5c37 100644 --- a/Makefile +++ b/Makefile @@ -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\ diff --git a/build/compat.js b/build/compat.js deleted file mode 100644 index f0ba01b..0000000 --- a/build/compat.js +++ /dev/null @@ -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; - } - } -} diff --git a/build/ecma-5.js b/build/ecma-5.js new file mode 100644 index 0000000..8b467c0 --- /dev/null +++ b/build/ecma-5.js @@ -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, ''); + }; +} diff --git a/lib/less/browser.js b/lib/less/browser.js index fd81915..4cf69af 100644 --- a/lib/less/browser.js +++ b/lib/less/browser.js @@ -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 })); } }