Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental: support nested and attribute selectors #4213

Merged
merged 6 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 50 additions & 17 deletions packages/css-data/src/parse-css.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe("Parse CSS", () => {
test("longhand property name with keyword value", () => {
expect(parseCss(`.test { background-color: red }`)).toEqual([
{
selector: "test",
selector: ".test",
property: "backgroundColor",
value: { type: "keyword", value: "red" },
},
Expand All @@ -15,7 +15,7 @@ describe("Parse CSS", () => {
test("one class selector rules", () => {
expect(parseCss(`.test { color: #ff0000 }`)).toEqual([
{
selector: "test",
selector: ".test",
property: "color",
value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },
},
Expand All @@ -30,7 +30,7 @@ describe("Parse CSS", () => {
`;
expect(parseCss(css)).toEqual([
{
selector: "test",
selector: ".test",
property: "backgroundImage",
value: {
type: "layers",
Expand All @@ -45,7 +45,7 @@ describe("Parse CSS", () => {
},
},
{
selector: "test",
selector: ".test",
property: "backgroundPositionX",
value: {
type: "layers",
Expand All @@ -56,7 +56,7 @@ describe("Parse CSS", () => {
},
},
{
selector: "test",
selector: ".test",
property: "backgroundPositionY",
value: {
type: "layers",
Expand All @@ -67,7 +67,7 @@ describe("Parse CSS", () => {
},
},
{
selector: "test",
selector: ".test",
property: "backgroundSize",
value: {
type: "layers",
Expand All @@ -90,7 +90,7 @@ describe("Parse CSS", () => {
},
},
{
selector: "test",
selector: ".test",
property: "backgroundRepeat",
value: {
type: "layers",
Expand All @@ -101,7 +101,7 @@ describe("Parse CSS", () => {
},
},
{
selector: "test",
selector: ".test",
property: "backgroundAttachment",
value: {
type: "layers",
Expand All @@ -112,7 +112,7 @@ describe("Parse CSS", () => {
},
},
{
selector: "test",
selector: ".test",
property: "backgroundOrigin",
value: {
type: "layers",
Expand All @@ -123,7 +123,7 @@ describe("Parse CSS", () => {
},
},
{
selector: "test",
selector: ".test",
property: "backgroundClip",
value: {
type: "layers",
Expand All @@ -134,7 +134,7 @@ describe("Parse CSS", () => {
},
},
{
selector: "test",
selector: ".test",
property: "backgroundColor",
value: { alpha: 1, b: 252, g: 255, r: 235, type: "rgb" },
},
Expand All @@ -149,31 +149,31 @@ describe("Parse CSS", () => {
`;
expect(parseCss(css)).toEqual([
{
selector: "test",
selector: ".test",
property: "backgroundImage",
value: {
type: "layers",
value: [{ type: "keyword", value: "none" }],
},
},
{
selector: "test",
selector: ".test",
property: "backgroundPositionX",
value: {
type: "layers",
value: [{ type: "unit", unit: "px", value: 0 }],
},
},
{
selector: "test",
selector: ".test",
property: "backgroundPositionY",
value: {
type: "layers",
value: [{ type: "unit", unit: "px", value: 0 }],
},
},
{
selector: "test",
selector: ".test",
property: "backgroundSize",
value: {
type: "layers",
Expand All @@ -194,7 +194,18 @@ describe("Parse CSS", () => {
]);
});

test("attribute selector", () => {
expect(parseCss(`[class^="a"] { color: #ff0000 }`)).toEqual([
{
selector: '[class^="a"]',
property: "color",
value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },
},
]);
});

test("parse first pseudo class as selector", () => {
// E.g. :root
expect(parseCss(`:first-pseudo:my-state { color: #ff0000 }`)).toEqual([
{
selector: ":first-pseudo",
Expand Down Expand Up @@ -471,11 +482,33 @@ describe("Parse CSS", () => {
});

test("parse child combinator", () => {
expect(parseCss(`a > b { color: #ff0000 }`)).toEqual([]);
expect(parseCss(`a > b { color: #ff0000 }`)).toEqual([
{
selector: "a > b",
property: "color",
value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },
},
]);
});

test("parse space combinator", () => {
expect(parseCss(`a b { color: #ff0000 }`)).toEqual([]);
expect(parseCss(`.a b { color: #ff0000 }`)).toEqual([
{
selector: ".a b",
property: "color",
value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },
},
]);
});

test("parse nested selectors as one token", () => {
expect(parseCss(`a b c.d { color: #ff0000 }`)).toEqual([
{
selector: "a b c.d",
property: "color",
value: { alpha: 1, b: 0, g: 0, r: 255, type: "rgb" },
},
]);
});
});

Expand Down
79 changes: 49 additions & 30 deletions packages/css-data/src/parse-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,51 +161,70 @@ export const parseCss = (css: string, options: ParserOptions = {}) => {
if (node.type === "MediaQuery" && node.children.size > 1) {
invalidBreakpoint = true;
}
``;
},
});
const generated = csstree.generate(this.atrule.prelude);
if (generated) {
breakpoint = generated;
}
}
if (invalidBreakpoint) {
if (invalidBreakpoint || this.rule.prelude.type !== "SelectorList") {
return;
}

const selectors: Selector[] = [];
if (this.rule.prelude.type === "SelectorList") {
for (const selector of this.rule.prelude.children) {
if (selector.type !== "Selector" || selector.children.size > 2) {
continue;
}
const [nameNode, stateNode] = selector.children;
let name;
if (
nameNode.type === "ClassSelector" ||
nameNode.type === "TypeSelector"
) {
name = nameNode.name;
} else if (nameNode.type === "PseudoClassSelector") {
name = `:${nameNode.name}`;
} else {
continue;

for (const node of this.rule.prelude.children) {
if (node.type !== "Selector") {
continue;
}
let selector: Selector | undefined = undefined;
for (const childNode of node.children) {
let name: string = "";
let state: string | undefined;
switch (childNode.type) {
case "TypeSelector":
name = childNode.name;
break;
case "ClassSelector":
name = `.${childNode.name}`;
break;
case "AttributeSelector":
name = csstree.generate(childNode);
break;
case "PseudoClassSelector": {
// First pseudo selector is not a state but an element selector, e.g. :root
if (selector) {
state = `:${childNode.name}`;
} else {
name = `:${childNode.name}`;
}
break;
}
case "PseudoElementSelector":
state = `::${childNode.name}`;
break;
case "Combinator":
// " " vs " > "
name =
childNode.name === " " ? childNode.name : ` ${childNode.name} `;
break;
}
if (stateNode?.type === "PseudoClassSelector") {
selectors.push({
name,
state: `:${stateNode.name}`,
});
} else if (stateNode?.type === "PseudoElementSelector") {
selectors.push({
name,
state: `::${stateNode.name}`,
});

if (selector) {
selector.name += name;
if (state) {
selector.state = state;
}
} else {
selectors.push({
name,
});
selector = { name, state };
}
}
if (selector) {
selectors.push(selector);
selector = undefined;
}
}

const stringValue = csstree.generate(node.value);
Expand Down