Skip to content

Commit a08c7ab

Browse files
authored
fix: allow relative color syntax for rgb() & rgba() (#80)
1 parent 92c3813 commit a08c7ab

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

data/patch.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,9 @@
584584
"comment": "missed, https://drafts.csswg.org/css-contain-3/#container-rule",
585585
"syntax": "not <query-in-parens> | <query-in-parens> [ [ and <query-in-parens> ]* | [ or <query-in-parens> ]* ]"
586586
},
587+
"component-ident": {
588+
"syntax": "r | g | b | alpha"
589+
},
587590
"coord-box": {
588591
"syntax": "content-box | padding-box | border-box | fill-box | stroke-box | view-box"
589592
},
@@ -770,6 +773,12 @@
770773
"color()": {
771774
"syntax": "color( <colorspace-params> [ / [ <alpha-value> | none ] ]? )"
772775
},
776+
"rgb()": {
777+
"syntax": "rgb( <number>#{3} , <alpha-value>? ) | rgb( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ) | rgb( from <color> [ <number> | <percentage> | <component-ident> | <calc()> ]{3} [ / [ <alpha-value> | <component-ident> | <calc()> ] ]? )"
778+
},
779+
"rgba()": {
780+
"syntax": "rgba( <number>#{3} , <alpha-value>? ) | rgba( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ) | rgba( from <color> [ <number> | <percentage> | <component-ident> | <calc()> ]{3} [ / [ <alpha-value> | <component-ident> | <calc()> ] ]? )"
781+
},
773782
"colorspace-params": {
774783
"syntax": "[ <predefined-rgb-params> | <xyz-params>]"
775784
},
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import assert from 'assert';
2+
import { lexer } from '../index.js';
3+
4+
describe('lexer relative colors', () => {
5+
describe('rgb() with relative colors', () => {
6+
it('should match rgb(25 25 25 / 50%)', () => {
7+
const result = lexer.matchProperty('color', 'rgb(25 25 25 / 50%)');
8+
9+
assert(result.matched, 'Should match relative color syntax');
10+
});
11+
12+
it('should match rgb(from hsl(0 100% 50%) r g b)', () => {
13+
const result = lexer.matchProperty('color', 'rgb(from hsl(0 100% 50%) r g b)');
14+
15+
assert(result.matched, 'Should match relative color syntax');
16+
});
17+
18+
it('should match rgb(from hsl(0 100% 50%) 132 132 224)', () => {
19+
const result = lexer.matchProperty('color', 'rgb(from hsl(0 100% 50%) 132 132 224)');
20+
21+
assert(result.matched, 'Should match relative color syntax');
22+
});
23+
24+
it('should match rgb(from #123456 calc(r + 40) calc(g + 40) b)', () => {
25+
const result = lexer.matchProperty('color', 'rgb(from #123456 calc(r + 40) calc(g + 40) b)');
26+
27+
assert(result.matched, 'Should match relative color with calc functions');
28+
});
29+
30+
it('should match rgb(from hwb(120deg 10% 20%) r g calc(b + 200))', () => {
31+
const result = lexer.matchProperty('color', 'rgb(from hwb(120deg 10% 20%) r g calc(b + 200))');
32+
33+
assert(result.matched, 'Should match relative color with hwb and calc');
34+
});
35+
36+
it('should match rgb(25 25 25 / 50%)', () => {
37+
const result = lexer.matchProperty('color', 'rgb(25 25 25 / 50%)');
38+
39+
assert(result.matched, 'Should match relative color syntax');
40+
});
41+
42+
it('should match rgb(from hsl(0 100% 50%) r 80 80)', () => {
43+
const result = lexer.matchProperty('color', 'rgb(from hsl(0 100% 50%) r 80 80)');
44+
45+
assert(result.matched, 'Should match relative color syntax');
46+
});
47+
48+
it('should match rgb(from hsl(0 100% 50% / 0.8) r g b / alpha)', () => {
49+
const result = lexer.matchProperty('color', 'rgb(from hsl(0 100% 50% / 0.8) r g b / alpha)');
50+
51+
assert(result.matched, 'Should match relative color syntax');
52+
});
53+
54+
it('should match rgb(from hsl(0 100% 50% / 0.8) r g b / 0.5)', () => {
55+
const result = lexer.matchProperty('color', 'rgb(from hsl(0 100% 50% / 0.8) r g b / 0.5)');
56+
57+
assert(result.matched, 'Should match relative color syntax');
58+
});
59+
60+
it('should match rgb(from hsl(0 100% 50%) calc(r/2) calc(g + 25) calc(b + 175) / calc(alpha - 0.1))', () => {
61+
const result = lexer.matchProperty('color', 'rgb(from hsl(0 100% 50%) calc(r/2) calc(g + 25) calc(b + 175) / calc(alpha - 0.1))');
62+
63+
assert(result.matched, 'Should match relative color syntax');
64+
});
65+
});
66+
67+
describe('rgba() with relative colors', () => {
68+
it('should match rgba(25 25 25)', () => {
69+
const result = lexer.matchProperty('color', 'rgba(25 25 25)');
70+
71+
assert(result.matched, 'Should match relative color syntax');
72+
});
73+
74+
it('should match rgba(25 25 25 / 50%)', () => {
75+
const result = lexer.matchProperty('color', 'rgba(25 25 25 / 50%)');
76+
77+
assert(result.matched, 'Should match relative color syntax');
78+
});
79+
80+
it('should match rgba(from hsl(0 100% 50%) r g b)', () => {
81+
const result = lexer.matchProperty('color', 'rgba(from hsl(0 100% 50%) r g b)');
82+
83+
assert(result.matched, 'Should match relative color syntax');
84+
});
85+
86+
it('should match rgba(from hsl(0 100% 50%) 132 132 224)', () => {
87+
const result = lexer.matchProperty('color', 'rgba(from hsl(0 100% 50%) 132 132 224)');
88+
89+
assert(result.matched, 'Should match relative color syntax');
90+
});
91+
92+
it('should match rgba(from #123456 calc(r + 40) calc(g + 40) b)', () => {
93+
const result = lexer.matchProperty('color', 'rgba(from #123456 calc(r + 40) calc(g + 40) b)');
94+
95+
assert(result.matched, 'Should match relative color with calc functions');
96+
});
97+
98+
it('should match rgba(from hwb(120deg 10% 20%) r g calc(b + 200))', () => {
99+
const result = lexer.matchProperty('color', 'rgba(from hwb(120deg 10% 20%) r g calc(b + 200))');
100+
101+
assert(result.matched, 'Should match relative color with hwb and calc');
102+
});
103+
104+
it('should match rgba(25 25 25 / 50%)', () => {
105+
const result = lexer.matchProperty('color', 'rgba(25 25 25 / 50%)');
106+
107+
assert(result.matched, 'Should match relative color syntax');
108+
});
109+
110+
it('should match rgba(from hsl(0 100% 50%) r 80 80)', () => {
111+
const result = lexer.matchProperty('color', 'rgba(from hsl(0 100% 50%) r 80 80)');
112+
113+
assert(result.matched, 'Should match relative color syntax');
114+
});
115+
116+
it('should match rgba(from hsl(0 100% 50% / 0.8) r g b / alpha)', () => {
117+
const result = lexer.matchProperty('color', 'rgba(from hsl(0 100% 50% / 0.8) r g b / alpha)');
118+
119+
assert(result.matched, 'Should match relative color syntax');
120+
});
121+
122+
it('should match rgba(from hsl(0 100% 50% / 0.8) r g b / 0.5)', () => {
123+
const result = lexer.matchProperty('color', 'rgba(from hsl(0 100% 50% / 0.8) r g b / 0.5)');
124+
125+
assert(result.matched, 'Should match relative color syntax');
126+
});
127+
128+
it('should match rgba(from hsl(0 100% 50%) calc(r/2) calc(g + 25) calc(b + 175) / calc(alpha - 0.1))', () => {
129+
const result = lexer.matchProperty('color', 'rgba(from hsl(0 100% 50%) calc(r/2) calc(g + 25) calc(b + 175) / calc(alpha - 0.1))');
130+
131+
assert(result.matched, 'Should match relative color syntax');
132+
});
133+
});
134+
});

0 commit comments

Comments
 (0)