index.js
2.3 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import postcss from 'postcss';
import parser from 'postcss-selector-parser';
function nodeIsInsensitiveAttribute(node) {
return node.type === 'attribute' && node.insensitive;
}
function selectorHasInsensitiveAttribute(selector) {
return selector.some(nodeIsInsensitiveAttribute);
}
function transformString(strings, charPos, string) {
const char = string.charAt(charPos);
if (char === '') {
return strings;
}
let newStrings = strings.map(x => x + char);
const upperChar = char.toLocaleUpperCase();
if (upperChar !== char) {
newStrings = newStrings.concat(strings.map(x => x + upperChar));
}
return transformString(newStrings, charPos + 1, string);
}
function createSensitiveAtributes(attribute) {
const attributes = transformString([''], 0, attribute.value);
return attributes.map(x => {
const newAttribute = attribute.clone({
spaces: {
after: attribute.spaces.after,
before: attribute.spaces.before
},
insensitive: false
});
newAttribute.setValue(x);
return newAttribute;
});
}
function createNewSelectors(selector) {
let newSelectors = [parser.selector()];
selector.walk(node => {
if (!nodeIsInsensitiveAttribute(node)) {
newSelectors.forEach(newSelector => {
newSelector.append(node.clone());
});
return;
}
const sensitiveAttributes = createSensitiveAtributes(node);
const newSelectorsWithSensitiveAttributes = [];
sensitiveAttributes.forEach(newNode => {
newSelectors.forEach(newSelector => {
const newSelectorWithNewNode = newSelector.clone();
newSelectorWithNewNode.append(newNode);
newSelectorsWithSensitiveAttributes.push(newSelectorWithNewNode);
});
});
newSelectors = newSelectorsWithSensitiveAttributes;
});
return newSelectors;
}
function transform(selectors) {
let newSelectors = [];
selectors.each(selector => {
if (selectorHasInsensitiveAttribute(selector)) {
newSelectors = newSelectors.concat(createNewSelectors(selector));
selector.remove();
}
});
if (newSelectors.length) {
newSelectors.forEach(newSelector => selectors.append(newSelector));
}
}
const caseInsensitiveRegExp = /i(\s*\/\*[\W\w]*?\*\/)*\s*\]/;
export default postcss.plugin('postcss-attribute-case-insensitive', () => css => {
css.walkRules(caseInsensitiveRegExp, rule => {
rule.selector = parser(transform).processSync(rule.selector);
});
});