var removeDuplicateMediaQueries = require('./remove-duplicate-media-queries');
var mergeMediaQueries = require('./merge-media-queries');
-function AdvancedOptimizer(options, context) {
- this.options = options;
- this.validator = context.validator;
-}
-
-AdvancedOptimizer.prototype.removeEmpty = function (tokens) {
+function removeEmpty(tokens) {
for (var i = 0, l = tokens.length; i < l; i++) {
var token = tokens[i];
var isEmpty = false;
isEmpty = token[1].length === 0 || token[2].length === 0;
break;
case 'block':
- this.removeEmpty(token[2]);
+ removeEmpty(token[2]);
isEmpty = token[2].length === 0;
}
l--;
}
}
-};
+}
+
+function recursivelyOptimizeBlocks(tokens, options, validator) {
+ for (var i = 0, l = tokens.length; i < l; i++) {
+ var token = tokens[i];
+
+ if (token[0] == 'block') {
+ var isKeyframes = /@(-moz-|-o-|-webkit-)?keyframes/.test(token[1][0]);
+ optimize(token[2], options, validator, !isKeyframes);
+ }
+ }
+}
function recursivelyOptimizeProperties(tokens, options, validator) {
for (var i = 0, l = tokens.length; i < l; i++) {
}
}
-AdvancedOptimizer.prototype.optimize = function (tokens) {
- var self = this;
-
- function _optimize(tokens, withRestructuring) {
- tokens.forEach(function (token) {
- if (token[0] == 'block') {
- var isKeyframes = /@(-moz-|-o-|-webkit-)?keyframes/.test(token[1][0]);
- _optimize(token[2], !isKeyframes);
- }
- });
-
- recursivelyOptimizeProperties(tokens, self.options, self.validator);
+function optimize(tokens, options, validator, withRestructuring) {
+ recursivelyOptimizeBlocks(tokens, options, validator);
+ recursivelyOptimizeProperties(tokens, options, validator);
- removeDuplicates(tokens);
- mergeAdjacent(tokens, self.options, self.validator);
- reduceNonAdjacent(tokens, self.options, self.validator);
+ removeDuplicates(tokens);
+ mergeAdjacent(tokens, options, validator);
+ reduceNonAdjacent(tokens, options, validator);
- mergeNonAdjacentBySelector(tokens, self.options, self.validator);
- mergeNonAdjacentByBody(tokens, self.options);
+ mergeNonAdjacentBySelector(tokens, options, validator);
+ mergeNonAdjacentByBody(tokens, options);
- if (self.options.restructuring && withRestructuring) {
- restructure(tokens, self.options);
- mergeAdjacent(tokens, self.options, self.validator);
- }
+ if (options.restructuring && withRestructuring) {
+ restructure(tokens, options);
+ mergeAdjacent(tokens, options, validator);
+ }
- if (self.options.mediaMerging) {
- removeDuplicateMediaQueries(tokens);
- var reduced = mergeMediaQueries(tokens);
- for (var i = reduced.length - 1; i >= 0; i--) {
- _optimize(reduced[i][2]);
- }
+ if (options.mediaMerging) {
+ removeDuplicateMediaQueries(tokens);
+ var reduced = mergeMediaQueries(tokens);
+ for (var i = reduced.length - 1; i >= 0; i--) {
+ optimize(reduced[i][2], options, validator, false);
}
-
- self.removeEmpty(tokens);
}
- _optimize(tokens, true);
-};
+ removeEmpty(tokens);
+}
-module.exports = AdvancedOptimizer;
+module.exports = optimize;
var optimizerContext = require('../test-helper').optimizerContext;
vows.describe('advanced optimizer')
- .addBatch(
- optimizerContext('@media', {
- 'empty': [
- '@media (min-width:980px){}',
- ''
- ],
- 'whitespace': [
- ' @media ( min-width: 980px ){}',
- ''
- ],
- 'body': [
- '@media (min-width:980px){\na\n{color:red}}',
- '@media (min-width:980px){a{color:red}}'
- ],
- 'multiple': [
- '@media screen, print, (min-width:980px){a{color:red}}',
- '@media screen,print,(min-width:980px){a{color:red}}'
- ],
- 'nested once': [
- '@media screen { @media print { a{color:red} } }',
- '@media screen{@media print{a{color:red}}}'
- ],
- 'nested twice': [
- '@media screen { @media print { @media (min-width:980px) { a{color:red} } } }',
- '@media screen{@media print{@media (min-width:980px){a{color:red}}}}'
- ]
- })
- )
.addBatch(
optimizerContext('advanced on & aggressive merging on', {
'repeated' : [
]
}, { advanced: false })
)
+ .addBatch(
+ optimizerContext('@media', {
+ 'empty': [
+ '@media (min-width:980px){}',
+ ''
+ ],
+ 'whitespace': [
+ ' @media ( min-width: 980px ){}',
+ ''
+ ],
+ 'body': [
+ '@media (min-width:980px){\na\n{color:red}}',
+ '@media (min-width:980px){a{color:red}}'
+ ],
+ 'multiple': [
+ '@media screen, print, (min-width:980px){a{color:red}}',
+ '@media screen,print,(min-width:980px){a{color:red}}'
+ ],
+ 'nested once': [
+ '@media screen { @media print { a{color:red} } }',
+ '@media screen{@media print{a{color:red}}}'
+ ],
+ 'nested twice': [
+ '@media screen { @media print { @media (min-width:980px) { a{color:red} } } }',
+ '@media screen{@media print{@media (min-width:980px){a{color:red}}}}'
+ ]
+ })
+ )
.addBatch(
optimizerContext('@font-face', {
'rebuilding': [