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';
|
2019-01-09 16:49:16 -05:00
|
|
|
import {Provider} from '@angular/core/src/di/interface/provider';
|
2018-09-21 21:38:13 -04:00
|
|
|
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';
|
2019-04-01 18:36:43 -04:00
|
|
|
import {renderTemplate} from '@angular/core/src/render3/instructions/shared';
|
2018-11-22 00:14:06 -05:00
|
|
|
import {getLView} from '@angular/core/src/render3/state';
|
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';
|
2019-01-11 02:45:02 -05:00
|
|
|
import {Injector} from '../../src/di/injector';
|
2019-01-09 16:49:16 -05:00
|
|
|
import {Type} from '../../src/interface/type';
|
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';
|
2018-12-11 02:40:19 -05:00
|
|
|
import {RendererStyleFlags2, RendererType2, 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-11-28 18:54:38 -05:00
|
|
|
import {getDirectivesAtNodeIndex, getLContext, 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';
|
2019-05-09 14:47:25 -04:00
|
|
|
import {ComponentTemplate, ComponentType, DirectiveDef, DirectiveType, RenderFlags, renderComponent as _renderComponent, tick, ΔProvidersFeature, ΔdefineComponent, ΔdefineDirective} from '../../src/render3/index';
|
2019-02-12 15:04:44 -05:00
|
|
|
import {DirectiveDefList, DirectiveTypesOrFactory, HostBindingsFunction, PipeDef, PipeDefList, PipeTypesOrFactory} from '../../src/render3/interfaces/definition';
|
2018-09-28 15:38:16 -04:00
|
|
|
import {PlayerHandler} from '../../src/render3/interfaces/player';
|
2018-12-11 02:40:19 -05:00
|
|
|
import {ProceduralRenderer3, RComment, RElement, RNode, RText, Renderer3, RendererFactory3, RendererStyleFlags3, domRendererFactory3} from '../../src/render3/interfaces/renderer';
|
2018-11-22 00:14:06 -05:00
|
|
|
import {HEADER_OFFSET, LView} from '../../src/render3/interfaces/view';
|
2018-12-19 18:03:47 -05:00
|
|
|
import {destroyLView} from '../../src/render3/node_manipulation';
|
2019-02-20 17:21:20 -05:00
|
|
|
import {getRootView} from '../../src/render3/util/view_traversal_utils';
|
2018-05-09 18:30:16 -04:00
|
|
|
import {Sanitizer} from '../../src/sanitization/security';
|
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 {
|
2018-10-17 05:01:35 -04:00
|
|
|
/**
|
|
|
|
* Each fixture creates the following initial DOM structure:
|
|
|
|
* <div fixture="mark">
|
|
|
|
* <div host="mark"></div>
|
|
|
|
* </div>
|
|
|
|
*
|
|
|
|
* Components are bootstrapped into the <div host="mark"></div>.
|
|
|
|
* The <div fixture="mark"> is there for cases where the root component creates DOM node _outside_
|
|
|
|
* of its host element (for example when the root component injectes ViewContainerRef or does
|
|
|
|
* low-level DOM manipulation).
|
|
|
|
*
|
|
|
|
* The <div fixture="mark"> is _not_ attached to the document body.
|
|
|
|
*/
|
|
|
|
containerElement: HTMLElement;
|
2018-03-13 15:31:21 -04:00
|
|
|
hostElement: HTMLElement;
|
|
|
|
|
|
|
|
constructor() {
|
2018-10-17 05:01:35 -04:00
|
|
|
this.containerElement = document.createElement('div');
|
|
|
|
this.containerElement.setAttribute('fixture', 'mark');
|
2018-03-13 15:31:21 -04:00
|
|
|
this.hostElement = document.createElement('div');
|
2018-10-17 05:01:35 -04:00
|
|
|
this.hostElement.setAttribute('host', 'mark');
|
|
|
|
this.containerElement.appendChild(this.hostElement);
|
2018-03-13 15:31:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-17 05:01:35 -04:00
|
|
|
* Current state of HTML rendered by the bootstrapped component.
|
2018-03-13 15:31:21 -04:00
|
|
|
*/
|
2018-06-06 11:30:48 -04:00
|
|
|
get html(): string { return toHtml(this.hostElement as any as Element); }
|
2018-10-17 05:01:35 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current state of HTML rendered by the fixture (will include HTML rendered by the bootstrapped
|
|
|
|
* component as well as any elements outside of the component's host).
|
|
|
|
*/
|
|
|
|
get outerHtml(): string { return toHtml(this.containerElement 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-11-22 00:14:06 -05:00
|
|
|
hostView: LView;
|
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-12-19 18:03:47 -05:00
|
|
|
|
|
|
|
destroy(): void {
|
|
|
|
this.containerElement.removeChild(this.hostElement);
|
|
|
|
destroyLView(this.hostView);
|
|
|
|
}
|
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-12-19 18:03:47 -05:00
|
|
|
|
|
|
|
destroy(): void {
|
2019-01-28 18:23:53 -05:00
|
|
|
// Skip removing the DOM element if it has already been removed (the view has already
|
|
|
|
// been destroyed).
|
|
|
|
if (this.hostElement.parentNode === this.containerElement) {
|
|
|
|
this.containerElement.removeChild(this.hostElement);
|
|
|
|
}
|
|
|
|
|
2018-12-19 18:03:47 -05:00
|
|
|
destroyLView(getRootView(this.component));
|
|
|
|
}
|
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-11-22 00:14:06 -05:00
|
|
|
let hostView: LView|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,
|
2018-11-21 07:43:00 -05:00
|
|
|
providedRendererFactory?: RendererFactory3 | null, keepNgReflect = false) {
|
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));
|
2018-11-21 07:43:00 -05:00
|
|
|
return toHtml(containerEl, keepNgReflect);
|
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`
|
|
|
|
*/
|
2018-11-21 07:43:00 -05:00
|
|
|
export function toHtml<T>(componentOrElement: T | RElement, keepNgReflect = false): string {
|
2018-08-29 16:52:03 -04:00
|
|
|
let element: any;
|
|
|
|
if (isComponentInstance(componentOrElement)) {
|
2018-11-28 18:54:38 -05:00
|
|
|
const context = getLContext(componentOrElement);
|
2018-08-29 16:52:03 -04:00
|
|
|
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) {
|
2018-11-29 10:39:43 -05:00
|
|
|
let html = stringifyElement(element);
|
|
|
|
|
2018-11-21 07:43:00 -05:00
|
|
|
if (!keepNgReflect) {
|
2018-11-29 10:39:43 -05:00
|
|
|
html = html.replace(/\sng-reflect-\S*="[^"]*"/g, '')
|
|
|
|
.replace(/<!--bindings=\{(\W.*\W\s*)?\}-->/g, '');
|
2018-11-21 07:43:00 -05:00
|
|
|
}
|
2018-11-29 10:39:43 -05:00
|
|
|
|
|
|
|
html = html.replace(/^<div host="">(.*)<\/div>$/, '$1')
|
|
|
|
.replace(/^<div fixture="mark">(.*)<\/div>$/, '$1')
|
|
|
|
.replace(/^<div host="mark">(.*)<\/div>$/, '$1')
|
|
|
|
.replace(' style=""', '')
|
|
|
|
.replace(/<!--container-->/g, '')
|
|
|
|
.replace(/<!--ng-container-->/g, '');
|
2018-11-21 07:43:00 -05:00
|
|
|
return html;
|
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-12-13 05:14:33 -05:00
|
|
|
viewQuery: ComponentTemplate<any>| null = null, providers: Provider[] = [],
|
2019-02-12 15:04:44 -05:00
|
|
|
viewProviders: Provider[] = [], hostBindings?: HostBindingsFunction<any>): ComponentType<any> {
|
2017-12-01 17:23:03 -05:00
|
|
|
return class Component {
|
|
|
|
value: any;
|
2019-05-09 14:47:25 -04:00
|
|
|
static ngComponentDef = ΔdefineComponent({
|
2018-01-22 18:27:21 -05:00
|
|
|
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,
|
2019-02-12 15:04:44 -05:00
|
|
|
directives: directives, hostBindings,
|
2018-12-13 05:14:33 -05:00
|
|
|
pipes: pipes,
|
|
|
|
features: (providers.length > 0 || viewProviders.length > 0)?
|
2019-05-09 14:47:25 -04:00
|
|
|
[ΔProvidersFeature(providers || [], viewProviders || [])]: []
|
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(
|
2019-01-10 16:24:32 -05:00
|
|
|
name: string, {exportAs}: {exportAs?: string[]} = {}): DirectiveType<any> {
|
2017-12-19 10:51:42 -05:00
|
|
|
return class Directive {
|
2019-05-09 14:47:25 -04:00
|
|
|
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(),
|
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) {
|
2018-11-22 00:14:06 -05:00
|
|
|
const directives = getDirectivesAtNodeIndex(nodeIndex + HEADER_OFFSET, getLView(), true);
|
2018-10-06 00:23:41 -04:00
|
|
|
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
|
|
|
}
|
2018-12-11 02:40:19 -05:00
|
|
|
|
|
|
|
export class MockRendererFactory implements RendererFactory3 {
|
|
|
|
lastRenderer: any;
|
|
|
|
private _spyOnMethods: string[];
|
|
|
|
|
|
|
|
constructor(spyOnMethods?: string[]) { this._spyOnMethods = spyOnMethods || []; }
|
|
|
|
|
|
|
|
createRenderer(hostElement: RElement|null, rendererType: RendererType2|null): Renderer3 {
|
|
|
|
const renderer = this.lastRenderer = new MockRenderer(this._spyOnMethods);
|
|
|
|
return renderer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockRenderer implements ProceduralRenderer3 {
|
|
|
|
public spies: {[methodName: string]: any} = {};
|
|
|
|
|
|
|
|
constructor(spyOnMethods: string[]) {
|
|
|
|
spyOnMethods.forEach(methodName => {
|
|
|
|
this.spies[methodName] = spyOn(this as any, methodName).and.callThrough();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(): void {}
|
|
|
|
createComment(value: string): RComment { return document.createComment(value); }
|
|
|
|
createElement(name: string, namespace?: string|null): RElement {
|
2019-01-22 17:21:53 -05:00
|
|
|
return namespace ? document.createElementNS(namespace, name) : document.createElement(name);
|
2018-12-11 02:40:19 -05:00
|
|
|
}
|
|
|
|
createText(value: string): RText { return document.createTextNode(value); }
|
|
|
|
appendChild(parent: RElement, newChild: RNode): void { parent.appendChild(newChild); }
|
|
|
|
insertBefore(parent: RNode, newChild: RNode, refChild: RNode|null): void {
|
|
|
|
parent.insertBefore(newChild, refChild, false);
|
|
|
|
}
|
|
|
|
removeChild(parent: RElement, oldChild: RNode): void { parent.removeChild(oldChild); }
|
|
|
|
selectRootElement(selectorOrNode: string|any): RElement {
|
|
|
|
return ({} as any);
|
|
|
|
}
|
|
|
|
parentNode(node: RNode): RElement|null { return node.parentNode as RElement; }
|
|
|
|
nextSibling(node: RNode): RNode|null { return node.nextSibling; }
|
|
|
|
setAttribute(el: RElement, name: string, value: string, namespace?: string|null): void {
|
|
|
|
// set all synthetic attributes as properties
|
|
|
|
if (name[0] === '@') {
|
|
|
|
this.setProperty(el, name, value);
|
|
|
|
} else {
|
|
|
|
el.setAttribute(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
removeAttribute(el: RElement, name: string, namespace?: string|null): void {}
|
|
|
|
addClass(el: RElement, name: string): void {}
|
|
|
|
removeClass(el: RElement, name: string): void {}
|
|
|
|
setStyle(
|
|
|
|
el: RElement, style: string, value: any,
|
|
|
|
flags?: RendererStyleFlags2|RendererStyleFlags3): void {}
|
|
|
|
removeStyle(el: RElement, style: string, flags?: RendererStyleFlags2|RendererStyleFlags3): void {}
|
|
|
|
setProperty(el: RElement, name: string, value: any): void { (el as any)[name] = value; }
|
|
|
|
setValue(node: RText, value: string): void { node.textContent = value; }
|
|
|
|
|
|
|
|
// TODO(misko): Deprecate in favor of addEventListener/removeEventListener
|
|
|
|
listen(target: RNode, eventName: string, callback: (event: any) => boolean | void): () => void {
|
|
|
|
return () => {};
|
|
|
|
}
|
2019-01-10 16:24:32 -05:00
|
|
|
}
|