* Returns an Observable which produces the string contents of the given icon. Results may be * cached, so future calls with the same URL may not cause another HTTP request.
(iconConfig: SvgIconConfig)
| 629 | * cached, so future calls with the same URL may not cause another HTTP request. |
| 630 | */ |
| 631 | private _fetchIcon(iconConfig: SvgIconConfig): Observable<TrustedHTML> { |
| 632 | const {url: safeUrl, options} = iconConfig; |
| 633 | const withCredentials = options?.withCredentials ?? false; |
| 634 | |
| 635 | if (!this._httpClient) { |
| 636 | throw getMatIconNoHttpProviderError(); |
| 637 | } |
| 638 | |
| 639 | // TODO: add an ngDevMode check |
| 640 | if (safeUrl == null) { |
| 641 | throw Error(`Cannot fetch icon from URL "${safeUrl}".`); |
| 642 | } |
| 643 | |
| 644 | const url = this._sanitizer.sanitize(SecurityContext.RESOURCE_URL, safeUrl); |
| 645 | |
| 646 | // TODO: add an ngDevMode check |
| 647 | if (!url) { |
| 648 | throw getMatIconFailedToSanitizeUrlError(safeUrl); |
| 649 | } |
| 650 | |
| 651 | // Store in-progress fetches to avoid sending a duplicate request for a URL when there is |
| 652 | // already a request in progress for that URL. It's necessary to call share() on the |
| 653 | // Observable returned by http.get() so that multiple subscribers don't cause multiple XHRs. |
| 654 | const inProgressFetch = this._inProgressUrlFetches.get(url); |
| 655 | |
| 656 | if (inProgressFetch) { |
| 657 | return inProgressFetch; |
| 658 | } |
| 659 | |
| 660 | const req = this._httpClient.get(url, {responseType: 'text', withCredentials}).pipe( |
| 661 | map(svg => { |
| 662 | // Security: This SVG is fetched from a SafeResourceUrl, and is thus |
| 663 | // trusted HTML. |
| 664 | return trustedHTMLFromString(svg); |
| 665 | }), |
| 666 | finalize(() => this._inProgressUrlFetches.delete(url)), |
| 667 | share(), |
| 668 | ); |
| 669 | |
| 670 | this._inProgressUrlFetches.set(url, req); |
| 671 | return req; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Registers an icon config by name in the specified namespace. |
no test coverage detected