Skip to content

Commit 5509c23

Browse files
crisbetommalerba
authored andcommitted
fix(overlay): error when trying to add/remove empty string cla… (#14919)
Fixes an error being thrown if an empty string is passed as one of the values to `addPanelClass` or `removePanelClass`.
1 parent bebb9ff commit 5509c23

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/cdk/overlay/overlay-ref.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,10 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
480480

481481
coerceArray(cssClasses).forEach(cssClass => {
482482
// We can't do a spread here, because IE doesn't support setting multiple classes.
483-
isAdd ? classList.add(cssClass) : classList.remove(cssClass);
483+
// Also trying to add an empty string to a DOMTokenList will throw.
484+
if (cssClass) {
485+
isAdd ? classList.add(cssClass) : classList.remove(cssClass);
486+
}
484487
});
485488
}
486489

src/cdk/overlay/overlay.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,14 @@ describe('Overlay', () => {
419419
expect(() => overlayRef.addPanelClass('custom-class-two')).not.toThrowError();
420420
});
421421

422+
it('should not throw when trying to add or remove and empty string class', () => {
423+
const overlayRef = overlay.create();
424+
overlayRef.attach(componentPortal);
425+
426+
expect(() => overlayRef.addPanelClass('')).not.toThrow();
427+
expect(() => overlayRef.removePanelClass('')).not.toThrow();
428+
});
429+
422430
describe('positioning', () => {
423431
let config: OverlayConfig;
424432

0 commit comments

Comments
 (0)