[6a3a178] | 1 | var List = require('css-tree').List;
|
---|
| 2 | var resolveKeyword = require('css-tree').keyword;
|
---|
| 3 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
---|
| 4 | var walk = require('css-tree').walk;
|
---|
| 5 |
|
---|
| 6 | function 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 |
|
---|
| 26 | function 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 |
|
---|
| 69 | function isMediaRule(node) {
|
---|
| 70 | return node.type === 'Atrule' && node.name === 'media';
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | function 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 |
|
---|
| 99 | module.exports = function rejoinAtrule(ast, options) {
|
---|
| 100 | relocateAtrules(ast, options);
|
---|
| 101 |
|
---|
| 102 | walk(ast, {
|
---|
| 103 | visit: 'Atrule',
|
---|
| 104 | reverse: true,
|
---|
| 105 | enter: processAtrule
|
---|
| 106 | });
|
---|
| 107 | };
|
---|