Comparing version
@@ -172,2 +172,4 @@ function TRBL(name) { | ||
'compressFontWeight': 1, | ||
'compressFont': 1, | ||
'compressBackground': 1, | ||
'cleanEmpty': 1 | ||
@@ -220,2 +222,4 @@ }; | ||
'compressFontWeight', | ||
'compressFont', | ||
'compressBackground', | ||
'destroyDelims', | ||
@@ -260,2 +264,8 @@ 'preTranslate', | ||
}, | ||
'compressFont': { | ||
'declaration': 1 | ||
}, | ||
'compressBackground': { | ||
'declaration': 1 | ||
}, | ||
'cleanComment': { | ||
@@ -320,3 +330,4 @@ 'comment': 1 | ||
var r = rules[rule], | ||
x1 = token, x2; | ||
x1 = token, x2, | ||
o = this.order, k; | ||
for (var k = 0; k < r.length; k++) { | ||
@@ -331,3 +342,3 @@ x2 = this[r[k]](x1, rule, container, i, path); | ||
CSSOCompressor.prototype.compress = function(tree) { | ||
CSSOCompressor.prototype.compress = function(tree, ro) { | ||
this.init(); | ||
@@ -345,18 +356,20 @@ this.info = typeof tree[0] !== 'string'; | ||
ls = translator.translate(cleanInfo(x)).length; | ||
xs = this.copyArray(x); | ||
this.disjoin(x); | ||
x = this.walk(this.msrules, x, '/0'); | ||
x = this.walk(this.csrules, x, '/0'); | ||
x = this.walk(this.rbrules, x, '/0'); | ||
do { | ||
l0 = l1; | ||
x0 = this.copyArray(x); | ||
x = this.walk(this.rjrules, x, '/0'); | ||
x = this.walk(this.rrrules, x, '/0'); | ||
l1 = translator.translate(cleanInfo(x)).length; | ||
x1 = this.copyArray(x); | ||
} while (l0 > l1); | ||
if (ls < l0 && ls < l1) x = xs; | ||
else if (l0 < l1) x = x0; | ||
if (!ro) { // restructure ON | ||
xs = this.copyArray(x); | ||
this.disjoin(x); | ||
x = this.walk(this.msrules, x, '/0'); | ||
x = this.walk(this.csrules, x, '/0'); | ||
x = this.walk(this.rbrules, x, '/0'); | ||
do { | ||
l0 = l1; | ||
x0 = this.copyArray(x); | ||
x = this.walk(this.rjrules, x, '/0'); | ||
x = this.walk(this.rrrules, x, '/0'); | ||
l1 = translator.translate(cleanInfo(x)).length; | ||
x1 = this.copyArray(x); | ||
} while (l0 > l1); | ||
if (ls < l0 && ls < l1) x = xs; | ||
else if (l0 < l1) x = x0; | ||
} | ||
@@ -649,2 +662,53 @@ x = this.walk(this.frules, x, '/0'); | ||
CSSOCompressor.prototype.compressFont = function(token) { | ||
var p = token[2], | ||
v = token[3], | ||
i, x, t; | ||
if (/font$/.test(p[2][2]) && v.length) { | ||
v.splice(2, 0, [{}, 's', '']); | ||
for (i = v.length - 1; i > 2; i--) { | ||
x = v[i]; | ||
if (x[1] === 'ident') { | ||
x = x[2]; | ||
if (x === 'bold') v[i] = [{}, 'number', '700']; | ||
else if (x === 'normal') { | ||
t = v[i - 1]; | ||
if (t[1] === 'operator' && t[2] === '/') v.splice(--i, 2); | ||
else v.splice(i, 1); | ||
if (v[i - 1][1] === 's') v.splice(--i, 1); | ||
} | ||
else if (x === 'medium' && v[i + 1] && v[i + 1][2] !== '/') { | ||
v.splice(i, 1); | ||
if (v[i - 1][1] === 's') v.splice(--i, 1); | ||
} | ||
} | ||
} | ||
if (v.length > 2 && v[2][1] === 's') v.splice(2, 1); | ||
if (v.length === 2) v.push([{}, 'ident', 'normal']); | ||
return token; | ||
} | ||
}; | ||
CSSOCompressor.prototype.compressBackground = function(token) { | ||
var p = token[2], | ||
v = token[3], | ||
i, x, t; | ||
if (/background$/.test(p[2][2]) && v.length) { | ||
v.splice(2, 0, [{}, 's', '']); | ||
for (i = v.length - 1; i > 2; i--) { | ||
x = v[i]; | ||
if (x[1] === 'ident') { | ||
x = x[2]; | ||
if (x === 'transparent' || x === 'none' || x === 'repeat' || x === 'scroll') { | ||
v.splice(i, 1); | ||
if (v[i - 1][1] === 's') v.splice(--i, 1); | ||
} | ||
} | ||
} | ||
if (v.length > 2 && v[2][1] === 's') v.splice(2, 1); | ||
if (v.length === 2) v.splice(2, 0, [{}, 'number', '0'], [{}, 's', ' '], [{}, 'number', '0']); | ||
return token; | ||
} | ||
}; | ||
CSSOCompressor.prototype.cleanEmpty = function(token, rule) { | ||
@@ -1041,4 +1105,4 @@ switch(rule) { | ||
exports.compress = function(tree) { | ||
return new CSSOCompressor().compress(tree); | ||
exports.compress = function(tree, ro) { | ||
return new CSSOCompressor().compress(tree, ro); | ||
}; |
@@ -10,3 +10,4 @@ var fs = require('fs'), | ||
'-h', '--help', | ||
'-dp', '--parser' | ||
'-dp', '--parser', | ||
'-off', '--restructure-off' | ||
], [ | ||
@@ -20,2 +21,3 @@ '-r', '--rule', | ||
other = opts && opts.other, | ||
ro = single && single.contains(['-off', '--restructure-off']), | ||
inFile = (pairs && (pairs['-i'] || pairs['--input'])) || (other && other[0]), | ||
@@ -34,4 +36,4 @@ outFile = (pairs && (pairs['-o'] || pairs['--output'])) || (other && other[1]), | ||
else { | ||
if (!outFile) print(csso.justDoIt(src)); | ||
else fs.writeFileSync(outFile, csso.justDoIt(src)); | ||
if (!outFile) print(csso.justDoIt(src, ro)); | ||
else fs.writeFileSync(outFile, csso.justDoIt(src, ro)); | ||
} | ||
@@ -38,0 +40,0 @@ } |
@@ -18,4 +18,4 @@ var util = require('./util.js'), | ||
exports.justDoIt = function(src) { | ||
return translate(cleanInfo(compress(parse(src, 'stylesheet')))); | ||
exports.justDoIt = function(src, ro) { | ||
return translate(cleanInfo(compress(parse(src, 'stylesheet'), ro))); | ||
}; |
{ | ||
"name": "csso", | ||
"description": "CSSO — CSS optimizer", | ||
"version": "1.2.7", | ||
"version": "1.2.8", | ||
"homepage": "http://github.com/afelix/csso", | ||
@@ -6,0 +6,0 @@ "author": "Sergey Kryzhanovsky <skryzhanovsky@ya.ru> (http://github.com/afelix)", |
@@ -66,2 +66,3 @@ # 1. Introduction | ||
.test{color:#fff} | ||
Use `csso.justDoIt(css, true)` to turn structure minimization off. | ||
@@ -84,2 +85,5 @@ ## 3.3. From the command line | ||
minimizes the CSS in <in_filename> and outputs the result to <out_filename> | ||
csso -off | ||
csso --restructure-off | ||
turns structure minimization off | ||
csso -h | ||
@@ -126,8 +130,4 @@ csso --help | ||
# 5. Note to developers | ||
# 5. Authors | ||
CSSO is written in easy-to-understand JavaScript. It's easily portable to other languages, such as Python, Java, PHP, Perl, C++, C, etc. The source code is licensed under [MIT](https://github.com/afelix/csso/blob/master/MIT-LICENSE.txt). | ||
# 6. Authors | ||
* initial idea — Vitaly Harisov (<vitaly@harisov.name>) | ||
@@ -137,4 +137,4 @@ * implementation — Sergey Kryzhanovsky (<skryzhanovsky@ya.ru>) | ||
# 7. And finally | ||
# 6. And finally | ||
* CSSO is licensed under [MIT](https://github.com/afelix/csso/blob/master/MIT-LICENSE.txt) |
@@ -66,2 +66,3 @@ # 1. Описание | ||
.test{color:#fff} | ||
Используйте `csso.justDoIt(css, true)`, если требуется выключить структурную минимизацию. | ||
@@ -84,2 +85,5 @@ ## 3.3. Через командную строку | ||
минимизирует CSS из <in_имя_файла> и записывает результат в <out_имя_файла> | ||
csso -off | ||
csso --restructure-off | ||
turns structure minimization off | ||
csso -h | ||
@@ -126,8 +130,4 @@ csso --help | ||
# 5. Разработчикам | ||
# 5. Авторы | ||
Исходный код CSSO написан на очень простом JavaScript. Это позволяет легко портировать CSSO на распространённые языки типа Python, Java, PHP, Perl, C++, C и т.п. Лицензия MIT позволяет вам использовать CSSO так, как вам угодно, но на всякий случай прочтите текст [лицензии](https://github.com/afelix/csso/blob/master/MIT-LICENSE.txt). | ||
# 6. Авторы | ||
* идея и поддержка — Виталий Харисов (<vitaly@harisov.name>) | ||
@@ -137,4 +137,4 @@ * реализация — Сергей Крыжановский (<skryzhanovsky@ya.ru>) | ||
# 7. Остальное | ||
# 6. Остальное | ||
* CSSO распространяется под [лицензией MIT](https://github.com/afelix/csso/blob/master/MIT-LICENSE.txt) |
var translator = require('./translator.js').translator(), | ||
cleanInfo = require('./util.js').cleanInfo; | ||
exports.compress = function(tree) { | ||
return new CSSOCompressor().compress(tree); | ||
exports.compress = function(tree, ro) { | ||
return new CSSOCompressor().compress(tree, ro); | ||
}; |
@@ -56,2 +56,4 @@ function CSSOCompressor() {} | ||
'compressFontWeight': 1, | ||
'compressFont': 1, | ||
'compressBackground': 1, | ||
'cleanEmpty': 1 | ||
@@ -104,2 +106,4 @@ }; | ||
'compressFontWeight', | ||
'compressFont', | ||
'compressBackground', | ||
'destroyDelims', | ||
@@ -144,2 +148,8 @@ 'preTranslate', | ||
}, | ||
'compressFont': { | ||
'declaration': 1 | ||
}, | ||
'compressBackground': { | ||
'declaration': 1 | ||
}, | ||
'cleanComment': { | ||
@@ -204,3 +214,4 @@ 'comment': 1 | ||
var r = rules[rule], | ||
x1 = token, x2; | ||
x1 = token, x2, | ||
o = this.order, k; | ||
for (var k = 0; k < r.length; k++) { | ||
@@ -215,3 +226,3 @@ x2 = this[r[k]](x1, rule, container, i, path); | ||
CSSOCompressor.prototype.compress = function(tree) { | ||
CSSOCompressor.prototype.compress = function(tree, ro) { | ||
this.init(); | ||
@@ -229,18 +240,20 @@ this.info = typeof tree[0] !== 'string'; | ||
ls = translator.translate(cleanInfo(x)).length; | ||
xs = this.copyArray(x); | ||
this.disjoin(x); | ||
x = this.walk(this.msrules, x, '/0'); | ||
x = this.walk(this.csrules, x, '/0'); | ||
x = this.walk(this.rbrules, x, '/0'); | ||
do { | ||
l0 = l1; | ||
x0 = this.copyArray(x); | ||
x = this.walk(this.rjrules, x, '/0'); | ||
x = this.walk(this.rrrules, x, '/0'); | ||
l1 = translator.translate(cleanInfo(x)).length; | ||
x1 = this.copyArray(x); | ||
} while (l0 > l1); | ||
if (ls < l0 && ls < l1) x = xs; | ||
else if (l0 < l1) x = x0; | ||
if (!ro) { // restructure ON | ||
xs = this.copyArray(x); | ||
this.disjoin(x); | ||
x = this.walk(this.msrules, x, '/0'); | ||
x = this.walk(this.csrules, x, '/0'); | ||
x = this.walk(this.rbrules, x, '/0'); | ||
do { | ||
l0 = l1; | ||
x0 = this.copyArray(x); | ||
x = this.walk(this.rjrules, x, '/0'); | ||
x = this.walk(this.rrrules, x, '/0'); | ||
l1 = translator.translate(cleanInfo(x)).length; | ||
x1 = this.copyArray(x); | ||
} while (l0 > l1); | ||
if (ls < l0 && ls < l1) x = xs; | ||
else if (l0 < l1) x = x0; | ||
} | ||
@@ -533,2 +546,53 @@ x = this.walk(this.frules, x, '/0'); | ||
CSSOCompressor.prototype.compressFont = function(token) { | ||
var p = token[2], | ||
v = token[3], | ||
i, x, t; | ||
if (/font$/.test(p[2][2]) && v.length) { | ||
v.splice(2, 0, [{}, 's', '']); | ||
for (i = v.length - 1; i > 2; i--) { | ||
x = v[i]; | ||
if (x[1] === 'ident') { | ||
x = x[2]; | ||
if (x === 'bold') v[i] = [{}, 'number', '700']; | ||
else if (x === 'normal') { | ||
t = v[i - 1]; | ||
if (t[1] === 'operator' && t[2] === '/') v.splice(--i, 2); | ||
else v.splice(i, 1); | ||
if (v[i - 1][1] === 's') v.splice(--i, 1); | ||
} | ||
else if (x === 'medium' && v[i + 1] && v[i + 1][2] !== '/') { | ||
v.splice(i, 1); | ||
if (v[i - 1][1] === 's') v.splice(--i, 1); | ||
} | ||
} | ||
} | ||
if (v.length > 2 && v[2][1] === 's') v.splice(2, 1); | ||
if (v.length === 2) v.push([{}, 'ident', 'normal']); | ||
return token; | ||
} | ||
}; | ||
CSSOCompressor.prototype.compressBackground = function(token) { | ||
var p = token[2], | ||
v = token[3], | ||
i, x, t; | ||
if (/background$/.test(p[2][2]) && v.length) { | ||
v.splice(2, 0, [{}, 's', '']); | ||
for (i = v.length - 1; i > 2; i--) { | ||
x = v[i]; | ||
if (x[1] === 'ident') { | ||
x = x[2]; | ||
if (x === 'transparent' || x === 'none' || x === 'repeat' || x === 'scroll') { | ||
v.splice(i, 1); | ||
if (v[i - 1][1] === 's') v.splice(--i, 1); | ||
} | ||
} | ||
} | ||
if (v.length > 2 && v[2][1] === 's') v.splice(2, 1); | ||
if (v.length === 2) v.splice(2, 0, [{}, 'number', '0'], [{}, 's', ' '], [{}, 'number', '0']); | ||
return token; | ||
} | ||
}; | ||
CSSOCompressor.prototype.cleanEmpty = function(token, rule) { | ||
@@ -535,0 +599,0 @@ switch(rule) { |
@@ -1084,2 +1084,4 @@ var $util = {}; | ||
'compressFontWeight': 1, | ||
'compressFont': 1, | ||
'compressBackground': 1, | ||
'cleanEmpty': 1 | ||
@@ -1132,2 +1134,4 @@ }; | ||
'compressFontWeight', | ||
'compressFont', | ||
'compressBackground', | ||
'destroyDelims', | ||
@@ -1172,2 +1176,8 @@ 'preTranslate', | ||
}, | ||
'compressFont': { | ||
'declaration': 1 | ||
}, | ||
'compressBackground': { | ||
'declaration': 1 | ||
}, | ||
'cleanComment': { | ||
@@ -1232,3 +1242,4 @@ 'comment': 1 | ||
var r = rules[rule], | ||
x1 = token, x2; | ||
x1 = token, x2, | ||
o = this.order, k; | ||
for (var k = 0; k < r.length; k++) { | ||
@@ -1243,3 +1254,3 @@ x2 = this[r[k]](x1, rule, container, i, path); | ||
CSSOCompressor.prototype.compress = function(tree) { | ||
CSSOCompressor.prototype.compress = function(tree, ro) { | ||
this.init(); | ||
@@ -1257,18 +1268,20 @@ this.info = typeof tree[0] !== 'string'; | ||
ls = translator.translate(cleanInfo(x)).length; | ||
xs = this.copyArray(x); | ||
this.disjoin(x); | ||
x = this.walk(this.msrules, x, '/0'); | ||
x = this.walk(this.csrules, x, '/0'); | ||
x = this.walk(this.rbrules, x, '/0'); | ||
do { | ||
l0 = l1; | ||
x0 = this.copyArray(x); | ||
x = this.walk(this.rjrules, x, '/0'); | ||
x = this.walk(this.rrrules, x, '/0'); | ||
l1 = translator.translate(cleanInfo(x)).length; | ||
x1 = this.copyArray(x); | ||
} while (l0 > l1); | ||
if (ls < l0 && ls < l1) x = xs; | ||
else if (l0 < l1) x = x0; | ||
if (!ro) { // restructure ON | ||
xs = this.copyArray(x); | ||
this.disjoin(x); | ||
x = this.walk(this.msrules, x, '/0'); | ||
x = this.walk(this.csrules, x, '/0'); | ||
x = this.walk(this.rbrules, x, '/0'); | ||
do { | ||
l0 = l1; | ||
x0 = this.copyArray(x); | ||
x = this.walk(this.rjrules, x, '/0'); | ||
x = this.walk(this.rrrules, x, '/0'); | ||
l1 = translator.translate(cleanInfo(x)).length; | ||
x1 = this.copyArray(x); | ||
} while (l0 > l1); | ||
if (ls < l0 && ls < l1) x = xs; | ||
else if (l0 < l1) x = x0; | ||
} | ||
@@ -1561,2 +1574,53 @@ x = this.walk(this.frules, x, '/0'); | ||
CSSOCompressor.prototype.compressFont = function(token) { | ||
var p = token[2], | ||
v = token[3], | ||
i, x, t; | ||
if (/font$/.test(p[2][2]) && v.length) { | ||
v.splice(2, 0, [{}, 's', '']); | ||
for (i = v.length - 1; i > 2; i--) { | ||
x = v[i]; | ||
if (x[1] === 'ident') { | ||
x = x[2]; | ||
if (x === 'bold') v[i] = [{}, 'number', '700']; | ||
else if (x === 'normal') { | ||
t = v[i - 1]; | ||
if (t[1] === 'operator' && t[2] === '/') v.splice(--i, 2); | ||
else v.splice(i, 1); | ||
if (v[i - 1][1] === 's') v.splice(--i, 1); | ||
} | ||
else if (x === 'medium' && v[i + 1] && v[i + 1][2] !== '/') { | ||
v.splice(i, 1); | ||
if (v[i - 1][1] === 's') v.splice(--i, 1); | ||
} | ||
} | ||
} | ||
if (v.length > 2 && v[2][1] === 's') v.splice(2, 1); | ||
if (v.length === 2) v.push([{}, 'ident', 'normal']); | ||
return token; | ||
} | ||
}; | ||
CSSOCompressor.prototype.compressBackground = function(token) { | ||
var p = token[2], | ||
v = token[3], | ||
i, x, t; | ||
if (/background$/.test(p[2][2]) && v.length) { | ||
v.splice(2, 0, [{}, 's', '']); | ||
for (i = v.length - 1; i > 2; i--) { | ||
x = v[i]; | ||
if (x[1] === 'ident') { | ||
x = x[2]; | ||
if (x === 'transparent' || x === 'none' || x === 'repeat' || x === 'scroll') { | ||
v.splice(i, 1); | ||
if (v[i - 1][1] === 's') v.splice(--i, 1); | ||
} | ||
} | ||
} | ||
if (v.length > 2 && v[2][1] === 's') v.splice(2, 1); | ||
if (v.length === 2) v.splice(2, 0, [{}, 'number', '0'], [{}, 's', ' '], [{}, 'number', '0']); | ||
return token; | ||
} | ||
}; | ||
CSSOCompressor.prototype.cleanEmpty = function(token, rule) { | ||
@@ -1563,0 +1627,0 @@ switch(rule) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
298492
2.46%1165
0.43%6289
3.44%