|
316 | 316 |
|
317 | 317 | },
|
318 | 318 |
|
319 |
| - $get : ['$http', '$q', '$log', '$templateCache', function($http, $q, $log, $templateCache) { |
| 319 | + $get : ['$http', '$q', '$log', '$templateCache', '$mdUtil', function($http, $q, $log, $templateCache, $mdUtil) { |
320 | 320 | this.preloadIcons($templateCache);
|
321 |
| - return MdIconService(config, $http, $q, $log, $templateCache); |
| 321 | + return MdIconService(config, $http, $q, $log, $templateCache, $mdUtil); |
322 | 322 | }]
|
323 | 323 | };
|
324 | 324 |
|
|
373 | 373 | */
|
374 | 374 |
|
375 | 375 | /* @ngInject */
|
376 |
| - function MdIconService(config, $http, $q, $log, $templateCache) { |
| 376 | + function MdIconService(config, $http, $q, $log, $templateCache, $mdUtil) { |
377 | 377 | var iconCache = {};
|
378 | 378 | var urlRegex = /[-\w@:%\+.~#?&//=]{2,}\.[a-z]{2,4}\b(\/[-\w@:%\+.~#?&//=]*)?/i;
|
379 | 379 | var dataUrlRegex = /^data:image\/svg\+xml[\s*;\w\-\=]*?(base64)?,(.*)$/i;
|
|
393 | 393 | // If already loaded and cached, use a clone of the cached icon.
|
394 | 394 | // Otherwise either load by URL, or lookup in the registry and then load by URL, and cache.
|
395 | 395 |
|
396 |
| - if ( iconCache[id] ) return $q.when( iconCache[id].clone() ); |
| 396 | + if ( iconCache[id] ) return $q.when( transformClone(iconCache[id]) ); |
397 | 397 | if ( urlRegex.test(id) || dataUrlRegex.test(id) ) return loadByURL(id).then( cacheIcon(id) );
|
398 | 398 | if ( id.indexOf(':') == -1 ) id = '$default:' + id;
|
399 | 399 |
|
|
418 | 418 | return result;
|
419 | 419 | }
|
420 | 420 |
|
| 421 | + function transformClone(cacheElement) { |
| 422 | + var clone = cacheElement.clone(); |
| 423 | + var cacheSuffix = '_cache' + $mdUtil.nextUid(); |
| 424 | + |
| 425 | + // We need to modify for each cached icon the id attributes. |
| 426 | + // This is needed because SVG id's are treated as normal DOM ids |
| 427 | + // and should not have a duplicated id. |
| 428 | + if (clone.id) clone.id += cacheSuffix; |
| 429 | + angular.forEach(clone.querySelectorAll('[id]'), function (item) { |
| 430 | + item.id += cacheSuffix; |
| 431 | + }); |
| 432 | + |
| 433 | + return clone; |
| 434 | + } |
| 435 | + |
421 | 436 | /**
|
422 | 437 | * Prepare and cache the loaded icon for the specified `id`
|
423 | 438 | */
|
424 | 439 | function cacheIcon( id ) {
|
425 | 440 |
|
426 |
| - return function updateCache( _icon ) { |
427 |
| - var icon = isIcon(_icon) ? _icon : new Icon(_icon, config[id]); |
428 |
| - |
429 |
| - //clear id attributes to prevent aria issues |
430 |
| - var elem = icon.element; |
431 |
| - elem.removeAttribute('id'); |
432 |
| - |
433 |
| - angular.forEach(elem.querySelectorAll('[id]'), function(item) { |
434 |
| - item.removeAttribute('id'); |
435 |
| - }); |
436 |
| - |
437 |
| - iconCache[id] = icon; |
438 |
| - |
| 441 | + return function updateCache( icon ) { |
| 442 | + iconCache[id] = isIcon(icon) ? icon : new Icon(icon, config[id]); |
439 | 443 |
|
440 | 444 | return iconCache[id].clone();
|
441 | 445 | };
|
|
0 commit comments