Skip to content

Commit 45cd818

Browse files
simeonoffChronosSF
andauthored
feat(icon-service): optionally remove title and desc when caching (#10364)
Co-authored-by: Stamen Stoychev <[email protected]>
1 parent 4d3309c commit 45cd818

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

projects/igniteui-angular/src/lib/icon/icon.service.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class IgxIconService {
105105
* this.iconService.addSvgIcon('aruba', '/assets/svg/country_flags/aruba.svg', 'svg-flags');
106106
* ```
107107
*/
108-
public addSvgIcon(name: string, url: string, family = this._family) {
108+
public addSvgIcon(name: string, url: string, family = this._family, stripMeta = false) {
109109
if (name && url) {
110110
const safeUrl = this._sanitizer.bypassSecurityTrustResourceUrl(url);
111111
if (!safeUrl) {
@@ -119,7 +119,7 @@ export class IgxIconService {
119119

120120
if (!this.isSvgIconCached(name, family)) {
121121
this.fetchSvg(url).subscribe((res) => {
122-
this.cacheSvgIcon(name, res, family);
122+
this.cacheSvgIcon(name, res, family, stripMeta);
123123
this._iconLoaded.next({ name, value: res, family });
124124
});
125125
}
@@ -135,13 +135,13 @@ export class IgxIconService {
135135
* <path d="M74 74h54v54H74" /></svg>', 'svg-flags');
136136
* ```
137137
*/
138-
public addSvgIconFromText(name: string, iconText: string, family: string = '') {
138+
public addSvgIconFromText(name: string, iconText: string, family: string = '', stripMeta = false) {
139139
if (name && iconText) {
140-
if(this.isSvgIconCached(name, family)) {
140+
if (this.isSvgIconCached(name, family)) {
141141
return;
142142
}
143143

144-
this.cacheSvgIcon(name, iconText, family);
144+
this.cacheSvgIcon(name, iconText, family, stripMeta);
145145
} else {
146146
throw new Error('You should provide at least `name` and `iconText` to register an svg icon.');
147147
}
@@ -155,7 +155,7 @@ export class IgxIconService {
155155
*/
156156
public isSvgIconCached(name: string, family: string = ''): boolean {
157157
const familyClassName = this.familyClassName(family);
158-
if(this._cachedSvgIcons.has(familyClassName)) {
158+
if (this._cachedSvgIcons.has(familyClassName)) {
159159
const familyRegistry = this._cachedSvgIcons.get(familyClassName) as Map<string, SafeHtml>;
160160
return familyRegistry.has(name);
161161
}
@@ -185,7 +185,9 @@ export class IgxIconService {
185185
/**
186186
* @hidden
187187
*/
188-
private cacheSvgIcon(name: string, value: string, family: string = '') {
188+
private cacheSvgIcon(name: string, value: string, family = this._family, stripMeta: boolean) {
189+
family = !!family ? family : this._family;
190+
189191
if (name && value) {
190192
const doc = this._domParser.parseFromString(value, 'image/svg+xml');
191193
const svg = doc.querySelector('svg') as SVGElement;
@@ -197,6 +199,20 @@ export class IgxIconService {
197199
if (svg) {
198200
svg.setAttribute('fit', '');
199201
svg.setAttribute('preserveAspectRatio', 'xMidYMid meet');
202+
203+
if (stripMeta) {
204+
const title = svg.querySelector('title');
205+
const desc = svg.querySelector('desc');
206+
207+
if (title) {
208+
svg.removeChild(title);
209+
}
210+
211+
if (desc) {
212+
svg.removeChild(desc);
213+
}
214+
}
215+
200216
const safeSvg = this._sanitizer.bypassSecurityTrustHtml(svg.outerHTML);
201217
this._cachedSvgIcons.get(family).set(name, safeSvg);
202218
}

src/app/icon/icon.sample.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ export class IconSampleComponent implements OnInit {
1919
this._iconService.addSvgIcon('equals', '/assets/svg/filtering/equals.svg', 'svg-flags');
2020
this._iconService.addSvgIcon('is_empty', '/assets/svg/filtering/is_empty.svg', 'svg-flags');
2121
this._iconService.addSvgIcon('starts_with', '/assets/svg/filtering/starts_with.svg', 'svg-flags');
22-
this._iconService.addSvgIcon('copy', '/assets/svg/filtering/copy.svg');
22+
this._iconService.addSvgIcon('copy', '/assets/svg/filtering/copy.svg', '', true);
2323
}
2424
}

src/assets/svg/filtering/copy.svg

+2
Loading

0 commit comments

Comments
 (0)