Skip to content

Commit

Permalink
fix(core): preserve static class/styles on root component
Browse files Browse the repository at this point in the history
Fix to preserve statically applied classes and styles on root component while using host binding.

Fixes angular#51460
  • Loading branch information
chintankavathia committed Sep 4, 2024
1 parent 8da9fb4 commit 0b35e23
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
27 changes: 25 additions & 2 deletions packages/core/src/render3/component_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {ComponentDef, DirectiveDef, HostDirectiveDefs} from './interfaces/defini
import {InputFlags} from './interfaces/input_flags';
import {
NodeInputBindings,
TAttributes,
TContainerNode,
TElementContainerNode,
TElementNode,
Expand Down Expand Up @@ -89,6 +90,7 @@ import {getComponentLViewByIndex, getNativeByTNode, getTNode} from './util/view_
import {ViewRef} from './view_ref';
import {ChainedInjector} from './chained_injector';
import {unregisterLView} from './interfaces/lview_tracking';
import {AttributeMarker} from './interfaces/attribute_marker';

export class ComponentFactoryResolver extends AbstractComponentFactoryResolver {
/**
Expand Down Expand Up @@ -499,10 +501,29 @@ function createRootComponentTNode(lView: LView, rNode: RNode): TElementNode {
ngDevMode && assertIndexInRange(lView, index);
lView[index] = rNode;

let attr: TAttributes | null = null;
if (rNode && 'classList' in rNode && (rNode as any).classList?.length) {
attr = [AttributeMarker.Classes, ...(Array.from((rNode as any).classList) as string[])];
}
if (rNode && 'getAttribute' in rNode && (rNode as any).getAttribute('style')?.length) {
attr = attr?.length ? [...attr, AttributeMarker.Styles] : [AttributeMarker.Styles];
const styleAttribute = (rNode as any).getAttribute('style');

const styles: string[] = styleAttribute.split(';');

styles.forEach((style) => {
const [property, value] = style.split(':').map((s) => s.trim());

if (property && value) {
attr!.push(property, value);
}
});
}

// '#host' is added here as we don't know the real host DOM name (we don't want to read it) and at
// the same time we want to communicate the debug `TNode` that this is a special `TNode`
// representing a host element.
return getOrCreateTNode(tView, index, TNodeType.Element, '#host', null);
return getOrCreateTNode(tView, index, TNodeType.Element, '#host', attr);
}

/**
Expand Down Expand Up @@ -576,7 +597,9 @@ function applyRootComponentStyling(
for (const def of rootDirectives) {
tNode.mergedAttrs = mergeHostAttrs(tNode.mergedAttrs, def.hostAttrs);
}

if (tNode.attrs !== null) {
tNode.mergedAttrs = mergeHostAttrs(tNode.mergedAttrs, tNode.attrs);
}
if (tNode.mergedAttrs !== null) {
computeStaticStyling(tNode, tNode.mergedAttrs, true);

Expand Down
26 changes: 26 additions & 0 deletions packages/core/test/acceptance/bootstrap_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ViewEncapsulation,
ɵNoopNgZone,
ɵZONELESS_ENABLED,
ElementRef,
} from '@angular/core';
import {bootstrapApplication, BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
Expand Down Expand Up @@ -82,6 +83,31 @@ describe('bootstrap', () => {
}),
);

it(
'should preserve static class and styles on root component',
withBody('<test-cmp class="foo" style="height: 100%"></test-cmp>', async () => {
@Component({
selector: 'test-cmp',
standalone: true,
template: '(test)',
host: {
class: 'baar',
style: 'width: 100%;',
},
})
class TestCmp {
constructor(public element: ElementRef) {}
}
const appRef = await bootstrapApplication(TestCmp);
expect(appRef.components[0].instance.element.nativeElement.className).toBe('baar foo');
expect(appRef.components[0].instance.element.nativeElement.getAttribute('style')).toBe(
'width: 100%; height: 100%;',
);

appRef.destroy();
}),
);

describe('options', () => {
function createComponentAndModule(
options: {
Expand Down

0 comments on commit 0b35e23

Please sign in to comment.