2017-12-01 17:23:03 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2018-09-21 21:38:13 -04:00
|
|
|
import {ChangeDetectorRef} from '@angular/core/src/change_detection/change_detector_ref';
|
|
|
|
import {ElementRef} from '@angular/core/src/linker/element_ref';
|
|
|
|
import {TemplateRef} from '@angular/core/src/linker/template_ref';
|
|
|
|
import {ViewContainerRef} from '@angular/core/src/linker/view_container_ref';
|
2018-10-10 09:53:14 -04:00
|
|
|
import {Renderer2} from '@angular/core/src/render/api';
|
2018-01-03 05:42:48 -05:00
|
|
|
import {stringifyElement} from '@angular/platform-browser/testing/src/browser_util';
|
2018-01-09 19:43:12 -05:00
|
|
|
|
refactor(ivy): obviate the Bazel component of the ivy_switch (#26550)
Originally, the ivy_switch mechanism used Bazel genrules to conditionally
compile one TS file or another depending on whether ngc or ngtsc was the
selected compiler. This was done because we wanted to avoid importing
certain modules (and thus pulling them into the build) if Ivy was on or
off. This mechanism had a major drawback: ivy_switch became a bottleneck
in the import graph, as it both imports from many places in the codebase
and is imported by many modules in the codebase. This frequently resulted
in cyclic imports which caused issues both with TS and Closure compilation.
It turns out ngcc needs both code paths in the bundle to perform the switch
during its operation anyway, so import switching was later abandoned. This
means that there's no real reason why the ivy_switch mechanism needed to
operate at the Bazel level, and for the ivy_switch file to be a bottleneck.
This commit removes the Bazel-level ivy_switch mechanism, and introduces
an additional TypeScript transform in ngtsc (and the pass-through tsc
compiler used for testing JIT) to perform the same operation that ngcc
does, and flip the switch during ngtsc compilation. This allows the
ivy_switch file to be removed, and the individual switches to be located
directly next to their consumers in the codebase, greatly mitigating the
circular import issues and making the mechanism much easier to use.
As part of this commit, the tag for marking switched variables was changed
from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which
flips these tags. Most variables were renamed from R3_* to SWITCH_* as well,
since they're referenced mostly in render2 code.
Test strategy: existing test coverage is more than sufficient - if this
didn't work correctly it would break the hello world and todo apps.
PR Close #26550
2018-10-17 18:44:44 -04:00
|
|
|
import {SWITCH_CHANGE_DETECTOR_REF_FACTORY__POST_R3__ as R3_CHANGE_DETECTOR_REF_FACTORY} from '../../src/change_detection/change_detector_ref';
|
2018-04-12 18:54:16 -04:00
|
|
|
import {Injector} from '../../src/di/injector';
|
refactor(ivy): obviate the Bazel component of the ivy_switch (#26550)
Originally, the ivy_switch mechanism used Bazel genrules to conditionally
compile one TS file or another depending on whether ngc or ngtsc was the
selected compiler. This was done because we wanted to avoid importing
certain modules (and thus pulling them into the build) if Ivy was on or
off. This mechanism had a major drawback: ivy_switch became a bottleneck
in the import graph, as it both imports from many places in the codebase
and is imported by many modules in the codebase. This frequently resulted
in cyclic imports which caused issues both with TS and Closure compilation.
It turns out ngcc needs both code paths in the bundle to perform the switch
during its operation anyway, so import switching was later abandoned. This
means that there's no real reason why the ivy_switch mechanism needed to
operate at the Bazel level, and for the ivy_switch file to be a bottleneck.
This commit removes the Bazel-level ivy_switch mechanism, and introduces
an additional TypeScript transform in ngtsc (and the pass-through tsc
compiler used for testing JIT) to perform the same operation that ngcc
does, and flip the switch during ngtsc compilation. This allows the
ivy_switch file to be removed, and the individual switches to be located
directly next to their consumers in the codebase, greatly mitigating the
circular import issues and making the mechanism much easier to use.
As part of this commit, the tag for marking switched variables was changed
from __PRE_NGCC__ to __PRE_R3__, since it's no longer just ngcc which
flips these tags. Most variables were renamed from R3_* to SWITCH_* as well,
since they're referenced mostly in render2 code.
Test strategy: existing test coverage is more than sufficient - if this
didn't work correctly it would break the hello world and todo apps.
PR Close #26550
2018-10-17 18:44:44 -04:00
|
|
|
import {SWITCH_ELEMENT_REF_FACTORY__POST_R3__ as R3_ELEMENT_REF_FACTORY} from '../../src/linker/element_ref';
|
|
|
|
import {SWITCH_TEMPLATE_REF_FACTORY__POST_R3__ as R3_TEMPLATE_REF_FACTORY} from '../../src/linker/template_ref';
|
|
|
|
import {SWITCH_VIEW_CONTAINER_REF_FACTORY__POST_R3__ as R3_VIEW_CONTAINER_REF_FACTORY} from '../../src/linker/view_container_ref';
|
|
|
|
import {SWITCH_RENDERER2_FACTORY__POST_R3__ as R3_RENDERER2_FACTORY} from '../../src/render/api';
|
2018-03-06 14:58:08 -05:00
|
|
|
import {CreateComponentOptions} from '../../src/render3/component';
|
2018-10-06 00:23:41 -04:00
|
|
|
import {discoverDirectives, getContext, isComponentInstance} from '../../src/render3/context_discovery';
|
2018-03-29 15:58:41 -04:00
|
|
|
import {extractDirectiveDef, extractPipeDef} from '../../src/render3/definition';
|
2018-09-21 21:38:13 -04:00
|
|
|
import {NG_ELEMENT_ID} from '../../src/render3/fields';
|
2018-09-21 15:12:06 -04:00
|
|
|
import {ComponentTemplate, ComponentType, DirectiveDef, DirectiveType, PublicFeature, RenderFlags, defineComponent, defineDirective, renderComponent as _renderComponent, tick} from '../../src/render3/index';
|
2018-10-06 00:23:41 -04:00
|
|
|
import {_getViewData, renderTemplate} from '../../src/render3/instructions';
|
2018-09-21 15:12:06 -04:00
|
|
|
import {DirectiveDefList, DirectiveTypesOrFactory, PipeDef, PipeDefList, PipeTypesOrFactory} from '../../src/render3/interfaces/definition';
|
2018-09-28 15:38:16 -04:00
|
|
|
import {PlayerHandler} from '../../src/render3/interfaces/player';
|
2018-01-09 21:38:17 -05:00
|
|
|
import {RElement, RText, Renderer3, RendererFactory3, domRendererFactory3} from '../../src/render3/interfaces/renderer';
|
2018-10-12 18:02:54 -04:00
|
|
|
import {HEADER_OFFSET, LViewData} from '../../src/render3/interfaces/view';
|
2018-05-09 18:30:16 -04:00
|
|
|
import {Sanitizer} from '../../src/sanitization/security';
|
2018-03-29 15:58:41 -04:00
|
|
|
import {Type} from '../../src/type';
|
2018-01-09 19:43:12 -05:00
|
|
|
|
2017-12-11 10:30:46 -05:00
|
|
|
import {getRendererFactory2} from './imported_renderer2';
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-03-13 15:31:21 -04:00
|
|
|
export abstract class BaseFixture {
|
|
|
|
hostElement: HTMLElement;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.hostElement = document.createElement('div');
|
|
|
|
this.hostElement.setAttribute('fixture', 'mark');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current state of rendered HTML.
|
|
|
|
*/
|
2018-06-06 11:30:48 -04:00
|
|
|
get html(): string { return toHtml(this.hostElement as any as Element); }
|
2018-03-13 15:31:21 -04:00
|
|
|
}
|
|
|
|
|
2018-03-01 20:14:01 -05:00
|
|
|
function noop() {}
|
|
|
|
/**
|
|
|
|
* Fixture for testing template functions in a convenient way.
|
|
|
|
*
|
|
|
|
* This fixture allows:
|
|
|
|
* - specifying the creation block and update block as two separate functions,
|
|
|
|
* - maintaining the template state between invocations,
|
|
|
|
* - access to the render `html`.
|
|
|
|
*/
|
2018-03-13 15:31:21 -04:00
|
|
|
export class TemplateFixture extends BaseFixture {
|
2018-10-12 18:02:54 -04:00
|
|
|
hostView: LViewData;
|
2018-04-05 04:29:47 -04:00
|
|
|
private _directiveDefs: DirectiveDefList|null;
|
|
|
|
private _pipeDefs: PipeDefList|null;
|
2018-05-09 18:30:16 -04:00
|
|
|
private _sanitizer: Sanitizer|null;
|
2018-05-22 11:40:59 -04:00
|
|
|
private _rendererFactory: RendererFactory3;
|
2018-05-09 18:30:16 -04:00
|
|
|
|
2018-03-01 20:14:01 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param createBlock Instructions which go into the creation block:
|
2018-05-13 15:01:37 -04:00
|
|
|
* `if (rf & RenderFlags.Create) { __here__ }`.
|
|
|
|
* @param updateBlock Optional instructions which go into the update block:
|
|
|
|
* `if (rf & RenderFlags.Update) { __here__ }`.
|
2018-03-01 20:14:01 -05:00
|
|
|
*/
|
2018-04-05 04:29:47 -04:00
|
|
|
constructor(
|
2018-08-16 21:53:21 -04:00
|
|
|
private createBlock: () => void, private updateBlock: () => void = noop, consts: number = 0,
|
2018-08-18 14:14:50 -04:00
|
|
|
private vars: number = 0, directives?: DirectiveTypesOrFactory|null,
|
|
|
|
pipes?: PipeTypesOrFactory|null, sanitizer?: Sanitizer|null,
|
|
|
|
rendererFactory?: RendererFactory3) {
|
2018-03-13 15:31:21 -04:00
|
|
|
super();
|
2018-04-05 04:29:47 -04:00
|
|
|
this._directiveDefs = toDefs(directives, extractDirectiveDef);
|
|
|
|
this._pipeDefs = toDefs(pipes, extractPipeDef);
|
2018-05-09 18:30:16 -04:00
|
|
|
this._sanitizer = sanitizer || null;
|
2018-05-22 11:40:59 -04:00
|
|
|
this._rendererFactory = rendererFactory || domRendererFactory3;
|
2018-10-12 18:02:54 -04:00
|
|
|
this.hostView = renderTemplate(
|
2018-08-18 14:14:50 -04:00
|
|
|
this.hostElement,
|
|
|
|
(rf: RenderFlags, ctx: any) => {
|
|
|
|
if (rf & RenderFlags.Create) {
|
|
|
|
this.createBlock();
|
|
|
|
}
|
|
|
|
if (rf & RenderFlags.Update) {
|
|
|
|
this.updateBlock();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
consts, vars, null !, this._rendererFactory, null, this._directiveDefs, this._pipeDefs,
|
|
|
|
sanitizer);
|
2018-03-01 20:14:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the existing template
|
|
|
|
*
|
|
|
|
* @param updateBlock Optional update block.
|
|
|
|
*/
|
|
|
|
update(updateBlock?: () => void): void {
|
|
|
|
renderTemplate(
|
2018-10-12 18:02:54 -04:00
|
|
|
this.hostElement, updateBlock || this.updateBlock, 0, this.vars, null !,
|
|
|
|
this._rendererFactory, this.hostView, this._directiveDefs, this._pipeDefs, this._sanitizer);
|
2018-03-01 20:14:01 -05:00
|
|
|
}
|
2018-03-13 15:31:21 -04:00
|
|
|
}
|
2018-03-01 20:14:01 -05:00
|
|
|
|
2018-03-13 15:31:21 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fixture for testing Components in a convenient way.
|
|
|
|
*/
|
|
|
|
export class ComponentFixture<T> extends BaseFixture {
|
|
|
|
component: T;
|
|
|
|
requestAnimationFrame: {(fn: () => void): void; flush(): void; queue: (() => void)[];};
|
|
|
|
|
2018-08-28 19:49:52 -04:00
|
|
|
constructor(private componentType: ComponentType<T>, opts: {
|
|
|
|
injector?: Injector,
|
|
|
|
sanitizer?: Sanitizer,
|
|
|
|
rendererFactory?: RendererFactory3,
|
|
|
|
playerHandler?: PlayerHandler
|
|
|
|
} = {}) {
|
2018-03-13 15:31:21 -04:00
|
|
|
super();
|
|
|
|
this.requestAnimationFrame = function(fn: () => void) {
|
|
|
|
requestAnimationFrame.queue.push(fn);
|
|
|
|
} as any;
|
|
|
|
this.requestAnimationFrame.queue = [];
|
|
|
|
this.requestAnimationFrame.flush = function() {
|
|
|
|
while (requestAnimationFrame.queue.length) {
|
|
|
|
requestAnimationFrame.queue.shift() !();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-09 18:30:16 -04:00
|
|
|
this.component = _renderComponent(componentType, {
|
|
|
|
host: this.hostElement,
|
|
|
|
scheduler: this.requestAnimationFrame,
|
|
|
|
injector: opts.injector,
|
2018-06-05 18:28:15 -04:00
|
|
|
sanitizer: opts.sanitizer,
|
2018-08-28 19:49:52 -04:00
|
|
|
rendererFactory: opts.rendererFactory || domRendererFactory3,
|
|
|
|
playerHandler: opts.playerHandler
|
2018-05-09 18:30:16 -04:00
|
|
|
});
|
2018-03-13 15:31:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
update(): void {
|
|
|
|
tick(this.component);
|
|
|
|
this.requestAnimationFrame.flush();
|
2018-03-01 20:14:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 15:31:21 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The methods below use global state and we should stop using them.
|
|
|
|
// Fixtures above are preferred way of testing Components and Templates
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2018-03-01 20:14:01 -05:00
|
|
|
|
2018-05-09 19:49:39 -04:00
|
|
|
export const document = ((typeof global == 'object' && global || window) as any).document;
|
2017-12-01 17:23:03 -05:00
|
|
|
export let containerEl: HTMLElement = null !;
|
2018-10-12 18:02:54 -04:00
|
|
|
let hostView: LViewData|null;
|
2017-12-27 13:54:19 -05:00
|
|
|
const isRenderer2 =
|
|
|
|
typeof process == 'object' && process.argv[3] && process.argv[3] === '--r=renderer2';
|
2017-12-01 17:23:03 -05:00
|
|
|
// tslint:disable-next-line:no-console
|
2017-12-11 10:30:46 -05:00
|
|
|
console.log(`Running tests with ${!isRenderer2 ? 'document' : 'Renderer2'} renderer...`);
|
|
|
|
const testRendererFactory: RendererFactory3 =
|
|
|
|
isRenderer2 ? getRendererFactory2(document) : domRendererFactory3;
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
export const requestAnimationFrame:
|
|
|
|
{(fn: () => void): void; flush(): void; queue: (() => void)[];} = function(fn: () => void) {
|
|
|
|
requestAnimationFrame.queue.push(fn);
|
|
|
|
} as any;
|
|
|
|
requestAnimationFrame.flush = function() {
|
|
|
|
while (requestAnimationFrame.queue.length) {
|
|
|
|
requestAnimationFrame.queue.shift() !();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export function resetDOM() {
|
|
|
|
requestAnimationFrame.queue = [];
|
2017-12-15 11:50:54 -05:00
|
|
|
if (containerEl) {
|
2018-01-03 04:45:09 -05:00
|
|
|
try {
|
|
|
|
document.body.removeChild(containerEl);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
2017-12-15 11:50:54 -05:00
|
|
|
}
|
2017-12-01 17:23:03 -05:00
|
|
|
containerEl = document.createElement('div');
|
|
|
|
containerEl.setAttribute('host', '');
|
2017-12-15 11:50:54 -05:00
|
|
|
document.body.appendChild(containerEl);
|
2018-10-12 18:02:54 -04:00
|
|
|
hostView = null;
|
2017-12-01 17:23:03 -05:00
|
|
|
// TODO: assert that the global state is clean (e.g. ngData, previousOrParentNode, etc)
|
|
|
|
}
|
|
|
|
|
2018-03-13 15:31:21 -04:00
|
|
|
/**
|
|
|
|
* @deprecated use `TemplateFixture` or `ComponentFixture`
|
|
|
|
*/
|
2017-12-11 10:30:46 -05:00
|
|
|
export function renderToHtml(
|
2018-08-18 14:14:50 -04:00
|
|
|
template: ComponentTemplate<any>, ctx: any, consts: number = 0, vars: number = 0,
|
2018-08-16 21:53:21 -04:00
|
|
|
directives?: DirectiveTypesOrFactory | null, pipes?: PipeTypesOrFactory | null,
|
|
|
|
providedRendererFactory?: RendererFactory3 | null) {
|
2018-10-12 18:02:54 -04:00
|
|
|
hostView = renderTemplate(
|
2018-08-18 14:14:50 -04:00
|
|
|
containerEl, template, consts, vars, ctx, providedRendererFactory || testRendererFactory,
|
2018-10-12 18:02:54 -04:00
|
|
|
hostView, toDefs(directives, extractDirectiveDef), toDefs(pipes, extractPipeDef));
|
2017-12-11 10:30:46 -05:00
|
|
|
return toHtml(containerEl);
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:58:41 -04:00
|
|
|
function toDefs(
|
|
|
|
types: DirectiveTypesOrFactory | undefined | null,
|
2018-09-21 15:12:06 -04:00
|
|
|
mapFn: (type: Type<any>) => DirectiveDef<any>): DirectiveDefList|null;
|
2018-03-29 15:58:41 -04:00
|
|
|
function toDefs(
|
|
|
|
types: PipeTypesOrFactory | undefined | null,
|
2018-09-21 15:12:06 -04:00
|
|
|
mapFn: (type: Type<any>) => PipeDef<any>): PipeDefList|null;
|
2018-03-29 15:58:41 -04:00
|
|
|
function toDefs(
|
|
|
|
types: PipeTypesOrFactory | DirectiveTypesOrFactory | undefined | null,
|
2018-09-21 15:12:06 -04:00
|
|
|
mapFn: (type: Type<any>) => PipeDef<any>| DirectiveDef<any>): any {
|
2018-03-29 15:58:41 -04:00
|
|
|
if (!types) return null;
|
|
|
|
if (typeof types == 'function') {
|
|
|
|
types = types();
|
|
|
|
}
|
|
|
|
return types.map(mapFn);
|
|
|
|
}
|
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
beforeEach(resetDOM);
|
|
|
|
|
2018-09-21 21:38:13 -04:00
|
|
|
// This is necessary so we can switch between the Render2 version and the Ivy version
|
|
|
|
// of special objects like ElementRef and TemplateRef.
|
|
|
|
beforeEach(enableIvyInjectableFactories);
|
|
|
|
|
2018-03-13 15:31:21 -04:00
|
|
|
/**
|
|
|
|
* @deprecated use `TemplateFixture` or `ComponentFixture`
|
|
|
|
*/
|
2018-03-06 13:13:49 -05:00
|
|
|
export function renderComponent<T>(type: ComponentType<T>, opts?: CreateComponentOptions): T {
|
2018-02-03 23:34:30 -05:00
|
|
|
return _renderComponent(type, {
|
2018-03-06 13:13:49 -05:00
|
|
|
rendererFactory: opts && opts.rendererFactory || testRendererFactory,
|
2018-02-03 23:34:30 -05:00
|
|
|
host: containerEl,
|
|
|
|
scheduler: requestAnimationFrame,
|
2018-05-09 18:30:16 -04:00
|
|
|
sanitizer: opts ? opts.sanitizer : undefined,
|
2018-03-06 14:58:08 -05:00
|
|
|
hostFeatures: opts && opts.hostFeatures
|
2018-02-03 23:34:30 -05:00
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
|
2018-03-13 15:31:21 -04:00
|
|
|
/**
|
|
|
|
* @deprecated use `TemplateFixture` or `ComponentFixture`
|
|
|
|
*/
|
2017-12-01 17:23:03 -05:00
|
|
|
export function toHtml<T>(componentOrElement: T | RElement): string {
|
2018-08-29 16:52:03 -04:00
|
|
|
let element: any;
|
|
|
|
if (isComponentInstance(componentOrElement)) {
|
|
|
|
const context = getContext(componentOrElement);
|
|
|
|
element = context ? context.native : null;
|
2017-12-01 17:23:03 -05:00
|
|
|
} else {
|
2018-08-29 16:52:03 -04:00
|
|
|
element = componentOrElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
return stringifyElement(element)
|
2018-01-03 05:42:48 -05:00
|
|
|
.replace(/^<div host="">/, '')
|
2018-06-06 11:30:48 -04:00
|
|
|
.replace(/^<div fixture="mark">/, '')
|
2018-01-03 05:42:48 -05:00
|
|
|
.replace(/<\/div>$/, '')
|
|
|
|
.replace(' style=""', '')
|
2018-07-26 11:22:41 -04:00
|
|
|
.replace(/<!--container-->/g, '')
|
|
|
|
.replace(/<!--ng-container-->/g, '');
|
2018-08-29 16:52:03 -04:00
|
|
|
} else {
|
|
|
|
return '';
|
2017-12-01 17:23:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createComponent(
|
2018-08-18 14:14:50 -04:00
|
|
|
name: string, template: ComponentTemplate<any>, consts: number = 0, vars: number = 0,
|
2018-08-16 21:53:21 -04:00
|
|
|
directives: DirectiveTypesOrFactory = [], pipes: PipeTypesOrFactory = [],
|
2018-06-19 11:58:42 -04:00
|
|
|
viewQuery: ComponentTemplate<any>| null = null): ComponentType<any> {
|
2017-12-01 17:23:03 -05:00
|
|
|
return class Component {
|
|
|
|
value: any;
|
2018-01-22 18:27:21 -05:00
|
|
|
static ngComponentDef = defineComponent({
|
|
|
|
type: Component,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [[name]],
|
2018-08-16 21:53:21 -04:00
|
|
|
consts: consts,
|
2018-08-18 14:14:50 -04:00
|
|
|
vars: vars,
|
2018-01-22 18:27:21 -05:00
|
|
|
factory: () => new Component,
|
|
|
|
template: template,
|
2018-06-19 11:58:42 -04:00
|
|
|
viewQuery: viewQuery,
|
2018-03-26 00:32:39 -04:00
|
|
|
features: [PublicFeature],
|
2018-03-29 15:58:41 -04:00
|
|
|
directives: directives,
|
|
|
|
pipes: pipes
|
2018-01-22 18:27:21 -05:00
|
|
|
});
|
2017-12-01 17:23:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-26 00:32:39 -04:00
|
|
|
export function createDirective(
|
|
|
|
name: string, {exportAs}: {exportAs?: string} = {}): DirectiveType<any> {
|
2017-12-19 10:51:42 -05:00
|
|
|
return class Directive {
|
|
|
|
static ngDirectiveDef = defineDirective({
|
2018-01-22 18:27:21 -05:00
|
|
|
type: Directive,
|
2018-03-29 19:41:45 -04:00
|
|
|
selectors: [['', name, '']],
|
2017-12-19 10:51:42 -05:00
|
|
|
factory: () => new Directive(),
|
2017-12-20 06:19:59 -05:00
|
|
|
features: [PublicFeature],
|
2018-01-09 00:57:50 -05:00
|
|
|
exportAs: exportAs,
|
2017-12-19 10:51:42 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2017-12-01 17:23:03 -05:00
|
|
|
|
2018-10-06 00:23:41 -04:00
|
|
|
/** Gets the directive on the given node at the given index */
|
|
|
|
export function getDirectiveOnNode(nodeIndex: number, dirIndex: number = 0) {
|
|
|
|
const directives = discoverDirectives(nodeIndex + HEADER_OFFSET, _getViewData(), true);
|
|
|
|
if (directives == null) {
|
|
|
|
throw new Error(`No directives exist on node in slot ${nodeIndex}`);
|
|
|
|
}
|
|
|
|
return directives[dirIndex];
|
|
|
|
}
|
|
|
|
|
2017-12-01 17:23:03 -05:00
|
|
|
|
|
|
|
// Verify that DOM is a type of render. This is here for error checking only and has no use.
|
|
|
|
export const renderer: Renderer3 = null as any as Document;
|
|
|
|
export const element: RElement = null as any as HTMLElement;
|
|
|
|
export const text: RText = null as any as Text;
|
2018-09-21 21:38:13 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switches between Render2 version of special objects like ElementRef and the Ivy version
|
|
|
|
* of these objects. It's necessary to keep them separate so that we don't pull in fns
|
|
|
|
* like injectElementRef() prematurely.
|
|
|
|
*/
|
|
|
|
export function enableIvyInjectableFactories() {
|
|
|
|
(ElementRef as any)[NG_ELEMENT_ID] = () => R3_ELEMENT_REF_FACTORY(ElementRef);
|
|
|
|
(TemplateRef as any)[NG_ELEMENT_ID] = () => R3_TEMPLATE_REF_FACTORY(TemplateRef, ElementRef);
|
|
|
|
(ViewContainerRef as any)[NG_ELEMENT_ID] = () =>
|
|
|
|
R3_VIEW_CONTAINER_REF_FACTORY(ViewContainerRef, ElementRef);
|
|
|
|
(ChangeDetectorRef as any)[NG_ELEMENT_ID] = () => R3_CHANGE_DETECTOR_REF_FACTORY();
|
2018-10-10 09:53:14 -04:00
|
|
|
(Renderer2 as any)[NG_ELEMENT_ID] = () => R3_RENDERER2_FACTORY();
|
2018-09-21 21:38:13 -04:00
|
|
|
}
|