Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 6c209b9

Browse files
ThomasBurlesonmmalerba
authored andcommitted
fix(mdTooltip): Tooltip parent aria label override (#10464)
The tooltip now does not override or update the parent element `aria-label` unless the parent's `aria-label` does not exist, or it has been previously set by the tooltip. It determines if it has been set by the tooltip by placing the `aria-labelledby` attribute on the parent element and setting it to `tooltip`. Fixes #10242.
1 parent e1bb097 commit 6c209b9

File tree

2 files changed

+280
-198
lines changed

2 files changed

+280
-198
lines changed

src/components/tooltip/tooltip.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $interpolate,
7575

7676
function linkFunc(scope, element, attr) {
7777
// Set constants.
78+
var tooltipId = 'md-tooltip-' + $mdUtil.nextUid();
7879
var parent = $mdUtil.getParentWithPointerEvents(element);
7980
var debouncedOnResize = $$rAF.throttle(updatePosition);
8081
var mouseActive = false;
@@ -102,12 +103,24 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $interpolate,
102103
}
103104
}
104105

105-
function addAriaLabel(override) {
106-
if (override || !parent.attr('aria-label')) {
107-
// Only interpolate the text from the HTML element because otherwise the custom text
108-
// could be interpolated twice and cause XSS violations.
109-
var interpolatedText = override || $interpolate(element.text().trim())(scope.$parent);
106+
function addAriaLabel(labelText) {
107+
// Only interpolate the text from the HTML element because otherwise the custom text could
108+
// be interpolated twice and cause XSS violations.
109+
var interpolatedText = labelText || $interpolate(element.text().trim())(parent.scope);
110+
111+
// Only add the `aria-label` to the parent if there isn't already one, if there isn't an
112+
// already present `aria-labelledby`, or if the previous `aria-label` was added by the
113+
// tooltip directive.
114+
if (
115+
(!parent.attr('aria-label') && !parent.attr('aria-labelledby')) ||
116+
parent.attr('aria-labelledby') === tooltipId
117+
) {
110118
parent.attr('aria-label', interpolatedText);
119+
120+
// Set the `aria-labelledby` attribute if it has not already been set.
121+
if (!parent.attr('aria-labelledby')) {
122+
parent.attr('aria-labelledby', tooltipId);
123+
}
111124
}
112125
}
113126

@@ -360,7 +373,6 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $interpolate,
360373
}
361374

362375
if (!panelRef) {
363-
var id = 'tooltip-' + $mdUtil.nextUid();
364376
var attachTo = angular.element(document.body);
365377
var panelAnimation = $mdPanel.newPanelAnimation()
366378
.openFrom(parent)
@@ -371,7 +383,7 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $interpolate,
371383
});
372384

373385
var panelConfig = {
374-
id: id,
386+
id: tooltipId,
375387
attachTo: attachTo,
376388
contentElement: element,
377389
propagateContainerEvents: true,

0 commit comments

Comments
 (0)