From 6ea56f35abe2fcea436a1341a685fea37e71a293 Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Tue, 3 Aug 2021 11:04:45 -0700 Subject: [PATCH] Add aria-labels to XFA form elements. (bug 1723422) --- src/core/xfa/template.js | 22 ++++++++++ test/unit/xfa_tohtml_spec.js | 84 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/src/core/xfa/template.js b/src/core/xfa/template.js index 0d336faff..964edbac7 100644 --- a/src/core/xfa/template.js +++ b/src/core/xfa/template.js @@ -201,6 +201,22 @@ function setTabIndex(node) { } } +function ariaLabel(obj) { + if (!obj.assist) { + return null; + } + const assist = obj.assist; + if (assist.speak && assist.speak[$content] !== "") { + return assist.speak[$content]; + } + if (assist.toolTip) { + return assist.toolTip[$content]; + } + // TODO: support finding the related caption element. See xfa_bug1718037.pdf + // for an example. + return null; +} + function valueToHtml(value) { return HTMLResult.success({ name: "div", @@ -1231,6 +1247,7 @@ class CheckButton extends XFAObject { type, checked, xfaOn: exportedValue.on, + "aria-label": ariaLabel(field), }, }; @@ -1322,6 +1339,7 @@ class ChoiceList extends XFAObject { fieldId: field[$uid], dataId: (field[$data] && field[$data][$uid]) || field[$uid], style, + "aria-label": ariaLabel(field), }; if (this.open === "multiSelect") { @@ -1560,6 +1578,7 @@ class DateTimeEdit extends XFAObject { dataId: (field[$data] && field[$data][$uid]) || field[$uid], class: ["xfaTextfield"], style, + "aria-label": ariaLabel(field), }, }; @@ -3684,6 +3703,7 @@ class NumericEdit extends XFAObject { dataId: (field[$data] && field[$data][$uid]) || field[$uid], class: ["xfaTextfield"], style, + "aria-label": ariaLabel(field), }, }; @@ -5649,6 +5669,7 @@ class TextEdit extends XFAObject { fieldId: field[$uid], class: ["xfaTextfield"], style, + "aria-label": ariaLabel(field), }, }; } else { @@ -5660,6 +5681,7 @@ class TextEdit extends XFAObject { fieldId: field[$uid], class: ["xfaTextfield"], style, + "aria-label": ariaLabel(field), }, }; } diff --git a/test/unit/xfa_tohtml_spec.js b/test/unit/xfa_tohtml_spec.js index 2693eeaf5..4e43f891e 100644 --- a/test/unit/xfa_tohtml_spec.js +++ b/test/unit/xfa_tohtml_spec.js @@ -179,6 +179,90 @@ describe("XFAFactory", function () { expect(field.attributes.maxLength).toEqual(123); }); + it("should have an aria-label property from speak", function () { + const xml = ` + + + + + + + + + `; + const factory = new XFAFactory({ "xdp:xdp": xml }); + + expect(factory.numberPages).toEqual(1); + + const pages = factory.getPages(); + const field = searchHtmlNode(pages, "name", "input"); + + expect(field.attributes["aria-label"]).toEqual("Screen Reader"); + }); + + it("should have an aria-label property from toolTip", function () { + const xml = ` + + + + + + + + + `; + const factory = new XFAFactory({ "xdp:xdp": xml }); + + expect(factory.numberPages).toEqual(1); + + const pages = factory.getPages(); + const field = searchHtmlNode(pages, "name", "input"); + + expect(field.attributes["aria-label"]).toEqual("Screen Reader"); + }); + it("should have an input or textarea", function () { const xml = `