| 492 | } |
| 493 | |
| 494 | class ShadowDomRenderer extends DefaultDomRenderer2 { |
| 495 | private shadowRoot: any; |
| 496 | |
| 497 | constructor( |
| 498 | eventManager: EventManager, |
| 499 | private hostEl: any, |
| 500 | component: RendererType2, |
| 501 | doc: Document, |
| 502 | ngZone: NgZone, |
| 503 | nonce: string | null, |
| 504 | tracingService: TracingService<TracingSnapshot> | null, |
| 505 | private sharedStylesHost?: SharedStylesHost, |
| 506 | ) { |
| 507 | super(eventManager, doc, ngZone, tracingService); |
| 508 | this.shadowRoot = (hostEl as any).attachShadow({mode: 'open'}); |
| 509 | |
| 510 | // SharedStylesHost is used to add styles to the shadow root by ShadowDom. |
| 511 | // This is optional as it is not used by ExperimentalIsolatedShadowDom. |
| 512 | if (this.sharedStylesHost) { |
| 513 | this.sharedStylesHost.addHost(this.shadowRoot); |
| 514 | } |
| 515 | let styles = component.styles; |
| 516 | if (ngDevMode) { |
| 517 | // We only do this in development, as for production users should not add CSS sourcemaps to components. |
| 518 | const baseHref = getDOM().getBaseHref(doc) ?? ''; |
| 519 | styles = addBaseHrefToCssSourceMap(baseHref, styles); |
| 520 | } |
| 521 | |
| 522 | styles = shimStylesContent(component.id, styles); |
| 523 | |
| 524 | for (const style of styles) { |
| 525 | const styleEl = document.createElement('style'); |
| 526 | |
| 527 | if (nonce) { |
| 528 | styleEl.setAttribute('nonce', nonce); |
| 529 | } |
| 530 | |
| 531 | styleEl.textContent = style; |
| 532 | this.shadowRoot.appendChild(styleEl); |
| 533 | } |
| 534 | |
| 535 | // Apply any external component styles to the shadow root for the component's element. |
| 536 | // The ShadowDOM renderer uses an alternative execution path for component styles that |
| 537 | // does not use the SharedStylesHost that other encapsulation modes leverage. Much like |
| 538 | // the manual addition of embedded styles directly above, any external stylesheets |
| 539 | // must be manually added here to ensure ShadowDOM components are correctly styled. |
| 540 | // TODO: Consider reworking the DOM Renderers to consolidate style handling. |
| 541 | const styleUrls = component.getExternalStyles?.(); |
| 542 | if (styleUrls) { |
| 543 | for (const styleUrl of styleUrls) { |
| 544 | const linkEl = createLinkElement(styleUrl, doc); |
| 545 | if (nonce) { |
| 546 | linkEl.setAttribute('nonce', nonce); |
| 547 | } |
| 548 | this.shadowRoot.appendChild(linkEl); |
| 549 | } |
| 550 | } |
| 551 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…