Skip to content

Commit 8b69db0

Browse files
crisbetojelbourn
authored andcommitted
refactor(observers): allow ElementRef to be passed to content observer (#12762)
Allows for the `ContentObserver` to accept an `ElementRef`, in addition to an `Element`. This is more convenient since we deal with element refs most of the time anyway.
1 parent 52473f5 commit 8b69db0

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/cdk/a11y/live-announcer/live-announcer.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,11 @@ export class CdkAriaLive implements OnDestroy {
117117
this._subscription.unsubscribe();
118118
this._subscription = null;
119119
}
120-
} else {
121-
if (!this._subscription) {
122-
this._subscription = this._ngZone.runOutsideAngular(
123-
() => this._contentObserver.observe(this._elementRef.nativeElement).subscribe(
124-
() => this._liveAnnouncer.announce(
125-
this._elementRef.nativeElement.innerText, this._politeness)));
126-
}
120+
} else if (!this._subscription) {
121+
this._subscription = this._ngZone.runOutsideAngular(
122+
() => this._contentObserver.observe(this._elementRef).subscribe(
123+
() => this._liveAnnouncer.announce(
124+
this._elementRef.nativeElement.innerText, this._politeness)));
127125
}
128126
}
129127
private _politeness: AriaLivePoliteness = 'off';

src/cdk/observers/observe-content.spec.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import {Component, ElementRef, ViewChild} from '@angular/core';
22
import {async, ComponentFixture, fakeAsync, inject, TestBed, tick} from '@angular/core/testing';
33
import {ContentObserver, MutationObserverFactory, ObserversModule} from './observe-content';
44

5-
// TODO(elad): `ProxyZone` doesn't seem to capture the events raised by
6-
// `MutationObserver` and needs to be investigated
7-
85
describe('Observe content directive', () => {
96
describe('basic usage', () => {
107
beforeEach(async(() => {
@@ -159,7 +156,7 @@ describe('ContentObserver injectable', () => {
159156
const fixture = TestBed.createComponent(UnobservedComponentWithTextContent);
160157
fixture.detectChanges();
161158

162-
contentObserver.observe(fixture.componentInstance.contentEl.nativeElement)
159+
contentObserver.observe(fixture.componentInstance.contentEl)
163160
.subscribe(() => spy());
164161

165162
expect(spy).not.toHaveBeenCalled();
@@ -177,9 +174,9 @@ describe('ContentObserver injectable', () => {
177174
const fixture = TestBed.createComponent(UnobservedComponentWithTextContent);
178175
fixture.detectChanges();
179176

180-
const sub1 = contentObserver.observe(fixture.componentInstance.contentEl.nativeElement)
177+
const sub1 = contentObserver.observe(fixture.componentInstance.contentEl)
181178
.subscribe(() => spy());
182-
contentObserver.observe(fixture.componentInstance.contentEl.nativeElement)
179+
contentObserver.observe(fixture.componentInstance.contentEl)
183180
.subscribe(() => spy());
184181

185182
expect(mof.create).toHaveBeenCalledTimes(1);

src/cdk/observers/observe-content.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,17 @@ export class ContentObserver implements OnDestroy {
5454
* Observe content changes on an element.
5555
* @param element The element to observe for content changes.
5656
*/
57-
observe(element: Element): Observable<MutationRecord[]> {
57+
observe(element: Element): Observable<MutationRecord[]>;
58+
59+
/**
60+
* Observe content changes on an element.
61+
* @param element The element to observe for content changes.
62+
*/
63+
observe(element: ElementRef<Element>): Observable<MutationRecord[]>;
64+
65+
observe(elementOrRef: Element | ElementRef<Element>): Observable<MutationRecord[]> {
66+
const element = elementOrRef instanceof ElementRef ? elementOrRef.nativeElement : elementOrRef;
67+
5868
return Observable.create(observer => {
5969
const stream = this._observeElement(element);
6070
const subscription = stream.subscribe(observer);
@@ -169,7 +179,7 @@ export class CdkObserveContent implements AfterContentInit, OnDestroy {
169179

170180
private _subscribe() {
171181
this._unsubscribe();
172-
const stream = this._contentObserver.observe(this._elementRef.nativeElement);
182+
const stream = this._contentObserver.observe(this._elementRef);
173183

174184
// TODO(mmalerba): We shouldn't be emitting on this @Output() outside the zone.
175185
// Consider brining it back inside the zone next time we're making breaking changes.

0 commit comments

Comments
 (0)