Skip to content

Commit

Permalink
fix: properly check CSS length units
Browse files Browse the repository at this point in the history
* Use regex for all CSS values

Regression from #159 causes all rule values containing `vi` to be validated against `viewport-unit-variants`.

Fixes #164
  • Loading branch information
clshortfuse committed Jul 7, 2023
1 parent 2ad9e2b commit 1275026
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 53 deletions.
12 changes: 4 additions & 8 deletions data/features/ch-unit.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/**
* Unit representing the width of the character "0" in the current font, of particular use in combination with monospace fonts.
*
* See: https://caniuse.com/ch-unit
*/
import { checkCSSLengthUnits } from '../../utils/util.js';

/**
* Unit representing the width of the character "0" in the current font, of particular use in combination with monospace fonts.
* @see https://caniuse.com/ch-unit
* @type {import('../features').Feature}
*/
export default {
'': 'ch',
};
export default checkCSSLengthUnits('ch');
20 changes: 10 additions & 10 deletions data/features/css-container-query-units.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export default {
'': [
'cqw',
'cqh',
'cqi',
'cqb',
'cqmin',
'cqmax',
],
};
import { checkCSSLengthUnits } from '../../utils/util.js';

export default checkCSSLengthUnits(
'cqw',
'cqh',
'cqi',
'cqb',
'cqmin',
'cqmax',
);
6 changes: 3 additions & 3 deletions data/features/rem.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default {
'': 'rem',
};
import { checkCSSLengthUnits } from '../../utils/util.js';

export default checkCSSLengthUnits('rem');
67 changes: 35 additions & 32 deletions data/features/viewport-unit-variants.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
import { checkCSSLengthUnits } from '../../utils/util.js';

/**
* Small, Large, and Dynamic viewport units
* 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.
* 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.
* @see https://caniuse.com/viewport-unit-variants
*/

/**
* @type {import('../features').Feature}
*/
export default {
'': [
/* sv* units */
'svb',
'svh',
'svi',
'svmax',
'svmin',
'svw',
export default checkCSSLengthUnits(
/* sv* units */
'svb',
'svh',
'svi',
'svmax',
'svmin',
'svw',

/* lv* units */
'lvb',
'lvh',
'lvi',
'lvmax',
'lvmin',
'lvw',
/* lv* units */
'lvb',
'lvh',
'lvi',
'lvmax',
'lvmin',
'lvw',

/* dv* units */
'dvb',
'dvh',
'dvi',
'dvmax',
'dvmin',
'dvw',
/* dv* units */
'dvb',
'dvh',
'dvi',
'dvmax',
'dvmin',
'dvw',

/* vi/vb units */
'vi',
'vb',
],
};
/* vi/vb units */
'vi',
'vb',
);
8 changes: 8 additions & 0 deletions test/cases/generic/issue164.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
expect:
viewport-unit-variants: 0
*/

.test {
visibility: visible;
}
14 changes: 14 additions & 0 deletions utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,17 @@ export function checkAtRule(name, params) {
// @ts-ignore rule.params can be `undefined`
&& (!params || performFeatureCheck(params, rule.params));
}

/**
* @see https://drafts.csswg.org/css-values/#lengths
* @see https://drafts.csswg.org/css-values/#number-value
* @param {string[]} units
* @return {(rule:import('postcss').ChildNode) => boolean}
*/
export function checkCSSLengthUnits(...units) {
const regexp = new RegExp(`(\\+-)?[\\d.]*\\.?\\d+(e(\\+-)?\\d+)?(${units.join('|')})`, 'i');
return (rule) => (
(rule.type === 'decl') ? performFeatureCheck(regexp, rule.value)
: ((rule.type === 'atrule') ? performFeatureCheck(regexp, rule.params)
: false));
}

0 comments on commit 1275026

Please sign in to comment.