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

Fix a couple of CSS feature bugs #1725

Merged
merged 5 commits into from
Dec 3, 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
5 changes: 5 additions & 0 deletions .changeset/pretty-scissors-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@siteimprove/alfa-device": patch
---

**Fixed:** Values of undefined user preferences are now correctly set to their default.
5 changes: 5 additions & 0 deletions .changeset/short-bees-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@siteimprove/alfa-css-feature": patch
---

**Fixed:** Matching of user-preferences in the boolean context now correctly handles `none` defaults.
4 changes: 2 additions & 2 deletions packages/alfa-css-feature/src/media/feature/discrete.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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<Value<Keyword<K>>>): Discrete {
Expand Down
25 changes: 23 additions & 2 deletions packages/alfa-css-feature/test/media.spec.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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
Expand All @@ -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 boolean prefers-reduced-motion", (t) => {
const prefersReducedMotion = parse("(prefers-reduced-motion)").getUnsafe();

t.deepEqual(prefersReducedMotion.matches(smallPortrait), false);

const withReducedMotion = Device.of(
Device.Type.Screen,
Viewport.of(200, 400, Viewport.Orientation.Portrait),
Display.of(300),
undefined,
[Preference.of("prefers-reduced-motion", "reduce")],
);

t.deepEqual(prefersReducedMotion.matches(withReducedMotion), true);
});
4 changes: 3 additions & 1 deletion packages/alfa-device/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export class Device implements Equatable, Hashable, Serializable {
public preference<N extends Preference.Name>(name: N): Preference<N> {
return this._preferences
.get(name)
.getOrElse(() => Preference.unset(name)) as Preference<N>;
.getOrElse(() =>
Preference.of(name, Preference.unset(name)),
) as Preference<N>;
}

public equals(value: unknown): value is this {
Expand Down