Skip to content

Commit a5a18ae

Browse files
refactor(core): minimize code in the element-like instructions (angular#61563)
Minor changes to the element and elementContainer instructions to move as much code as possible out of those instructions. PR Close angular#61563
1 parent 0cf1001 commit a5a18ae

4 files changed

Lines changed: 20 additions & 34 deletions

File tree

packages/core/src/render3/instructions/element.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export function ɵɵelementStart(
7171
): typeof ɵɵelementStart {
7272
const lView = getLView();
7373
const tView = getTView();
74-
const bindingsEnabled = getBindingsEnabled();
7574
ngDevMode &&
7675
assertEqual(
7776
getBindingIndex(),
@@ -80,25 +79,17 @@ export function ɵɵelementStart(
8079
);
8180
const tNode = elementLikeStartShared(
8281
lView,
83-
tView,
8482
index,
8583
TNodeType.Element,
8684
name,
8785
_locateOrCreateElementNode,
88-
bindingsEnabled,
86+
getBindingsEnabled(),
8987
attrsIndex,
9088
localRefsIndex,
9189
);
9290

9391
if (ngDevMode && tView.firstCreatePass) {
94-
const lView = getLView();
95-
validateElementIsKnown(
96-
lView[tNode.index],
97-
lView,
98-
tNode.value,
99-
tView.schemas,
100-
isDirectiveHost(tNode),
101-
);
92+
validateElementIsKnown(lView, tNode);
10293
}
10394

10495
return ɵɵelementStart;

packages/core/src/render3/instructions/element_container.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export function ɵɵelementContainerStart(
5151
): typeof ɵɵelementContainerStart {
5252
const lView = getLView();
5353
const tView = getTView();
54-
const bindingsEnabled = getBindingsEnabled();
5554
ngDevMode &&
5655
assertEqual(
5756
getBindingIndex(),
@@ -60,12 +59,11 @@ export function ɵɵelementContainerStart(
6059
);
6160
elementLikeStartShared(
6261
lView,
63-
tView,
6462
index,
6563
TNodeType.ElementContainer,
6664
'ng-container',
6765
_locateOrCreateElementContainerNode,
68-
bindingsEnabled,
66+
getBindingsEnabled(),
6967
attrsIndex,
7068
localRefsIndex,
7169
);

packages/core/src/render3/instructions/element_validation.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import {CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, SchemaMetadata} from '../../me
1212
import {throwError} from '../../util/assert';
1313
import {getComponentDef} from '../def_getters';
1414
import {ComponentDef} from '../interfaces/definition';
15-
import {TNodeType} from '../interfaces/node';
15+
import {TElementNode, TNode, TNodeType} from '../interfaces/node';
1616
import {RComment, RElement} from '../interfaces/renderer_dom';
17-
import {CONTEXT, DECLARATION_COMPONENT_VIEW, LView} from '../interfaces/view';
17+
import {isDirectiveHost} from '../interfaces/type_checks';
18+
import {CONTEXT, DECLARATION_COMPONENT_VIEW, LView, TVIEW} from '../interfaces/view';
1819
import {isAnimationProp} from '../util/attrs_utils';
20+
import {getNativeByTNode} from '../util/view_utils';
1921

2022
let shouldThrowErrorOnUnknownElement = false;
2123

@@ -65,27 +67,22 @@ export function ɵgetUnknownPropertyStrictMode() {
6567
* - the element matches any directive
6668
* - the element is allowed by one of the schemas
6769
*
68-
* @param element Element to validate
69-
* @param lView An `LView` that represents a current component that is being rendered
70-
* @param tagName Name of the tag to check
71-
* @param schemas Array of schemas
72-
* @param hasDirectives Boolean indicating that the element matches any directive
70+
* @param lView An `LView` associated with a template is being rendered
71+
* @param tNode TNode representing an element to be validated
7372
*/
74-
export function validateElementIsKnown(
75-
element: RElement,
76-
lView: LView,
77-
tagName: string | null,
78-
schemas: SchemaMetadata[] | null,
79-
hasDirectives: boolean,
80-
): void {
73+
export function validateElementIsKnown(lView: LView, tNode: TElementNode): void {
74+
const tView = lView[TVIEW];
75+
8176
// If `schemas` is set to `null`, that's an indication that this Component was compiled in AOT
8277
// mode where this check happens at compile time. In JIT mode, `schemas` is always present and
8378
// defined as an array (as an empty array in case `schemas` field is not defined) and we should
8479
// execute the check below.
85-
if (schemas === null) return;
80+
if (tView.schemas === null) return;
81+
82+
const tagName = tNode.value;
8683

8784
// If the element matches any directive, it's considered as valid.
88-
if (!hasDirectives && tagName !== null) {
85+
if (!isDirectiveHost(tNode) && tagName !== null) {
8986
// The element is unknown if it's an instance of HTMLUnknownElement, or it isn't registered
9087
// as a custom element. Note that unknown elements with a dash in their name won't be instances
9188
// of HTMLUnknownElement in browsers that support web components.
@@ -94,12 +91,12 @@ export function validateElementIsKnown(
9491
// Domino doesn't expose HTMLUnknownElement globally.
9592
(typeof HTMLUnknownElement !== 'undefined' &&
9693
HTMLUnknownElement &&
97-
element instanceof HTMLUnknownElement) ||
94+
getNativeByTNode(tNode, lView) instanceof HTMLUnknownElement) ||
9895
(typeof customElements !== 'undefined' &&
9996
tagName.indexOf('-') > -1 &&
10097
!customElements.get(tagName));
10198

102-
if (isUnknown && !matchingSchemas(schemas, tagName)) {
99+
if (isUnknown && !matchingSchemas(tView.schemas, tagName)) {
103100
const isHostStandalone = isHostComponentStandalone(lView);
104101
const templateLocation = getTemplateLocationDetails(lView);
105102
const schemas = `'${isHostStandalone ? '@Component' : '@NgModule'}.schemas'`;

packages/core/src/render3/instructions/shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,6 @@ function setInputsFromAttrs<T>(
568568
/** Shared code between instructions that indicate the start of an element. */
569569
export function elementLikeStartShared(
570570
lView: LView,
571-
tView: TView,
572571
index: number,
573572
type: TNodeType.Element | TNodeType.ElementContainer,
574573
name: string,
@@ -584,9 +583,9 @@ export function elementLikeStartShared(
584583
localRefsIndex: number | undefined,
585584
) {
586585
const adjustedIndex = HEADER_OFFSET + index;
587-
const isElement = type === TNodeType.Element;
588586
ngDevMode && assertIndexInRange(lView, adjustedIndex);
589587

588+
const tView = lView[TVIEW];
590589
const tNode = tView.firstCreatePass
591590
? elementLikeStartFirstCreatePass(
592591
adjustedIndex,
@@ -606,6 +605,7 @@ export function elementLikeStartShared(
606605
setCurrentTNode(tNode, true);
607606

608607
// It's important that this runs before we've instantiated the directives.
608+
const isElement = type === TNodeType.Element;
609609
if (isElement) {
610610
setupStaticAttributes(lView[RENDERER], native as RElement, tNode);
611611

0 commit comments

Comments
 (0)