Skip to content

fix(icon): handle unescaped characters in names #15678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/material/icon/fake-svgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
export const FAKE_SVGS = {
cat: '<svg><path id="meow" name="meow"></path></svg>',
dog: '<svg><path id="woof" name="woof"></path></svg>',
dogWithSpaces: '<svg><path id="woof says the dog" name="woof"></path></svg>',
farmSet1: `
<svg>
<defs>
Expand All @@ -37,6 +38,13 @@ export const FAKE_SVGS = {
</symbol>
</svg>
`,
farmSet4: `
<svg>
<defs>
<g id="pig with spaces" name="pig"><path name="oink"></path></g>
</defs>
</svg>
`,
arrows: `
<svg>
<defs>
Expand Down
4 changes: 3 additions & 1 deletion src/material/icon/icon-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,9 @@ export class MatIconRegistry implements OnDestroy {
* returns it. Returns null if no matching element is found.
*/
private _extractSvgIconFromSet(iconSet: SVGElement, iconName: string): SVGElement | null {
const iconSource = iconSet.querySelector('#' + iconName);
// Use the `id="iconName"` syntax in order to escape special
// characters in the ID (versus using the #iconName syntax).
const iconSource = iconSet.querySelector(`[id="${iconName}"]`);

if (!iconSource) {
return null;
Expand Down
23 changes: 23 additions & 0 deletions src/material/icon/icon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,29 @@ describe('MatIcon', () => {
verifyPathChildElement(svgChild, 'moo');
});

it('should handle unescape characters in icon names', () => {
iconRegistry.addSvgIconSetInNamespace('farm', trustUrl('farm-set-4.svg'));

const fixture = TestBed.createComponent(IconFromSvgName);
const testComponent = fixture.componentInstance;
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
let svgElement: any;
let svgChild: any;

testComponent.iconName = 'farm:pig with spaces';
fixture.detectChanges();
http.expectOne('farm-set-4.svg').flush(FAKE_SVGS.farmSet4);

expect(matIconElement.childNodes.length).toBe(1);
svgElement = verifyAndGetSingleSvgChild(matIconElement);
expect(svgElement.childNodes.length).toBe(1);
svgChild = svgElement.childNodes[0];
// The first <svg> child should be the <g id="pig"> element.
expect(svgChild.tagName.toLowerCase()).toBe('g');
expect(svgChild.getAttribute('name')).toBe('pig');
verifyPathChildElement(svgChild, 'oink');
});

it('should never parse the same icon set multiple times', () => {
// Normally we avoid spying on private methods like this, but the parsing is a private
// implementation detail that should not be exposed to the public API. This test, though,
Expand Down