refactoring - add a env type to better organise its properties

This commit is contained in:
Luke Page
2013-01-23 19:07:05 +00:00
parent 26d35c98fe
commit e45ec8a31e
6 changed files with 110 additions and 87 deletions

View File

@@ -103,7 +103,7 @@ less.modifyVars = function(record) {
str += ((name.slice(0,1) === '@')? '' : '@') + name +': '+
((record[name].slice(-1) === ';')? record[name] : record[name] +';');
}
new(less.Parser)().parse(str, function (e, root) {
new(less.Parser)(new less.tree.parseEnv(less)).parse(str, function (e, root) {
createCSS(root.toCSS(), less.sheets[less.sheets.length - 1]);
});
};
@@ -134,11 +134,11 @@ function loadStyles() {
var styles = document.getElementsByTagName('style');
for (var i = 0; i < styles.length; i++) {
if (styles[i].type.match(typePattern)) {
new(less.Parser)({
filename: document.location.href.replace(/#.*$/, ''),
dumpLineNumbers: less.dumpLineNumbers
}).parse(styles[i].innerHTML || '', function (e, tree) {
var css = tree.toCSS();
var env = new less.tree.parseEnv(less);
env.filename = document.location.href.replace(/#.*$/, '');
new(less.Parser)(env).parse(styles[i].innerHTML || '', function (e, cssAST) {
var css = cssAST.toCSS();
var style = styles[i];
style.type = 'text/css';
if (style.styleSheet) {
@@ -228,36 +228,35 @@ function extractUrlParts(url, baseUrl) {
}
function loadStyleSheet(sheet, callback, reload, remaining) {
// sheet may be set to the stylesheet for the initial load or a collection of properties including
// some env variables for imports
var contents = sheet.contents || {};
var files = sheet.files || {};
var hrefParts = extractUrlParts(sheet.href, window.location.href);
var href = hrefParts.url;
var css = cache && cache.getItem(href);
var timestamp = cache && cache.getItem(href + ':timestamp');
var styles = { css: css, timestamp: timestamp };
var rootpath;
var env;
if (less.relativeUrls) {
if (sheet instanceof less.tree.parseEnv) {
env = new less.tree.parseEnv(sheet);
} else {
env = new less.tree.parseEnv(less);
env.entryPath = hrefParts.path;
env.mime = sheet.type;
}
if (env.relativeUrls) {
//todo - this relies on option being set on less object rather than being passed in as an option
// - need an originalRootpath
if (less.rootpath) {
if (sheet.entryPath) {
rootpath = extractUrlParts(less.rootpath + pathDiff(hrefParts.path, sheet.entryPath)).path;
} else {
rootpath = less.rootpath;
}
env.rootpath = extractUrlParts(less.rootpath + pathDiff(hrefParts.path, env.entryPath)).path;
} else {
rootpath = hrefParts.path;
env.rootpath = hrefParts.path;
}
} else {
if (less.rootpath) {
rootpath = less.rootpath;
} else {
if (sheet.entryPath) {
rootpath = sheet.entryPath;
} else {
rootpath = hrefParts.path;
}
if (!less.rootpath) {
env.rootpath = env.entryPath;
}
}
@@ -274,20 +273,11 @@ function loadStyleSheet(sheet, callback, reload, remaining) {
} else {
// Use remote copy (re-parse)
try {
contents[href] = data; // Updating top importing parser content cache
new(less.Parser)({
optimization: less.optimization,
paths: [hrefParts.path],
entryPath: sheet.entryPath || hrefParts.path,
mime: sheet.type,
filename: href,
rootpath: rootpath,
relativeUrls: sheet.relativeUrls,
contents: contents, // Passing top importing parser content cache ref down.
files: files,
dumpLineNumbers: less.dumpLineNumbers
}).parse(data, function (e, root) {
if (e) { return error(e, href) }
env.contents[href] = data; // Updating content cache
env.paths = [hrefParts.path];
env.filename = href;
new(less.Parser)(env).parse(data, function (e, root) {
if (e) { return error(e, href); }
try {
callback(e, root, data, sheet, { local: false, lastModified: lastModified, remaining: remaining }, href);
removeNode(document.getElementById('less-error-message:' + extractId(href)));