Skip to content

Commit

Permalink
fix(core): allow to use the Renderer outside of views. (angular#14882)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbosch authored and Zhicheng Wang committed Aug 11, 2017
1 parent 3a9bcbf commit 5cd007f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
29 changes: 19 additions & 10 deletions modules/@angular/core/src/view/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ function callWithDebugContext(action: DebugAction, fn: any, self: any, args: any
}

export function getCurrentDebugContext(): DebugContext {
return new DebugContext_(_currentView, _currentNodeIndex);
return _currentView ? new DebugContext_(_currentView, _currentNodeIndex) : null;
}


Expand Down Expand Up @@ -440,23 +440,30 @@ class DebugRendererV2 implements RendererV2 {

createElement(name: string, namespace?: string): any {
const el = this.delegate.createElement(name, namespace);
const debugEl = new DebugElement(el, null, getCurrentDebugContext());
debugEl.name = name;
indexDebugNode(debugEl);
const debugCtx = getCurrentDebugContext();
if (debugCtx) {
const debugEl = new DebugElement(el, null, debugCtx);
debugEl.name = name;
indexDebugNode(debugEl);
}
return el;
}

createComment(value: string): any {
const comment = this.delegate.createComment(value);
const debugEl = new DebugNode(comment, null, getCurrentDebugContext());
indexDebugNode(debugEl);
const debugCtx = getCurrentDebugContext();
if (debugCtx) {
indexDebugNode(new DebugNode(comment, null, debugCtx));
}
return comment;
}

createText(value: string): any {
const text = this.delegate.createText(value);
const debugEl = new DebugNode(text, null, getCurrentDebugContext());
indexDebugNode(debugEl);
const debugCtx = getCurrentDebugContext();
if (debugCtx) {
indexDebugNode(new DebugNode(text, null, debugCtx));
}
return text;
}

Expand Down Expand Up @@ -491,8 +498,10 @@ class DebugRendererV2 implements RendererV2 {

selectRootElement(selectorOrNode: string|any): any {
const el = this.delegate.selectRootElement(selectorOrNode);
const debugEl = new DebugElement(el, null, getCurrentDebugContext());
indexDebugNode(debugEl);
const debugCtx = getCurrentDebugContext();
if (debugCtx) {
indexDebugNode(new DebugElement(el, null, debugCtx));
}
return el;
}

Expand Down
15 changes: 14 additions & 1 deletion modules/@angular/core/test/linker/regression_integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ANALYZE_FOR_ENTRY_COMPONENTS, Component, InjectionToken, Injector, Pipe, PipeTransform, Provider} from '@angular/core';
import {ANALYZE_FOR_ENTRY_COMPONENTS, Component, InjectionToken, Injector, Pipe, PipeTransform, Provider, RendererV2} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {expect} from '@angular/platform-browser/testing/matchers';

Expand Down Expand Up @@ -209,6 +209,19 @@ function declareTests({useJit}: {useJit: boolean}) {
const fixture = TestBed.createComponent(MainComponent);
expect(fixture.nativeElement).toHaveText('I was saved by my hero from a villian.');
});

it('should allow to use the renderer outside of views', () => {
@Component({template: ''})
class MyComp {
constructor(public renderer: RendererV2) {}
}

TestBed.configureTestingModule({declarations: [MyComp]});
const ctx = TestBed.createComponent(MyComp);

const txtNode = ctx.componentInstance.renderer.createText('test');
expect(txtNode).toHaveText('test');
});
});
}

Expand Down

0 comments on commit 5cd007f

Please sign in to comment.