Skip to content
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

fix(focus-trap): exception when element contains SVG on IE #3432

Merged
merged 1 commit into from
Mar 7, 2017
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
36 changes: 35 additions & 1 deletion src/lib/core/a11y/focus-trap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ describe('FocusTrap', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [FocusTrapDirective, FocusTrapWithBindings, SimpleFocusTrap, FocusTrapTargets],
declarations: [
FocusTrapDirective,
FocusTrapWithBindings,
SimpleFocusTrap,
FocusTrapTargets,
FocusTrapWithSvg
],
providers: [InteractivityChecker, Platform, FocusTrapFactory]
});

Expand Down Expand Up @@ -112,6 +118,20 @@ describe('FocusTrap', () => {
expect(document.activeElement.id).toBe('last');
});
});

describe('special cases', () => {
it('should not throw when it has a SVG child', () => {
let fixture = TestBed.createComponent(FocusTrapWithSvg);

fixture.detectChanges();

let focusTrapInstance = fixture.componentInstance.focusTrapDirective.focusTrap;

expect(() => focusTrapInstance.focusFirstTabbableElement()).not.toThrow();
expect(() => focusTrapInstance.focusLastTabbableElement()).not.toThrow();
});
});

});


Expand Down Expand Up @@ -156,3 +176,17 @@ class FocusTrapWithBindings {
class FocusTrapTargets {
@ViewChild(FocusTrapDirective) focusTrapDirective: FocusTrapDirective;
}


@Component({
template: `
<div cdkTrapFocus>
<svg xmlns="http://www.w3.org/2000/svg">
<circle cx="100" cy="100" r="100"/>
</svg>
</div>
`
})
class FocusTrapWithSvg {
@ViewChild(FocusTrapDirective) focusTrapDirective: FocusTrapDirective;
}
22 changes: 16 additions & 6 deletions src/lib/core/a11y/focus-trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,15 @@ export class FocusTrap {
return root;
}

// Iterate in DOM order.
let childCount = root.children.length;
for (let i = 0; i < childCount; i++) {
let tabbableChild = this._getFirstTabbableElement(root.children[i] as HTMLElement);
// Iterate in DOM order. Note that IE doesn't have `children` for SVG so we fall
// back to `childNodes` which includes text nodes, comments etc.
let children = root.children || root.childNodes;

for (let i = 0; i < children.length; i++) {
let tabbableChild = children[i].nodeType === Node.ELEMENT_NODE ?
this._getFirstTabbableElement(children[i] as HTMLElement) :
null;

if (tabbableChild) {
return tabbableChild;
}
Expand All @@ -149,8 +154,13 @@ export class FocusTrap {
}

// Iterate in reverse DOM order.
for (let i = root.children.length - 1; i >= 0; i--) {
let tabbableChild = this._getLastTabbableElement(root.children[i] as HTMLElement);
let children = root.children || root.childNodes;

for (let i = children.length - 1; i >= 0; i--) {
let tabbableChild = children[i].nodeType === Node.ELEMENT_NODE ?
this._getLastTabbableElement(children[i] as HTMLElement) :
null;

if (tabbableChild) {
return tabbableChild;
}
Expand Down