From 9c7ef87c6e28e310d18812bd83be9305bf4ed87a Mon Sep 17 00:00:00 2001 From: Jean-Yves Moyen Date: Mon, 2 Dec 2024 11:54:09 +0100 Subject: [PATCH 1/5] Add breaking test --- packages/alfa-css-feature/test/media.spec.ts | 25 ++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/alfa-css-feature/test/media.spec.ts b/packages/alfa-css-feature/test/media.spec.ts index 5bf4257bef..5684dc05c5 100644 --- a/packages/alfa-css-feature/test/media.spec.ts +++ b/packages/alfa-css-feature/test/media.spec.ts @@ -1,7 +1,12 @@ import { test } from "@siteimprove/alfa-test"; import { Lexer } from "@siteimprove/alfa-css"; -import { Device, Viewport, Display } from "@siteimprove/alfa-device"; +import { + Device, + Display, + Preference, + Viewport, +} from "@siteimprove/alfa-device"; import { Feature } from "../dist/index.js"; @@ -460,7 +465,7 @@ test("#matches() matches ranges", (t) => { t.deepEqual(isGoldylocks.matches(largeLandscape), false); }); -test("#matches correctly behave at boundaries", (t) => { +test("#matches() correctly behave at boundaries", (t) => { // Inclusive bound is matched inclusively const isLarge = parse(`(width >= ${width}px)`).getUnsafe(); // Exclusive bound is matched exclusively @@ -472,3 +477,19 @@ test("#matches correctly behave at boundaries", (t) => { t.deepEqual(isSmall.matches(largeLandscape), false); t.deepEqual(isLargeToo.matches(largeLandscape), true); }); + +test("#matches() matches unset prefers-color-scheme", (t) => { + const prefersColorScheme = parse("(prefers-color-scheme)").getUnsafe(); + + t.deepEqual(prefersColorScheme.matches(smallPortrait), true); + + const withLightScheme = Device.of( + Device.Type.Screen, + Viewport.of(200, 400, Viewport.Orientation.Portrait), + Display.of(300), + undefined, + [Preference.of("prefers-color-scheme", "light")], + ); + + t.deepEqual(prefersColorScheme.matches(withLightScheme), false); +}); From 6b4b3d05db188b51623ed41e38000cedc75d7f7d Mon Sep 17 00:00:00 2001 From: Jean-Yves Moyen Date: Mon, 2 Dec 2024 13:50:14 +0100 Subject: [PATCH 2/5] Improve test --- packages/alfa-css-feature/test/media.spec.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/alfa-css-feature/test/media.spec.ts b/packages/alfa-css-feature/test/media.spec.ts index 5684dc05c5..ce9a60d654 100644 --- a/packages/alfa-css-feature/test/media.spec.ts +++ b/packages/alfa-css-feature/test/media.spec.ts @@ -1,3 +1,4 @@ +/// import { test } from "@siteimprove/alfa-test"; import { Lexer } from "@siteimprove/alfa-css"; @@ -478,18 +479,18 @@ test("#matches() correctly behave at boundaries", (t) => { t.deepEqual(isLargeToo.matches(largeLandscape), true); }); -test("#matches() matches unset prefers-color-scheme", (t) => { - const prefersColorScheme = parse("(prefers-color-scheme)").getUnsafe(); +test("#matches() matches boolean prefers-reduced-motion", (t) => { + const prefersReducedMotion = parse("(prefers-reduced-motion)").getUnsafe(); - t.deepEqual(prefersColorScheme.matches(smallPortrait), true); + t.deepEqual(prefersReducedMotion.matches(smallPortrait), false); - const withLightScheme = Device.of( + const withReducedMotion = Device.of( Device.Type.Screen, Viewport.of(200, 400, Viewport.Orientation.Portrait), Display.of(300), undefined, - [Preference.of("prefers-color-scheme", "light")], + [Preference.of("prefers-reduced-motion", "reduce")], ); - t.deepEqual(prefersColorScheme.matches(withLightScheme), false); + t.deepEqual(prefersReducedMotion.matches(withReducedMotion), true); }); From 9375f22f6d69c88d6ba8edbff8a22e10ac7fb82a Mon Sep 17 00:00:00 2001 From: Jean-Yves Moyen Date: Mon, 2 Dec 2024 13:51:39 +0100 Subject: [PATCH 3/5] Fix defaulting of unset preferences --- .changeset/pretty-scissors-explode.md | 5 +++++ packages/alfa-device/src/device.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 .changeset/pretty-scissors-explode.md diff --git a/.changeset/pretty-scissors-explode.md b/.changeset/pretty-scissors-explode.md new file mode 100644 index 0000000000..9ccb899ad8 --- /dev/null +++ b/.changeset/pretty-scissors-explode.md @@ -0,0 +1,5 @@ +--- +"@siteimprove/alfa-device": patch +--- + +**Fixed:** Values of undefined user preferences are now correctly set to their default. diff --git a/packages/alfa-device/src/device.ts b/packages/alfa-device/src/device.ts index e1a0c86d3a..daf690bcb0 100644 --- a/packages/alfa-device/src/device.ts +++ b/packages/alfa-device/src/device.ts @@ -84,7 +84,9 @@ export class Device implements Equatable, Hashable, Serializable { public preference(name: N): Preference { return this._preferences .get(name) - .getOrElse(() => Preference.unset(name)) as Preference; + .getOrElse(() => + Preference.of(name, Preference.unset(name)), + ) as Preference; } public equals(value: unknown): value is this { From e7f8fb7c61b4cb0f5bf72c4dcb524180c34edba0 Mon Sep 17 00:00:00 2001 From: Jean-Yves Moyen Date: Mon, 2 Dec 2024 13:52:44 +0100 Subject: [PATCH 4/5] Fix priorities in matching expression --- .changeset/short-bees-behave.md | 5 +++++ packages/alfa-css-feature/src/media/feature/discrete.ts | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 .changeset/short-bees-behave.md diff --git a/.changeset/short-bees-behave.md b/.changeset/short-bees-behave.md new file mode 100644 index 0000000000..0cab58d137 --- /dev/null +++ b/.changeset/short-bees-behave.md @@ -0,0 +1,5 @@ +--- +"@siteimprove/alfa-css-feature": patch +--- + +**Fixed:** Matching of user-preferences in the boolean context now correctly handles non-`none` defaults. diff --git a/packages/alfa-css-feature/src/media/feature/discrete.ts b/packages/alfa-css-feature/src/media/feature/discrete.ts index f1cf7b67b6..48f2e5e58e 100644 --- a/packages/alfa-css-feature/src/media/feature/discrete.ts +++ b/packages/alfa-css-feature/src/media/feature/discrete.ts @@ -1,5 +1,5 @@ import { Keyword, type Parser as CSSParser } from "@siteimprove/alfa-css"; -import type { Device} from "@siteimprove/alfa-device"; +import type { Device } from "@siteimprove/alfa-device"; import { Preference } from "@siteimprove/alfa-device"; import { None, Option } from "@siteimprove/alfa-option"; @@ -41,7 +41,7 @@ export namespace Discrete { return this._value .map((value) => value.matches(Keyword.of(deviceValue))) - .getOr(deviceValue !== booleanFalse ?? "none"); + .getOr(deviceValue !== (booleanFalse ?? "none")); } private static _from(value: Option>>): Discrete { From 4b3838cad2645abb3b036c781b0e99fd9136b182 Mon Sep 17 00:00:00 2001 From: Jean-Yves Moyen Date: Mon, 2 Dec 2024 14:01:22 +0100 Subject: [PATCH 5/5] Clean up --- .changeset/short-bees-behave.md | 2 +- packages/alfa-css-feature/test/media.spec.ts | 1 - packages/alfa-device/src/device.ts | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.changeset/short-bees-behave.md b/.changeset/short-bees-behave.md index 0cab58d137..b37e2ebbc6 100644 --- a/.changeset/short-bees-behave.md +++ b/.changeset/short-bees-behave.md @@ -2,4 +2,4 @@ "@siteimprove/alfa-css-feature": patch --- -**Fixed:** Matching of user-preferences in the boolean context now correctly handles non-`none` defaults. +**Fixed:** Matching of user-preferences in the boolean context now correctly handles `none` defaults. diff --git a/packages/alfa-css-feature/test/media.spec.ts b/packages/alfa-css-feature/test/media.spec.ts index ce9a60d654..b15eaa205d 100644 --- a/packages/alfa-css-feature/test/media.spec.ts +++ b/packages/alfa-css-feature/test/media.spec.ts @@ -1,4 +1,3 @@ -/// import { test } from "@siteimprove/alfa-test"; import { Lexer } from "@siteimprove/alfa-css"; diff --git a/packages/alfa-device/src/device.ts b/packages/alfa-device/src/device.ts index daf690bcb0..ffbb51e1c5 100644 --- a/packages/alfa-device/src/device.ts +++ b/packages/alfa-device/src/device.ts @@ -85,7 +85,7 @@ export class Device implements Equatable, Hashable, Serializable { return this._preferences .get(name) .getOrElse(() => - Preference.of(name, Preference.unset(name)), + Preference.of(name, Preference.unset(name)), ) as Preference; }