From 978b09fc9e5911d35290f3a94e935b19f86551fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rolf=20Christian=20J=C3=B8rgensen?= <114920418+rcj-siteimprove@users.noreply.github.com> Date: Thu, 12 Dec 2024 14:28:13 +0100 Subject: [PATCH] Use `Tuple` in `mask-size` --- packages/alfa-style/src/property/mask-size.ts | 43 +++++++++++-------- .../test/property/mask-size.spec.tsx | 43 +++++++++++-------- .../alfa-style/test/property/mask.spec.tsx | 24 +++++++---- 3 files changed, 67 insertions(+), 43 deletions(-) diff --git a/packages/alfa-style/src/property/mask-size.ts b/packages/alfa-style/src/property/mask-size.ts index c6fd85c78e..8d5482f7f9 100644 --- a/packages/alfa-style/src/property/mask-size.ts +++ b/packages/alfa-style/src/property/mask-size.ts @@ -1,10 +1,11 @@ -import { Keyword, LengthPercentage, List } from "@siteimprove/alfa-css"; +import { Keyword, LengthPercentage, List, Tuple } from "@siteimprove/alfa-css"; import { Parser } from "@siteimprove/alfa-parser"; +import { Selective } from "@siteimprove/alfa-selective"; import { Longhand } from "../longhand.js"; import { Resolver } from "../resolver.js"; -const { either } = Parser; +const { either, map } = Parser; type Specified = List; @@ -13,7 +14,9 @@ type Specified = List; */ export namespace Specified { export type Item = - | List> + | Tuple< + [LengthPercentage | Keyword<"auto">, LengthPercentage | Keyword<"auto">] + > | Keyword<"cover"> | Keyword<"contain">; } @@ -24,10 +27,13 @@ type Computed = Specified; * @internal */ export const parse = either( - List.parseSpaceSeparated( - either(LengthPercentage.parse, Keyword.parse("auto")), - 1, - 2, + map( + List.parseSpaceSeparated( + either(LengthPercentage.parse, Keyword.parse("auto")), + 1, + 2, + ), + ([horizontal, vertical = horizontal]) => Tuple.of(horizontal, vertical), ), Keyword.parse("cover", "contain"), ); @@ -37,7 +43,7 @@ const parseList = List.parseCommaSeparated(parse); /** * @internal */ -export const initialItem = List.of([Keyword.of("auto")], " "); +export const initialItem = Tuple.of(Keyword.of("auto"), Keyword.of("auto")); /** * {@link https://developer.mozilla.org/en-US/docs/Web/CSS/mask-size} @@ -49,19 +55,22 @@ export default Longhand.of( parseList, (value, style) => { const layers = Resolver.layers(style, "mask-image"); + const lengthResolver = LengthPercentage.partiallyResolve( + Resolver.length(style), + ); return value.map((sizes) => layers( sizes.map((size) => - Keyword.isKeyword(size) - ? size - : size.map((value) => - Keyword.isKeyword(value) - ? value - : LengthPercentage.partiallyResolve(Resolver.length(style))( - value, - ), - ), + Selective.of(size) + .if(Tuple.isTuple, (tuple) => { + const [h, v] = tuple.values; + return Tuple.of( + Keyword.isKeyword(h) ? h : lengthResolver(h), + Keyword.isKeyword(v) ? v : lengthResolver(v), + ); + }) + .get(), ), ), ); diff --git a/packages/alfa-style/test/property/mask-size.spec.tsx b/packages/alfa-style/test/property/mask-size.spec.tsx index 155c59abb8..e7b37221f5 100644 --- a/packages/alfa-style/test/property/mask-size.spec.tsx +++ b/packages/alfa-style/test/property/mask-size.spec.tsx @@ -15,14 +15,18 @@ test("initial value is auto", (t) => { separator: ", ", values: [ { - type: "list", - separator: " ", - values: [{ type: "keyword", value: "auto" }], + type: "tuple", + values: [ + { type: "keyword", value: "auto" }, + { type: "keyword", value: "auto" }, + ], }, { - type: "list", - separator: " ", - values: [{ type: "keyword", value: "auto" }], + type: "tuple", + values: [ + { type: "keyword", value: "auto" }, + { type: "keyword", value: "auto" }, + ], }, ], }, @@ -51,9 +55,11 @@ test("#computed parses percentage width", (t) => { separator: ", ", values: [ { - type: "list", - separator: " ", - values: [{ type: "percentage", value: 0.5 }], + type: "tuple", + values: [ + { type: "percentage", value: 0.5 }, + { type: "percentage", value: 0.5 }, + ], }, ], }, @@ -68,9 +74,11 @@ test("#computed resolves em width", (t) => { separator: ", ", values: [ { - type: "list", - separator: " ", - values: [{ type: "length", unit: "px", value: 48 }], + type: "tuple", + values: [ + { type: "length", unit: "px", value: 48 }, + { type: "length", unit: "px", value: 48 }, + ], }, ], }, @@ -85,9 +93,11 @@ test("#computed parses pixel width", (t) => { separator: ", ", values: [ { - type: "list", - separator: " ", - values: [{ type: "length", unit: "px", value: 12 }], + type: "tuple", + values: [ + { type: "length", unit: "px", value: 12 }, + { type: "length", unit: "px", value: 12 }, + ], }, ], }, @@ -104,8 +114,7 @@ test("#computed parses width and height", (t) => { separator: ", ", values: [ { - type: "list", - separator: " ", + type: "tuple", values: [ { type: "length", unit: "px", value: 48 }, { type: "percentage", value: 0.25 }, diff --git a/packages/alfa-style/test/property/mask.spec.tsx b/packages/alfa-style/test/property/mask.spec.tsx index 8a4d6b27e9..ca70af7073 100644 --- a/packages/alfa-style/test/property/mask.spec.tsx +++ b/packages/alfa-style/test/property/mask.spec.tsx @@ -47,9 +47,11 @@ test("longhands resolve correctly from shorthand", (t) => { separator: ", ", values: [ { - type: "list", - separator: " ", - values: [{ type: "length", unit: "px", value: 12 }], + type: "tuple", + values: [ + { type: "length", unit: "px", value: 12 }, + { type: "length", unit: "px", value: 12 }, + ], }, ], }, @@ -217,14 +219,18 @@ test("longhands resolves correctly from shorthand with layers", (t) => { separator: ", ", values: [ { - type: "list", - separator: " ", - values: [{ type: "length", unit: "px", value: 12 }], + type: "tuple", + values: [ + { type: "length", unit: "px", value: 12 }, + { type: "length", unit: "px", value: 12 }, + ], }, { - type: "list", - separator: " ", - values: [{ type: "keyword", value: "auto" }], + type: "tuple", + values: [ + { type: "keyword", value: "auto" }, + { type: "keyword", value: "auto" }, + ], }, ], },