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

Class ShadowDomRenderer

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected