Skip to content

Commit

Permalink
Merge pull request #1 from jacobwarduk/type-checking-length-units
Browse files Browse the repository at this point in the history
Type checking length units
  • Loading branch information
jacobwardio authored Feb 21, 2018
2 parents 3699921 + cb7f563 commit 8487426
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
18 changes: 18 additions & 0 deletions lib/less/functions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ functionRegistry.addMultiple({
isem: function (n) {
return isunit(n, 'em');
},
isrem: function (n) {
return isunit(n, 'rem');
},
isvw: function (n) {
return isunit(n, 'vw');
},
isvh: function (n) {
return isunit(n, 'vh');
},
isvmin: function (n) {
return isunit(n, 'vmin');
},
isvmax: function (n) {
return isunit(n, 'vmax');
},
isch: function (n) {
return isunit(n, 'ch');
},
isunit: isunit,
unit: function (val, unit) {
if (!(val instanceof Dimension)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/less/tree/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Unit.prototype.is = function (unitString) {
return this.toString().toUpperCase() === unitString.toUpperCase();
};
Unit.prototype.isLength = function () {
return Boolean(this.toCSS().match(/px|em|%|in|cm|mm|pc|pt|ex/));
return RegExp('^(px|em|rem|in|cm|mm|pc|pt|ex|vw|vh|vmin|vmax)$', 'g').test(this.toCSS());
};
Unit.prototype.isEmpty = function () {
return this.numerator.length === 0 && this.denominator.length === 0;
Expand Down
6 changes: 6 additions & 0 deletions test/css/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@
pixel: true;
percent: true;
em: true;
rem: true;
vw: true;
vh: true;
vmin: true;
vmax: true;
ch: true;
cat: true;
no-unit-is-empty: true;
case-insensitive-1: true;
Expand Down
12 changes: 9 additions & 3 deletions test/less/functions.less
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@

fade-out: fadeout(red, 5%); // support fadeOut and fadeout
fade-in: fadein(fadeout(red, 10%), 5%);
fade-out-relative: fadeout(red, 5%,relative);
fade-out-relative: fadeout(red, 5%,relative);
fade-in-relative: fadein(fadeout(red, 10%, relative), 5%, relative);
fade-out2: fadeout(fadeout(red, 50%), 50%);
fade-out2: fadeout(fadeout(red, 50%), 50%);
fade-out2-relative: fadeout(fadeout(red, 50%, relative), 50%, relative);

hsv: hsv(5, 50%, 30%);
hsva: hsva(3, 50%, 30%, 0.2);

Expand All @@ -163,6 +163,12 @@
pixel: ispixel(32px);
percent: ispercentage(32%);
em: isem(32em);
rem: isrem(32rem);
vw: isvw(32vw);
vh: isvh(32vh);
vmin: isvmin(32vmin);
vmax: isvmax(32vmax);
ch: isch(32ch);
cat: isunit(32cat, cat);
no-unit-is-empty: isunit(32, '');
case-insensitive-1: isunit(32CAT, cat);
Expand Down

0 comments on commit 8487426

Please sign in to comment.