Skip to content

fix(icon): not copying attributes from symbol nodes #16896

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
Sep 9, 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
7 changes: 7 additions & 0 deletions src/material/icon/fake-svgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export const FAKE_SVGS = {
</defs>
</svg>
`,
farmSet5: `
<svg>
<symbol id="duck" viewBox="0 0 13 37">
<path id="quack" name="quack"></path>
</symbol>
</svg>
`,
arrows: `
<svg>
<defs>
Expand Down
10 changes: 10 additions & 0 deletions src/material/icon/icon-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,16 @@ export class MatIconRegistry implements OnDestroy {
*/
private _toSvgElement(element: Element): SVGElement {
const svg = this._svgElementFromString('<svg></svg>');
const attributes = element.attributes;

// Copy over all the attributes from the `symbol` to the new SVG, except the id.
for (let i = 0; i < attributes.length; i++) {
const {name, value} = attributes[i];

if (name !== 'id') {
svg.setAttribute(name, value);
}
}

for (let i = 0; i < element.childNodes.length; i++) {
if (element.childNodes[i].nodeType === this._document.ELEMENT_NODE) {
Expand Down
17 changes: 17 additions & 0 deletions src/material/icon/icon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,23 @@ describe('MatIcon', () => {
expect((firstChild as HTMLElement).getAttribute('name')).toBe('quack');
});

it('should copy over the attributes when unwrapping <symbol> nodes', () => {
iconRegistry.addSvgIconSetInNamespace('farm', trustUrl('farm-set-5.svg'));

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

testComponent.iconName = 'farm:duck';
fixture.detectChanges();
http.expectOne('farm-set-5.svg').flush(FAKE_SVGS.farmSet5);

const svgElement = verifyAndGetSingleSvgChild(matIconElement);
expect(svgElement.getAttribute('viewBox')).toBe('0 0 13 37');
expect(svgElement.getAttribute('id')).toBeFalsy();
expect(svgElement.querySelector('symbol')).toBeFalsy();
});

it('should not wrap <svg> elements in icon sets in another svg tag', () => {
iconRegistry.addSvgIconSet(trustUrl('arrow-set.svg'));

Expand Down