-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
69 lines (62 loc) · 1.97 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
'use strict';
const postcss = require('postcss');
/**
* update the selector with the matching .hf-* classes
*
* If extended == true, also add some polyfills. Please note, that the
* polyfills _may_ be dangerous, because they might not have the scope
* you thought. Example:
*
* *:enabled {
* // match _input_ elements, that are not disabled
* }
*
* *:not([disabled]) {
* // match _any_ element, also div and body and iframe, without
* // a `disabled` attribute
* }
*
* If you use these polyfills, please always make sure that you scope
* your selectors appropriately yourself.
*/
function updateSelector(selector, extended) {
if (extended) {
selector = selector
.replace(/:enabled\b/, ':not([disabled])')
.replace(/:disabled\b/, '[disabled]')
.replace(/:read-only\b/, '[readonly]')
.replace(/:read-write\b/, ':not([readonly])')
.replace(/:required\b/, '[required]')
.replace(/:optional\b/, ':not([required])')
;
}
return (selector
.replace(/:user-(invalid|error)\b/g, '.hf-user-invalid')
.replace(/:user-valid\b/g, '.hf-user-valid')
.replace(/:in-range\b/g, '.hf-in-range')
.replace(/:out-of-range\b/g, '.hf-out-of-range')
.replace(/:invalid\b/g, '.hf-invalid')
.replace(/:valid\b/g, '.hf-valid')
);
}
module.exports = postcss.plugin('postcss-hyperform', (options = {
/* extended polyfill is disabled by default due to above scoping issue */
extended: false,
}) => {
return root => {
root.walkRules(rule => {
rule.walkDecls(decl => {
var userErrorPseudos = [];
rule.selectors.forEach(function (selector) {
const new_selector = updateSelector(selector, !! options.extended);
if (selector !== new_selector) {
userErrorPseudos.push(new_selector);
}
});
if (userErrorPseudos.length) {
rule.selectors = rule.selectors.concat(userErrorPseudos);
}
});
});
};
});