implement log to console and fix browser tests

This commit is contained in:
Luke Page
2013-08-21 19:35:28 +01:00
parent c1fe375cdc
commit 6c5072ebbc
7 changed files with 95 additions and 12 deletions

View File

@@ -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\

View File

@@ -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 = '<li><label>{line}</label><pre class="{class}">{content}</pre></li>';
var elem = document.createElement('div'), timer, content, errors = [];

View File

@@ -17,8 +17,8 @@ var createTestRunnerPage = function(dir, exclude, testSuiteName, dir2) {
if (exclude && name.match(exclude)) { return; }
output += '<link id="original-less:' + id + '" rel="stylesheet/less" type="text/css" href="/' + path.join(dir, 'less', dir2 || "", name) + '.less' +'">\n';
output += '<link id="expected-less:' + id + '" rel="stylesheet" type="text/css" href="/' + path.join(dir, 'css', dir2 || "", name) + '.css' + '">\n';
output += '<link id="original-less:' + id + '" rel="stylesheet/less" type="text/css" href="/' + path.join(dir, 'less', dir2 || "", name).replace("\\", "/") + '.less' +'" />\n';
output += '<link id="expected-less:' + id + '" rel="stylesheet" type="text/css" href="/' + path.join(dir, 'css', dir2 || "", name).replace("\\", "/") + '.css' + '" />\n';
});
output += String(fs.readFileSync(path.join('test/browser', 'template.htm'))).replace("{runner-name}", testSuiteName);

View File

@@ -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");

View File

@@ -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");

27
test/browser/es5.js Normal file
View File

@@ -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;
};
}

View File

@@ -1,9 +1,10 @@
<script src="/browser/jasmine.js" type="text/javascript"></script>
<script src="/browser/jasmine-html.js" type="text/javascript"></script>
<script src="/browser/common.js" type="text/javascript"></script>
<script src="/browser/es5.js" type="text/javascript"></script>
<script src="/browser/runner-{runner-name}.js" type="text/javascript"></script>
<script src="/browser/less.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="/browser/jasmine.css"></link>
<link rel="stylesheet" type="text/css" href="/browser/jasmine.css" />
</head>
<body>
</body>