source: trip-planner-front/node_modules/csso/lib/restructure/1-mergeAtrule.js@ fa375fe

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

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1var List = require('css-tree').List;
2var resolveKeyword = require('css-tree').keyword;
3var hasOwnProperty = Object.prototype.hasOwnProperty;
4var walk = require('css-tree').walk;
5
6function addRuleToMap(map, item, list, single) {
7 var node = item.data;
8 var name = resolveKeyword(node.name).basename;
9 var id = node.name.toLowerCase() + '/' + (node.prelude ? node.prelude.id : null);
10
11 if (!hasOwnProperty.call(map, name)) {
12 map[name] = Object.create(null);
13 }
14
15 if (single) {
16 delete map[name][id];
17 }
18
19 if (!hasOwnProperty.call(map[name], id)) {
20 map[name][id] = new List();
21 }
22
23 map[name][id].append(list.remove(item));
24}
25
26function relocateAtrules(ast, options) {
27 var collected = Object.create(null);
28 var topInjectPoint = null;
29
30 ast.children.each(function(node, item, list) {
31 if (node.type === 'Atrule') {
32 var name = resolveKeyword(node.name).basename;
33
34 switch (name) {
35 case 'keyframes':
36 addRuleToMap(collected, item, list, true);
37 return;
38
39 case 'media':
40 if (options.forceMediaMerge) {
41 addRuleToMap(collected, item, list, false);
42 return;
43 }
44 break;
45 }
46
47 if (topInjectPoint === null &&
48 name !== 'charset' &&
49 name !== 'import') {
50 topInjectPoint = item;
51 }
52 } else {
53 if (topInjectPoint === null) {
54 topInjectPoint = item;
55 }
56 }
57 });
58
59 for (var atrule in collected) {
60 for (var id in collected[atrule]) {
61 ast.children.insertList(
62 collected[atrule][id],
63 atrule === 'media' ? null : topInjectPoint
64 );
65 }
66 }
67};
68
69function isMediaRule(node) {
70 return node.type === 'Atrule' && node.name === 'media';
71}
72
73function processAtrule(node, item, list) {
74 if (!isMediaRule(node)) {
75 return;
76 }
77
78 var prev = item.prev && item.prev.data;
79
80 if (!prev || !isMediaRule(prev)) {
81 return;
82 }
83
84 // merge @media with same query
85 if (node.prelude &&
86 prev.prelude &&
87 node.prelude.id === prev.prelude.id) {
88 prev.block.children.appendList(node.block.children);
89 list.remove(item);
90
91 // TODO: use it when we can refer to several points in source
92 // prev.loc = {
93 // primary: prev.loc,
94 // merged: node.loc
95 // };
96 }
97}
98
99module.exports = function rejoinAtrule(ast, options) {
100 relocateAtrules(ast, options);
101
102 walk(ast, {
103 visit: 'Atrule',
104 reverse: true,
105 enter: processAtrule
106 });
107};
Note: See TracBrowser for help on using the repository browser.