From 6c5072ebbca97138f81d02b19a4c2e70b13bb2f8 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Wed, 21 Aug 2013 19:35:28 +0100 Subject: [PATCH] implement log to console and fix browser tests --- Makefile | 2 +- lib/less/browser.js | 69 ++++++++++++++++++++++--- test/browser-test-prepare.js | 4 +- test/browser/css/relative-urls/urls.css | 1 - test/browser/css/rootpath/urls.css | 1 - test/browser/es5.js | 27 ++++++++++ test/browser/template.htm | 3 +- 7 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 test/browser/es5.js diff --git a/Makefile b/Makefile index c9920c5e..ed898a9e 100644 --- a/Makefile +++ b/Makefile @@ -39,8 +39,8 @@ less: ${SRC}/parser.js\ ${SRC}/functions.js\ ${SRC}/colors.js\ - ${SRC}/tree/*.js\ ${SRC}/tree.js\ + ${SRC}/tree/*.js\ ${SRC}/env.js\ ${SRC}/visitor.js\ ${SRC}/import-visitor.js\ diff --git a/lib/less/browser.js b/lib/less/browser.js index 79267092..2cef7ea4 100644 --- a/lib/less/browser.js +++ b/lib/less/browser.js @@ -124,7 +124,9 @@ less.refresh = function (reload, newVars) { createCSS(root.toCSS(less), sheet, env.lastModified); } log("css for " + sheet.href + " generated in " + (new Date() - endTime) + 'ms'); - (env.remaining === 0) && log("css generated in " + (new Date() - startTime) + 'ms'); + if (env.remaining === 0) { + log("css generated in " + (new Date() - startTime) + 'ms'); + } endTime = new Date(); }, reload, newVars); @@ -280,7 +282,7 @@ function loadStyleSheet(sheet, callback, reload, remaining, newVars) { } //TODO add tests around how this behaves when reloading - removeNode(document.getElementById('less-error-message:' + extractId(path))); + removeError(path); if (data) { env.currentFileInfo = newFileInfo; @@ -485,15 +487,70 @@ function getXMLHttpRequest() { } } -function removeNode(node) { - return node && node.parentNode.removeChild(node); -} - function log(str) { if (less.env == 'development' && typeof(console) !== "undefined") { console.log('less: ' + str); } } function error(e, rootHref) { + if (!less.errorReporting || less.errorReporting === "html") { + errorHTML(e, rootHref); + } else if (less.errorReporting === "console") { + errorConsole(e, rootHref); + } else if (typeof less.errorReporting === 'function') { + less.errorReporting("add", e, rootHref); + } +} + +function removeError(path) { + if (!less.errorReporting || less.errorReporting === "html") { + removeErrorHTML(path); + } else if (less.errorReporting === "console") { + removeErrorConsole(path); + } else if (typeof less.errorReporting === 'function') { + less.errorReporting("remove", path); + } +} + +function removeErrorHTML(path) { + var node = document.getElementById('less-error-message:' + extractId(path)); + if (node) { + node.parentNode.removeChild(node); + } +} + +function removeErrorConsole(path) { + //no action +} + +function errorConsole(e, rootHref) { + var template = '{line}\n{content}'; + var filename = e.filename || rootHref; + var filenameNoPath = filename.match(/([^\/]+(\?.*)?)$/)[1]; + + content = (e.type || "Syntax") + "Error: " + (e.message || 'There is an error in your .less file') + + "'" + filename + "'"; + + var errorline = function (e, i, classname) { + if (e.extract[i] !== undefined) { + errors.push(template.replace(/\{line\}/, (parseInt(e.line, 10) || 0) + (i - 1)) + .replace(/\{class\}/, classname) + .replace(/\{content\}/, e.extract[i])); + } + }; + + if (e.extract) { + errorline(e, 0, ''); + errorline(e, 1, 'line'); + errorline(e, 2, ''); + content += 'on line ' + e.line + ', column ' + (e.column + 1) + ':\n' + + errors.join('\n'); + } else if (e.stack) { + content += e.stack; + } + log(content); +} + +function errorHTML(e, rootHref) { var id = 'less-error-message:' + extractId(rootHref || ""); var template = '
  • {content}
  • '; var elem = document.createElement('div'), timer, content, errors = []; diff --git a/test/browser-test-prepare.js b/test/browser-test-prepare.js index 95a99703..bb9ac3d1 100644 --- a/test/browser-test-prepare.js +++ b/test/browser-test-prepare.js @@ -17,8 +17,8 @@ var createTestRunnerPage = function(dir, exclude, testSuiteName, dir2) { if (exclude && name.match(exclude)) { return; } - output += '\n'; - output += '\n'; + output += '\n'; + output += '\n'; }); output += String(fs.readFileSync(path.join('test/browser', 'template.htm'))).replace("{runner-name}", testSuiteName); diff --git a/test/browser/css/relative-urls/urls.css b/test/browser/css/relative-urls/urls.css index d0da4990..96bbf604 100644 --- a/test/browser/css/relative-urls/urls.css +++ b/test/browser/css/relative-urls/urls.css @@ -1,5 +1,4 @@ @import "http://localhost:8081/browser/less/imports/modify-this.css"; - @import "http://localhost:8081/browser/less/imports/modify-again.css"; .modify { my-url: url("http://localhost:8081/browser/less/imports/a.png"); diff --git a/test/browser/css/rootpath/urls.css b/test/browser/css/rootpath/urls.css index a3ef5d9e..9618e8e5 100644 --- a/test/browser/css/rootpath/urls.css +++ b/test/browser/css/rootpath/urls.css @@ -1,5 +1,4 @@ @import "https://www.github.com/modify-this.css"; - @import "https://www.github.com/modify-again.css"; .modify { my-url: url("https://www.github.com/a.png"); diff --git a/test/browser/es5.js b/test/browser/es5.js new file mode 100644 index 00000000..c4fcc802 --- /dev/null +++ b/test/browser/es5.js @@ -0,0 +1,27 @@ +/* + PhantomJS does not implement bind. this is from + https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind + */ +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), + 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; + }; +} \ No newline at end of file diff --git a/test/browser/template.htm b/test/browser/template.htm index c000fdc2..41be7c0e 100644 --- a/test/browser/template.htm +++ b/test/browser/template.htm @@ -1,9 +1,10 @@ + - +