/
counter.js
85 lines (69 loc) · 2.62 KB
/
counter.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import valueParser, {walk} from "postcss-value-parser";
import addToCache from "./cache";
import isNum from "./isNum";
const RESERVED_KEYWORDS = [
"unset", "initial", "inherit", "none",
];
export default function () {
let cache = {};
let declOneCache = [];
let declTwoCache = [];
return {
collect (node, encoder) {
const {prop, type} = node;
if (type !== 'decl') {
return;
}
if (/counter-(reset|increment)/i.test(prop)) {
node.value = valueParser(node.value).walk(child => {
if (
child.type === 'word' &&
!isNum(child) &&
RESERVED_KEYWORDS.indexOf(child.value.toLowerCase()) === -1
) {
addToCache(child.value, encoder, cache);
child.value = cache[child.value].ident;
}
});
declOneCache.push(node);
} else if (/content/i.test(prop)) {
declTwoCache.push(node);
}
},
transform () {
declTwoCache.forEach(decl => {
decl.value = valueParser(decl.value).walk(node => {
const {type} = node;
const value = node.value.toLowerCase();
if (type === 'function' && (value === 'counter' || value === 'counters')) {
walk(node.nodes, child => {
if (child.type === 'word' && child.value in cache) {
cache[child.value].count++;
child.value = cache[child.value].ident;
}
});
}
if (type === 'space') {
node.value = ' ';
}
return false;
}).toString();
});
declOneCache.forEach(decl => {
decl.value = decl.value.walk(node => {
if (node.type === 'word' && !isNum(node)) {
Object.keys(cache).forEach(key => {
const cached = cache[key];
if (cached.ident === node.value && !cached.count) {
node.value = key;
}
});
}
}).toString();
});
// reset cache after transform
declOneCache = [];
declTwoCache = [];
},
};
}