Skip to content

Commit 9f3ae01

Browse files
committed
feat(platform-browser): - Updates from pr comments
1 parent f8deb43 commit 9f3ae01

File tree

3 files changed

+29
-41
lines changed

3 files changed

+29
-41
lines changed

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

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ export class DomRendererFactory2 implements RendererFactory2, OnDestroy {
242242
ngZone,
243243
platformIsServer,
244244
tracingService,
245-
undefined,
246245
this.styleScopeService,
246+
undefined,
247247
);
248248
break;
249249
}
@@ -503,7 +503,7 @@ function isTemplateNode(node: any): node is HTMLTemplateElement {
503503
}
504504

505505
class ShadowDomRenderer extends DefaultDomRenderer2 {
506-
private shadowRoot: ShadowRoot | HTMLElement;
506+
private shadowRoot: ShadowRoot;
507507

508508
constructor(
509509
eventManager: EventManager,
@@ -515,12 +515,12 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
515515
platformIsServer: boolean,
516516
tracingService: TracingService<TracingSnapshot> | null,
517517
private sharedStylesHost: SharedStylesHost,
518-
private styleScopeService?: IsolatedStyleScopeService,
518+
private styleScopeService: IsolatedStyleScopeService,
519519
) {
520520
super(eventManager, doc, ngZone, platformIsServer, tracingService);
521521

522522
// Only create shadow root in browser environments
523-
if (typeof ShadowRoot !== 'undefined' && !platformIsServer) {
523+
if (!platformIsServer) {
524524
this.shadowRoot = (hostEl as HTMLElement).attachShadow({mode: 'open'});
525525
} else {
526526
// In SSR or environments without shadow DOM support, throw
@@ -533,12 +533,7 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
533533
const isIsolated = component.encapsulation === ViewEncapsulation.IsolatedShadowDom;
534534

535535
// Register shadow root with StyleScopeService for style targeting (only if service available and in browser)
536-
if (
537-
this.styleScopeService &&
538-
typeof ShadowRoot !== 'undefined' &&
539-
!platformIsServer &&
540-
this.shadowRoot instanceof ShadowRoot
541-
) {
536+
if (this.styleScopeService) {
542537
if (isIsolated) {
543538
this.styleScopeService.registerIsolatedShadowRoot(this.shadowRoot);
544539
this.sharedStylesHost.addShadowRoot(this.shadowRoot);
@@ -555,13 +550,7 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
555550
styles = addBaseHrefToCssSourceMap(baseHref, styles);
556551
}
557552

558-
if (
559-
isIsolated &&
560-
this.styleScopeService &&
561-
typeof ShadowRoot !== 'undefined' &&
562-
!platformIsServer &&
563-
this.shadowRoot instanceof ShadowRoot
564-
) {
553+
if (isIsolated && !platformIsServer) {
565554
// For IsolatedShadowDom, use SharedStylesHost with shadowRoot targeting
566555
this.sharedStylesHost.addStyles(styles, component.getExternalStyles?.(), this.shadowRoot);
567556
} else {
@@ -620,12 +609,7 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
620609

621610
override destroy() {
622611
// Query the service to determine what type this shadow root is (only if service available and in browser)
623-
if (
624-
!this.platformIsServer &&
625-
this.styleScopeService &&
626-
typeof ShadowRoot !== 'undefined' &&
627-
this.shadowRoot instanceof ShadowRoot
628-
) {
612+
if (!this.platformIsServer) {
629613
if (this.styleScopeService.isIsolatedShadowRoot(this.shadowRoot)) {
630614
this.styleScopeService.deregisterIsolatedShadowRoot(this.shadowRoot);
631615
this.sharedStylesHost.removeShadowRoot(this.shadowRoot);
@@ -650,8 +634,8 @@ class NoneEncapsulationDomRenderer extends DefaultDomRenderer2 {
650634
ngZone: NgZone,
651635
platformIsServer: boolean,
652636
tracingService: TracingService<TracingSnapshot> | null,
637+
protected styleScopeService: IsolatedStyleScopeService,
653638
compId?: string,
654-
protected styleScopeService?: IsolatedStyleScopeService,
655639
) {
656640
super(eventManager, doc, ngZone, platformIsServer, tracingService);
657641
let styles = component.styles;
@@ -665,9 +649,9 @@ class NoneEncapsulationDomRenderer extends DefaultDomRenderer2 {
665649
this.styleUrls = component.getExternalStyles?.(compId);
666650
}
667651

668-
applyStyles(element?: HTMLElement): void {
652+
applyStyles(element: HTMLElement): void {
669653
// Check if we should target specific shadow roots (only if service and element available)
670-
if (element && this.styleScopeService && !this.platformIsServer) {
654+
if (element && !this.platformIsServer) {
671655
const targetShadowRoots = this.styleScopeService.determineStyleTargets(element);
672656
if (targetShadowRoots.length > 0) {
673657
for (const shadowRoot of targetShadowRoots) {
@@ -705,7 +689,7 @@ class EmulatedEncapsulationDomRenderer2 extends NoneEncapsulationDomRenderer {
705689
ngZone: NgZone,
706690
platformIsServer: boolean,
707691
tracingService: TracingService<TracingSnapshot> | null,
708-
styleScopeService?: IsolatedStyleScopeService,
692+
styleScopeService: IsolatedStyleScopeService,
709693
) {
710694
const compId = appId + '-' + component.id;
711695
super(
@@ -717,19 +701,17 @@ class EmulatedEncapsulationDomRenderer2 extends NoneEncapsulationDomRenderer {
717701
ngZone,
718702
platformIsServer,
719703
tracingService,
720-
compId,
721704
styleScopeService,
705+
compId,
722706
);
723707
this.contentAttr = shimContentAttribute(compId);
724708
this.hostAttr = shimHostAttribute(compId);
725709
}
726710

727711
applyToHost(element: HTMLElement): void {
728-
// Set host attribute first
729-
this.setAttribute(element, this.hostAttr, '');
730-
731712
// Use inherited applyStyles method to handle shadow root targeting and fallback
732713
this.applyStyles(element);
714+
this.setAttribute(element, this.hostAttr, '');
733715
}
734716

735717
override createElement(parent: any, name: string): Element {

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,40 @@ export class IsolatedStyleScopeService {
3636
}
3737

3838
/**
39-
* Determines where styles should be applied:
40-
* - For elements in isolated shadow roots: return only that shadow root
41-
* - For all other cases: return empty array to indicate default broadcast behavior
42-
* - Prioritize projected content (assignedSlot) over direct containment
39+
* Determines where styles should be applied by checking the immediate shadow DOM context.
40+
* Handles both projected content and elements inside shadow roots.
4341
*/
4442
determineStyleTargets(element: HTMLElement): ShadowRoot[] {
4543
// Check if we're in an environment that supports ShadowRoot (browser only)
4644
if (typeof ShadowRoot === 'undefined') {
4745
return [];
4846
}
4947

50-
// Priority 1: Check if element's parent is a shadow root host
48+
// Priority 1: Check if element's parent is a shadow root host (handles projected content)
5149
const parent = element.parentElement;
5250
if (parent) {
5351
for (const [shadowRoot, host] of [...this.standardShadowRoots, ...this.isolatedShadowRoots]) {
5452
if (host === parent) {
55-
return this.isIsolatedShadowRoot(shadowRoot) ? [shadowRoot] : [];
53+
if (this.isIsolatedShadowRoot(shadowRoot)) {
54+
return [shadowRoot]; // Isolated: apply styles to this one root
55+
} else {
56+
return Array.from(this.standardShadowRoots.keys()); // Standard: apply to all standard roots
57+
}
5658
}
5759
}
5860
}
5961

60-
// Priority 2: Check if element is directly in a shadow root
62+
// Priority 2: Check what shadow root context this element is in (handles elements inside shadow roots)
6163
const elementRoot = element.getRootNode();
6264
if (elementRoot instanceof ShadowRoot && this.isRegisteredShadowRoot(elementRoot)) {
63-
return this.isIsolatedShadowRoot(elementRoot) ? [elementRoot] : [];
65+
if (this.isIsolatedShadowRoot(elementRoot)) {
66+
return [elementRoot]; // Isolated: apply styles to this one root
67+
} else {
68+
return Array.from(this.standardShadowRoots.keys()); // Standard: apply to all standard roots
69+
}
6470
}
6571

66-
// Default: return an empty array to indicate default broadcast behavior
72+
// Element is not in a shadow root context, use broadcast behavior
6773
return [];
6874
}
6975

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ export class SharedStylesHost implements OnDestroy {
392392
removeShadowRoot(shadowRoot: ShadowRoot): void {
393393
const index = this.isolatedShadowRoots.indexOf(shadowRoot);
394394

395-
if (ngDevMode && index === -1) {
395+
if (typeof ngDevMode !== 'undefined' && ngDevMode && ngDevMode && index === -1) {
396396
throw new Error('Attempted to remove shadow root that was not previously added.');
397397
}
398398

0 commit comments

Comments
 (0)