get the browser file running and processing less (still doesnt pass tests though)

This commit is contained in:
Luke Page
2014-02-27 22:25:51 +00:00
parent 37cd664a88
commit 3229b45fea
3 changed files with 50 additions and 34 deletions

View File

@@ -51,7 +51,12 @@ module.exports = function(grunt) {
browserify: {
dist: {
files: {
'dist/less.js': ['lib/less/non-node-index.js', 'lib/less/browser.js', 'lib/less/environment/browser.js']
'dist/less.js': ['lib/less/browser.js']
}
},
browsertest: {
files: {
'test/browser/less.js': ['lib/less/browser.js']
}
}
},
@@ -274,12 +279,13 @@ module.exports = function(grunt) {
// setup a web server to run the browser tests in a browser rather than phantom
grunt.registerTask('browsertest-server', [
'browser',
'shell:browsertest-server'
]);
// Create the browser version of less.js
grunt.registerTask('browser', [
'concat:browsertest'
'browserify:browsertest'
]);
// Run all tests

View File

@@ -1,11 +1,23 @@
//
// browser.js - client-side engine
//
/*global less, window, document, location */
/*global window, document, location */
var less = require('./non-node-index.js'),
options = window.less;
function log(str, level) {
if (typeof(console) !== 'undefined' && less.logLevel >= level) {
console.log('less: ' + str);
}
}
var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(location.protocol);
less.env = less.env || (location.hostname == '127.0.0.1' ||
less.environment = require("./environments/browser.js")(less, isFileProtocol, log, 1/*TODO*/);
window.less = less;
less.env = options.env || (location.hostname == '127.0.0.1' ||
location.hostname == '0.0.0.0' ||
location.hostname == 'localhost' ||
(location.port &&
@@ -26,7 +38,7 @@ var logLevel = {
// 1 - Errors
// 0 - None
// Defaults to 2
less.logLevel = typeof(less.logLevel) != 'undefined' ? less.logLevel : (less.env === 'development' ? logLevel.debug : logLevel.errors);
less.logLevel = typeof(options.logLevel) != 'undefined' ? options.logLevel : (less.env === 'development' ? logLevel.debug : logLevel.errors);
// Load styles asynchronously (default: false)
//
@@ -41,10 +53,10 @@ less.fileAsync = less.fileAsync || false;
less.poll = less.poll || (isFileProtocol ? 1000 : 1500);
//Setup user functions
if (less.functions) {
for(var func in less.functions) {
if (less.functions.hasOwnProperty(func)) {
less.tree.functions[func] = less.functions[func];
if (options.functions) {
for(var func in options.functions) {
if (options.functions.hasOwnProperty(func)) {
less.tree.functions[func] = options.functions[func];
}
}
}
@@ -57,12 +69,6 @@ if (dumpLineNumbers) {
var typePattern = /^text\/(x-)?less$/;
var cache = null;
function log(str, level) {
if (typeof(console) !== 'undefined' && less.logLevel >= level) {
console.log('less: ' + str);
}
}
function extractId(href) {
return href.replace(/^[a-z-]+:\/+?[^\/]+/, '' ) // Remove protocol & domain
.replace(/^\//, '' ) // Remove root /
@@ -161,8 +167,8 @@ function createCSS(styles, sheet, lastModified) {
}
function postProcessCSS(styles) {
if (less.postProcessor && typeof less.postProcessor === 'function') {
styles = less.postProcessor.call(styles, styles) || styles;
if (options.postProcessor && typeof options.postProcessor === 'function') {
styles = options.postProcessor.call(styles, styles) || styles;
}
return styles;
}
@@ -266,12 +272,12 @@ function errorHTML(e, rootHref) {
}
function error(e, rootHref) {
if (!less.errorReporting || less.errorReporting === "html") {
if (!options.errorReporting || options.errorReporting === "html") {
errorHTML(e, rootHref);
} else if (less.errorReporting === "console") {
} else if (options.errorReporting === "console") {
errorConsole(e, rootHref);
} else if (typeof less.errorReporting === 'function') {
less.errorReporting("add", e, rootHref);
} else if (typeof options.errorReporting === 'function') {
options.errorReporting("add", e, rootHref);
}
}
@@ -287,12 +293,12 @@ function removeErrorConsole(path) {
}
function removeError(path) {
if (!less.errorReporting || less.errorReporting === "html") {
if (!options.errorReporting || options.errorReporting === "html") {
removeErrorHTML(path);
} else if (less.errorReporting === "console") {
} else if (options.errorReporting === "console") {
removeErrorConsole(path);
} else if (typeof less.errorReporting === 'function') {
less.errorReporting("remove", path);
} else if (typeof options.errorReporting === 'function') {
options.errorReporting("remove", path);
}
}
@@ -306,7 +312,7 @@ function loadStyles(modifyVars) {
lessText = style.innerHTML || '';
env.filename = document.location.href.replace(/#.*$/, '');
if (modifyVars || less.globalVars) {
if (modifyVars || options.globalVars) {
env.useFileCache = true;
}
@@ -317,7 +323,7 @@ function loadStyles(modifyVars) {
if (e) {
return error(e, "inline");
}
var css = cssAST.toCSS(less);
var css = cssAST.toCSS(options);
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
@@ -326,7 +332,7 @@ function loadStyles(modifyVars) {
}
};
})(style);
new(less.Parser)(env).parse(lessText, callback, {globalVars: less.globalVars, modifyVars: modifyVars});
new(less.Parser)(env).parse(lessText, callback, {globalVars: options.globalVars, modifyVars: modifyVars});
}
}
}
@@ -336,7 +342,7 @@ function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {
var env = new less.tree.parseEnv(less);
env.mime = sheet.type;
if (modifyVars || less.globalVars) {
if (modifyVars || options.globalVars) {
env.useFileCache = true;
}
@@ -380,7 +386,7 @@ function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {
} catch (e) {
callback(e, null, null, sheet);
}
}, {modifyVars: modifyVars, globalVars: less.globalVars});
}, {modifyVars: modifyVars, globalVars: options.globalVars});
} else {
callback(e, null, null, sheet, webInfo, path);
}
@@ -407,7 +413,7 @@ function initRunningMode(){
}
});
}
}, less.poll);
}, options.poll);
}
}
@@ -469,7 +475,7 @@ less.refresh = function (reload, modifyVars) {
log("loading " + sheet.href + " from cache.", logLevel.info);
} else {
log("parsed " + sheet.href + " successfully.", logLevel.debug);
var styles = root.toCSS(less);
var styles = root.toCSS(options);
styles = postProcessCSS(styles);
createCSS(styles, sheet, env.lastModified);
}

View File

@@ -1,4 +1,6 @@
/*global less, window, XMLHttpRequest, log, logLevel, isFileProtocol */
/*global window, XMLHttpRequest */
module.exports = function(less, isFileProtocol, log, logLevel) {
var fileCache = {};
@@ -19,7 +21,7 @@ function getXMLHttpRequest() {
}
}
less.environment = {
return {
// make generic but overriddable
warn: function warn(env, msg) {
console.warn(msg);
@@ -193,4 +195,6 @@ less.environment = {
});
}
};
};