mirror of
https://github.com/less/less.js.git
synced 2026-05-01 03:00:22 -04:00
(new) css compression support
This commit is contained in:
@@ -213,16 +213,25 @@ less.Parser = function Parser(env) {
|
||||
root.toCSS = (function (toCSS) {
|
||||
var line, lines, column;
|
||||
|
||||
return function () {
|
||||
return function (options) {
|
||||
options = options || {};
|
||||
try {
|
||||
return toCSS.call(this);
|
||||
var css = toCSS.call(this, [], {
|
||||
frames: [],
|
||||
compress: options.compress || false
|
||||
});
|
||||
if (options.compress) {
|
||||
return css.replace(/(\s)+/g, "$1");
|
||||
} else {
|
||||
return css;
|
||||
}
|
||||
} catch (e) {
|
||||
lines = input.split('\n');
|
||||
line = (input.slice(0, e.index).match(/\n/g) || "").length + 1;
|
||||
|
||||
for (var n = e.index, column = -1;
|
||||
n >= 0 && input.charAt(n) !== '\n';
|
||||
n--) { column++ }
|
||||
|
||||
throw {
|
||||
name: "NameError",
|
||||
message: e.message,
|
||||
|
||||
@@ -4,7 +4,7 @@ tree.Comment = function Comment(value) {
|
||||
this.value = value;
|
||||
};
|
||||
tree.Comment.prototype = {
|
||||
toCSS: function () {
|
||||
return this.value;
|
||||
toCSS: function (env) {
|
||||
return env.compress ? '' : this.value;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -12,8 +12,9 @@ tree.Directive.prototype = {
|
||||
toCSS: function (ctx, env) {
|
||||
if (this.ruleset) {
|
||||
this.ruleset.root = true;
|
||||
return this.name + ' {\n ' +
|
||||
this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') + '\n}\n';
|
||||
return this.name + (env.compress ? '{' : ' {\n ') +
|
||||
this.ruleset.toCSS(ctx, env).trim().replace(/\n/g, '\n ') +
|
||||
(env.compress ? '}': '\n}\n');
|
||||
} else {
|
||||
return this.name + ' ' + this.value.toCSS() + ';\n';
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ tree.Element = function Element(combinator, value) {
|
||||
combinator : new(tree.Combinator)(combinator);
|
||||
this.value = value.trim();
|
||||
};
|
||||
tree.Element.prototype.toCSS = function () {
|
||||
return this.combinator.toCSS() + this.value;
|
||||
tree.Element.prototype.toCSS = function (env) {
|
||||
return this.combinator.toCSS(env || {}) + this.value;
|
||||
};
|
||||
|
||||
tree.Combinator = function Combinator(value) {
|
||||
@@ -16,15 +16,15 @@ tree.Combinator = function Combinator(value) {
|
||||
this.value = value ? value.trim() : "";
|
||||
}
|
||||
};
|
||||
tree.Combinator.prototype.toCSS = function () {
|
||||
switch (this.value) {
|
||||
case '' : return '';
|
||||
case ' ' : return ' ';
|
||||
case '&' : return '';
|
||||
case ':' : return ' :';
|
||||
case '::': return '::';
|
||||
case '+' : return ' + ';
|
||||
case '~' : return ' ~ ';
|
||||
case '>' : return ' > ';
|
||||
}
|
||||
tree.Combinator.prototype.toCSS = function (env) {
|
||||
return {
|
||||
'' : '',
|
||||
' ' : ' ',
|
||||
'&' : '',
|
||||
':' : ' :',
|
||||
'::': '::',
|
||||
'+' : env.compress ? '+' : ' + ',
|
||||
'~' : env.compress ? '~' : ' ~ ',
|
||||
'>' : env.compress ? '>' : ' > '
|
||||
}[this.value];
|
||||
};
|
||||
|
||||
@@ -9,10 +9,10 @@ tree.Rule = function Rule(name, value, index) {
|
||||
this.variable = true;
|
||||
} else { this.variable = false }
|
||||
};
|
||||
tree.Rule.prototype.toCSS = function () {
|
||||
tree.Rule.prototype.toCSS = function (env) {
|
||||
if (this.variable) { return "" }
|
||||
else {
|
||||
return this.name + ": " + this.value.toCSS() + ";";
|
||||
return this.name + (env.compress ? ':' : ': ') + this.value.toCSS(env) + ";";
|
||||
}
|
||||
};
|
||||
|
||||
@@ -34,10 +34,10 @@ tree.Value.prototype = {
|
||||
}));
|
||||
}
|
||||
},
|
||||
toCSS: function () {
|
||||
toCSS: function (env) {
|
||||
return this.value.map(function (e) {
|
||||
return e.toCSS();
|
||||
}).join(', ');
|
||||
return e.toCSS(env);
|
||||
}).join(env.compress ? ',' : ', ');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -92,7 +92,6 @@ tree.Ruleset.prototype = {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
context = [], env = { frames: [] }
|
||||
for (var i = 0; i < this.rules.length; i++) {
|
||||
if (this.rules[i] instanceof tree.Import) {
|
||||
Array.prototype.splice
|
||||
@@ -122,13 +121,13 @@ tree.Ruleset.prototype = {
|
||||
rulesets.push(rule.toCSS(paths, env));
|
||||
} else if (rule instanceof tree.Comment) {
|
||||
if (this.root) {
|
||||
rulesets.push(rule.toCSS());
|
||||
rulesets.push(rule.toCSS(env));
|
||||
} else {
|
||||
rules.push(rule.toCSS());
|
||||
rules.push(rule.toCSS(env));
|
||||
}
|
||||
} else {
|
||||
if (rule.toCSS && !rule.variable) {
|
||||
rules.push(rule.eval(env).toCSS());
|
||||
rules.push(rule.eval(env).toCSS(env));
|
||||
} else if (rule.value && !rule.variable) {
|
||||
rules.push(rule.value.toString());
|
||||
}
|
||||
@@ -141,15 +140,18 @@ tree.Ruleset.prototype = {
|
||||
// a selector, or {}.
|
||||
// Otherwise, only output if this ruleset has rules.
|
||||
if (this.root) {
|
||||
css.push(rules.join('\n'));
|
||||
css.push(rules.join(env.compress ? '' : '\n'));
|
||||
} else {
|
||||
if (rules.length > 0) {
|
||||
selector = paths.map(function (p) {
|
||||
return p.map(function (s) {
|
||||
return s.toCSS();
|
||||
return s.toCSS(env);
|
||||
}).join('').trim();
|
||||
}).join(paths.length > 3 ? ',\n' : ', ');
|
||||
css.push(selector, " {\n " + rules.join('\n ') + "\n}\n");
|
||||
}).join(env.compress ? ',' : (paths.length > 3 ? ',\n' : ', '));
|
||||
css.push(selector,
|
||||
(env.compress ? '{' : ' {\n ') +
|
||||
rules.join(env.compress ? '' : '\n ') +
|
||||
(env.compress ? '}' : '\n}\n'));
|
||||
}
|
||||
}
|
||||
css.push(rulesets);
|
||||
@@ -157,7 +159,7 @@ tree.Ruleset.prototype = {
|
||||
// Pop the stack
|
||||
env.frames.shift();
|
||||
|
||||
return css.join('');
|
||||
return css.join('') + (env.compress ? '\n' : '');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -13,14 +13,14 @@ tree.Selector.prototype.match = function (other) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
tree.Selector.prototype.toCSS = function () {
|
||||
tree.Selector.prototype.toCSS = function (env) {
|
||||
if (this._css) { return this._css }
|
||||
|
||||
return this._css = this.elements.map(function (e) {
|
||||
if (typeof(e) === 'string') {
|
||||
return ' ' + e.trim();
|
||||
} else {
|
||||
return e.toCSS();
|
||||
return e.toCSS(env);
|
||||
}
|
||||
}).join('');
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user