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

Commit 121a39d

Browse files
devversionThomasBurleson
authored andcommitted
fix(tabs): properly detect text changes
* Currently the MutationObserver is watching for attribute changes and subtree modifications. It also watches for text content changes, but that's not working properly for span elements with `<md-tab-label>` elements. See https://bugzilla.mozilla.org/show_bug.cgi?id=1138368 The MutationObserver should watch for `characterData` to properly detect text changes. Fixes #8667. Closes #8803
1 parent bb04bfa commit 121a39d

File tree

2 files changed

+73
-14
lines changed

2 files changed

+73
-14
lines changed

src/components/tabs/js/tabsDummyWrapperDirective.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ function MdTabsDummyWrapper ($mdUtil) {
2121
ctrl.updatePagination();
2222
ctrl.updateInkBarStyles();
2323
});
24-
var config = { childList: true, subtree: true };
24+
25+
var config = {
26+
childList: true,
27+
subtree: true,
28+
// Per https://bugzilla.mozilla.org/show_bug.cgi?id=1138368, browsers will not fire
29+
// the childList mutation, once a <span> element's innerText changes.
30+
// The characterData of the <span> element will change.
31+
characterData: true
32+
};
2533

2634
observer.observe(element[0], config);
2735

src/components/tabs/tabs.spec.js

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ describe('<md-tabs>', function () {
233233
});
234234

235235
it('updates pagination and ink styles when string labels change', function(done) {
236-
inject(function($rootScope) {
236+
inject(function($rootScope, $timeout) {
237237
// Setup our initial label
238238
$rootScope.$apply('label = "Some Label"');
239239

@@ -242,21 +242,72 @@ describe('<md-tabs>', function () {
242242
var tabs = setup(template);
243243
var ctrl = tabs.controller('mdTabs');
244244

245-
// Setup spies
246-
spyOn(ctrl, 'updatePagination');
247-
spyOn(ctrl, 'updateInkBarStyles');
248-
249-
// Change the label
250-
$rootScope.$apply('label="Another Label"');
245+
// Flush the tabs controller timeout for initialization.
246+
$timeout.flush();
251247

252-
// Use window.setTimeout to add our expectations to the end of the call stack, after the
248+
// After the first timeout the mutation observer should have been fired once, because
249+
// the initialization of the dummy tabs, already causes some mutations.
250+
// Use setTimeout to add our expectations to the end of the call stack, after the
253251
// MutationObservers have already fired
254-
window.setTimeout(function() {
255-
// Fire expectations
256-
expect(ctrl.updatePagination.calls.count()).toBe(1);
257-
expect(ctrl.updateInkBarStyles.calls.count()).toBe(1);
252+
setTimeout(function() {
253+
// Setup spies
254+
spyOn(ctrl, 'updatePagination');
255+
spyOn(ctrl, 'updateInkBarStyles');
256+
257+
// Update the label to trigger a new update of the pagination and InkBar styles.
258+
$rootScope.$apply('label = "Another Label"');
259+
260+
// Use setTimeout to add our expectations to the end of the call stack, after the
261+
// MutationObservers have already fired
262+
setTimeout(function() {
263+
expect(ctrl.updatePagination).toHaveBeenCalledTimes(1);
264+
expect(ctrl.updateInkBarStyles).toHaveBeenCalledTimes(1);
265+
266+
done();
267+
});
268+
});
269+
})
270+
});
258271

259-
done();
272+
it('updates pagination and ink styles when content label changes', function(done) {
273+
inject(function($rootScope, $timeout) {
274+
// Setup our initial label
275+
$rootScope.$apply('label = "Default Label"');
276+
277+
// Init our variables
278+
var template = '' +
279+
'<md-tabs>' +
280+
'<md-tab>' +
281+
'<md-tab-label>{{ label }}</md-tab-label>' +
282+
'</md-tab>' +
283+
'</md-tabs>';
284+
285+
var tabs = setup(template);
286+
var ctrl = tabs.controller('mdTabs');
287+
288+
// Flush the tabs controller timeout for initialization.
289+
$timeout.flush();
290+
291+
// After the first timeout the mutation observer should have been fired once, because
292+
// the initialization of the dummy tabs, already causes some mutations.
293+
// Use setTimeout to add our expectations to the end of the call stack, after the
294+
// MutationObservers have already fired
295+
setTimeout(function() {
296+
// Setup spies
297+
spyOn(ctrl, 'updatePagination');
298+
spyOn(ctrl, 'updateInkBarStyles');
299+
300+
// Update the label to trigger a new update of the pagination and InkBar styles.
301+
$rootScope.$apply('label = "New Label"');
302+
303+
// Use setTimeout to add our expectations to the end of the call stack, after the
304+
// MutationObservers have already fired
305+
setTimeout(function() {
306+
expect(ctrl.updatePagination).toHaveBeenCalledTimes(1);
307+
expect(ctrl.updateInkBarStyles).toHaveBeenCalledTimes(1);
308+
309+
done();
310+
});
260311
});
261312
})
262313
});

0 commit comments

Comments
 (0)