Skip to content

Commit

Permalink
fix(issue:4224) improve CSS custom property logic
Browse files Browse the repository at this point in the history
* Fixes issue less#4224 by improving CSS custom property handling.
  • Loading branch information
puckowski committed Dec 6, 2024
1 parent 829a635 commit a963f11
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/less/src/less/functions/math-helper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import Call from '../tree/call';
import CustomProperty from '../tree/custom-property';
import Dimension from '../tree/dimension';

const MathHelper = (fn, unit, n) => {
if (n instanceof Call && n.name === 'var') {
if (n.args && n.args.length === 1) {
return new Call(fn.name, [new CustomProperty(n.args[0].toCSS(), n._index, n._fileInfo)], n._index, n._fileInfo);
} else {
throw { type: 'Argument', message: 'var must contain one expression' };
}
}
if (!(n instanceof Dimension)) {
throw { type: 'Argument', message: 'argument must be a number' };
}
Expand Down
18 changes: 18 additions & 0 deletions packages/less/src/less/tree/custom-property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable no-prototype-builtins */
import Node from './node';

const CustomProperty = function (name, index, currentFileInfo) {
this.name = name;
this._index = index;
this._fileInfo = currentFileInfo;
};

CustomProperty.prototype = Object.assign(new Node(), {
type: 'CustomProperty',

genCSS: function (context, output) {
output.add('var(' + this.name + ')');
}
});

export default CustomProperty;
4 changes: 4 additions & 0 deletions packages/test-data/css/_main/custom-property.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.test {
--basic-deg: 20deg;
--basic-deg-tan: tan(var(--basic-deg));
}
4 changes: 4 additions & 0 deletions packages/test-data/less/_main/custom-property.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.test {
--basic-deg: 20deg;
--basic-deg-tan: tan(var(--basic-deg));
}

0 comments on commit a963f11

Please sign in to comment.