-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (42 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict'
var postcss = require('postcss')
var isBlank = require('is-blank')
var isRoot = require('is-css-root')
var getCssClasses = require('get-css-classes')
var stripPseudos = require('strip-pseudos')
module.exports = postcss.plugin('postcss-select', function (selectors) {
return function removePrefixes (root, result) {
selectors = selectors || []
if (isBlank(selectors)) {
result.warn('No selectors were provide to filter with')
}
root.walkRules(function (rule) {
var selected = rule.selectors.filter(function (selector) {
if (isRoot(selector)) {
return true
} else {
return hasSelector(selectors, selector)
}
})
if (selected.length) {
rule.selectors = selected
} else {
rule.remove()
}
})
}
})
function hasSelector(selectors, potentialMatch) {
return selectors.some(function (selector) {
return potentialMatch === selector ||
containsWithoutPseudos(selector, potentialMatch)
})
}
// If selecting for class .clearfix, .clearfix:after should
// also match
function containsWithoutPseudos(selector, potentialMatch) {
var classesInPotentialMatch = getCssClasses(potentialMatch).map(stripPseudos)
return classesInPotentialMatch.some(function (c) {
return selector === c
})
}