From 1602babaccdeb43e70af70d0500aa87c6fc23d50 Mon Sep 17 00:00:00 2001 From: elenagarrone <8397116+elenagarrone@users.noreply.github.com> Date: Fri, 8 Nov 2024 10:02:13 +0000 Subject: [PATCH] fix(brand-icon): only warns when icon is not registered --- .../src/lib/brand-icon/brand-icon.component.spec.ts | 6 ++++++ .../src/lib/brand-icon/brand-icon.component.ts | 13 +++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/projects/canopy/src/lib/brand-icon/brand-icon.component.spec.ts b/projects/canopy/src/lib/brand-icon/brand-icon.component.spec.ts index f2fe9a8ff..60dddc291 100644 --- a/projects/canopy/src/lib/brand-icon/brand-icon.component.spec.ts +++ b/projects/canopy/src/lib/brand-icon/brand-icon.component.spec.ts @@ -66,6 +66,12 @@ describe('LgBrandIconComponent', () => { expect(pathEl.getAttribute('id')).not.toContain('lg-icon-fill-primary'); expect(pathEl.getAttribute('data-colour')).toContain('lg-icon-fill-primary'); }); + + it('should not throw an error when an icon is not registered', () => { + expect(() => { + component.name = 'sun'; + }).not.toThrow(); + }); }); describe('the colour input', () => { diff --git a/projects/canopy/src/lib/brand-icon/brand-icon.component.ts b/projects/canopy/src/lib/brand-icon/brand-icon.component.ts index 2081cbf6c..149b860d9 100644 --- a/projects/canopy/src/lib/brand-icon/brand-icon.component.ts +++ b/projects/canopy/src/lib/brand-icon/brand-icon.component.ts @@ -88,8 +88,10 @@ export class LgBrandIconComponent { const svgData = this.setSVGAttributes(this.iconRegistry.getBrandIcon(name)); - this.svgElement = this.svgElementFromString(svgData); - this.hostElement.nativeElement.appendChild(this.svgElement); + if (svgData) { + this.svgElement = this.svgElementFromString(svgData); + this.hostElement.nativeElement.appendChild(this.svgElement); + } } constructor( @@ -115,9 +117,12 @@ export class LgBrandIconComponent { let xlinkCount = 0; return ( + // Changes the lg-icon-fill-primary id to be a data attribute to avoid issues with duplicated ids. svgData - // Changes the lg-icon-fill-primary id to be a data attribute to avoid issues with duplicated ids. - .replace(/id="lg-icon-fill-primary"/g, () => 'data-colour="lg-icon-fill-primary"') + ?.replace( + /id="lg-icon-fill-primary"/g, + () => 'data-colour="lg-icon-fill-primary"', + ) .replace(/id="Half-tone"/g, () => 'data-colour="lg-icon-half-tone-fill"') .replace(/id="Outlines"/g, () => 'data-colour="lg-icon-outlines-fill"') .replace(/id="([^"]+)"/g, () => `id="lg-brand-icon-${this.id}-${idCount++}"`)