Skip to content

Commit 7f43620

Browse files
committed
wip
1 parent 075d9e5 commit 7f43620

File tree

11 files changed

+50
-12
lines changed

11 files changed

+50
-12
lines changed

packages/core/src/render3/component_ref.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
LView,
5757
LViewEnvironment,
5858
LViewFlags,
59+
ON_DESTROY_HOOKS,
5960
TView,
6061
TVIEW,
6162
TViewType,
@@ -84,6 +85,9 @@ import {BINDING, Binding, BindingInternal, DirectiveWithBindings} from './dynami
8485
import {NG_REFLECT_ATTRS_FLAG, NG_REFLECT_ATTRS_FLAG_DEFAULT} from '../ng_reflect';
8586
import {TracingService} from '../application/tracing';
8687
import {getComponentName} from '../internal/get_closest_component_name';
88+
import {SHARED_STYLES_HOST} from './interfaces/shared_styles_host';
89+
import {DOCUMENT} from '../document';
90+
import {getDocument} from './interfaces/document';
8791

8892
export class ComponentFactoryResolver extends AbstractComponentFactoryResolver {
8993
/**
@@ -318,6 +322,14 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
318322
const hostElement = rootSelectorOrNode
319323
? locateHostElement(hostRenderer, rootSelectorOrNode, cmpDef.encapsulation, rootViewInjector)
320324
: createHostElement(cmpDef, hostRenderer);
325+
326+
const sharedStylesHost = rootViewInjector.get(SHARED_STYLES_HOST, null);
327+
const styleHost = getStyleHost(
328+
hostElement,
329+
() => rootViewInjector.get(DOCUMENT, null) ?? getDocument(),
330+
);
331+
if (sharedStylesHost) sharedStylesHost.addHost(styleHost);
332+
321333
const hasInputBindings =
322334
componentBindings?.some(isInputBinding) ||
323335
directives?.some((d) => typeof d !== 'function' && d.bindings.some(isInputBinding));
@@ -335,6 +347,15 @@ export class ComponentFactory<T> extends AbstractComponentFactory<T> {
335347
null,
336348
retrieveHydrationInfo(hostElement, rootViewInjector, true /* isRootView */),
337349
);
350+
if (sharedStylesHost) {
351+
rootLView[ON_DESTROY_HOOKS] ??= [];
352+
rootLView[ON_DESTROY_HOOKS].push(() => {
353+
// Either need to continue to leak styles to an unused shadow root (and keep it in
354+
// memory even when detached from the DOM) or assume the shadow root contains at most
355+
// one Angular root view at a time and drop it.
356+
sharedStylesHost.removeHost(styleHost);
357+
});
358+
}
338359

339360
rootLView[HEADER_OFFSET] = hostElement;
340361

@@ -482,6 +503,20 @@ function createRootTView(
482503
return rootTView;
483504
}
484505

