Skip to content

Commit

Permalink
fix(platform-browser): correctly add external stylesheets to ShadowDO…
Browse files Browse the repository at this point in the history
…M components

Angular components that use ShadowDOM view encapsulation have an alternate
execution path for adding component styles to the DOM that does not use the
SharedStylesHost that all other view encapsulation modes leverage. To ensure
that ShadowDOM components receive all defined styles, additional logic has been
added to the ShadowDOM specific renderer to also cover external styles.
  • Loading branch information
clydin committed Nov 2, 2024
1 parent 7d0ba0c commit 562d8e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/platform-browser/src/dom/dom_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import {RuntimeErrorCode} from '../errors';

import {EventManager} from './events/event_manager';
import {SharedStylesHost} from './shared_styles_host';
import {createLinkElement, SharedStylesHost} from './shared_styles_host';

export const NAMESPACE_URIS: {[ns: string]: string} = {
'svg': 'http://www.w3.org/2000/svg',
Expand Down Expand Up @@ -434,6 +434,18 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
styleEl.textContent = style;
this.shadowRoot.appendChild(styleEl);
}

// Apply any external component styles to the shadow root for the component's element
const styleUrls = component.getExternalStyles?.(component.id);
if (styleUrls) {
for (const styleUrl of styleUrls) {
const linkEl = createLinkElement(styleUrl, doc);
if (nonce) {
linkEl.setAttribute('nonce', nonce);
}
this.shadowRoot.appendChild(linkEl);
}
}
}

private nodeOrShadowRoot(node: any): any {
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-browser/src/dom/shared_styles_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function addServerStyles(
* @param doc A DOM Document to use to create the element.
* @returns An HTMLLinkElement instance.
*/
function createLinkElement(url: string, doc: Document): HTMLLinkElement {
export function createLinkElement(url: string, doc: Document): HTMLLinkElement {
const linkElement = doc.createElement('link');
linkElement.setAttribute('rel', 'stylesheet');
linkElement.setAttribute('href', url);
Expand Down

0 comments on commit 562d8e9

Please sign in to comment.