Skip to content

fix(platform): change isBrowser check to use Angular PLATFORM_ID #10659

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

Merged
merged 1 commit into from
Apr 11, 2018
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
5 changes: 3 additions & 2 deletions src/cdk/a11y/focus-trap/focus-trap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Platform} from '@angular/cdk/platform';
import {Component, ViewChild} from '@angular/core';
import {Component, PLATFORM_ID, ViewChild} from '@angular/core';
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {A11yModule, FocusTrap, CdkTrapFocus} from '../index';

Expand Down Expand Up @@ -47,8 +47,9 @@ describe('FocusTrap', () => {
// focus event handler directly.
const result = focusTrapInstance.focusLastTabbableElement();

const platformId = TestBed.get(PLATFORM_ID);
// In iOS button elements are never tabbable, so the last element will be the input.
const lastElement = new Platform().IOS ? 'input' : 'button';
const lastElement = new Platform(platformId).IOS ? 'input' : 'button';

expect(document.activeElement.nodeName.toLowerCase())
.toBe(lastElement, `Expected ${lastElement} element to be focused`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {InteractivityChecker} from './interactivity-checker';
describe('InteractivityChecker', () => {
let testContainerElement: HTMLElement;
let checker: InteractivityChecker;
// TODO: refactor this to be injected with the platformId
// Needs to be done carefully due to the runIf checks below executing
// before injection
let platform: Platform = new Platform();

beforeEach(() => {
Expand Down
6 changes: 4 additions & 2 deletions src/cdk/overlay/scroll/block-scroll-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Overlay, OverlayContainer, OverlayModule, OverlayRef, OverlayConfig} fro


describe('BlockScrollStrategy', () => {
let platform = new Platform();
let platform: Platform;
let viewport: ViewportRuler;
let overlayRef: OverlayRef;
let componentPortal: ComponentPortal<FocacciaMsg>;
Expand All @@ -22,7 +22,8 @@ describe('BlockScrollStrategy', () => {
}).compileComponents();
}));

beforeEach(inject([Overlay, ViewportRuler], (overlay: Overlay, viewportRuler: ViewportRuler) => {
beforeEach(inject([Overlay, ViewportRuler, Platform],
(overlay: Overlay, viewportRuler: ViewportRuler, _platform: Platform) => {
let overlayConfig = new OverlayConfig({scrollStrategy: overlay.scrollStrategies.block()});

overlayRef = overlay.create(overlayConfig);
Expand All @@ -34,6 +35,7 @@ describe('BlockScrollStrategy', () => {
forceScrollElement.style.width = '100px';
forceScrollElement.style.height = '3000px';
forceScrollElement.style.background = 'rebeccapurple';
platform = _platform;
}));

afterEach(inject([OverlayContainer], (container: OverlayContainer) => {
Expand Down
22 changes: 18 additions & 4 deletions src/cdk/platform/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Injectable} from '@angular/core';
import {Inject, Injectable, Optional, PLATFORM_ID} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';


// Whether the current platform supports the V8 Break Iterator. The V8 check
Expand All @@ -19,8 +20,14 @@ const hasV8BreakIterator = (typeof Intl !== 'undefined' && (Intl as any).v8Break
*/
@Injectable({providedIn: 'root'})
export class Platform {
/** Whether the Angular application is being rendered in the browser. */
isBrowser: boolean = typeof document === 'object' && !!document;
/**
* Whether the Angular application is being rendered in the browser.
* We want to use the Angular platform check because if the Document is shimmed
* without the navigator, the following checks will fail. This is preferred because
* sometimes the Document may be shimmed without the user's knowledge or intention
*/
isBrowser: boolean = this._platformId ?
isPlatformBrowser(this._platformId) : typeof document === 'object' && !!document;

/** Whether the current browser is Microsoft Edge. */
EDGE: boolean = this.isBrowser && /(edge)/i.test(navigator.userAgent);
Expand All @@ -31,7 +38,7 @@ export class Platform {
/** Whether the current rendering engine is Blink. */
// EdgeHTML and Trident mock Blink specific things and need to be excluded from this check.
BLINK: boolean = this.isBrowser && (!!((window as any).chrome || hasV8BreakIterator) &&
typeof CSS !== 'undefined' && !this.EDGE && !this.TRIDENT);
typeof CSS !== 'undefined' && !this.EDGE && !this.TRIDENT);

/** Whether the current rendering engine is WebKit. */
// Webkit is part of the userAgent in EdgeHTML, Blink and Trident. Therefore we need to
Expand Down Expand Up @@ -59,4 +66,11 @@ export class Platform {
// this and just place the Safari keyword in the userAgent. To be more safe about Safari every
// Safari browser should also use Webkit as its layout engine.
SAFARI: boolean = this.isBrowser && /safari/i.test(navigator.userAgent) && this.WEBKIT;

/**
* @deletion-target v7.0.0 remove optional decorator
*/
constructor(@Optional() @Inject(PLATFORM_ID) private _platformId?: Object) {
}
}

6 changes: 4 additions & 2 deletions src/lib/core/datetime/native-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const SUPPORTS_INTL = typeof Intl != 'undefined';


describe('NativeDateAdapter', () => {
const platform = new Platform();
let platform: Platform;
let adapter: NativeDateAdapter;
let assertValidDate: (d: Date | null, valid: boolean) => void;

Expand All @@ -18,8 +18,10 @@ describe('NativeDateAdapter', () => {
}).compileComponents();
}));

beforeEach(inject([DateAdapter], (dateAdapter: NativeDateAdapter) => {
beforeEach(inject([DateAdapter, Platform],
(dateAdapter: NativeDateAdapter, _platform: Platform) => {
adapter = dateAdapter;
platform = _platform;

assertValidDate = (d: Date | null, valid: boolean) => {
expect(adapter.isDateInstance(d)).not.toBeNull(`Expected ${d} to be a date instance`);
Expand Down