Skip to content

Commit 4d58576

Browse files
committed
fix(clipboard): not restoring focus to SVG elements
When the clipboard is copying something, focus is temporarily moved to a `textarea` and moved back. The logic that moves it checks whether the previous element was an `HTMLElement`, but that doesn't cover returning focus to an SVG element.
1 parent 9262c63 commit 4d58576

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/cdk/clipboard/clipboard.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ describe('Clipboard', () => {
6868
expect(document.activeElement).toBe(focusedInput);
6969
});
7070

71+
it('does not move focus away from focused SVG element', () => {
72+
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
73+
svg.setAttribute('focusable', 'true');
74+
svg.setAttribute('tabindex', '0');
75+
document.body.appendChild(svg);
76+
77+
svg.focus();
78+
expect(document.activeElement).toBe(svg);
79+
80+
expect(clipboard.copy(COPY_CONTENT)).toBe(true);
81+
82+
expect(document.activeElement).toBe(svg);
83+
});
84+
7185
describe('when execCommand fails', () => {
7286
beforeEach(() => {
7387
execCommand.and.throwError('could not copy');

src/cdk/clipboard/pending-copy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ export class PendingCopy {
4343

4444
try { // Older browsers could throw if copy is not supported.
4545
if (textarea) {
46-
const currentFocus = this._document.activeElement;
46+
const currentFocus = this._document.activeElement as HTMLOrSVGElement | null;
4747

4848
textarea.select();
4949
textarea.setSelectionRange(0, textarea.value.length);
5050
successful = this._document.execCommand('copy');
5151

52-
if (currentFocus && currentFocus instanceof HTMLElement) {
52+
if (currentFocus) {
5353
currentFocus.focus();
5454
}
5555
}

0 commit comments

Comments
 (0)