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 background size #788

Merged
merged 5 commits into from
May 4, 2021
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
50 changes: 31 additions & 19 deletions packages/alfa-style/src/property/background-size.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Keyword, Length, Percentage, Token } from "@siteimprove/alfa-css";
import { Iterable } from "@siteimprove/alfa-iterable";
import { Parser } from "@siteimprove/alfa-parser";
import { Slice } from "@siteimprove/alfa-slice";

import { Property } from "../property";
import { Resolver } from "../resolver";

import { List } from "./value/list";
import { Tuple } from "./value/tuple";

const { map, either, delimited, option, pair, separatedList } = Parser;
const { map, either, delimited, option, pair, right, separatedList } = Parser;

declare module "../property" {
interface Longhands {
Expand All @@ -24,11 +26,10 @@ export type Specified = List<Specified.Item>;
* @internal
*/
export namespace Specified {
export type Dimension = Length | Percentage | Keyword<"auto">;

export type Item =
| [
Length | Percentage | Keyword<"auto">,
Length | Percentage | Keyword<"auto">
]
| Tuple<[Dimension, Dimension]>
| Keyword<"cover">
| Keyword<"contain">;
}
Expand All @@ -42,24 +43,35 @@ export type Computed = List<Computed.Item>;
* @internal
*/
export namespace Computed {
export type Dimension = Length<"px"> | Percentage | Keyword<"auto">;

export type Item =
| [
Length<"px"> | Percentage | Keyword<"auto">,
Length<"px"> | Percentage | Keyword<"auto">
]
| Tuple<[Dimension, Dimension]>
| Keyword<"cover">
| Keyword<"contain">;
}

/**
* @internal
*/
const parseDimension = either<Slice<Token>, Specified.Dimension, string>(
Length.parse,
Percentage.parse,
Keyword.parse("auto")
);

/**
* @internal
*/
export const parse = either(
pair(
either(Length.parse, Keyword.parse("auto")),
map(option(either(Length.parse, Keyword.parse("auto"))), (y) =>
y.getOrElse(() => Keyword.of("auto"))
)
map(
pair(
parseDimension,
map(option(right(Token.parseWhitespace, parseDimension)), (y) =>
y.getOrElse(() => Keyword.of("auto"))
)
),
([x, y]) => Tuple.of(x, y)
),
Keyword.parse("contain", "cover")
);
Expand All @@ -82,7 +94,7 @@ export const parseList = map(
export default Property.register(
"background-size",
Property.of<Specified, Computed>(
List.of([[Keyword.of("auto"), Keyword.of("auto")]], ", "),
List.of([Tuple.of(Keyword.of("auto"), Keyword.of("auto"))], ", "),
parseList,
(value, style) =>
value.map((sizes) =>
Expand All @@ -92,12 +104,12 @@ export default Property.register(
return size;
}

const [x, y] = size;
const [x, y] = size.values;

return [
return Tuple.of(
x.type === "length" ? Resolver.length(x, style) : x,
y.type === "length" ? Resolver.length(y, style) : y,
];
y.type === "length" ? Resolver.length(y, style) : y
);
}),
", "
)
Expand Down
3 changes: 2 additions & 1 deletion packages/alfa-style/src/property/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Slice } from "@siteimprove/alfa-slice";
import { Property } from "../property";

import { List } from "./value/list";
import { Tuple } from "./value/tuple";

import * as Attachment from "./background-attachment";
import * as Clip from "./background-clip";
Expand Down Expand Up @@ -253,7 +254,7 @@ export default Property.registerShorthand(
image.push(layer[1] ?? Keyword.of("none"));
positionX.push(layer[2] ?? Percentage.of(0));
positionY.push(layer[3] ?? Percentage.of(0));
size.push(layer[4] ?? [Keyword.of("auto"), Keyword.of("auto")]);
size.push(layer[4] ?? Tuple.of(Keyword.of("auto"), Keyword.of("auto")));
repeatX.push(layer[5] ?? Keyword.of("repeat"));
repeatY.push(layer[6] ?? Keyword.of("repeat"));
attachment.push(layer[7] ?? Keyword.of("scroll"));
Expand Down
160 changes: 160 additions & 0 deletions packages/alfa-style/test/property/background-size.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { Device } from "@siteimprove/alfa-device";
import { h } from "@siteimprove/alfa-dom";
import { test } from "@siteimprove/alfa-test";
import { Style } from "../../src";

const device = Device.standard();

test("#cascaded() parses `background-size: cover`", (t) => {
const element = <div style={{ backgroundSize: `cover` }} />;

const style = Style.from(element, device);

t.deepEqual(style.cascaded("background-size").get().toJSON(), {
value: {
type: "list",
values: [
{
type: "keyword",
value: "cover",
},
],
separator: ", ",
},
source: h.declaration("background-size", "cover").toJSON(),
});
});

test("#cascaded() parses `background-size: 10px`", (t) => {
const element = <div style={{ backgroundSize: `10px` }} />;

const style = Style.from(element, device);

t.deepEqual(style.cascaded("background-size").get().toJSON(), {
value: {
type: "list",
values: [
{
type: "tuple",
values: [
{
type: "length",
value: 10,
unit: "px",
},
{
type: "keyword",
value: "auto",
},
],
},
],
separator: ", ",
},
source: h.declaration("background-size", "10px").toJSON(),
});
});

test("#cascaded() parses `background-size: 10%`", (t) => {
const element = <div style={{ backgroundSize: `10%` }} />;

const style = Style.from(element, device);

t.deepEqual(style.cascaded("background-size").get().toJSON(), {
value: {
type: "list",
values: [
{
type: "tuple",
values: [
{
type: "percentage",
value: 0.1,
},
{
type: "keyword",
value: "auto",
},
],
},
],
separator: ", ",
},
source: h.declaration("background-size", "10%").toJSON(),
});
});

test("#cascaded() parses `background-size: 10px 20px`", (t) => {
const element = <div style={{ backgroundSize: `10px 20px` }} />;

const style = Style.from(element, device);

t.deepEqual(style.cascaded("background-size").get().toJSON(), {
value: {
type: "list",
values: [
{
type: "tuple",
values: [
{
type: "length",
value: 10,
unit: "px",
},
{
type: "length",
value: 20,
unit: "px",
},
],
},
],
separator: ", ",
},
source: h.declaration("background-size", "10px 20px").toJSON(),
});
});

test("#cascaded() parses `background-size: 10px, 20px`", (t) => {
const element = <div style={{ backgroundSize: `10px, 20px` }} />;

const style = Style.from(element, device);

t.deepEqual(style.cascaded("background-size").get().toJSON(), {
value: {
type: "list",
values: [
{
type: "tuple",
values: [
{
type: "length",
value: 10,
unit: "px",
},
{
type: "keyword",
value: "auto",
},
],
},
{
type: "tuple",
values: [
{
type: "length",
value: 20,
unit: "px",
},
{
type: "keyword",
value: "auto",
},
],
},
],
separator: ", ",
},
source: h.declaration("background-size", "10px, 20px").toJSON(),
});
});
1 change: 1 addition & 0 deletions packages/alfa-style/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"test/property/background-color.spec.tsx",
"test/property/background-image.spec.tsx",
"test/property/background-position.spec.tsx",
"test/property/background-size.spec.tsx",
"test/property/border.spec.tsx",
"test/property/border-[block,inline].spec.tsx",
"test/property/border-[block,inline]-[end,start].spec.tsx",
Expand Down