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

Commit 60910ef

Browse files
crisbetoThomasBurleson
authored andcommitted
fix(tooltip): interpolate the tooltip text when adding it to the aria-label
Fixes the raw tooltip text text being added to the parent, if a tooltip contained a data binding, instead of the interpolated value. Fixes #6855. Closes #8170
1 parent 9f6eecb commit 60910ef

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/components/tooltip/tooltip.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ angular
3333
* @param {string=} md-direction Which direction would you like the tooltip to go? Supports left, right, top, and bottom. Defaults to bottom.
3434
*/
3535
function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdTheming, $rootElement,
36-
$animate, $q) {
36+
$animate, $q, $interpolate) {
3737

3838
var TOOLTIP_SHOW_DELAY = 0;
3939
var TOOLTIP_WINDOW_EDGE_SPACE = 8;
@@ -150,11 +150,22 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe
150150
element.remove();
151151
attributeObserver && attributeObserver.disconnect();
152152
});
153+
154+
// Updates the aria-label when the element text changes. This watch
155+
// doesn't need to be set up if the element doesn't have any data
156+
// bindings.
157+
if (element.text().indexOf($interpolate.startSymbol()) > -1) {
158+
scope.$watch(function() {
159+
return element.text().trim();
160+
}, addAriaLabel);
161+
}
153162
}
154163

155-
function addAriaLabel () {
156-
if (!parent.attr('aria-label') && !parent.text().trim()) {
157-
parent.attr('aria-label', element.text().trim());
164+
function addAriaLabel (override) {
165+
if ((override || !parent.attr('aria-label')) && !parent.text().trim()) {
166+
var rawText = override || element.text().trim();
167+
var interpolatedText = $interpolate(rawText)(parent.scope());
168+
parent.attr('aria-label', interpolatedText);
158169
}
159170
}
160171

src/components/tooltip/tooltip.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,36 @@ describe('<md-tooltip> directive', function() {
6767
expect(element.attr('aria-label')).toEqual('Tooltip');
6868
});
6969

70+
it('should interpolate the aria-label', function(){
71+
buildTooltip(
72+
'<md-button>' +
73+
'<md-tooltip>{{ "hello" | uppercase }}</md-tooltip>' +
74+
'</md-button>'
75+
);
76+
77+
expect(element.attr('aria-label')).toBe('HELLO');
78+
});
79+
80+
it('should update the aria-label when the interpolated value changes', function(){
81+
buildTooltip(
82+
'<md-button>' +
83+
'<md-tooltip>{{ testModel.ariaTest }}</md-tooltip>' +
84+
'</md-button>'
85+
);
86+
87+
$rootScope.$apply(function() {
88+
$rootScope.testModel.ariaTest = 'test 1';
89+
});
90+
91+
expect(element.attr('aria-label')).toBe('test 1');
92+
93+
$rootScope.$apply(function() {
94+
$rootScope.testModel.ariaTest = 'test 2';
95+
});
96+
97+
expect(element.attr('aria-label')).toBe('test 2');
98+
});
99+
70100
it('should not set parent to items with no pointer events', inject(function($window){
71101
spyOn($window, 'getComputedStyle').and.callFake(function(el) {
72102
return { 'pointer-events': el ? 'none' : '' };

0 commit comments

Comments
 (0)