506+
function getStyleHost(node: RNode, doc: () => Document): Node {
507+
const rootNode = node.getRootNode?.();
508+
509+
if (typeof Document !== 'undefined' && rootNode instanceof Document) {
510+
return rootNode.head; // Connected to document.
511+
} else if (!rootNode) {
512+
return doc().head; // `getRootNode` not supported, Node.js use case.
513+
} else if (typeof ShadowRoot !== 'undefined' && rootNode instanceof ShadowRoot) {
514+
return rootNode as Node; // Shadow root
515+
} else {
516+
return doc().head; // Disconnected element, use fallback document.
517+
}
518+
}
519+
485520
function getRootTViewTemplate(
486521
creationBindings: Binding[] | null,
487522
updateBindings: Binding[] | null,

packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@
505505
"getDOM",
506506
"getDeclarationTNode",
507507
"getDirectiveDef",
508+
"getDocument",
508509
"getElementDepthCount",
509510
"getFactoryDef",
510511
"getFirstLContainer",
@@ -550,6 +551,7 @@
550551
"getSelectedIndex",
551552
"getSelectedTNode",
552553
"getSimpleChangesStore",
554+
"getStyleHost",
553555
"getTNode",
554556
"getTNodeFromLView",
555557
"getTView",

packages/core/test/bundling/create_component/bundle.golden_symbols.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@
408408
"getDOM",
409409
"getDeclarationTNode",
410410
"getDirectiveDef",
411+
"getDocument",
411412
"getFactoryDef",
412413
"getFirstLContainer",
413414
"getFirstNativeNode",
@@ -454,6 +455,7 @@
454455
"getSelectedIndex",
455456
"getSelectedTNode",
456457
"getSimpleChangesStore",
458+
"getStyleHost",
457459
"getTNode",
458460
"getTNodeFromLView",
459461
"getTView",

packages/core/test/bundling/defer/bundle.golden_symbols.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@
451451
"getDeclarationTNode",
452452
"getDeferBlockDataIndex",
453453
"getDirectiveDef",
454+
"getDocument",
454455
"getElementDepthCount",
455456
"getFactoryDef",
456457
"getFirstLContainer",
@@ -500,6 +501,7 @@
500501
"getSelectedIndex",
501502
"getSelectedTNode",
502503
"getSimpleChangesStore",
504+
"getStyleHost",
503505
"getTDeferBlockDetails",
504506
"getTNode",
505507
"getTNodeFromLView",

packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@
594594
"getDOM",
595595
"getDeclarationTNode",
596596
"getDirectiveDef",
597+
"getDocument",
597598
"getElementDepthCount",
598599
"getFactoryDef",
599600
"getFactoryOf",
@@ -647,6 +648,7 @@
647648
"getSelectedIndex",
648649
"getSelectedTNode",
649650
"getSimpleChangesStore",
651+
"getStyleHost",
650652
"getSuperType",
651653
"getSymbolIterator",
652654
"getTNode",

packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@
590590
"getDOM",
591591
"getDeclarationTNode",
592592
"getDirectiveDef",
593+
"getDocument",
593594
"getElementDepthCount",
594595
"getFactoryDef",
595596
"getFactoryOf",
@@ -643,6 +644,7 @@
643644
"getSelectedIndex",
644645
"getSelectedTNode",
645646
"getSimpleChangesStore",
647+
"getStyleHost",
646648
"getSuperType",
647649
"getSymbolIterator",
648650
"getTNode",

packages/core/test/bundling/hydration/bundle.golden_symbols.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@
521521
"getSelectedIndex",
522522
"getSerializedContainerViews",
523523
"getSimpleChangesStore",
524+
"getStyleHost",
524525
"getSymbolIterator",
525526
"getTNode",
526527
"getTNodeFromLView",

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@
669669
"getDataKeys",
670670
"getDeclarationTNode",
671671
"getDirectiveDef",
672+
"getDocument",
672673
"getElementDepthCount",
673674
"getFactoryDef",
674675
"getFactoryOf",
@@ -732,6 +733,7 @@
732733
"getSelectedIndex",
733734
"getSelectedTNode",
734735
"getSimpleChangesStore",
736+
"getStyleHost",
735737
"getSymbolIterator",
736738
"getTNode",
737739
"getTNodeFromLView",

packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@
376376
"getDOM",
377377
"getDeclarationTNode",
378378
"getDirectiveDef",
379+
"getDocument",
379380
"getFactoryDef",
380381
"getFirstLContainer",
381382
"getGlobalLocale",
@@ -415,6 +416,7 @@
415416
"getRuntimeErrorCode",
416417
"getSelectedIndex",
417418
"getSimpleChangesStore",
419+
"getStyleHost",
418420
"getTNode",
419421
"getTNodeFromLView",
420422
"getTView",

packages/platform-browser/src/dom/shared_styles_host.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ export class SharedStylesHost implements ɵSharedStylesHost, OnDestroy {
130130
@Inject(PLATFORM_ID) platformId: object = {},
131131
) {
132132
addServerStyles(doc, appId, this.inline, this.external);
133-
this.hosts.add(doc.head);
134133
}
135134

136135
addStyles(styles: string[], urls?: string[]): void {

0 commit comments

Comments
 (0)