Skip to content

Commit 1275026

Browse files
committed
fix: properly check CSS length units
* Use regex for all CSS values Regression from #159 causes all rule values containing `vi` to be validated against `viewport-unit-variants`. Fixes #164
1 parent 2ad9e2b commit 1275026

File tree

6 files changed

+74
-53
lines changed

6 files changed

+74
-53
lines changed

data/features/ch-unit.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
/**
2-
* Unit representing the width of the character "0" in the current font, of particular use in combination with monospace fonts.
3-
*
4-
* See: https://caniuse.com/ch-unit
5-
*/
1+
import { checkCSSLengthUnits } from '../../utils/util.js';
62

73
/**
4+
* Unit representing the width of the character "0" in the current font, of particular use in combination with monospace fonts.
5+
* @see https://caniuse.com/ch-unit
86
* @type {import('../features').Feature}
97
*/
10-
export default {
11-
'': 'ch',
12-
};
8+
export default checkCSSLengthUnits('ch');
+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
export default {
2-
'': [
3-
'cqw',
4-
'cqh',
5-
'cqi',
6-
'cqb',
7-
'cqmin',
8-
'cqmax',
9-
],
10-
};
1+
import { checkCSSLengthUnits } from '../../utils/util.js';
2+
3+
export default checkCSSLengthUnits(
4+
'cqw',
5+
'cqh',
6+
'cqi',
7+
'cqb',
8+
'cqmin',
9+
'cqmax',
10+
);

data/features/rem.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export default {
2-
'': 'rem',
3-
};
1+
import { checkCSSLengthUnits } from '../../utils/util.js';
2+
3+
export default checkCSSLengthUnits('rem');
+35-32
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
1+
import { checkCSSLengthUnits } from '../../utils/util.js';
2+
13
/**
24
* Small, Large, and Dynamic viewport units
3-
* Viewport units similar to `vw` and `vh` that are based on shown or hidden browser UI states to address shortcomings of the original units. Currently defined as the `sv*` units (`svb`, `svh`, `svi`, `svmax`, `svmin`, `svw`), `lv*` units (`lvb`, `lvh`, `lvi`, `lvmax`, `lvmin`, `lvw`), `dv*` units (`dvb`, `dvh`, `dvi`, `dvmax`, `dvmin`, `dvw`) and the logical `vi`/`vb` units.
5+
* Viewport units similar to `vw` and `vh` that are based on shown or hidden
6+
* browser UI states to address shortcomings of the original units. Currently
7+
* defined as the
8+
* `sv*` units (`svb`, `svh`, `svi`, `svmax`, `svmin`, `svw`),
9+
* `lv*` units (`lvb`, `lvh`, `lvi`, `lvmax`, `lvmin`, `lvw`),
10+
* `dv*` units (`dvb`, `dvh`, `dvi`, `dvmax`, `dvmin`, `dvw`)
11+
* and the logical `vi`/`vb` units.
412
* @see https://caniuse.com/viewport-unit-variants
5-
*/
6-
7-
/**
813
* @type {import('../features').Feature}
914
*/
10-
export default {
11-
'': [
12-
/* sv* units */
13-
'svb',
14-
'svh',
15-
'svi',
16-
'svmax',
17-
'svmin',
18-
'svw',
15+
export default checkCSSLengthUnits(
16+
/* sv* units */
17+
'svb',
18+
'svh',
19+
'svi',
20+
'svmax',
21+
'svmin',
22+
'svw',
1923

20-
/* lv* units */
21-
'lvb',
22-
'lvh',
23-
'lvi',
24-
'lvmax',
25-
'lvmin',
26-
'lvw',
24+
/* lv* units */
25+
'lvb',
26+
'lvh',
27+
'lvi',
28+
'lvmax',
29+
'lvmin',
30+
'lvw',
2731

28-
/* dv* units */
29-
'dvb',
30-
'dvh',
31-
'dvi',
32-
'dvmax',
33-
'dvmin',
34-
'dvw',
32+
/* dv* units */
33+
'dvb',
34+
'dvh',
35+
'dvi',
36+
'dvmax',
37+
'dvmin',
38+
'dvw',
3539

36-
/* vi/vb units */
37-
'vi',
38-
'vb',
39-
],
40-
};
40+
/* vi/vb units */
41+
'vi',
42+
'vb',
43+
);

test/cases/generic/issue164.css

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
expect:
3+
viewport-unit-variants: 0
4+
*/
5+
6+
.test {
7+
visibility: visible;
8+
}

utils/util.js

+14
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,17 @@ export function checkAtRule(name, params) {
6464
// @ts-ignore rule.params can be `undefined`
6565
&& (!params || performFeatureCheck(params, rule.params));
6666
}
67+
68+
/**
69+
* @see https://drafts.csswg.org/css-values/#lengths
70+
* @see https://drafts.csswg.org/css-values/#number-value
71+
* @param {string[]} units
72+
* @return {(rule:import('postcss').ChildNode) => boolean}
73+
*/
74+
export function checkCSSLengthUnits(...units) {
75+
const regexp = new RegExp(`(\\+-)?[\\d.]*\\.?\\d+(e(\\+-)?\\d+)?(${units.join('|')})`, 'i');
76+
return (rule) => (
77+
(rule.type === 'decl') ? performFeatureCheck(regexp, rule.value)
78+
: ((rule.type === 'atrule') ? performFeatureCheck(regexp, rule.params)
79+
: false));
80+
}

0 commit comments

Comments
 (0)