Skip to content

Commit 2647941

Browse files
author
GerganaKremenska
authored
fix(ui5-card): correct aria-labelledBy to card and header (#2577)
Now the aria-labelledBy of card and header contain only the rendered elements. Fixes: #2426
1 parent dba0265 commit 2647941

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

packages/main/src/Card.hbs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
dir="{{effectiveDir}}"
44
role="region"
55
aria-label="{{ariaLabelText}}"
6-
aria-labelledby="{{_id}}-desc {{_id}}-heading">
6+
aria-labelledby="{{ariaLabelledByCard}}">
77
{{#if hasHeader}}
88
<div class="{{classes.header}}"
99
@click="{{_headerClick}}"
1010
@keydown="{{_headerKeydown}}"
1111
@keyup="{{_headerKeyup}}"
1212
role="{{ariaHeaderRole}}"
13-
aria-labelledby="{{_id}}-subheading {{_id}}-status"
13+
aria-labelledby="{{ariaLabelledByHeader}}"
1414
aria-level="{{ariaLevel}}"
1515
aria-roledescription="{{ariaCardHeaderRoleDescription}}"
1616
tabindex="0">
1717

1818
{{#if hasAvatar}}
19-
<div class="ui5-card-avatar" aria-label="{{ariaCardAvatarLabel}}">
19+
<div id="{{_id}}-avatar" class="ui5-card-avatar" aria-label="{{ariaCardAvatarLabel}}">
2020
<slot name="avatar"></slot>
2121
</div>
2222
{{/if}}

packages/main/src/Card.js

+22
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,28 @@ class Card extends UI5Element {
258258
return this.i18nBundle.getText(ARIA_LABEL_CARD_CONTENT);
259259
}
260260

261+
get ariaLabelledByHeader() {
262+
const labels = [];
263+
264+
if (this.subheading) {
265+
labels.push(`${this._id}-subheading`);
266+
}
267+
268+
if (this.status) {
269+
labels.push(`${this._id}-status`);
270+
}
271+
272+
if (this.hasAvatar) {
273+
labels.push(`${this._id}-avatar`);
274+
}
275+
276+
return labels.length !== 0 ? labels.join(" ") : undefined;
277+
}
278+
279+
get ariaLabelledByCard() {
280+
return this.heading ? `${this._id}-heading ${this._id}-desc` : `${this._id}-desc`;
281+
}
282+
261283
get hasAvatar() {
262284
return !!this.avatar.length;
263285
}

packages/main/test/pages/Card.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
</ui5-list>
5050
</ui5-card>
5151

52-
<ui5-card heading="Quick Links" subheading="quick links">
52+
<ui5-card id="card3" heading="Quick Links" subheading="quick links">
5353
<ui5-list id="myList3" separators="Inner">
5454
<ui5-li icon="horizontal-bullet-chart" >Template Based Segmentation</ui5-li>
5555
<ui5-li icon="opportunity" >Segmentation Models</ui5-li>

packages/main/test/specs/Card.spec.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe("Card general interaction", () => {
3333
assert.strictEqual(field.getProperty("value"), "3", "The events count should remain 3 as the header is not interactive.");
3434
});
3535

36-
it("Tests aria-label and aria-labelledby", () => {
36+
it("Tests aria-label", () => {
3737
const card1 = browser.$("#textAreaAriaLabel").shadow$(".ui5-card-root");
3838
const card2 = browser.$("#textAreaAriaLabelledBy").shadow$(".ui5-card-root");
3939
const EXPECTED_ARIA_LABEL1 = "Hello World";
@@ -44,4 +44,26 @@ describe("Card general interaction", () => {
4444
assert.strictEqual(card2.getAttribute("aria-label"), EXPECTED_ARIA_LABEL2,
4545
"The aria-label is correctly set internally.");
4646
});
47+
48+
it("Tests internal aria-labelledby labeling", () => {
49+
const card1 = $("#card2").shadow$(".ui5-card-root");
50+
const card1Id = $("#card2").getProperty("_id");
51+
const header = $("#card2").shadow$(".ui5-card-header");
52+
const card2 = $("#card3").shadow$(".ui5-card-root");
53+
const card2Id = $("#card3").getProperty("_id");
54+
const header2 = $("#card3").shadow$(".ui5-card-header");
55+
const EXPECTED_ARIA_LABELLEDBY_CARD = `${card1Id}-heading ${card1Id}-desc`;
56+
const EXPECTED_ARIA_LABELLEDBY_HEADER = `${card1Id}-subheading ${card1Id}-status ${card1Id}-avatar`;
57+
const EXPECTED_ARIA_LABELLEDBY_CARD2 = `${card2Id}-heading ${card2Id}-desc`;
58+
const EXPECTED_ARIA_LABELLEDBY_HEADER2 = `${card2Id}-subheading`;
59+
60+
assert.strictEqual(card1.getAttribute("aria-labelledby"), EXPECTED_ARIA_LABELLEDBY_CARD,
61+
"The aria-labelledby of card is correctly set internally.");
62+
assert.strictEqual(header.getAttribute("aria-labelledby"), EXPECTED_ARIA_LABELLEDBY_HEADER,
63+
"The aria-labelledby is correctly set internally.");
64+
assert.strictEqual(card2.getAttribute("aria-labelledby"), EXPECTED_ARIA_LABELLEDBY_CARD2,
65+
"The aria-labelledby of card is correctly set internally.");
66+
assert.strictEqual(header2.getAttribute("aria-labelledby"), EXPECTED_ARIA_LABELLEDBY_HEADER2,
67+
"The aria-labelledby is correctly set internally.");
68+
});
4769
});

0 commit comments

Comments
 (0)