Skip to content

Commit

Permalink
Deep clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Jym77 committed Nov 23, 2023
1 parent 41d3074 commit dfc7a57
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 78 deletions.
5 changes: 4 additions & 1 deletion packages/alfa-style/src/property/background-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export type Specified = Color;
type Computed = Color.Canonical;

/**
* @remarks
* This is used by the shorthand parser for background.
*
* @internal
*/
export const parse = Color.parse;
Expand All @@ -26,5 +29,5 @@ export default Longhand.of<Specified, Computed>(
Percentage.of(0),
),
parse,
(value) => value.map((color) => color.resolve()),
(value) => value.resolve(),
);
2 changes: 1 addition & 1 deletion packages/alfa-style/src/property/border-image-outset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ export const parse = map(
export default Longhand.of<Specified, Computed>(
Tuple.of(Number.of(0), Number.of(0), Number.of(0), Number.of(0)),
parse,
(value, style) => value.map((tuple) => tuple.resolve(Resolver.length(style))),
(value, style) => value.resolve(Resolver.length(style)),
);
5 changes: 4 additions & 1 deletion packages/alfa-style/src/property/border-top-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export type Specified = Color;
type Computed = Color.Canonical;

/**
* @remarks
* This is used by the shorthand
*
* @internal
*/
export const parse = Color.parse;
Expand All @@ -19,5 +22,5 @@ export const parse = Color.parse;
* @internal
*/
export default Longhand.of<Specified, Computed>(Color.current, parse, (value) =>
value.map((color) => color.resolve()),
value.resolve(),
);
3 changes: 1 addition & 2 deletions packages/alfa-style/src/property/box-shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ const parseList = List.parseCommaSeparated(Shadow.parse());
export default Longhand.of<Specified, Computed>(
Keyword.of("none"),
either(Keyword.parse("none"), parseList),
(boxShadow, style) =>
boxShadow.map((value) => value.resolve(Resolver.length(style))),
(value, style) => value.resolve(Resolver.length(style)),
);
2 changes: 1 addition & 1 deletion packages/alfa-style/src/property/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Computed = Color.Canonical;
export default Longhand.of<Specified, Computed>(
Color.system("canvastext"),
Color.parse,
(value) => value.map((color) => color.resolve()),
(value) => value.resolve(),
{
inherits: true,
},
Expand Down
9 changes: 1 addition & 8 deletions packages/alfa-style/src/property/outline-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,5 @@ type Computed = Color.Canonical | Keyword<"invert">;
export default Longhand.of<Specified, Computed>(
Keyword.of("invert"),
Color.parse,
(outlineColor) =>
outlineColor.map((color) => {
if (color.type === "keyword" && color.value === "invert") {
return color;
}

return color.resolve();
}),
(value) => value.resolve(),
);
3 changes: 1 addition & 2 deletions packages/alfa-style/src/property/outline-offset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ type Computed = Length.Canonical;
export default Longhand.of<Specified, Computed>(
Length.of(0, "px"),
Length.parse,
(value, style) =>
value.map((offset) => offset.resolve(Resolver.length(style))),
(value, style) => value.resolve(Resolver.length(style)),
);
6 changes: 2 additions & 4 deletions packages/alfa-style/src/property/text-decoration-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ type Specified = Color;

type Computed = Color.Canonical;

const parse = Color.parse;

/**
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration-color}
*
* @internal
*/
export default Longhand.of<Specified, Computed>(
Color.current,
parse,
(textDecorationColor) => textDecorationColor.map((color) => color.resolve()),
Color.parse,
(value) => value.resolve(),
);
29 changes: 9 additions & 20 deletions packages/alfa-style/src/property/text-decoration-thickness.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { Keyword, LengthPercentage, type Token } from "@siteimprove/alfa-css";
import { Parser } from "@siteimprove/alfa-parser";
import { Selective } from "@siteimprove/alfa-selective";
import type { Slice } from "@siteimprove/alfa-slice";

import { Longhand } from "../longhand";
import { Resolver } from "../resolver";

import type { Computed as FontSize } from "./font-size";

const { either } = Parser;

type Specified = LengthPercentage | Keyword<"auto"> | Keyword<"from-font">;
Expand All @@ -22,24 +19,16 @@ const parse = either<Slice<Token>, Specified, string>(
LengthPercentage.parse,
);

const longhand: Longhand<Specified, Computed> = Longhand.of(
Keyword.of("auto"),
parse,
(value, style) =>
value.resolve(
Resolver.lengthPercentage(style.computed("font-size").value, style),
),
);
/**
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration-thickness}
* @internal
*/
export default Longhand.of<Specified, Computed>(
Keyword.of("auto"),
parse,
(thickness, style) =>
thickness.map((value) => {
// We need the type assertion to help TS break a circular type reference:
// this -> style.computed -> Longhands.Name -> Longhands.longhands -> this.
const fontSize = style.computed("font-size").value as FontSize;

return Selective.of(value)
.if(
LengthPercentage.isLengthPercentage,
LengthPercentage.resolve(Resolver.lengthPercentage(fontSize, style)),
)
.get();
}),
);
export default longhand;
8 changes: 0 additions & 8 deletions packages/alfa-style/src/property/text-indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ import { Resolver } from "../resolver";

type Specified = LengthPercentage;

/**
* @remarks
* TODO: percentages resolve relative to the dimensions of the containing block,
* which we do not handle.
* This results in length-percentage calculations leaking to computed
* values, which is a bit annoying.
*
*/
type Computed = LengthPercentage.PartiallyResolved;

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/alfa-style/src/property/text-shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const parseList = List.parseCommaSeparated(
export default Longhand.of<Specified, Computed>(
Keyword.of("none"),
either(Keyword.parse("none"), parseList),
(textShadow, style) =>
textShadow.map((value) => value.resolve(Resolver.length(style))),
(value, style) => value.resolve(Resolver.length(style)),
{ inherits: true },
);
31 changes: 13 additions & 18 deletions packages/alfa-style/src/property/width.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { Keyword, Length, Percentage } from "@siteimprove/alfa-css";
import { Keyword, LengthPercentage } from "@siteimprove/alfa-css";
import { Parser } from "@siteimprove/alfa-parser";
import { Selective } from "@siteimprove/alfa-selective";

import { Longhand } from "../longhand";
import { Resolver } from "../resolver";

const { either } = Parser;

type Specified = Keyword<"auto"> | Length | Percentage;
type Specified = Keyword<"auto"> | LengthPercentage;

type Computed = Keyword<"auto"> | Length.Canonical | Percentage.Canonical;
type Computed = Keyword<"auto"> | LengthPercentage.PartiallyResolved;

const parse = either(
Keyword.parse("auto"),
either(Length.parse, Percentage.parse),
);
const parse = either(Keyword.parse("auto"), LengthPercentage.parse);

/**
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/width}
Expand All @@ -23,15 +21,12 @@ export default Longhand.of<Specified, Computed>(
Keyword.of("auto"),
parse,
(width, style) =>
width.map((width) => {
switch (width.type) {
case "keyword":
return width;
case "percentage":
return width.resolve();

case "length":
return width.resolve(Resolver.length(style));
}
}),
width.map((width) =>
Selective.of(width)
.if(
LengthPercentage.isLengthPercentage,
LengthPercentage.partiallyResolve(Resolver.length(style)),
)
.get(),
),
);
16 changes: 6 additions & 10 deletions packages/alfa-style/src/property/word-spacing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Keyword } from "@siteimprove/alfa-css";
import { Length } from "@siteimprove/alfa-css/src/value/numeric";
import { Parser } from "@siteimprove/alfa-parser";
import { Selective } from "@siteimprove/alfa-selective";

import { Longhand } from "../longhand";
import { Resolver } from "../resolver";
Expand All @@ -21,15 +20,12 @@ const parse = either(Keyword.parse("normal"), Length.parse);
export default Longhand.of<Specified, Computed>(
Length.of(0, "px"),
parse,
(wordSpacing, style) =>
wordSpacing.map((wordSpacing) =>
Selective.of(wordSpacing)
.if(Length.isLength, (spacing) =>
spacing.resolve(Resolver.length(style)),
)
.else(() => Length.of(0, "px"))
.get(),
),
(value, style) =>
value
.resolve(Resolver.length(style))
.map((wordSpacing) =>
Keyword.isKeyword(wordSpacing) ? Length.of(0, "px") : wordSpacing,
),
{
inherits: true,
},
Expand Down

0 comments on commit dfc7a57

Please sign in to comment.