Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix the sort order when minWidth is undefined

Oleg Isonendf72608b2d2e
diff --git a/packages/css-engine/src/core/css-engine.test.ts b/packages/css-engine/src/core/css-engine.test.ts
index 035548b124..ac38fbb164 100644
--- a/packages/css-engine/src/core/css-engine.test.ts
+++ b/packages/css-engine/src/core/css-engine.test.ts
@@ -17,9 +17,11 @@ const mediaId1 = "1";
describe("CssEngine", () => {
let engine: CssEngine;
- beforeEach(() => {
+ const reset = () => {
engine = new CssEngine();
- });
+ };
+
+ beforeEach(reset);
test("use default media rule when there is no matching one registered", () => {
engine.addStyleRule(".c", {
@@ -49,17 +51,17 @@ describe("CssEngine", () => {
});
// Default media query should allways be the first to have the lowest source order specificity
expect(engine.cssText).toMatchInlineSnapshot(`
- "@media all and (min-width: 0px) {
- .c1 { color: blue }
- }
- @media all {
+ "@media all {
.c { display: block }
.c1 { color: red }
+ }
+ @media all and (min-width: 0px) {
+ .c1 { color: blue }
}"
`);
});
- test("sort media queries based on min-width", () => {
+ test("sort media queries based on lower min-width", () => {
engine.addMediaRule(mediaId1, mediaRuleOptions1);
engine.addStyleRule(".c2", {
style: style1,
@@ -91,6 +93,40 @@ describe("CssEngine", () => {
`);
});
+ test("keep the sort order when minWidth is not defined", () => {
+ engine.addStyleRule(".c0", {
+ style: style0,
+ breakpoint: "x",
+ });
+ engine.addStyleRule(".c1", {
+ style: style1,
+ breakpoint: "x",
+ });
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media all {
+ .c0 { display: block }
+ .c1 { display: flex }
+ }"
+ `);
+
+ reset();
+
+ engine.addStyleRule(".c1", {
+ style: style1,
+ breakpoint: "x",
+ });
+ engine.addStyleRule(".c0", {
+ style: style0,
+ breakpoint: "x",
+ });
+ expect(engine.cssText).toMatchInlineSnapshot(`
+ "@media all {
+ .c1 { display: flex }
+ .c0 { display: block }
+ }"
+ `);
+ });
+
test("rule with multiple properties", () => {
engine.addMediaRule(mediaId0, mediaRuleOptions0);
engine.addStyleRule(".c", {
diff --git a/packages/css-engine/src/core/rules.ts b/packages/css-engine/src/core/rules.ts
index af78749769..57945cfcf2 100644
--- a/packages/css-engine/src/core/rules.ts
+++ b/packages/css-engine/src/core/rules.ts
@@ -67,14 +67,11 @@ export class MediaRule {
// Needed to ensure that more specific media rules are inserted after less specific ones.
// So that they get a higher specificity.
static sort(mediaRules: Iterable<MediaRule>) {
- return Array.from(mediaRules).sort((a, b) => {
- if (
- a.options.minWidth === undefined ||
- b.options.minWidth === undefined
- ) {
- return -1;
- }
- return a.options.minWidth - b.options.minWidth;
+ return Array.from(mediaRules).sort((ruleA, ruleB) => {
+ return (
+ (ruleA.options.minWidth ?? -Infinity) -
+ (ruleB.options.minWidth ?? -Infinity)
+ );
});
}
options: MediaRuleOptions;