Skip to content

Commit bb9c7ae

Browse files
vicbIgorMinar
authored andcommitted
feat: RendererV2 integration (angular#14469)
1 parent b4d444a commit bb9c7ae

File tree

38 files changed

+892
-611
lines changed

38 files changed

+892
-611
lines changed

modules/@angular/common/test/directives/ng_class_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function main() {
1515

1616
function detectChangesAndExpectClassName(classes: string): void {
1717
fixture.detectChanges();
18-
expect(fixture.debugElement.children[0].nativeElement.className).toEqual(classes);
18+
expect(fixture.debugElement.children[0].nativeElement.className.trim()).toEqual(classes);
1919
}
2020

2121
function getComponent(): TestComponent { return fixture.debugElement.componentInstance; }

modules/@angular/compiler/src/view_compiler_next/view_compiler.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver, BuiltinConverter
107107
}
108108

109109
templateVisitAll(this, astNodes, {elementDepth});
110-
if (astNodes.length === 0 || (this.parent && hasViewContainer(astNodes[astNodes.length - 1]))) {
110+
if (astNodes.length === 0 ||
111+
(this.parent && needsAdditionalRootNode(astNodes[astNodes.length - 1]))) {
111112
// if the view is empty, or an embedded view has a view container as last root nde,
112113
// create an additional root node.
113114
this.nodeDefs.push(o.importExpr(createIdentifier(Identifiers.anchorDef)).callFn([
@@ -752,13 +753,16 @@ function depDef(dep: CompileDiDependencyMetadata): o.Expression {
752753
return flags === viewEngine.DepFlags.None ? expr : o.literalArr([o.literal(flags), expr]);
753754
}
754755

755-
function hasViewContainer(ast: TemplateAst): boolean {
756+
function needsAdditionalRootNode(ast: TemplateAst): boolean {
756757
if (ast instanceof EmbeddedTemplateAst) {
757758
return ast.hasViewContainer;
758-
} else if (ast instanceof ElementAst) {
759+
}
760+
761+
if (ast instanceof ElementAst) {
759762
return ast.hasViewContainer;
760763
}
761-
return false;
764+
765+
return ast instanceof NgContentAst;
762766
}
763767

764768
function calcQueryId(queryId: QueryId): string {

modules/@angular/core/src/core_private_export.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export const __core_private__: {
9292
makeDecorator: typeof decorators.makeDecorator,
9393
DebugDomRootRenderer: typeof debug.DebugDomRootRenderer,
9494
_DebugDomRootRenderer: debug.DebugDomRootRenderer,
95+
DebugDomRendererV2: typeof debug.DebugDomRendererV2,
96+
_DebugDomRendererV2: debug.DebugDomRendererV2,
9597
Console: typeof console.Console,
9698
_Console: console.Console,
9799
reflector: typeof reflection.reflector,
@@ -101,9 +103,6 @@ export const __core_private__: {
101103
_NoOpAnimationPlayer: NoOpAnimationPlayer_,
102104
AnimationPlayer: typeof AnimationPlayer_,
103105
_AnimationPlayer: AnimationPlayer_,
104-
105-
106-
107106
AnimationSequencePlayer: typeof AnimationSequencePlayer_,
108107
_AnimationSequencePlayer: AnimationSequencePlayer_,
109108
AnimationGroupPlayer: typeof AnimationGroupPlayer_,
@@ -157,6 +156,7 @@ export const __core_private__: {
157156
ReflectionCapabilities: reflection_capabilities.ReflectionCapabilities,
158157
makeDecorator: decorators.makeDecorator,
159158
DebugDomRootRenderer: debug.DebugDomRootRenderer,
159+
DebugDomRendererV2: debug.DebugDomRendererV2,
160160
Console: console.Console,
161161
reflector: reflection.reflector,
162162
Reflector: reflection.Reflector,

modules/@angular/core/src/debug/debug_node.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,26 @@ export class DebugElement extends DebugNode {
8585
insertChildrenAfter(child: DebugNode, newChildren: DebugNode[]) {
8686
const siblingIndex = this.childNodes.indexOf(child);
8787
if (siblingIndex !== -1) {
88-
const previousChildren = this.childNodes.slice(0, siblingIndex + 1);
89-
const nextChildren = this.childNodes.slice(siblingIndex + 1);
90-
this.childNodes = previousChildren.concat(newChildren, nextChildren);
91-
for (let i = 0; i < newChildren.length; ++i) {
92-
const newChild = newChildren[i];
93-
if (newChild.parent) {
94-
newChild.parent.removeChild(newChild);
88+
this.childNodes.splice(siblingIndex + 1, 0, ...newChildren);
89+
newChildren.forEach(c => {
90+
if (c.parent) {
91+
c.parent.removeChild(c);
9592
}
96-
newChild.parent = this;
93+
c.parent = this;
94+
});
95+
}
96+
}
97+
98+
insertBefore(refChild: DebugNode, newChild: DebugNode): void {
99+
const refIndex = this.childNodes.indexOf(refChild);
100+
if (refIndex === -1) {
101+
this.addChild(newChild);
102+
} else {
103+
if (newChild.parent) {
104+
newChild.parent.removeChild(newChild);
97105
}
106+
newChild.parent = this;
107+
this.childNodes.splice(refIndex, 0, newChild);
98108
}
99109
}
100110

modules/@angular/core/src/debug/debug_renderer.ts

Lines changed: 151 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {AnimationKeyframe} from '../animation/animation_keyframe';
1010
import {AnimationPlayer} from '../animation/animation_player';
1111
import {AnimationStyles} from '../animation/animation_styles';
1212
import {isPresent} from '../facade/lang';
13-
import {RenderComponentType, RenderDebugInfo, Renderer, RootRenderer} from '../render/api';
13+
import {RenderComponentType, RenderDebugInfo, Renderer, RendererV2, RootRenderer} from '../render/api';
1414

1515
import {DebugElement, DebugNode, EventListener, getDebugNode, indexDebugNode, removeDebugNodeFromIndex} from './debug_node';
1616

@@ -22,7 +22,7 @@ export class DebugDomRootRenderer implements RootRenderer {
2222
}
2323
}
2424

25-
export class DebugDomRenderer {
25+
export class DebugDomRenderer implements Renderer {
2626
constructor(private _delegate: Renderer) {}
2727

2828
selectRootElement(selectorOrNode: string|any, debugInfo?: RenderDebugInfo): any {
@@ -81,7 +81,7 @@ export class DebugDomRenderer {
8181
detachView(viewRootNodes: any[]) {
8282
viewRootNodes.forEach((node) => {
8383
const debugNode = getDebugNode(node);
84-
if (isPresent(debugNode) && isPresent(debugNode.parent)) {
84+
if (debugNode && debugNode.parent) {
8585
debugNode.parent.removeChild(debugNode);
8686
}
8787
});
@@ -156,3 +156,151 @@ export class DebugDomRenderer {
156156
element, startingStyles, keyframes, duration, delay, easing, previousPlayers);
157157
}
158158
}
159+
160+
export class DebugDomRendererV2 implements RendererV2 {
161+
constructor(private _delegate: RendererV2) {}
162+
163+
createElement(name: string, namespace?: string, debugInfo?: any): any {
164+
const el = this._delegate.createElement(name, namespace, debugInfo);
165+
const debugEl = new DebugElement(el, null, debugInfo);
166+
debugEl.name = name;
167+
indexDebugNode(debugEl);
168+
return el;
169+
}
170+
171+
createComment(value: string, debugInfo?: any): any {
172+
const comment = this._delegate.createComment(value, debugInfo);
173+
const debugEl = new DebugNode(comment, null, debugInfo);
174+
indexDebugNode(debugEl);
175+
return comment;
176+
}
177+
178+
createText(value: string, debugInfo?: any): any {
179+
const text = this._delegate.createText(value, debugInfo);
180+
const debugEl = new DebugNode(text, null, debugInfo);
181+
indexDebugNode(debugEl);
182+
return text;
183+
}
184+
185+
appendChild(parent: any, newChild: any): void {
186+
const debugEl = getDebugNode(parent);
187+
const debugChildEl = getDebugNode(newChild);
188+
if (debugEl && debugChildEl && debugEl instanceof DebugElement) {
189+
debugEl.addChild(debugChildEl);
190+
}
191+
this._delegate.appendChild(parent, newChild);
192+
}
193+
194+
insertBefore(parent: any, newChild: any, refChild: any): void {
195+
const debugEl = getDebugNode(parent);
196+
const debugChildEl = getDebugNode(newChild);
197+
const debugRefEl = getDebugNode(refChild);
198+
if (debugEl && debugChildEl && debugEl instanceof DebugElement) {
199+
debugEl.insertBefore(debugRefEl, debugChildEl);
200+
}
201+
202+
this._delegate.insertBefore(parent, newChild, refChild);
203+
}
204+
205+
removeChild(parent: any, oldChild: any): void {
206+
const debugEl = getDebugNode(parent);
207+
const debugChildEl = getDebugNode(oldChild);
208+
if (debugEl && debugChildEl && debugEl instanceof DebugElement) {
209+
debugEl.removeChild(debugChildEl);
210+
}
211+
this._delegate.removeChild(parent, oldChild);
212+
}
213+
214+
selectRootElement(selectorOrNode: string|any, debugInfo?: any): any {
215+
const el = this._delegate.selectRootElement(selectorOrNode, debugInfo);
216+
const debugEl = new DebugElement(el, null, debugInfo);
217+
indexDebugNode(debugEl);
218+
return el;
219+
}
220+
221+
parentNode(node: any): any { return this._delegate.parentNode(node); }
222+
223+
nextSibling(node: any): any { return this._delegate.nextSibling(node); }
224+
225+
setAttribute(el: any, name: string, value: string, namespace?: string): void {
226+
const debugEl = getDebugNode(el);
227+
if (debugEl && debugEl instanceof DebugElement) {
228+
const fullName = namespace ? namespace + ':' + name : name;
229+
debugEl.attributes[fullName] = value;
230+
}
231+
this._delegate.setAttribute(el, name, value, namespace);
232+
}
233+
234+
removeAttribute(el: any, name: string, namespace?: string): void {
235+
const debugEl = getDebugNode(el);
236+
if (debugEl && debugEl instanceof DebugElement) {
237+
const fullName = namespace ? namespace + ':' + name : name;
238+
debugEl.attributes[fullName] = null;
239+
}
240+
this._delegate.removeAttribute(el, name, namespace);
241+
}
242+
243+
setBindingDebugInfo(el: any, propertyName: string, propertyValue: string): void {
244+
this._delegate.setBindingDebugInfo(el, propertyName, propertyValue);
245+
}
246+
247+
removeBindingDebugInfo(el: any, propertyName: string): void {
248+
this._delegate.removeBindingDebugInfo(el, propertyName);
249+
}
250+
251+
addClass(el: any, name: string): void {
252+
const debugEl = getDebugNode(el);
253+
if (debugEl && debugEl instanceof DebugElement) {
254+
debugEl.classes[name] = true;
255+
}
256+
this._delegate.addClass(el, name);
257+
}
258+
259+
removeClass(el: any, name: string): void {
260+
const debugEl = getDebugNode(el);
261+
if (debugEl && debugEl instanceof DebugElement) {
262+
debugEl.classes[name] = false;
263+
}
264+
this._delegate.removeClass(el, name);
265+
}
266+
267+
setStyle(el: any, style: string, value: any, hasVendorPrefix: boolean, hasImportant: boolean):
268+
void {
269+
const debugEl = getDebugNode(el);
270+
if (debugEl && debugEl instanceof DebugElement) {
271+
debugEl.styles[style] = value;
272+
}
273+
this._delegate.setStyle(el, style, value, hasVendorPrefix, hasImportant);
274+
}
275+
276+
removeStyle(el: any, style: string, hasVendorPrefix: boolean): void {
277+
const debugEl = getDebugNode(el);
278+
if (debugEl && debugEl instanceof DebugElement) {
279+
debugEl.styles[style] = null;
280+
}
281+
this._delegate.removeStyle(el, style, hasVendorPrefix);
282+
}
283+
284+
setProperty(el: any, name: string, value: any): void {
285+
const debugEl = getDebugNode(el);
286+
if (debugEl && debugEl instanceof DebugElement) {
287+
debugEl.properties[name] = value;
288+
}
289+
this._delegate.setProperty(el, name, value);
290+
}
291+
292+
setText(node: any, value: string): void { this._delegate.setText(node, value); }
293+
294+
listen(
295+
target: 'document'|'windows'|'body'|any, eventName: string,
296+
callback: (event: any) => boolean): () => void {
297+
if (typeof target !== 'string') {
298+
const debugEl = getDebugNode(target);
299+
if (debugEl) {
300+
debugEl.listeners.push(new EventListener(eventName, callback));
301+
}
302+
}
303+
304+
return this._delegate.listen(target, eventName, callback);
305+
}
306+
}

modules/@angular/core/src/errors.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Type} from './type';
109
import {DebugContext} from './view';
1110

1211
export const ERROR_TYPE = 'ngType';

modules/@angular/core/src/linker/view_container.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ export class ViewContainer {
8080
return result;
8181
}
8282

83-
moveView(view: AppView<any>, currentIndex: number) {
84-
const previousIndex = this.nestedViews.indexOf(view);
83+
moveView(view: AppView<any>, toIndex: number) {
84+
const fromIndex = this.nestedViews.indexOf(view);
8585
if (view.type === ViewType.COMPONENT) {
8686
throw new Error(`Component views can't be moved!`);
8787
}
@@ -90,9 +90,9 @@ export class ViewContainer {
9090
nestedViews = [];
9191
this.nestedViews = nestedViews;
9292
}
93-
nestedViews.splice(previousIndex, 1);
94-
nestedViews.splice(currentIndex, 0, view);
95-
const prevView = currentIndex > 0 ? nestedViews[currentIndex - 1] : null;
93+
nestedViews.splice(fromIndex, 1);
94+
nestedViews.splice(toIndex, 0, view);
95+
const prevView = toIndex > 0 ? nestedViews[toIndex - 1] : null;
9696
view.moveAfter(this, prevView);
9797
}
9898

modules/@angular/core/src/linker/view_container_ref.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import {Injector} from '../di/injector';
10-
import {isPresent} from '../facade/lang';
1110
import {WtfScopeFn, wtfCreateScope, wtfLeave} from '../profile/profile';
1211

1312
import {ComponentFactory, ComponentRef} from './component_factory';
@@ -133,7 +132,7 @@ export class ViewContainerRef_ implements ViewContainerRef {
133132
get(index: number): ViewRef { return this._element.nestedViews[index].ref; }
134133
get length(): number {
135134
const views = this._element.nestedViews;
136-
return isPresent(views) ? views.length : 0;
135+
return views ? views.length : 0;
137136
}
138137

139138
get element(): ElementRef { return this._element.elementRef; }

modules/@angular/core/src/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
*/
88

99
// Public API for render
10-
export {RenderComponentType, Renderer, RootRenderer} from './render/api';
10+
export {RENDERER_V2_DIRECT, RenderComponentType, Renderer, RendererV2, RootRenderer} from './render/api';

0 commit comments

Comments
 (0)