Skip to content

Commit 314b846

Browse files
committed
Merge branch 'main' into fix-wf-paste
2 parents 27c870a + da7e9cd commit 314b846

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

packages/css-data/src/parse-css.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe("Parse CSS", () => {
55
test("longhand property name with keyword value", () => {
66
expect(parseCss(`.test { background-color: red }`)).toEqual([
77
{
8-
selector: "test",
8+
selector: ".test",
99
property: "backgroundColor",
1010
value: { type: "keyword", value: "red" },
1111
},
@@ -15,7 +15,7 @@ describe("Parse CSS", () => {
1515
test("one class selector rules", () => {
1616
expect(parseCss(`.test { color: #ff0000 }`)).toEqual([
1717
{
18-
selector: "test",
18+
selector: ".test",
1919
property: "color",
2020
value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },
2121
},
@@ -30,7 +30,7 @@ describe("Parse CSS", () => {
3030
`;
3131
expect(parseCss(css)).toEqual([
3232
{
33-
selector: "test",
33+
selector: ".test",
3434
property: "backgroundImage",
3535
value: {
3636
type: "layers",
@@ -45,7 +45,7 @@ describe("Parse CSS", () => {
4545
},
4646
},
4747
{
48-
selector: "test",
48+
selector: ".test",
4949
property: "backgroundPositionX",
5050
value: {
5151
type: "layers",
@@ -56,7 +56,7 @@ describe("Parse CSS", () => {
5656
},
5757
},
5858
{
59-
selector: "test",
59+
selector: ".test",
6060
property: "backgroundPositionY",
6161
value: {
6262
type: "layers",
@@ -67,7 +67,7 @@ describe("Parse CSS", () => {
6767
},
6868
},
6969
{
70-
selector: "test",
70+
selector: ".test",
7171
property: "backgroundSize",
7272
value: {
7373
type: "layers",
@@ -90,7 +90,7 @@ describe("Parse CSS", () => {
9090
},
9191
},
9292
{
93-
selector: "test",
93+
selector: ".test",
9494
property: "backgroundRepeat",
9595
value: {
9696
type: "layers",
@@ -101,7 +101,7 @@ describe("Parse CSS", () => {
101101
},
102102
},
103103
{
104-
selector: "test",
104+
selector: ".test",
105105
property: "backgroundAttachment",
106106
value: {
107107
type: "layers",
@@ -112,7 +112,7 @@ describe("Parse CSS", () => {
112112
},
113113
},
114114
{
115-
selector: "test",
115+
selector: ".test",
116116
property: "backgroundOrigin",
117117
value: {
118118
type: "layers",
@@ -123,7 +123,7 @@ describe("Parse CSS", () => {
123123
},
124124
},
125125
{
126-
selector: "test",
126+
selector: ".test",
127127
property: "backgroundClip",
128128
value: {
129129
type: "layers",
@@ -134,7 +134,7 @@ describe("Parse CSS", () => {
134134
},
135135
},
136136
{
137-
selector: "test",
137+
selector: ".test",
138138
property: "backgroundColor",
139139
value: { alpha: 1, b: 252, g: 255, r: 235, type: "rgb" },
140140
},
@@ -149,31 +149,31 @@ describe("Parse CSS", () => {
149149
`;
150150
expect(parseCss(css)).toEqual([
151151
{
152-
selector: "test",
152+
selector: ".test",
153153
property: "backgroundImage",
154154
value: {
155155
type: "layers",
156156
value: [{ type: "keyword", value: "none" }],
157157
},
158158
},
159159
{
160-
selector: "test",
160+
selector: ".test",
161161
property: "backgroundPositionX",
162162
value: {
163163
type: "layers",
164164
value: [{ type: "unit", unit: "px", value: 0 }],
165165
},
166166
},
167167
{
168-
selector: "test",
168+
selector: ".test",
169169
property: "backgroundPositionY",
170170
value: {
171171
type: "layers",
172172
value: [{ type: "unit", unit: "px", value: 0 }],
173173
},
174174
},
175175
{
176-
selector: "test",
176+
selector: ".test",
177177
property: "backgroundSize",
178178
value: {
179179
type: "layers",
@@ -494,7 +494,7 @@ describe("Parse CSS", () => {
494494
test("parse space combinator", () => {
495495
expect(parseCss(`.a b { color: #ff0000 }`)).toEqual([
496496
{
497-
selector: "a b",
497+
selector: ".a b",
498498
property: "color",
499499
value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },
500500
},

packages/css-data/src/parse-css.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -180,37 +180,35 @@ export const parseCss = (css: string, options: ParserOptions = {}) => {
180180
continue;
181181
}
182182
let selector: Selector | undefined = undefined;
183-
node.children.forEach((node) => {
183+
for (const childNode of node.children) {
184184
let name: string = "";
185185
let state: string | undefined;
186-
switch (node.type) {
186+
switch (childNode.type) {
187187
case "TypeSelector":
188-
name = node.name;
188+
name = childNode.name;
189189
break;
190190
case "ClassSelector":
191-
// .a {} vs a.b {}
192-
name = selector ? `.${node.name}` : node.name;
191+
name = `.${childNode.name}`;
193192
break;
194193
case "AttributeSelector":
195-
if (node.value) {
196-
name = `[${csstree.generate(node.name)}${node.matcher}${csstree.generate(node.value)}]`;
197-
}
194+
name = csstree.generate(childNode);
198195
break;
199196
case "PseudoClassSelector": {
200197
// First pseudo selector is not a state but an element selector, e.g. :root
201198
if (selector) {
202-
state = `:${node.name}`;
199+
state = `:${childNode.name}`;
203200
} else {
204-
name = `:${node.name}`;
201+
name = `:${childNode.name}`;
205202
}
206203
break;
207204
}
205+
case "PseudoElementSelector":
206+
state = `::${childNode.name}`;
207+
break;
208208
case "Combinator":
209209
// " " vs " > "
210-
name = node.name === " " ? node.name : ` ${node.name} `;
211-
break;
212-
case "PseudoElementSelector":
213-
state = `::${node.name}`;
210+
name =
211+
childNode.name === " " ? childNode.name : ` ${childNode.name} `;
214212
break;
215213
}
216214

@@ -219,10 +217,10 @@ export const parseCss = (css: string, options: ParserOptions = {}) => {
219217
if (state) {
220218
selector.state = state;
221219
}
222-
return;
220+
} else {
221+
selector = { name, state };
223222
}
224-
selector = { name, state };
225-
});
223+
}
226224
if (selector) {
227225
selectors.push(selector);
228226
selector = undefined;

0 commit comments

Comments
 (0)