[6a3a178] | 1 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
---|
| 2 | var walk = require('css-tree').walk;
|
---|
| 3 | var { hasNoChildren } = require('./utils');
|
---|
| 4 |
|
---|
| 5 | function cleanUnused(selectorList, usageData) {
|
---|
| 6 | selectorList.children.each(function(selector, item, list) {
|
---|
| 7 | var shouldRemove = false;
|
---|
| 8 |
|
---|
| 9 | walk(selector, function(node) {
|
---|
| 10 | // ignore nodes in nested selectors
|
---|
| 11 | if (this.selector === null || this.selector === selectorList) {
|
---|
| 12 | switch (node.type) {
|
---|
| 13 | case 'SelectorList':
|
---|
| 14 | // TODO: remove toLowerCase when pseudo selectors will be normalized
|
---|
| 15 | // ignore selectors inside :not()
|
---|
| 16 | if (this.function === null || this.function.name.toLowerCase() !== 'not') {
|
---|
| 17 | if (cleanUnused(node, usageData)) {
|
---|
| 18 | shouldRemove = true;
|
---|
| 19 | }
|
---|
| 20 | }
|
---|
| 21 | break;
|
---|
| 22 |
|
---|
| 23 | case 'ClassSelector':
|
---|
| 24 | if (usageData.whitelist !== null &&
|
---|
| 25 | usageData.whitelist.classes !== null &&
|
---|
| 26 | !hasOwnProperty.call(usageData.whitelist.classes, node.name)) {
|
---|
| 27 | shouldRemove = true;
|
---|
| 28 | }
|
---|
| 29 | if (usageData.blacklist !== null &&
|
---|
| 30 | usageData.blacklist.classes !== null &&
|
---|
| 31 | hasOwnProperty.call(usageData.blacklist.classes, node.name)) {
|
---|
| 32 | shouldRemove = true;
|
---|
| 33 | }
|
---|
| 34 | break;
|
---|
| 35 |
|
---|
| 36 | case 'IdSelector':
|
---|
| 37 | if (usageData.whitelist !== null &&
|
---|
| 38 | usageData.whitelist.ids !== null &&
|
---|
| 39 | !hasOwnProperty.call(usageData.whitelist.ids, node.name)) {
|
---|
| 40 | shouldRemove = true;
|
---|
| 41 | }
|
---|
| 42 | if (usageData.blacklist !== null &&
|
---|
| 43 | usageData.blacklist.ids !== null &&
|
---|
| 44 | hasOwnProperty.call(usageData.blacklist.ids, node.name)) {
|
---|
| 45 | shouldRemove = true;
|
---|
| 46 | }
|
---|
| 47 | break;
|
---|
| 48 |
|
---|
| 49 | case 'TypeSelector':
|
---|
| 50 | // TODO: remove toLowerCase when type selectors will be normalized
|
---|
| 51 | // ignore universal selectors
|
---|
| 52 | if (node.name.charAt(node.name.length - 1) !== '*') {
|
---|
| 53 | if (usageData.whitelist !== null &&
|
---|
| 54 | usageData.whitelist.tags !== null &&
|
---|
| 55 | !hasOwnProperty.call(usageData.whitelist.tags, node.name.toLowerCase())) {
|
---|
| 56 | shouldRemove = true;
|
---|
| 57 | }
|
---|
| 58 | if (usageData.blacklist !== null &&
|
---|
| 59 | usageData.blacklist.tags !== null &&
|
---|
| 60 | hasOwnProperty.call(usageData.blacklist.tags, node.name.toLowerCase())) {
|
---|
| 61 | shouldRemove = true;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | break;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | });
|
---|
| 68 |
|
---|
| 69 | if (shouldRemove) {
|
---|
| 70 | list.remove(item);
|
---|
| 71 | }
|
---|
| 72 | });
|
---|
| 73 |
|
---|
| 74 | return selectorList.children.isEmpty();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | module.exports = function cleanRule(node, item, list, options) {
|
---|
| 78 | if (hasNoChildren(node.prelude) || hasNoChildren(node.block)) {
|
---|
| 79 | list.remove(item);
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | var usageData = options.usage;
|
---|
| 84 |
|
---|
| 85 | if (usageData && (usageData.whitelist !== null || usageData.blacklist !== null)) {
|
---|
| 86 | cleanUnused(node.prelude, usageData);
|
---|
| 87 |
|
---|
| 88 | if (hasNoChildren(node.prelude)) {
|
---|
| 89 | list.remove(item);
|
---|
| 90 | return;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | };
|
---|