Skip to content

Commit 00f13cc

Browse files
crisbetoAndrewKushnir
authored andcommitted
fix(ivy): DebugNode.classes not working on SVG elements (angular#34872)
Fixes Ivy throwing an error when trying to access the `DebugNode.classes` of an SVG element. The problem is that the `className` of an SVG element is an `SVGAnimatedString`, rather than a plain string. Fixes angular#34868. PR Close angular#34872
1 parent e1fc44f commit 00f13cc

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

packages/core/src/debug/debug_node.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,14 @@ class DebugElement__POST_R3__ extends DebugNode__POST_R3__ implements DebugEleme
352352

353353
get classes(): {[key: string]: boolean;} {
354354
const result: {[key: string]: boolean;} = {};
355-
const element = this.nativeElement as HTMLElement;
356-
const classNames = element.className.split(' ');
355+
const element = this.nativeElement as HTMLElement | SVGElement;
357356

358-
classNames.forEach((value: string) => result[value] = true);
357+
// SVG elements return an `SVGAnimatedString` instead of a plain string for the `className`.
358+
const className = element.className as string | SVGAnimatedString;
359+
const classes = className && typeof className !== 'string' ? className.baseVal.split(' ') :
360+
className.split(' ');
361+
362+
classes.forEach((value: string) => result[value] = true);
359363

360364
return result;
361365
}

packages/core/test/debug/debug_node_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,22 @@ class TestCmptWithPropInterpolation {
368368
.toBeFalsy('Expected bound host CSS class "absent-class" to be absent');
369369
});
370370

371+
it('should list classes on SVG nodes', () => {
372+
// Class bindings on SVG elements require a polyfill
373+
// on IE which we don't include when running tests.
374+
if (typeof SVGElement !== 'undefined' && !('classList' in SVGElement.prototype)) {
375+
return;
376+
}
377+
378+
TestBed.overrideTemplate(TestApp, `<svg [class.foo]="true" [class.bar]="true"></svg>`);
379+
fixture = TestBed.createComponent(TestApp);
380+
fixture.detectChanges();
381+
const classes = fixture.debugElement.children[0].classes;
382+
383+
expect(classes['foo']).toBe(true);
384+
expect(classes['bar']).toBe(true);
385+
});
386+
371387
it('should list element styles', () => {
372388
fixture = TestBed.createComponent(TestApp);
373389
fixture.detectChanges();

0 commit comments

Comments
 (0)