Skip to content

Commit f4202bd

Browse files
crisbetojelbourn
authored andcommitted
fix(focus-trap): server-side rendering error (#9001)
Fixes a server-side rendering error due to the `Node` class not being being available in Universal. Fixes #8981.
1 parent 04411ea commit f4202bd

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/cdk/a11y/focus-trap.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ import {take} from 'rxjs/operators/take';
2121
import {InteractivityChecker} from './interactivity-checker';
2222
import {DOCUMENT} from '@angular/common';
2323

24+
/**
25+
* Node type of element nodes. Used instead of Node.ELEMENT_NODE
26+
* which is unsupported in Universal.
27+
* @docs-private
28+
*/
29+
const ELEMENT_NODE_TYPE = 1;
2430

2531
/**
2632
* Class that allows for trapping focus within a DOM element.
@@ -223,7 +229,7 @@ export class FocusTrap {
223229
let children = root.children || root.childNodes;
224230

225231
for (let i = 0; i < children.length; i++) {
226-
let tabbableChild = children[i].nodeType === Node.ELEMENT_NODE ?
232+
let tabbableChild = children[i].nodeType === ELEMENT_NODE_TYPE ?
227233
this._getFirstTabbableElement(children[i] as HTMLElement) :
228234
null;
229235

@@ -245,7 +251,7 @@ export class FocusTrap {
245251
let children = root.children || root.childNodes;
246252

247253
for (let i = children.length - 1; i >= 0; i--) {
248-
let tabbableChild = children[i].nodeType === Node.ELEMENT_NODE ?
254+
let tabbableChild = children[i].nodeType === ELEMENT_NODE_TYPE ?
249255
this._getLastTabbableElement(children[i] as HTMLElement) :
250256
null;
251257

src/lib/icon/icon-registry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ export class MatIconRegistry {
421421
let svg = this._svgElementFromString('<svg></svg>');
422422

423423
for (let i = 0; i < element.childNodes.length; i++) {
424-
if (element.childNodes[i].nodeType === Node.ELEMENT_NODE) {
424+
// Note: 1 corresponds to `Node.ELEMENT_NODE` which we can't use in Universal.
425+
if (element.childNodes[i].nodeType === 1) {
425426
svg.appendChild(element.childNodes[i].cloneNode(true));
426427
}
427428
}

src/universal-app/kitchen-sink/kitchen-sink.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ <h2>Select</h2>
177177

178178
<h2>Sidenav</h2>
179179
<mat-sidenav-container>
180-
<mat-sidenav opened>On the side</mat-sidenav>
180+
<mat-sidenav opened>
181+
On the side
182+
<button>Button for testing focus trapping</button>
183+
</mat-sidenav>
181184
Main content
182185
<button>Click me</button>
183186
</mat-sidenav-container>

0 commit comments

Comments
 (0)