Skip to content

Commit 207c2a0

Browse files
committed
ref: Simplified comma-separated parsing
1 parent dbcd673 commit 207c2a0

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

packages/core/ui/styling/css-utils.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';
2-
import { tokenize } from '@csstools/css-tokenizer';
31
import { CoreTypes } from '../../core-types';
42
import { Trace } from '../../trace';
53
import { Length } from './style-properties';
@@ -20,9 +18,14 @@ export function cleanupImportantFlags(value: unknown, propertyName: string) {
2018
}
2119

2220
/**
23-
* Matches whitespace except if the whitespace is contained in parenthesis - ex. rgb(a), hsl color.
21+
* Matches whitespace except if the whitespace is contained in parenthesis - e.g. rgb(a), hsl color.
2422
*/
25-
const PARTS_RE = /\s(?![^(]*\))/;
23+
const WHITE_SPACE_RE = /\s(?![^(]*\))/;
24+
25+
/**
26+
* Matches comma except if the comma is contained in parenthesis - e.g. rgb(a, b, c).
27+
*/
28+
const COMMA_RE = /,(?![^(]*\))/;
2629

2730
/**
2831
* Matches a Length value with or without a unit
@@ -34,34 +37,22 @@ const LENGTH_RE = /^-?[0-9]+[a-zA-Z%]*?$/;
3437
*/
3538
const isLength = (v) => v === '0' || LENGTH_RE.test(v);
3639

37-
export function parseCSSListOfValues(value: string): string[] {
40+
export function parseCSSCommaSeparatedListOfValues(value: string): string[] {
3841
const values: string[] = [];
3942

4043
if (!value) {
41-
return values;
42-
}
43-
44-
if (!value.includes(',')) {
45-
values.push(value);
46-
return values;
47-
}
48-
49-
const tokens = tokenize({ css: value });
50-
const componentValueSet = parseCommaSeparatedListOfComponentValues(tokens);
51-
52-
for (const componentValues of componentValueSet) {
53-
values.push(componentValues.join(''));
44+
return [];
5445
}
5546

56-
return values;
47+
return value.split(COMMA_RE);
5748
}
5849

5950
export function parseCSSShorthand(value: string): {
6051
values: Array<CoreTypes.LengthType>;
6152
color: string;
6253
inset: boolean;
6354
} {
64-
const parts = value.trim().split(PARTS_RE);
55+
const parts = value.trim().split(WHITE_SPACE_RE);
6556
const first = parts[0];
6657

6758
if (['', 'none', 'unset'].includes(first)) {

packages/core/ui/styling/style-properties.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { LinearGradient } from './linear-gradient';
1414
import { parseCSSShadow, ShadowCSSValues } from './css-shadow';
1515
import { transformConverter } from './css-transform';
1616
import { ClipPathFunction } from './clip-path-function';
17-
import { parseCSSListOfValues } from './css-utils';
17+
import { parseCSSCommaSeparatedListOfValues } from './css-utils';
1818

1919
interface ShorthandPositioning {
2020
top: string;
@@ -1113,12 +1113,12 @@ const boxShadowProperty = new CssProperty<Style, ShadowCSSValues[]>({
11131113
);
11141114
},
11151115
valueConverter: (value) => {
1116-
const values = parseCSSListOfValues(value);
1116+
const values = parseCSSCommaSeparatedListOfValues(value);
11171117
const result: ShadowCSSValues[] = [];
11181118

11191119
// The first layer specified is drawn as if it is closest to the user
11201120
for (let i = values.length - 1; i >= 0; i--) {
1121-
const shadowVal = parseCSSShadow(values[i].trim());
1121+
const shadowVal = parseCSSShadow(values[i]);
11221122
if (shadowVal) {
11231123
result.push(shadowVal);
11241124
}

0 commit comments

Comments
 (0)