1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const DevToolsUtils = require("resource://devtools/shared/DevToolsUtils.js");
const {
getCurrentZoom,
} = require("resource://devtools/shared/layout/utils.js");
const {
moveInfobar,
} = require("resource://devtools/server/actors/highlighters/utils/markup.js");
const {
truncateString,
} = require("resource://devtools/shared/inspector/utils.js");
const STRINGS_URI = "devtools/shared/locales/accessibility.properties";
loader.lazyRequireGetter(
this,
"LocalizationHelper",
"resource://devtools/shared/l10n.js",
true
);
DevToolsUtils.defineLazyGetter(
this,
"L10N",
() => new LocalizationHelper(STRINGS_URI)
);
const {
accessibility: {
AUDIT_TYPE,
ISSUE_TYPE: {
[AUDIT_TYPE.KEYBOARD]: {
FOCUSABLE_NO_SEMANTICS,
FOCUSABLE_POSITIVE_TABINDEX,
INTERACTIVE_NO_ACTION,
INTERACTIVE_NOT_FOCUSABLE,
MOUSE_INTERACTIVE_ONLY,
NO_FOCUS_VISIBLE,
},
[AUDIT_TYPE.TEXT_LABEL]: {
AREA_NO_NAME_FROM_ALT,
DIALOG_NO_NAME,
DOCUMENT_NO_TITLE,
EMBED_NO_NAME,
FIGURE_NO_NAME,
FORM_FIELDSET_NO_NAME,
FORM_FIELDSET_NO_NAME_FROM_LEGEND,
FORM_NO_NAME,
FORM_NO_VISIBLE_NAME,
FORM_OPTGROUP_NO_NAME_FROM_LABEL,
FRAME_NO_NAME,
HEADING_NO_CONTENT,
HEADING_NO_NAME,
IFRAME_NO_NAME_FROM_TITLE,
IMAGE_NO_NAME,
INTERACTIVE_NO_NAME,
MATHML_GLYPH_NO_NAME,
TOOLBAR_NO_NAME,
},
},
SCORES,
},
} = require("resource://devtools/shared/constants.js");
// Max string length for truncating accessible name values.
const MAX_STRING_LENGTH = 50;
/**
* The AccessibleInfobar is a class responsible for creating the markup for the
* accessible highlighter. It is also reponsible for updating content within the
* infobar such as role and name values.
*/
class Infobar {
constructor(highlighter) {
this.highlighter = highlighter;
this.audit = new Audit(this);
}
get markup() {
return this.highlighter.markup;
}
get document() {
return this.highlighter.win.document;
}
get bounds() {
return this.highlighter._bounds;
}
get options() {
return this.highlighter.options;
}
get prefix() {
return this.highlighter.ID_CLASS_PREFIX;
}
get win() {
return this.highlighter.win;
}
/**
* Move the Infobar to the right place in the highlighter.
*
* @param {Element} container
* Container of infobar.
*/
_moveInfobar(container) {
// Position the infobar using accessible's bounds
const { left: x, top: y, bottom, width } = this.bounds;
const infobarBounds = { x, y, bottom, width };
moveInfobar(container, infobarBounds, this.win);
}
/**
* Build markup for infobar.
*
* @param {Element} root
* Root element to build infobar with.
*/
buildMarkup(root) {
const container = this.markup.createNode({
parent: root,
attributes: {
class: "infobar-container",
id: "infobar-container",
"aria-hidden": "true",
hidden: "true",
},
prefix: this.prefix,
});
const infobar = this.markup.createNode({
parent: container,
attributes: {
class: "infobar",
id: "infobar",
},
prefix: this.prefix,
});
const infobarText = this.markup.createNode({
parent: infobar,
attributes: {
class: "infobar-text",
id: "infobar-text",
},
prefix: this.prefix,
});
this.markup.createNode({
nodeType: "span",
parent: infobarText,
attributes: {
class: "infobar-role",
id: "infobar-role",
},
prefix: this.prefix,
});
this.markup.createNode({
nodeType: "span",
parent: infobarText,
attributes: {
class: "infobar-name",
id: "infobar-name",
},
prefix: this.prefix,
});
this.audit.buildMarkup(infobarText);
}
/**
* Destroy the Infobar's highlighter.
*/
destroy() {
this.highlighter = null;
this.audit.destroy();
this.audit = null;
}
/**
* Gets the element with the specified ID.
*
* @param {String} id
* Element ID.
* @return {Element} The element with specified ID.
*/
getElement(id) {
return this.highlighter.getElement(id);
}
/**
* Gets the text content of element.
*
* @param {String} id
* Element ID to retrieve text content from.
* @return {String} The text content of the element.
*/
getTextContent(id) {
const anonymousContent = this.markup.content;
return anonymousContent.root.getElementById(`${this.prefix}${id}`)
.textContent;
}
/**
* Hide the accessible infobar.
*/
hide() {
const container = this.getElement("infobar-container");
container.setAttribute("hidden", "true");
}
/**
* Show the accessible infobar highlighter.
*/
show() {
const container = this.getElement("infobar-container");
// Remove accessible's infobar "hidden" attribute. We do this first to get the
// computed styles of the infobar container.
container.removeAttribute("hidden");
// Update the infobar's position and content.
this.update(container);
}
/**
* Update content of the infobar.
*/
update(container) {
const { audit, name, role } = this.options;
this.updateRole(role, this.getElement("infobar-role"));
this.updateName(name, this.getElement("infobar-name"));
this.audit.update(audit);
// Position the infobar.
this._moveInfobar(container);
}
/**
* Sets the text content of the specified element.
*
* @param {Element} el
* Element to set text content on.
* @param {String} text
* Text for content.
*/
setTextContent(el, text) {
el.setTextContent(text);
}
/**
* Show the accessible's name message.
*
* @param {String} name
* Accessible's name value.
* @param {Element} el
* Element to set text content on.
*/
updateName(name, el) {
const nameText = name ? `"${truncateString(name, MAX_STRING_LENGTH)}"` : "";
this.setTextContent(el, nameText);
}
/**
* Show the accessible's role.
*
* @param {String} role
* Accessible's role value.
* @param {Element} el
* Element to set text content on.
*/
updateRole(role, el) {
this.setTextContent(el, role);
}
}
/**
* Audit component used within the accessible highlighter infobar. This component is
* responsible for rendering and updating its containing AuditReport components that
* display various audit information such as contrast ratio score.
*/
class Audit {
constructor(infobar) {
this.infobar = infobar;
// A list of audit reports to be shown on the fly when highlighting an accessible
// object.
this.reports = {
[AUDIT_TYPE.CONTRAST]: new ContrastRatio(this),
[AUDIT_TYPE.KEYBOARD]: new Keyboard(this),
[AUDIT_TYPE.TEXT_LABEL]: new TextLabel(this),
};
}
get prefix() {
return this.infobar.prefix;
}
get markup() {
return this.infobar.markup;
}
buildMarkup(root) {
const audit = this.markup.createNode({
nodeType: "span",
parent: root,
attributes: {
class: "infobar-audit",
id: "infobar-audit",
},
prefix: this.prefix,
});
Object.values(this.reports).forEach(report => report.buildMarkup(audit));
}
update(audit = {}) {
const el = this.getElement("infobar-audit");
el.setAttribute("hidden", true);
let updated = false;
Object.values(this.reports).forEach(report => {
if (report.update(audit)) {
updated = true;
}
});
if (updated) {
el.removeAttribute("hidden");
}
}
getElement(id) {
return this.infobar.getElement(id);
}
setTextContent(el, text) {
return this.infobar.setTextContent(el, text);
}
destroy() {
this.infobar = null;
Object.values(this.reports).forEach(report => report.destroy());
this.reports = null;
}
}
/**
* A common interface between audit report components used to render accessibility audit
* information for the currently highlighted accessible object.
*/
class AuditReport {
constructor(audit) {
this.audit = audit;
}
get prefix() {
return this.audit.prefix;
}
get markup() {
return this.audit.markup;
}
getElement(id) {
return this.audit.getElement(id);
}
setTextContent(el, text) {
return this.audit.setTextContent(el, text);
}
destroy() {
this.audit = null;
}
}
/**
* Contrast ratio audit report that is used to display contrast ratio score as part of the
* inforbar,
*/
class ContrastRatio extends AuditReport {
buildMarkup(root) {
this.markup.createNode({
nodeType: "span",
parent: root,
attributes: {
class: "contrast-ratio-label",
id: "contrast-ratio-label",
},
prefix: this.prefix,
});
this.markup.createNode({
nodeType: "span",
parent: root,
attributes: {
class: "contrast-ratio-error",
id: "contrast-ratio-error",
},
prefix: this.prefix,
text: L10N.getStr("accessibility.contrast.ratio.error"),
});
this.markup.createNode({
nodeType: "span",
parent: root,
attributes: {
class: "contrast-ratio",
id: "contrast-ratio-min",
},
prefix: this.prefix,
});
this.markup.createNode({
nodeType: "span",
parent: root,
attributes: {
class: "contrast-ratio-separator",
id: "contrast-ratio-separator",
},
prefix: this.prefix,
});
this.markup.createNode({
nodeType: "span",
parent: root,
attributes: {
class: "contrast-ratio",
id: "contrast-ratio-max",
},
prefix: this.prefix,
});
}
_fillAndStyleContrastValue(el, { value, className, color, backgroundColor }) {
value = value.toFixed(2);
this.setTextContent(el, value);
el.classList.add(className);
el.setAttribute(
"style",
`--accessibility-highlighter-contrast-ratio-color: rgba(${color});` +
`--accessibility-highlighter-contrast-ratio-bg: rgba(${backgroundColor});`
);
el.removeAttribute("hidden");
}
/**
* Update contrast ratio score infobar markup.
* @param {Object}
* Audit report for a given highlighted accessible.
* @return {Boolean}
* True if the contrast ratio markup was updated correctly and infobar audit
* block should be visible.
*/
update(audit) {
const els = {};
for (const key of ["label", "min", "max", "error", "separator"]) {
const el = (els[key] = this.getElement(`contrast-ratio-${key}`));
if (["min", "max"].includes(key)) {
Object.values(SCORES).forEach(className =>
el.classList.remove(className)
);
this.setTextContent(el, "");
}
el.setAttribute("hidden", true);
el.removeAttribute("style");
}
if (!audit) {
return false;
}
const contrastRatio = audit[AUDIT_TYPE.CONTRAST];
if (!contrastRatio) {
return false;
}
const { isLargeText, error } = contrastRatio;
this.setTextContent(
els.label,
L10N.getStr(
`accessibility.contrast.ratio.label${isLargeText ? ".large" : ""}`
)
);
els.label.removeAttribute("hidden");
if (error) {
els.error.removeAttribute("hidden");
return true;
}
if (contrastRatio.value) {
const { value, color, score, backgroundColor } = contrastRatio;
this._fillAndStyleContrastValue(els.min, {
value,
className: score,
color,
backgroundColor,
});
return true;
}
const {
min,
max,
color,
backgroundColorMin,
backgroundColorMax,
scoreMin,
scoreMax,
} = contrastRatio;
this._fillAndStyleContrastValue(els.min, {
value: min,
className: scoreMin,
color,
backgroundColor: backgroundColorMin,
});
els.separator.removeAttribute("hidden");
this._fillAndStyleContrastValue(els.max, {
value: max,
className: scoreMax,
color,
backgroundColor: backgroundColorMax,
});
return true;
}
}
/**
* Keyboard audit report that is used to display a problem with keyboard
* accessibility as part of the inforbar.
*/
class Keyboard extends AuditReport {
/**
* A map from keyboard issues to annotation component properties.
*/
static get ISSUE_TO_INFOBAR_LABEL_MAP() {
return {
[FOCUSABLE_NO_SEMANTICS]: "accessibility.keyboard.issue.semantics",
[FOCUSABLE_POSITIVE_TABINDEX]: "accessibility.keyboard.issue.tabindex",
[INTERACTIVE_NO_ACTION]: "accessibility.keyboard.issue.action",
[INTERACTIVE_NOT_FOCUSABLE]: "accessibility.keyboard.issue.focusable",
[MOUSE_INTERACTIVE_ONLY]: "accessibility.keyboard.issue.mouse.only",
[NO_FOCUS_VISIBLE]: "accessibility.keyboard.issue.focus.visible",
};
}
buildMarkup(root) {
this.markup.createNode({
nodeType: "span",
parent: root,
attributes: {
class: "audit",
id: "keyboard",
},
prefix: this.prefix,
});
}
/**
* Update keyboard audit infobar markup.
* @param {Object}
* Audit report for a given highlighted accessible.
* @return {Boolean}
* True if the keyboard markup was updated correctly and infobar audit
* block should be visible.
*/
update(audit) {
const el = this.getElement("keyboard");
el.setAttribute("hidden", true);
Object.values(SCORES).forEach(className => el.classList.remove(className));
if (!audit) {
return false;
}
const keyboardAudit = audit[AUDIT_TYPE.KEYBOARD];
if (!keyboardAudit) {
return false;
}
const { issue, score } = keyboardAudit;
this.setTextContent(
el,
L10N.getStr(Keyboard.ISSUE_TO_INFOBAR_LABEL_MAP[issue])
);
el.classList.add(score);
el.removeAttribute("hidden");
return true;
}
}
/**
* Text label audit report that is used to display a problem with text alternatives
* as part of the inforbar.
*/
class TextLabel extends AuditReport {
/**
* A map from text label issues to annotation component properties.
*/
static get ISSUE_TO_INFOBAR_LABEL_MAP() {
return {
[AREA_NO_NAME_FROM_ALT]: "accessibility.text.label.issue.area",
[DIALOG_NO_NAME]: "accessibility.text.label.issue.dialog",
[DOCUMENT_NO_TITLE]: "accessibility.text.label.issue.document.title",
[EMBED_NO_NAME]: "accessibility.text.label.issue.embed",
[FIGURE_NO_NAME]: "accessibility.text.label.issue.figure",
[FORM_FIELDSET_NO_NAME]: "accessibility.text.label.issue.fieldset",
[FORM_FIELDSET_NO_NAME_FROM_LEGEND]:
"accessibility.text.label.issue.fieldset.legend2",
[FORM_NO_NAME]: "accessibility.text.label.issue.form",
[FORM_NO_VISIBLE_NAME]: "accessibility.text.label.issue.form.visible",
[FORM_OPTGROUP_NO_NAME_FROM_LABEL]:
"accessibility.text.label.issue.optgroup.label2",
[FRAME_NO_NAME]: "accessibility.text.label.issue.frame",
[HEADING_NO_CONTENT]: "accessibility.text.label.issue.heading.content",
[HEADING_NO_NAME]: "accessibility.text.label.issue.heading",
[IFRAME_NO_NAME_FROM_TITLE]: "accessibility.text.label.issue.iframe",
[IMAGE_NO_NAME]: "accessibility.text.label.issue.image",
[INTERACTIVE_NO_NAME]: "accessibility.text.label.issue.interactive",
[MATHML_GLYPH_NO_NAME]: "accessibility.text.label.issue.glyph",
[TOOLBAR_NO_NAME]: "accessibility.text.label.issue.toolbar",
};
}
buildMarkup(root) {
this.markup.createNode({
nodeType: "span",
parent: root,
attributes: {
class: "audit",
id: "text-label",
},
prefix: this.prefix,
});
}
/**
* Update text label audit infobar markup.
* @param {Object}
* Audit report for a given highlighted accessible.
* @return {Boolean}
* True if the text label markup was updated correctly and infobar
* audit block should be visible.
*/
update(audit) {
const el = this.getElement("text-label");
el.setAttribute("hidden", true);
Object.values(SCORES).forEach(className => el.classList.remove(className));
if (!audit) {
return false;
}
const textLabelAudit = audit[AUDIT_TYPE.TEXT_LABEL];
if (!textLabelAudit) {
return false;
}
const { issue, score } = textLabelAudit;
this.setTextContent(
el,
L10N.getStr(TextLabel.ISSUE_TO_INFOBAR_LABEL_MAP[issue])
);
el.classList.add(score);
el.removeAttribute("hidden");
return true;
}
}
/**
* A helper function that calculate accessible object bounds and positioning to
* be used for highlighting.
*
* @param {Object} win
* window that contains accessible object.
* @param {Object} options
* Object used for passing options:
* - {Number} x
* x coordinate of the top left corner of the accessible object
* - {Number} y
* y coordinate of the top left corner of the accessible object
* - {Number} w
* width of the the accessible object
* - {Number} h
* height of the the accessible object
* @return {Object|null} Returns, if available, positioning and bounds information for
* the accessible object.
*/
function getBounds(win, { x, y, w, h }) {
const { mozInnerScreenX, mozInnerScreenY, scrollX, scrollY } = win;
const zoom = getCurrentZoom(win);
let left = x;
let right = x + w;
let top = y;
let bottom = y + h;
left -= mozInnerScreenX - scrollX;
right -= mozInnerScreenX - scrollX;
top -= mozInnerScreenY - scrollY;
bottom -= mozInnerScreenY - scrollY;
left *= zoom;
right *= zoom;
top *= zoom;
bottom *= zoom;
const width = right - left;
const height = bottom - top;
return { left, right, top, bottom, width, height };
}
/**
* A helper function that calculate accessible object bounds and positioning to
* be used for highlighting in browser toolbox.
*
* @param {Object} win
* window that contains accessible object.
* @param {Object} options
* Object used for passing options:
* - {Number} x
* x coordinate of the top left corner of the accessible object
* - {Number} y
* y coordinate of the top left corner of the accessible object
* - {Number} w
* width of the the accessible object
* - {Number} h
* height of the the accessible object
* - {Number} zoom
* zoom level of the accessible object's parent window
* @return {Object|null} Returns, if available, positioning and bounds information for
* the accessible object.
*/
function getBoundsXUL(win, { x, y, w, h, zoom }) {
const { mozInnerScreenX, mozInnerScreenY } = win;
let left = x;
let right = x + w;
let top = y;
let bottom = y + h;
left *= zoom;
right *= zoom;
top *= zoom;
bottom *= zoom;
left -= mozInnerScreenX;
right -= mozInnerScreenX;
top -= mozInnerScreenY;
bottom -= mozInnerScreenY;
const width = right - left;
const height = bottom - top;
return { left, right, top, bottom, width, height };
}
exports.MAX_STRING_LENGTH = MAX_STRING_LENGTH;
exports.getBounds = getBounds;
exports.getBoundsXUL = getBoundsXUL;
exports.Infobar = Infobar;
|