Skip to content

Commit caaa7ca

Browse files
refactor(core): simplify attributes extraction logic for ComponentRef (#59678) (#59735)
Make extractAttrsAndClassesFromSelector to return TAttributes directly to simplify the overall logic and remove unecessary code. PR Close #59678 PR Close #59735
1 parent 0a9c03c commit caaa7ca

File tree

4 files changed

+17
-28
lines changed

4 files changed

+17
-28
lines changed

packages/core/src/render3/component_ref.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import {ComponentDef, DirectiveDef, HostDirectiveDefs} from './interfaces/defini
5252
import {InputFlags} from './interfaces/input_flags';
5353
import {
5454
NodeInputBindings,
55-
TAttributes,
5655
TContainerNode,
5756
TElementContainerNode,
5857
TElementNode,
@@ -73,9 +72,7 @@ import {MATH_ML_NAMESPACE, SVG_NAMESPACE} from './namespaces';
7372

7473
import {ChainedInjector} from './chained_injector';
7574
import {createElementNode, setupStaticAttributes} from './dom_node_manipulation';
76-
import {AttributeMarker} from './interfaces/attribute_marker';
7775
import {unregisterLView} from './interfaces/lview_tracking';
78-
import {CssSelector} from './interfaces/projection';
7976
import {
8077
extractAttrsAndClassesFromSelector,
8178
stringifyCSSSelectorList,
@@ -157,18 +154,6 @@ function getNamespace(elementName: string): string | null {
157154
return name === 'svg' ? SVG_NAMESPACE : name === 'math' ? MATH_ML_NAMESPACE : null;
158155
}
159156

160-
// TODO(pk): change the extractAttrsAndClassesFromSelector so it returns TAttributes already?
161-
function getRootTAttributesFromSelector(selector: CssSelector) {
162-
const {attrs, classes} = extractAttrsAndClassesFromSelector(selector);
163-
164-
const tAtts: TAttributes = attrs;
165-
if (classes.length) {
166-
tAtts.push(AttributeMarker.Classes, ...classes);
167-
}
168-
169-
return tAtts;
170-
}
171-
172157
/**
173158
* ComponentFactory interface implementation.
174159
*/
@@ -214,9 +199,7 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
214199
super();
215200
this.componentType = componentDef.type;
216201
this.selector = stringifyCSSSelectorList(componentDef.selectors);
217-
this.ngContentSelectors = componentDef.ngContentSelectors
218-
? componentDef.ngContentSelectors
219-
: [];
202+
this.ngContentSelectors = componentDef.ngContentSelectors ?? [];
220203
this.isBoundToModule = !!ngModule;
221204
}
222205

@@ -369,7 +352,7 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
369352
const tAttributes = rootSelectorOrNode
370353
? ['ng-version', '0.0.0-PLACEHOLDER']
371354
: // Extract attributes and classes from the first selector only to match VE behavior.
372-
getRootTAttributesFromSelector(this.componentDef.selectors[0]);
355+
extractAttrsAndClassesFromSelector(this.componentDef.selectors[0]);
373356

374357
// TODO: this logic is shared with the element instruction first create pass
375358
const hostTNode = getOrCreateTNode(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ export function getInitialLViewFlagsFromDef(def: ComponentDef<unknown>): LViewFl
12791279
return flags;
12801280
}
12811281

1282-
export function createComponentLView<T>(
1282+
function createComponentLView<T>(
12831283
lView: LView,
12841284
hostTNode: TElementNode,
12851285
def: ComponentDef<T>,

packages/core/src/render3/node_selector_matcher.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,8 @@ export function stringifyCSSSelectorList(selectorList: CssSelectorList): string
440440
* @param selector CSS selector in parsed form (in a form of array)
441441
* @returns object with `attrs` and `classes` fields that contain extracted information
442442
*/
443-
export function extractAttrsAndClassesFromSelector(selector: CssSelector): {
444-
attrs: string[];
445-
classes: string[];
446-
} {
447-
const attrs: string[] = [];
443+
export function extractAttrsAndClassesFromSelector(selector: CssSelector): TAttributes {
444+
const attrs: TAttributes = [];
448445
const classes: string[] = [];
449446
let i = 1;
450447
let mode = SelectorFlags.ATTRIBUTE;
@@ -467,5 +464,9 @@ export function extractAttrsAndClassesFromSelector(selector: CssSelector): {
467464
}
468465
i++;
469466
}
470-
return {attrs, classes};
467+
if (classes.length) {
468+
attrs.push(AttributeMarker.Classes, ...classes);
469+
}
470+
471+
return attrs;
471472
}

packages/core/test/render3/node_selector_matcher_spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,13 @@ describe('extractAttrsAndClassesFromSelector', () => {
741741
cases.forEach(([selector, attrs, classes]) => {
742742
it(`should process ${JSON.stringify(selector)} selector`, () => {
743743
const extracted = extractAttrsAndClassesFromSelector(selector);
744-
expect(extracted.attrs).toEqual(attrs as string[]);
745-
expect(extracted.classes).toEqual(classes as string[]);
744+
const cssClassMarker = extracted.indexOf(AttributeMarker.Classes);
745+
746+
const extractedAttrs = cssClassMarker > -1 ? extracted.slice(0, cssClassMarker) : extracted;
747+
const extractedClasses = cssClassMarker > -1 ? extracted.slice(cssClassMarker + 1) : [];
748+
749+
expect(extractedAttrs).toEqual(attrs as string[]);
750+
expect(extractedClasses).toEqual(classes as string[]);
746751
});
747752
});
748753
});

0 commit comments

Comments
 (0)