Skip to content

Commit f56ee78

Browse files
Copilotmatthew-dean
andcommitted
fix: extend colorOperand to cover all relative-color channel letters (a, w)
Co-authored-by: matthew-dean <414752+matthew-dean@users.noreply.github.com> Agent-Logs-Url: https://github.com/less/less.js/sessions/4d1ac45c-4305-43e1-962f-991415b969e0
1 parent c42e7af commit f56ee78

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

packages/less/lib/less/parser/parser.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,11 +2282,13 @@ const Parser = function Parser(context, imports, fileInfo, currentIndex) {
22822282
colorOperand: function () {
22832283
parserInput.save();
22842284

2285-
// hsl or rgb or lch operand
2286-
const match = parserInput.$re(/^[lchrgbs]\s+/);
2285+
// Single-letter channel keywords used in relative color syntax:
2286+
// r, g, b (rgb), h, s, l (hsl), h, w, b (hwb),
2287+
// l, a, b (lab/oklab), l, c, h (lch/oklch)
2288+
const match = parserInput.$re(/^([abcghlrsw])(?=\s|[,/*)]|$)/);
22872289
if (match) {
22882290
parserInput.forget();
2289-
return new tree.Keyword(match[0]);
2291+
return new tree.Keyword(match[1]);
22902292
}
22912293

22922294
parserInput.restore();

packages/test-data/tests-unit/color-functions/modern.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,21 @@
3434
.color-rgb-div {
3535
background: rgb(from #0000FF calc(r / 2) g b);
3636
}
37+
.color-rgb-calc-sub-right {
38+
background: rgb(from blue calc(100 - r) g b);
39+
}
40+
.color-rgb-calc-add-left {
41+
background: rgb(from blue calc(r + 100) g b);
42+
}
43+
.color-lab-calc-a-add {
44+
background: lab(from blue l calc(a + 5) b);
45+
}
46+
.color-lab-calc-a-sub {
47+
background: lab(from blue l calc(5 - a) b);
48+
}
49+
.color-hwb-calc-w-add {
50+
background: hwb(from blue h calc(w + 10) b);
51+
}
52+
.color-hwb-calc-w-sub {
53+
background: hwb(from blue h calc(10 - w) b);
54+
}

packages/test-data/tests-unit/color-functions/modern.less

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,30 @@
4646
.color-rgb-div {
4747
background: rgb(from #0000FF calc(r / 2) g b);
4848
}
49+
50+
// Regression tests for issue #4316: calc() with channel as right-hand operand
51+
.color-rgb-calc-sub-right {
52+
background: rgb(from blue calc(100 - r) g b);
53+
}
54+
55+
.color-rgb-calc-add-left {
56+
background: rgb(from blue calc(r + 100) g b);
57+
}
58+
59+
// lab/oklab 'a' channel (was missing from character class)
60+
.color-lab-calc-a-add {
61+
background: lab(from blue l calc(a + 5) b);
62+
}
63+
64+
.color-lab-calc-a-sub {
65+
background: lab(from blue l calc(5 - a) b);
66+
}
67+
68+
// hwb 'w' channel (was missing from character class)
69+
.color-hwb-calc-w-add {
70+
background: hwb(from blue h calc(w + 10) b);
71+
}
72+
73+
.color-hwb-calc-w-sub {
74+
background: hwb(from blue h calc(10 - w) b);
75+
}

0 commit comments

Comments
 (0)