From e501f2e47c19309f31d8446c7bb0a0875b9eac95 Mon Sep 17 00:00:00 2001 From: James Foster Date: Wed, 19 May 2010 08:28:24 +0800 Subject: [PATCH] Fixes for IE7 compatibility --- README.md | 2 +- lib/less/browser.js | 18 ++++++++++-------- lib/less/parser.js | 32 ++++++++++++++++++-------------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 9965b473..ced64e37 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ synopsis First, run `make less` in the command line. It will the build the *less.js* file in *dist/*. Then, you can use it as such: - + To build a minified version, run `make min` diff --git a/lib/less/browser.js b/lib/less/browser.js index 763aa1b6..68359d61 100644 --- a/lib/less/browser.js +++ b/lib/less/browser.js @@ -1,8 +1,11 @@ // // Select all links with the 'rel' attribute set to "less" // -var sheets = (document.querySelectorAll ? document.querySelectorAll - : jQuery).call(document, 'link[rel="stylesheet/less"]'); +var sheets = []; +if(!document.querySelectorAll && typeof(jQuery) === "undefined") + log("No selector method found"); +else + sheets = (document.querySelectorAll || jQuery).call(document, 'link[rel="stylesheet/less"]'); less.env = location.hostname == '127.0.0.1' || location.hostname == '0.0.0.0' || @@ -12,7 +15,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 && localStorage.getItem(sheet.href); + var css = typeof(localStorage) !== "undefined" && localStorage.getItem(sheet.href); var styles = css && JSON.parse(css); xhr(sheet.href, function (data, lastModified) { @@ -37,13 +40,13 @@ function createCSS(styles, sheet, lastModified) { var css = document.createElement('style'); css.type = 'text/css'; css.media = 'screen'; - css.title = 'lessheet'; + css.title = 'less-sheet'; if (sheet) { - css.title = sheet.title || sheet.href.match(/\/([-\w]+)\.[a-z]+$/i)[1]; + 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 && localStorage) { + if (lastModified && typeof(localStorage) !== "undefined") { localStorage.setItem(sheet.href, JSON.stringify({ timestamp: lastModified, css: styles })); } } @@ -84,7 +87,7 @@ function xhr(url, callback, errback) { } function log(str) { - if (less.env == 'development') { console.log(str) } + if (less.env == 'development' && typeof(console) !== "undefined") { console.log(str) } } function error(e, href) { @@ -100,7 +103,6 @@ function error(e, href) { '

' + href + " " + 'on line ' + e.line + ', column ' + e.column + ':

' + template.replace(/\[(-?\d)\]/g, function (_, i) { - console.log(i) return e.line + parseInt(i); }).replace(/\{(\d)\}/g, function (_, i) { return e.extract[parseInt(i)]; diff --git a/lib/less/parser.js b/lib/less/parser.js index 6c4bc8fc..ef398320 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -99,7 +99,7 @@ less.Parser = function Parser(env) { // or match a regexp in the current chunk (chunk[j]). // } else if (typeof(tok) === 'string') { - match = input[i] === tok ? tok : null; + match = input.charAt(i) === tok ? tok : null; length = 1; // 1. We move to the next chunk, if necessary. @@ -138,6 +138,10 @@ less.Parser = function Parser(env) { if (! (c === 32 || c === 10 || c === 9)) { break } i++; } + + if(typeof(match) === 'string') + return match; + return match.length === 1 ? match[0] : match; } } @@ -148,7 +152,7 @@ less.Parser = function Parser(env) { var match; if (typeof(tok) === 'string') { - return input[i] === tok; + return input.charAt(i) === tok; } else { tok.lastIndex = i; @@ -230,7 +234,7 @@ less.Parser = function Parser(env) { lines = input.split('\n'); line = (input.slice(0, i).match(/\n/g) || "").length + 1; - for (var n = i, column = -1; input[n] !== '\n'; n--) { column++ } + for (var n = i, column = -1; n >= 0 && input.charAt(n) !== '\n'; n--) { column++ } error = { name: "ParseError", @@ -314,7 +318,7 @@ less.Parser = function Parser(env) { comment: function () { var comment; - if (input[i] !== '/') return; + if (input.charAt(i) !== '/') return; if (comment = $(/\/\*(?:[^*]|\*+[^\/*])*\*+\/\n?/g)) { return new(tree.Comment)(comment); @@ -334,7 +338,7 @@ less.Parser = function Parser(env) { // quoted: function () { var str; - if (input[i] !== '"' && input[i] !== "'") return; + if (input.charAt(i) !== '"' && input.charAt(i) !== "'") return; if (str = $(/"((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)'/g)) { return new(tree.Quoted)(str[0], str[1] || str[2]); @@ -399,7 +403,7 @@ less.Parser = function Parser(env) { url: function () { var value; - if (input[i] !== 'u' || !$(/url\(/g)) return; + if (input.charAt(i) !== 'u' || !$(/url\(/g)) return; value = $(this.entities.quoted) || $(/[-a-zA-Z0-9_%@$\/.&=:;#+?]+/g); if (! $(')')) throw new(Error)("missing closing ) for url()"); @@ -417,7 +421,7 @@ less.Parser = function Parser(env) { variable: function () { var name; - if (input[i] === '@' && (name = $(/@[a-zA-Z0-9_-]+/g))) { + if (input.charAt(i) === '@' && (name = $(/@[a-zA-Z0-9_-]+/g))) { return new(tree.Variable)(name); } }, @@ -432,7 +436,7 @@ less.Parser = function Parser(env) { color: function () { var rgb; - if (input[i] === '#' && (rgb = $(/#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})/g))) { + if (input.charAt(i) === '#' && (rgb = $(/#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})/g))) { return new(tree.Color)(rgb[1]); } }, @@ -460,7 +464,7 @@ less.Parser = function Parser(env) { variable: function () { var name; - if (input[i] === '@' && (name = $(/(@[a-zA-Z0-9_-]+)\s*:/g))) { return name[1] } + if (input.charAt(i) === '@' && (name = $(/(@[a-zA-Z0-9_-]+)\s*:/g))) { return name[1] } }, // @@ -531,7 +535,7 @@ less.Parser = function Parser(env) { definition: function () { var name, params = [], match, ruleset, param, value; - if (input[i] !== '.' || peek(/[^{]*(;|})/g)) return; + if (input.charAt(i) !== '.' || peek(/[^{]*(;|})/g)) return; if (match = $(/([#.][a-zA-Z0-9_-]+)\s*\(/g)) { name = match[1]; @@ -633,7 +637,7 @@ less.Parser = function Parser(env) { if (match = $(/[+>~]/g) || $('&') || $(/::/g)) { return new(tree.Combinator)(match); } else { - return new(tree.Combinator)(input[i - 1] === " " ? " " : null); + return new(tree.Combinator)(input.charAt(i - 1) === " " ? " " : null); } }, @@ -714,7 +718,7 @@ less.Parser = function Parser(env) { var memo = i; if (name = $(this.property) || $(this.variable)) { - if ((name[0] != '@') && (match = peek(/([^@+\/*(;{}-]*);/g))) { + if ((name.charAt(0) != '@') && (match = peek(/([^@+\/*(;{}-]*);/g))) { i += match[0].length - 1; value = new(tree.Anonymous)(match[1]); } else if (name === "font") { @@ -759,7 +763,7 @@ less.Parser = function Parser(env) { directive: function () { var name, value, rules, types; - if (input[i] !== '@') return; + if (input.charAt(i) !== '@') return; if (value = $(this['import'])) { return value; @@ -838,7 +842,7 @@ less.Parser = function Parser(env) { addition: function () { var m, a, op, operation; if (m = $(this.multiplication)) { - while ((op = $(/[-+]\s+/g) || (input[i - 1] != ' ' && $(/[-+]/g))) && + while ((op = $(/[-+]\s+/g) || (input.charAt(i - 1) != ' ' && $(/[-+]/g))) && (a = $(this.multiplication))) { operation = new(tree.Operation)(op, [operation || m, a]); }