MCPcopy Create free account
hub / github.com/angular/angular / ShadowDomRenderer

Class ShadowDomRenderer

packages/platform-browser/src/dom/dom_renderer.ts:558–645  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

556}
557
558class ShadowDomRenderer extends DefaultDomRenderer2 {
559 private shadowRoot: any;
560
561 constructor(
562 eventManager: EventManager,
563 private hostEl: any,
564 component: RendererType2,
565 doc: Document,
566 ngZone: NgZone,
567 nonce: string | null,
568 tracingService: TracingService<TracingSnapshot> | null,
569 cssVarNamespace: string,
570 private sharedStylesHost?: SharedStylesHost,
571 ) {
572 super(eventManager, doc, ngZone, tracingService, cssVarNamespace);
573 this.shadowRoot = (hostEl as any).attachShadow({mode: 'open'});
574
575 // SharedStylesHost is used to add styles to the shadow root by ShadowDom.
576 // This is optional as it is not used by ExperimentalIsolatedShadowDom.
577 if (this.sharedStylesHost) {
578 this.sharedStylesHost.addHost(this.shadowRoot);
579 }
580 let styles = component.styles;
581 if (ngDevMode) {
582 // We only do this in development, as for production users should not add CSS sourcemaps to components.
583 const baseHref = getDOM().getBaseHref(doc) ?? '';
584 styles = addBaseHrefToCssSourceMap(baseHref, styles);
585 }
586
587 styles = shimStylesContent(component.id, styles).map((s) =>
588 s.replace(/%NS%/g, cssVarNamespace),
589 );
590
591 for (const style of styles) {
592 const styleEl = document.createElement('style');
593
594 if (nonce) {
595 styleEl.setAttribute('nonce', nonce);
596 }
597
598 styleEl.textContent = style;
599 this.shadowRoot.appendChild(styleEl);
600 }
601
602 // Apply any external component styles to the shadow root for the component's element.
603 // The ShadowDOM renderer uses an alternative execution path for component styles that
604 // does not use the SharedStylesHost that other encapsulation modes leverage. Much like
605 // the manual addition of embedded styles directly above, any external stylesheets
606 // must be manually added here to ensure ShadowDOM components are correctly styled.
607 // TODO: Consider reworking the DOM Renderers to consolidate style handling.
608 const styleUrls = component.getExternalStyles?.();
609 if (styleUrls) {
610 for (const styleUrl of styleUrls) {
611 const linkEl = createLinkElement(styleUrl, doc);
612 if (nonce) {
613 linkEl.setAttribute('nonce', nonce);
614 }
615 this.shadowRoot.appendChild(linkEl);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected