Skip to content

Commit b839caa

Browse files
im-shivShivam Agarwal
andauthored
FORMS-21799 fixing aria-label to display plain text (#1715)
* FORMS-21799 fixing aria-label to display plain text * FORMS-21799 correcting naming convention * FORMS-21799 adding tests * FORMS-21799 increasing test coverage * FORMS-21799 increasing test coverage --------- Co-authored-by: Shivam Agarwal <shivama@adobe.com>
1 parent 93f8f2a commit b839caa

File tree

17 files changed

+166
-30
lines changed

17 files changed

+166
-30
lines changed

bundles/af-core/src/main/java/com/adobe/cq/forms/core/components/models/form/OptionsConstraint.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.osgi.annotation.versioning.ProviderType;
1919

20+
import com.fasterxml.jackson.annotation.JsonIgnore;
2021
import com.fasterxml.jackson.annotation.JsonProperty;
2122

2223
/**
@@ -58,4 +59,13 @@ default boolean isEnforceEnum() {
5859
* @since com.adobe.cq.forms.core.components.models.form 0.0.1
5960
*/
6061
String[] getEnumNames();
62+
63+
/**
64+
* Returns screen reader friendly aria labels for the options.
65+
*
66+
* @return the list of aria labels for the options
67+
* @since com.adobe.cq.forms.core.components.models.form 5.12.3
68+
*/
69+
@JsonIgnore
70+
String[] getOptionScreenReaderLabels();
6171
}

bundles/af-core/src/main/java/com/adobe/cq/forms/core/components/models/form/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* </p>
3636
*/
3737

38-
@Version("5.12.2")
38+
@Version("5.12.3")
3939
package com.adobe.cq.forms.core.components.models.form;
4040

4141
import org.osgi.annotation.versioning.Version;

bundles/af-core/src/main/java/com/adobe/cq/forms/core/components/util/AbstractOptionsFieldImpl.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import com.adobe.cq.forms.core.components.internal.form.ReservedProperties;
3232
import com.adobe.cq.forms.core.components.models.form.Field;
33+
import com.adobe.cq.forms.core.components.models.form.Label;
3334
import com.adobe.cq.forms.core.components.models.form.OptionsConstraint;
3435

3536
/**
@@ -136,4 +137,28 @@ public Object[] getDefault() {
136137
}
137138
return typedDefaultValue;
138139
}
140+
141+
@Override
142+
public String[] getOptionScreenReaderLabels() {
143+
String[] enumNames = getEnumNames();
144+
if (enumNames == null) {
145+
return null;
146+
}
147+
148+
Label label = getLabel();
149+
String labelValue = (label != null && label.getValue() != null) ? label.getValue() : "";
150+
boolean hasRichTextLabel = label != null && label.isRichText() != null && label.isRichText();
151+
152+
// Strip HTML from label once if needed
153+
String cleanLabel = hasRichTextLabel ? labelValue.replaceAll("<[^>]*>", "") : labelValue;
154+
155+
String[] ariaLabels = new String[enumNames.length];
156+
for (int i = 0; i < enumNames.length; i++) {
157+
// Strip HTML from enum name for screen readers
158+
String cleanEnumName = enumNames[i].replaceAll("<[^>]*>", "");
159+
ariaLabels[i] = cleanLabel + ": " + cleanEnumName;
160+
}
161+
162+
return ariaLabels;
163+
}
139164
}

bundles/af-core/src/test/java/com/adobe/cq/forms/core/components/internal/models/v1/form/CheckBoxGroupImplTest.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@
3939
import io.wcm.testing.mock.aem.junit5.AemContext;
4040
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
4141

42-
import static org.junit.Assert.assertArrayEquals;
43-
import static org.junit.Assert.assertEquals;
44-
import static org.junit.Assert.assertFalse;
45-
import static org.junit.Assert.assertTrue;
42+
import static org.junit.Assert.*;
4643
import static org.mockito.Mockito.mock;
4744
import static org.mockito.Mockito.spy;
4845

@@ -57,6 +54,7 @@ public class CheckBoxGroupImplTest {
5754
private static final String PATH_CHECKBOX_GROUP_FOR_INSERTION_ORDER = CONTENT_ROOT + "/checkboxgroup-insertion-order";
5855
private static final String PATH_CHECKBOX_GROUP_FOR_BOOLEAN = CONTENT_ROOT + "/checkboxgroup-boolean";
5956
private static final String PATH_CHECKBOX_GROUP_NO_FIELDTYPE = CONTENT_ROOT + "/checkboxgroup-without-fieldtype";
57+
private static final String PATH_CHECKBOX_GROUP_OPTION_SCREEN_READER_LABEL = CONTENT_ROOT + "/checkboxgroup-option-screenreader-label";
6058
private static final String PATH_CHECKBOX_GROUP_WITH_NULL_VALUES = CONTENT_ROOT + "/checkboxgroup-with-null-values";
6159

6260
private final AemContext context = FormsCoreComponentTestContext.newAemContext();
@@ -401,6 +399,33 @@ void testInsertionOrderForEnumNames() {
401399
assertArrayEquals(set.toArray(new String[0]), checkboxGroup.getEnumNames());
402400
}
403401

402+
@Test
403+
void testGetOptionScreenReaderLabels() {
404+
CheckBoxGroup checkboxGroup = getCheckBoxGroupUnderTest(PATH_CHECKBOX_GROUP_OPTION_SCREEN_READER_LABEL);
405+
String[] screenReaderLabels = checkboxGroup.getOptionScreenReaderLabels();
406+
assertEquals("<b>Gender</b>: Male", screenReaderLabels[0]);
407+
assertEquals("<b>Gender</b>: Female", screenReaderLabels[1]);
408+
409+
CheckBoxGroup spyCheckboxGroup1 = Mockito.spy(checkboxGroup);
410+
Mockito.when(spyCheckboxGroup1.getEnumNames()).thenReturn(null);
411+
assertNull(spyCheckboxGroup1.getOptionScreenReaderLabels());
412+
413+
CheckBoxGroup spyCheckboxGroup2 = Mockito.spy(checkboxGroup);
414+
Mockito.when(spyCheckboxGroup2.getLabel()).thenReturn(null);
415+
screenReaderLabels = spyCheckboxGroup2.getOptionScreenReaderLabels();
416+
assertEquals(": Male", screenReaderLabels[0]);
417+
assertEquals(": Female", screenReaderLabels[1]);
418+
419+
Label label = Mockito.mock(Label.class);
420+
Mockito.when(label.getValue()).thenReturn(null);
421+
Mockito.when(label.isRichText()).thenReturn(null);
422+
CheckBoxGroup spyCheckboxGroup3 = Mockito.spy(checkboxGroup);
423+
Mockito.when(spyCheckboxGroup3.getLabel()).thenReturn(label);
424+
screenReaderLabels = spyCheckboxGroup3.getOptionScreenReaderLabels();
425+
assertEquals(": Male", screenReaderLabels[0]);
426+
assertEquals(": Female", screenReaderLabels[1]);
427+
}
428+
404429
@Test
405430
void testNoFieldType() {
406431
CheckBoxGroup checkboxGroup = getCheckBoxGroupUnderTest(PATH_CHECKBOX_GROUP_NO_FIELDTYPE);

bundles/af-core/src/test/java/com/adobe/cq/forms/core/components/internal/models/v1/form/RadioButtonImplTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class RadioButtonImplTest {
5454
private static final String PATH_RADIOBUTTON_WITH_DUPLICATE_ENUMS = CONTENT_ROOT + "/radiobutton-duplicate-enum";
5555
private static final String PATH_RADIOBUTTON_FOR_INSERTION_ORDER = CONTENT_ROOT + "/radiobutton-insertion-order";
5656
private static final String PATH_RADIOBUTTON_WITHOUT_FIELDTYPE = CONTENT_ROOT + "/radiobutton-without-fieldtype";
57+
private static final String PATH_RADIOBUTTON_OPTION_SCREEN_READER_LABEL = CONTENT_ROOT + "/radiobutton-option-screenreader-label";
5758

5859
private final AemContext context = FormsCoreComponentTestContext.newAemContext();
5960

@@ -395,6 +396,14 @@ void testInsertionOrderForEnumNames() {
395396
assertArrayEquals(set.toArray(new String[0]), radioButton.getEnumNames());
396397
}
397398

399+
@Test
400+
void testGetOptionScreenReaderLabels() {
401+
RadioButton radioButton = getRadioButtonUnderTest(PATH_RADIOBUTTON_OPTION_SCREEN_READER_LABEL);
402+
String[] screenReaderLabels = radioButton.getOptionScreenReaderLabels();
403+
assertEquals("Gender: Male", screenReaderLabels[0]);
404+
assertEquals("Gender: Female", screenReaderLabels[1]);
405+
}
406+
398407
@Test
399408
void testGetScreenReaderTextWithLabel() {
400409
RadioButton radioButton = getRadioButtonUnderTest(PATH_RADIOBUTTON_CUSTOMIZED_WITH_LABEL);

bundles/af-core/src/test/java/com/adobe/cq/forms/core/components/internal/models/v1/form/SwitchImplTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class SwitchImplTest {
4949
private static final String PATH_SWITCH_ENABLEUNCHECKED_BOOLEAN = CONTENT_ROOT + "/switch-boolean";
5050
private static final String PATH_SWITCH_ENABLEUNCHECKEDOFF_BOOLEAN = CONTENT_ROOT + "/switch-enableUncheckedValueFalse-boolean";
5151
private static final String PATH_SWITCH_WITHOUT_FIELDTYPE = CONTENT_ROOT + "/switch-without-fieldtype";
52+
private static final String PATH_SWITCH_OPTION_SCREEN_READER_LABEL = CONTENT_ROOT + "/switch-option-screenreader-label";
5253
private final AemContext context = FormsCoreComponentTestContext.newAemContext();
5354

5455
@BeforeEach
@@ -346,6 +347,14 @@ private Switch getSwitchUnderTest(String resourcePath) {
346347
return request.adaptTo(Switch.class);
347348
}
348349

350+
@Test
351+
void testGetOptionScreenReaderLabels() {
352+
Switch switchObject = getSwitchUnderTest(PATH_SWITCH_OPTION_SCREEN_READER_LABEL);
353+
String[] screenReaderLabels = switchObject.getOptionScreenReaderLabels();
354+
assertEquals("SWITCH: OFF", screenReaderLabels[0]);
355+
assertEquals("SWITCH: ON", screenReaderLabels[1]);
356+
}
357+
349358
@Test
350359
void testNoFieldType() {
351360
Switch switchComp = getSwitchUnderTest(PATH_SWITCH_WITHOUT_FIELDTYPE);

bundles/af-core/src/test/resources/form/checkboxgroup/test-content.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,24 @@
289289
"",
290290
"value3"
291291
]
292+
},
293+
"checkboxgroup-option-screenreader-label": {
294+
"id": "checkboxgroup-1076f3bd737",
295+
"jcr:primaryType": "nt:unstructured",
296+
"sling:resourceType": "core/fd/components/form/checkboxgroup/v1/checkboxgroup",
297+
"name": "checkboxgroup",
298+
"fieldType": "checkbox-group",
299+
"jcr:title": "<b>Gender</b>",
300+
"orientation": "vertical",
301+
"type": "number[]",
302+
"enum": [
303+
"0",
304+
"1"
305+
],
306+
"enforceEnum": true,
307+
"enumNames": [
308+
"<p><i>Male</i></p>",
309+
"<p><i>Female</i></p>"
310+
]
292311
}
293312
}

bundles/af-core/src/test/resources/form/radiobutton/test-content.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,5 +350,23 @@
350350
"<p>Item 2</p>"
351351
],
352352
"fieldType": "radio-group"
353+
},
354+
"radiobutton-option-screenreader-label": {
355+
"id": "radiobutton-1076f3bd737",
356+
"jcr:primaryType": "nt:unstructured",
357+
"sling:resourceType": "core/fd/components/form/radiobutton/v1/radiobutton",
358+
"name": "radiobutton",
359+
"isTitleRichText": "true",
360+
"jcr:title": "<b>Gender</b>",
361+
"orientation": "vertical",
362+
"enum": [
363+
"0",
364+
"1"
365+
],
366+
"enumNames": [
367+
"<p><i>Male</i></p>",
368+
"<p><i>Female</i></p>"
369+
],
370+
"fieldType": "radio-group"
353371
}
354372
}

bundles/af-core/src/test/resources/form/switch/test-content.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,25 @@
198198
"jcr:primaryType": "nt:unstructured"
199199
},
200200
"customProp": "customPropValue"
201+
},
202+
"switch-option-screenreader-label": {
203+
"id": "switch-1076f3bd737",
204+
"jcr:primaryType": "nt:unstructured",
205+
"sling:resourceType": "core/fd/components/form/switch/v1/switch",
206+
"name": "switch-option-screenreader-label",
207+
"fieldType": "checkbox",
208+
"isTitleRichText": "true",
209+
"jcr:title": "<b>SWITCH</b>",
210+
"tooltip": "test-short-description",
211+
"type" : "string",
212+
"enum": [
213+
"0",
214+
"1"
215+
],
216+
"enumNames": [
217+
"<p><i>OFF</i></p>",
218+
"<p><i>ON</i></p>"
219+
],
220+
"enableUncheckedValue": true
201221
}
202222
}

ui.af.apps/src/main/content/jcr_root/apps/core/fd/components/form/checkboxgroup/v1/checkboxgroup/checkboxgroup.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
~ See the License for the specific language governing permissions and
1414
~ limitations under the License.
1515
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
16-
<!--//TOFIX: Rich text in aria-label!-->
1716
<sly data-sly-use.renderer="${'checkboxgroup.js'}"
1817
data-sly-use.widget="widget.html"
1918
data-sly-use.clientlib="${'/libs/granite/sightly/templates/clientlib.html'}"

0 commit comments

Comments
 (0)