From 97e35dfb959a3cdb5afa3d71dc32dec9ab1c88dd Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 12 Jun 2017 22:30:16 +0200 Subject: [PATCH] fix(input,cdk): a couple of server-side rendering errors (#5066) --- src/lib/core/observe-content/observe-content.ts | 14 ++++++++------ src/lib/input/input-container.ts | 10 ++++++++-- src/universal-app/kitchen-sink/kitchen-sink.html | 4 ++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/lib/core/observe-content/observe-content.ts b/src/lib/core/observe-content/observe-content.ts index 5c410be4dcf8..102a24674e0c 100644 --- a/src/lib/core/observe-content/observe-content.ts +++ b/src/lib/core/observe-content/observe-content.ts @@ -19,7 +19,7 @@ import 'rxjs/add/operator/debounceTime'; @Injectable() export class MdMutationObserverFactory { create(callback): MutationObserver { - return new MutationObserver(callback); + return typeof MutationObserver === 'undefined' ? null : new MutationObserver(callback); } } @@ -59,11 +59,13 @@ export class ObserveContent implements AfterContentInit, OnDestroy { this._debouncer.next(mutations); }); - this._observer.observe(this._elementRef.nativeElement, { - characterData: true, - childList: true, - subtree: true - }); + if (this._observer) { + this._observer.observe(this._elementRef.nativeElement, { + characterData: true, + childList: true, + subtree: true + }); + } } ngOnDestroy() { diff --git a/src/lib/input/input-container.ts b/src/lib/input/input-container.ts index 3f191a846b89..85bc55c66a1c 100644 --- a/src/lib/input/input-container.ts +++ b/src/lib/input/input-container.ts @@ -20,7 +20,7 @@ import { Inject } from '@angular/core'; import {animate, state, style, transition, trigger} from '@angular/animations'; -import {coerceBooleanProperty} from '../core'; +import {coerceBooleanProperty, Platform} from '../core'; import {FormGroupDirective, NgControl, NgForm} from '@angular/forms'; import {getSupportedInputTypes} from '../core/platform/features'; import { @@ -213,6 +213,7 @@ export class MdInputDirective { constructor(private _elementRef: ElementRef, private _renderer: Renderer2, + private _platform: Platform, @Optional() @Self() public _ngControl: NgControl, @Optional() private _parentForm: NgForm, @Optional() private _parentFormGroup: FormGroupDirective) { @@ -267,7 +268,12 @@ export class MdInputDirective { /** Determines if the component host is a textarea. If not recognizable it returns false. */ private _isTextarea() { let nativeElement = this._elementRef.nativeElement; - return nativeElement ? nativeElement.nodeName.toLowerCase() === 'textarea' : false; + + // In Universal, we don't have access to `nodeName`, but the same can be achieved with `name`. + // Note that this shouldn't be necessary once Angular switches to an API that resembles the + // DOM closer. + let nodeName = this._platform.isBrowser ? nativeElement.nodeName : nativeElement.name; + return nodeName ? nodeName.toLowerCase() === 'textarea' : false; } } diff --git a/src/universal-app/kitchen-sink/kitchen-sink.html b/src/universal-app/kitchen-sink/kitchen-sink.html index ba06fe8a5037..fa237cca4f67 100644 --- a/src/universal-app/kitchen-sink/kitchen-sink.html +++ b/src/universal-app/kitchen-sink/kitchen-sink.html @@ -86,7 +86,7 @@

Chips

Datepicker

- + @@ -106,7 +106,7 @@

Icon

Input

- + .00 Dolla dolla bills