From 45cd8188a6990f73cb65ead22c6dc9e9882f2b90 Mon Sep 17 00:00:00 2001 From: Simeon Simeonoff Date: Thu, 28 Oct 2021 11:28:25 +0300 Subject: [PATCH] feat(icon-service): optionally remove title and desc when caching (#10364) Co-authored-by: Stamen Stoychev --- .../src/lib/icon/icon.service.ts | 30 ++++++++++++++----- src/app/icon/icon.sample.ts | 2 +- src/assets/svg/filtering/copy.svg | 2 ++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/projects/igniteui-angular/src/lib/icon/icon.service.ts b/projects/igniteui-angular/src/lib/icon/icon.service.ts index bab19ccea38..f9c5ec77c63 100644 --- a/projects/igniteui-angular/src/lib/icon/icon.service.ts +++ b/projects/igniteui-angular/src/lib/icon/icon.service.ts @@ -105,7 +105,7 @@ export class IgxIconService { * this.iconService.addSvgIcon('aruba', '/assets/svg/country_flags/aruba.svg', 'svg-flags'); * ``` */ - public addSvgIcon(name: string, url: string, family = this._family) { + public addSvgIcon(name: string, url: string, family = this._family, stripMeta = false) { if (name && url) { const safeUrl = this._sanitizer.bypassSecurityTrustResourceUrl(url); if (!safeUrl) { @@ -119,7 +119,7 @@ export class IgxIconService { if (!this.isSvgIconCached(name, family)) { this.fetchSvg(url).subscribe((res) => { - this.cacheSvgIcon(name, res, family); + this.cacheSvgIcon(name, res, family, stripMeta); this._iconLoaded.next({ name, value: res, family }); }); } @@ -135,13 +135,13 @@ export class IgxIconService { * ', 'svg-flags'); * ``` */ - public addSvgIconFromText(name: string, iconText: string, family: string = '') { + public addSvgIconFromText(name: string, iconText: string, family: string = '', stripMeta = false) { if (name && iconText) { - if(this.isSvgIconCached(name, family)) { + if (this.isSvgIconCached(name, family)) { return; } - this.cacheSvgIcon(name, iconText, family); + this.cacheSvgIcon(name, iconText, family, stripMeta); } else { throw new Error('You should provide at least `name` and `iconText` to register an svg icon.'); } @@ -155,7 +155,7 @@ export class IgxIconService { */ public isSvgIconCached(name: string, family: string = ''): boolean { const familyClassName = this.familyClassName(family); - if(this._cachedSvgIcons.has(familyClassName)) { + if (this._cachedSvgIcons.has(familyClassName)) { const familyRegistry = this._cachedSvgIcons.get(familyClassName) as Map; return familyRegistry.has(name); } @@ -185,7 +185,9 @@ export class IgxIconService { /** * @hidden */ - private cacheSvgIcon(name: string, value: string, family: string = '') { + private cacheSvgIcon(name: string, value: string, family = this._family, stripMeta: boolean) { + family = !!family ? family : this._family; + if (name && value) { const doc = this._domParser.parseFromString(value, 'image/svg+xml'); const svg = doc.querySelector('svg') as SVGElement; @@ -197,6 +199,20 @@ export class IgxIconService { if (svg) { svg.setAttribute('fit', ''); svg.setAttribute('preserveAspectRatio', 'xMidYMid meet'); + + if (stripMeta) { + const title = svg.querySelector('title'); + const desc = svg.querySelector('desc'); + + if (title) { + svg.removeChild(title); + } + + if (desc) { + svg.removeChild(desc); + } + } + const safeSvg = this._sanitizer.bypassSecurityTrustHtml(svg.outerHTML); this._cachedSvgIcons.get(family).set(name, safeSvg); } diff --git a/src/app/icon/icon.sample.ts b/src/app/icon/icon.sample.ts index 743d15d4b05..e541d70e512 100644 --- a/src/app/icon/icon.sample.ts +++ b/src/app/icon/icon.sample.ts @@ -19,6 +19,6 @@ export class IconSampleComponent implements OnInit { this._iconService.addSvgIcon('equals', '/assets/svg/filtering/equals.svg', 'svg-flags'); this._iconService.addSvgIcon('is_empty', '/assets/svg/filtering/is_empty.svg', 'svg-flags'); this._iconService.addSvgIcon('starts_with', '/assets/svg/filtering/starts_with.svg', 'svg-flags'); - this._iconService.addSvgIcon('copy', '/assets/svg/filtering/copy.svg'); + this._iconService.addSvgIcon('copy', '/assets/svg/filtering/copy.svg', '', true); } } diff --git a/src/assets/svg/filtering/copy.svg b/src/assets/svg/filtering/copy.svg index 9837dbd779f..c76fc91d140 100644 --- a/src/assets/svg/filtering/copy.svg +++ b/src/assets/svg/filtering/copy.svg @@ -1,3 +1,5 @@ + Some Title + Some Description