diff --git a/packages/compiler/src/template/pipeline/ir/index.ts b/packages/compiler/src/template/pipeline/ir/index.ts index 79466b900bc..cddbdb64546 100644 --- a/packages/compiler/src/template/pipeline/ir/index.ts +++ b/packages/compiler/src/template/pipeline/ir/index.ts @@ -6,6 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ +export * from './src/element'; export * from './src/expression'; export * from './src/enums'; export * from './src/operations'; diff --git a/packages/compiler/src/template/pipeline/ir/src/element.ts b/packages/compiler/src/template/pipeline/ir/src/element.ts new file mode 100644 index 00000000000..a0d5f1cedf2 --- /dev/null +++ b/packages/compiler/src/template/pipeline/ir/src/element.ts @@ -0,0 +1,121 @@ +/** + * @license + * Copyright Google LLC 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 + */ + +import * as core from '../../../../core'; +import {splitNsName} from '../../../../ml_parser/tags'; +import * as o from '../../../../output/output_ast'; + +/** + * Enumeration of the types of attributes which can be applied to an element. + */ +export enum ElementAttributeKind { + /** + * Static attributes. + */ + Attribute, + + /** + * Class bindings. + */ + Class, + + /** + * Style bindings. + */ + Style, + + /** + * Dynamic property or attribute bindings. + */ + Binding, + + /** + * Attributes on a template node. + */ + Template, + + /** + * Internationalized attributes. + */ + I18n, +} + +const FLYWEIGHT_ARRAY: ReadonlyArray = Object.freeze([]); + +/** + * Container for all of the various kinds of attributes which are applied on an element. + */ +export class ElementAttributes { + private known = new Set(); + private byKind = new Map; + + projectAs: string|null = null; + + get attributes(): ReadonlyArray { + return this.byKind.get(ElementAttributeKind.Attribute) ?? FLYWEIGHT_ARRAY; + } + + get classes(): ReadonlyArray { + return this.byKind.get(ElementAttributeKind.Class) ?? FLYWEIGHT_ARRAY; + } + + get styles(): ReadonlyArray { + return this.byKind.get(ElementAttributeKind.Style) ?? FLYWEIGHT_ARRAY; + } + + get bindings(): ReadonlyArray { + return this.byKind.get(ElementAttributeKind.Binding) ?? FLYWEIGHT_ARRAY; + } + + get template(): ReadonlyArray { + return this.byKind.get(ElementAttributeKind.Template) ?? FLYWEIGHT_ARRAY; + } + + get i18n(): ReadonlyArray { + return this.byKind.get(ElementAttributeKind.I18n) ?? FLYWEIGHT_ARRAY; + } + + add(kind: ElementAttributeKind, name: string, value: o.Expression|null): void { + if (this.known.has(name)) { + return; + } + this.known.add(name); + const array = this.arrayFor(kind); + array.push(...getAttributeNameLiterals(name)); + if (value !== null) { + array.push(value); + } + } + + private arrayFor(kind: ElementAttributeKind): o.Expression[] { + if (!this.byKind.has(kind)) { + this.byKind.set(kind, []); + } + return this.byKind.get(kind)!; + } +} + +function getAttributeNameLiterals(name: string): o.LiteralExpr[] { + const [attributeNamespace, attributeName] = splitNsName(name); + const nameLiteral = o.literal(attributeName); + + if (attributeNamespace) { + return [ + o.literal(core.AttributeMarker.NamespaceURI), o.literal(attributeNamespace), nameLiteral + ]; + } + + return [nameLiteral]; +} + +export function assertIsElementAttributes(attrs: any): asserts attrs is ElementAttributes { + if (!(attrs instanceof ElementAttributes)) { + throw new Error( + `AssertionError: ElementAttributes has already been coalesced into the view constants`); + } +} diff --git a/packages/compiler/src/template/pipeline/ir/src/enums.ts b/packages/compiler/src/template/pipeline/ir/src/enums.ts index ca60bc5590c..3bc4b73b2a6 100644 --- a/packages/compiler/src/template/pipeline/ir/src/enums.ts +++ b/packages/compiler/src/template/pipeline/ir/src/enums.ts @@ -22,6 +22,46 @@ export enum OpKind { * An operation which wraps an output AST statement. */ Statement, + + /** + * An operation to begin rendering of an element. + */ + ElementStart, + + /** + * An operation to render an element with no children. + */ + Element, + + /** + * An operation which declares an embedded view. + */ + Template, + + /** + * An operation to end rendering of an element previously started with `ElementStart`. + */ + ElementEnd, + + /** + * An operation to render a text node. + */ + Text, + + /** + * An operation declaring an event listener for an element. + */ + Listener, + + /** + * An operation to interpolate text into a text node. + */ + InterpolateText, + + /** + * An operation to bind an expression to a property of an element. + */ + Property, } /** diff --git a/packages/compiler/src/template/pipeline/ir/src/ops/create.ts b/packages/compiler/src/template/pipeline/ir/src/ops/create.ts index cf24b3004b1..f3ffed7c04c 100644 --- a/packages/compiler/src/template/pipeline/ir/src/ops/create.ts +++ b/packages/compiler/src/template/pipeline/ir/src/ops/create.ts @@ -6,9 +6,221 @@ * found in the LICENSE file at https://angular.io/license */ -import {ListEndOp, StatementOp} from './shared'; +import {ElementAttributes} from '../element'; +import {OpKind} from '../enums'; +import {Op, OpList, XrefId} from '../operations'; + +import {ListEndOp, NEW_OP, StatementOp} from './shared'; +import type {UpdateOp} from './update'; /** * An operation usable on the creation side of the IR. */ -export type CreateOp = ListEndOp|StatementOp; +export type CreateOp = ListEndOp|StatementOp|ElementOp|ElementStartOp| + ElementEndOp|TemplateOp|TextOp|ListenerOp; + +/** + * Representation of a local reference on an element. + */ +export interface LocalRef { + /** + * User-defined name of the local ref variable. + */ + name: string; + + /** + * Target of the local reference variable (often `''`). + */ + target: string; +} + +/** + * Base interface for `Element`, `ElementStart`, and `Template` operations, containing common fields + * used to represent their element-like nature. + */ +export interface ElementOpBase extends Op { + kind: OpKind.Element|OpKind.ElementStart|OpKind.Template; + + /** + * `XrefId` allocated for this element. + * + * This ID is used to reference this element from other IR structures. + */ + xref: XrefId; + + /** + * The HTML tag name for this element. + */ + tag: string; + + /** + * Attributes of various kinds on this element. + * + * Before attribute processing, this is an `ElementAttributes` structure representing the + * attributes on this element. + * + * After processing, it's a `ConstIndex` pointer into the shared `consts` array of the component + * compilation. + */ + attributes: ElementAttributes|ConstIndex|null; + + /** + * Local references to this element. + * + * Before local ref processing, this is an array of `LocalRef` declarations. + * + * After processing, it's a `ConstIndex` pointer into the shared `consts` array of the component + * compilation. + */ + localRefs: LocalRef[]|ConstIndex|null; +} + +/** + * Logical operation representing the start of an element in the creation IR. + */ +export interface ElementStartOp extends ElementOpBase { + kind: OpKind.ElementStart; +} + +/** + * Create an `ElementStartOp`. + */ +export function createElementStartOp(tag: string, xref: XrefId): ElementStartOp { + return { + kind: OpKind.ElementStart, + xref, + tag, + attributes: new ElementAttributes(), + localRefs: [], + ...NEW_OP, + }; +} + +/** + * Logical operation representing an element with no children in the creation IR. + */ +export interface ElementOp extends ElementOpBase { + kind: OpKind.Element; +} + +/** + * Logical operation representing an embedded view declaration in the creation IR. + */ +export interface TemplateOp extends ElementOpBase { + kind: OpKind.Template; +} + +/** + * Create a `TemplateOp`. + */ +export function createTemplateOp(xref: XrefId, tag: string): TemplateOp { + return { + kind: OpKind.Template, + xref, + attributes: new ElementAttributes(), + tag, + localRefs: [], + ...NEW_OP, + }; +} + +/** + * Logical operation representing the end of an element structure in the creation IR. + * + * Pairs with an `ElementStart` operation. + */ +export interface ElementEndOp extends Op { + kind: OpKind.ElementEnd; + + /** + * The `XrefId` of the element declared via `ElementStart`. + */ + xref: XrefId; +} + +/** + * Create an `ElementEndOp`. + */ +export function createElementEndOp(xref: XrefId): ElementEndOp { + return { + kind: OpKind.ElementEnd, + xref, + ...NEW_OP, + }; +} + +/** + * Logical operation representing a text node in the creation IR. + */ +export interface TextOp extends Op { + kind: OpKind.Text; + + /** + * `XrefId` used to reference this text node in other IR structures. + */ + xref: XrefId; + + /** + * The static initial value of the text node. + */ + initialValue: string; +} + +/** + * Create a `TextOp`. + */ +export function createTextOp(xref: XrefId, initialValue: string): TextOp { + return { + kind: OpKind.Text, + xref, + initialValue, + ...NEW_OP, + }; +} + +/** + * Logical operation representing an event listener on an element in the creation IR. + */ +export interface ListenerOp extends Op { + kind: OpKind.Listener; + + /** + * `XrefId` of the element on which the event listener is bound. + */ + xref: XrefId; + + /** + * Name of the event which is being listened to. + */ + name: string; + + /** + * A list of `UpdateOp`s representing the body of the event listener. + */ + handlerOps: OpList; + + /** + * Name of the function + */ + handlerFnName: string|null; +} + +/** + * Create a `ListenerOp`. + */ +export function createListenerOp(xref: XrefId, name: string): ListenerOp { + return { + kind: OpKind.Listener, + xref, + name, + handlerOps: new OpList(), + handlerFnName: null, + ...NEW_OP, + }; +} + +/** + * An index into the `consts` array which is shared across the compilation of all views in a + * component. + */ +export type ConstIndex = number&{__brand: 'ConstIndex'}; diff --git a/packages/compiler/src/template/pipeline/ir/src/ops/update.ts b/packages/compiler/src/template/pipeline/ir/src/ops/update.ts index 0914c396e79..c8acbd0ae2b 100644 --- a/packages/compiler/src/template/pipeline/ir/src/ops/update.ts +++ b/packages/compiler/src/template/pipeline/ir/src/ops/update.ts @@ -6,9 +6,92 @@ * found in the LICENSE file at https://angular.io/license */ -import {ListEndOp, StatementOp} from './shared'; +import * as o from '../../../../../output/output_ast'; +import {OpKind} from '../enums'; +import {Op, XrefId} from '../operations'; + +import {ListEndOp, NEW_OP, StatementOp} from './shared'; /** * An operation usable on the update side of the IR. */ -export type UpdateOp = ListEndOp|StatementOp; +export type UpdateOp = ListEndOp|StatementOp|PropertyOp|InterpolateTextOp; + +/** + * A logical operation to perform string interpolation on a text node. + * + * Interpolation inputs are stored as static `string`s and dynamic `o.Expression`s, in separate + * arrays. Thus, the interpolation `A{{b}}C{{d}}E` is stored as 3 static strings `['A', 'C', 'E']` + * and 2 dynamic expressions `[b, d]`. + */ +export interface InterpolateTextOp extends Op { + kind: OpKind.InterpolateText; + + /** + * Reference to the text node to which the interpolation is bound. + */ + xref: XrefId; + + /** + * All of the literal strings in the text interpolation, in order. + * + * Conceptually interwoven around the `expressions`. + */ + strings: string[]; + + /** + * All of the dynamic expressions in the text interpolation, in order. + * + * Conceptually interwoven in between the `strings`. + */ + expressions: o.Expression[]; +} + +/** + * Create an `InterpolationTextOp`. + */ +export function createInterpolateTextOp( + xref: XrefId, strings: string[], expressions: o.Expression[]): InterpolateTextOp { + return { + kind: OpKind.InterpolateText, + xref, + strings, + expressions, + ...NEW_OP, + }; +} + +/** + * A logical operation representing binding to a property in the update IR. + */ +export interface PropertyOp extends Op { + kind: OpKind.Property; + + /** + * Reference to the element on which the property is bound. + */ + xref: XrefId; + + /** + * Name of the bound property. + */ + name: string; + + /** + * Expression which is bound to the property. + */ + expression: o.Expression; +} + +/** + * Create a `PropertyOp`. + */ +export function createPropertyOp(xref: XrefId, name: string, expression: o.Expression): PropertyOp { + return { + kind: OpKind.Property, + xref, + name, + expression, + ...NEW_OP, + }; +} diff --git a/packages/compiler/src/template/pipeline/src/compilation.ts b/packages/compiler/src/template/pipeline/src/compilation.ts index 107f48370da..44b6dfc58fe 100644 --- a/packages/compiler/src/template/pipeline/src/compilation.ts +++ b/packages/compiler/src/template/pipeline/src/compilation.ts @@ -86,6 +86,11 @@ export class ViewCompilation { * ops(): Generator { for (const op of this.create) { yield op; + if (op.kind === ir.OpKind.Listener) { + for (const listenerOp of op.handlerOps) { + yield listenerOp; + } + } } for (const op of this.update) { yield op;