[6a3a178] | 1 | var walk = require('css-tree').walk;
|
---|
| 2 | var utils = require('./utils');
|
---|
| 3 |
|
---|
| 4 | function processRule(node, item, list) {
|
---|
| 5 | var selectors = node.prelude.children;
|
---|
| 6 | var declarations = node.block.children;
|
---|
| 7 |
|
---|
| 8 | list.prevUntil(item.prev, function(prev) {
|
---|
| 9 | // skip non-ruleset node if safe
|
---|
| 10 | if (prev.type !== 'Rule') {
|
---|
| 11 | return utils.unsafeToSkipNode.call(selectors, prev);
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | var prevSelectors = prev.prelude.children;
|
---|
| 15 | var prevDeclarations = prev.block.children;
|
---|
| 16 |
|
---|
| 17 | // try to join rulesets with equal pseudo signature
|
---|
| 18 | if (node.pseudoSignature === prev.pseudoSignature) {
|
---|
| 19 | // try to join by selectors
|
---|
| 20 | if (utils.isEqualSelectors(prevSelectors, selectors)) {
|
---|
| 21 | prevDeclarations.appendList(declarations);
|
---|
| 22 | list.remove(item);
|
---|
| 23 | return true;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | // try to join by declarations
|
---|
| 27 | if (utils.isEqualDeclarations(declarations, prevDeclarations)) {
|
---|
| 28 | utils.addSelectors(prevSelectors, selectors);
|
---|
| 29 | list.remove(item);
|
---|
| 30 | return true;
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | // go to prev ruleset if has no selector similarities
|
---|
| 35 | return utils.hasSimilarSelectors(selectors, prevSelectors);
|
---|
| 36 | });
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | // NOTE: direction should be left to right, since rulesets merge to left
|
---|
| 40 | // ruleset. When direction right to left unmerged rulesets may prevent lookup
|
---|
| 41 | // TODO: remove initial merge
|
---|
| 42 | module.exports = function initialMergeRule(ast) {
|
---|
| 43 | walk(ast, {
|
---|
| 44 | visit: 'Rule',
|
---|
| 45 | enter: processRule
|
---|
| 46 | });
|
---|
| 47 | };
|
---|