This repository has been archived by the owner on Jan 17, 2018. It is now read-only.
forked from johno/rework-class-prefix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
71 lines (59 loc) · 1.61 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
/**
* Convert regular expression string to RegExp.
* @param {RegExp|string} str Input string to convert
* @return {RegExp} Converted RegExp or the original string.
*/
function strToRegExp(str) {
if(str instanceof RegExp) {
return str;
}
var parts = str.match(/^\/(.*?)\/([gim]*)$/);
if (parts) {
return new RegExp(parts[1], parts[2]);
}
else {
return new RegExp(str);
}
}
function isClassSelector(selector) {
return selector.indexOf('.') === 0;
}
function notTester(matcher) {
// If we have no matcher (i.e. options.not is undefined),
// all classes will be accepted.
if(!matcher) {
return function testNotBlank(klass) { return false; };
}
matcher = strToRegExp(matcher);
return function testNotForClass(klass) {
return matcher.exec(klass);
};
}
module.exports = function classPrefix(prefix, options) {
options = options || {};
function isPrefixed(klass) {
return (klass.indexOf(prefix) === 0);
}
var isIgnoredClass = notTester(options.not);
return function classPrefix(styling) {
var walk = require('rework-walk');
walk(styling, function(rule, node) {
if (!rule.selectors){
return rule;
}
rule.selectors = rule.selectors.map(function(selector) {
if (!isClassSelector(selector)) {
return selector;
}
var classes = selector.split('.');
return classes.map(function(klass){
if(klass.trim().length === 0 || isIgnoredClass(klass) || isPrefixed(klass)) {
return klass;
}
return prefix + klass;
}).join('.');
});
});
};
};