diff --git a/src/lib/icon/fake-svgs.ts b/src/lib/icon/fake-svgs.ts
index 6d38254123ca..1148f2387584 100644
--- a/src/lib/icon/fake-svgs.ts
+++ b/src/lib/icon/fake-svgs.ts
@@ -8,39 +8,40 @@
/**
* Fake URLs and associated SVG documents used by tests.
+ * The ID attribute is used to load the icons, the name attribute is only used for testing.
* @docs-private
*/
export const FAKE_SVGS = {
- cat: '',
- dog: '',
+ cat: '',
+ dog: '',
farmSet1: `
`,
farmSet2: `
`,
farmSet3: `
`,
arrows: `
`
};
diff --git a/src/lib/icon/icon-registry.ts b/src/lib/icon/icon-registry.ts
index 6d054db8a7f7..828ab35e18d3 100644
--- a/src/lib/icon/icon-registry.ts
+++ b/src/lib/icon/icon-registry.ts
@@ -365,23 +365,28 @@ export class MatIconRegistry {
* returns it. Returns null if no matching element is found.
*/
private _extractSvgIconFromSet(iconSet: SVGElement, iconName: string): SVGElement | null {
- const iconNode = iconSet.querySelector('#' + iconName);
+ const iconSource = iconSet.querySelector('#' + iconName);
- if (!iconNode) {
+ if (!iconSource) {
return null;
}
+ // Clone the element and remove the ID to prevent multiple elements from being added
+ // to the page with the same ID.
+ const iconElement = iconSource.cloneNode(true) as Element;
+ iconElement.id = '';
+
// If the icon node is itself an node, clone and return it directly. If not, set it as
// the content of a new node.
- if (iconNode.tagName.toLowerCase() === 'svg') {
- return this._setSvgAttributes(iconNode.cloneNode(true) as SVGElement);
+ if (iconElement.nodeName.toLowerCase() === 'svg') {
+ return this._setSvgAttributes(iconElement as SVGElement);
}
// If the node is a , it won't be rendered so we have to convert it into . Note
// that the same could be achieved by referring to it via