-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
45 lines (40 loc) · 1.44 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
let postcss = require('postcss');
module.exports =
postcss.plugin(
'postcss-viewport-height-correction',
({ variable = 'vh' } = {}) => {
let finderRegex = /(-?[0-9.]+)vh/g;
// eslint-disable-next-line security/detect-non-literal-regexp
let excludeRegex = new RegExp(
`var\\(--${ variable },\\s*1vh\\)`
);
let replaceBy = `calc(var(--${ variable }, 1vh) * $1)`;
return function (root) {
/* Rules are the CSS Block */
root.walkRules(rule => {
/* Decls are the properties inside */
rule.walkDecls(decl => {
let value = decl.value;
let isImportant = decl.important;
// Checking that declaration has `vh` and is not corrected.
let isMatch = value.match(finderRegex) !== null;
let isPreParsed = value.match(excludeRegex) !== null;
if (isMatch && !isPreParsed) {
let correctedViewport = value.replace(finderRegex, replaceBy);
if (isImportant) {
correctedViewport += decl.raws.important || ' !important';
}
// Insert because we want to preserve
// the main property for fallback and its precedence
rule.insertAfter(
decl,
{
prop: decl.prop,
value: correctedViewport
}
);
}
})
})
}
});