source: trip-planner-front/node_modules/csso/lib/restructure/7-mergeRuleset.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1var walk = require('css-tree').walk;
2var utils = require('./utils');
3
4/*
5 At this step all rules has single simple selector. We try to join by equal
6 declaration blocks to first rule, e.g.
7
8 .a { color: red }
9 b { ... }
10 .b { color: red }
11 ->
12 .a, .b { color: red }
13 b { ... }
14*/
15
16function processRule(node, item, list) {
17 var selectors = node.prelude.children;
18 var declarations = node.block.children;
19 var nodeCompareMarker = selectors.first().compareMarker;
20 var skippedCompareMarkers = {};
21
22 list.nextUntil(item.next, function(next, nextItem) {
23 // skip non-ruleset node if safe
24 if (next.type !== 'Rule') {
25 return utils.unsafeToSkipNode.call(selectors, next);
26 }
27
28 if (node.pseudoSignature !== next.pseudoSignature) {
29 return true;
30 }
31
32 var nextFirstSelector = next.prelude.children.head;
33 var nextDeclarations = next.block.children;
34 var nextCompareMarker = nextFirstSelector.data.compareMarker;
35
36 // if next ruleset has same marked as one of skipped then stop joining
37 if (nextCompareMarker in skippedCompareMarkers) {
38 return true;
39 }
40
41 // try to join by selectors
42 if (selectors.head === selectors.tail) {
43 if (selectors.first().id === nextFirstSelector.data.id) {
44 declarations.appendList(nextDeclarations);
45 list.remove(nextItem);
46 return;
47 }
48 }
49
50 // try to join by properties
51 if (utils.isEqualDeclarations(declarations, nextDeclarations)) {
52 var nextStr = nextFirstSelector.data.id;
53
54 selectors.some(function(data, item) {
55 var curStr = data.id;
56
57 if (nextStr < curStr) {
58 selectors.insert(nextFirstSelector, item);
59 return true;
60 }
61
62 if (!item.next) {
63 selectors.insert(nextFirstSelector);
64 return true;
65 }
66 });
67
68 list.remove(nextItem);
69 return;
70 }
71
72 // go to next ruleset if current one can be skipped (has no equal specificity nor element selector)
73 if (nextCompareMarker === nodeCompareMarker) {
74 return true;
75 }
76
77 skippedCompareMarkers[nextCompareMarker] = true;
78 });
79}
80
81module.exports = function mergeRule(ast) {
82 walk(ast, {
83 visit: 'Rule',
84 enter: processRule
85 });
86};
Note: See TracBrowser for help on using the repository browser.