Skip to content

Commit

Permalink
Consider element with height or width of 0 as invisible unless th…
Browse files Browse the repository at this point in the history
…e text overflows (#827)
  • Loading branch information
elenamongelli authored Jun 16, 2021
1 parent 32280b4 commit 0ac1a28
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
31 changes: 28 additions & 3 deletions packages/alfa-rules/src/common/predicate/is-clipped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ function isClippedBySize(
const { value: x } = style.computed("overflow-x");
const { value: y } = style.computed("overflow-y");

if (x.value === "hidden" || y.value === "hidden") {
const { value: height } = style.computed("height");
const { value: width } = style.computed("width");
const { value: height } = style.computed("height");
const { value: width } = style.computed("width");

if (x.value === "hidden" || y.value === "hidden") {
for (const dimension of [height, width]) {
switch (dimension.type) {
case "percentage":
Expand All @@ -64,6 +64,31 @@ function isClippedBySize(
}
}
}

if (
x.value === "auto" ||
x.value === "scroll" ||
y.value === "auto" ||
y.value === "scroll"
) {
for (const dimension of [height, width]) {
switch (dimension.type) {
case "percentage":
if (dimension.value <= 0) {
return true;
} else {
break;
}

case "length":
if (dimension.value <= 0) {
return true;
} else {
break;
}
}
}
}
}

for (const parent of node.parent({ flattened: true })) {
Expand Down
36 changes: 36 additions & 0 deletions packages/alfa-rules/test/common/predicate/is-visible.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,42 @@ test(`isVisible() returns false when an element is hidden by reducing its size
t.equal(isVisible(element), false);
});

test(`isVisible() returns false when an element scrolls its overflow and its size is reduced to 0 pixel, thus hidding the scrollbar`, (t) => {
for (const element of [
<div style={{ width: "0", overflowX: "scroll" }}>Hello World</div>,

<div style={{ width: "0", overflowY: "scroll" }}>Hello World</div>,

<div style={{ height: "0", overflowX: "scroll" }}>Hello World</div>,

<div style={{ height: "0", overflowY: "scroll" }}>Hello World</div>,

<div style={{ width: "0", height: "0", overflow: "scroll" }}>
Hello World
</div>,
]) {
t.equal(isVisible(element), false);
}
});

test(`isVisible() returns false when an element has its overflow set to auto and its size is reduced to 0 pixel, thus hidding the scrollbar`, (t) => {
for (const element of [
<div style={{ width: "0", overflowX: "auto" }}>Hello World</div>,

<div style={{ width: "0", overflowY: "auto" }}>Hello World</div>,

<div style={{ height: "0", overflowX: "auto" }}>Hello World</div>,

<div style={{ height: "0", overflowY: "auto" }}>Hello World</div>,

<div style={{ width: "0", height: "0", overflow: "auto" }}>
Hello World
</div>,
]) {
t.equal(isVisible(element), false);
}
});

test("isVisible() returns false on empty elements", (t) => {
const element = <div />;

Expand Down

0 comments on commit 0ac1a28

Please sign in to comment.