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}