Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/core/src/render3/features/external_styles_feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ export function ɵɵExternalStylesFeature(styleUrls: string[]): ComponentDefFeat
}

definition.getExternalStyles = (encapsulationId) => {
// Add encapsulation ID search parameter `component` to support external style encapsulation
// Add encapsulation ID search parameter `ngcomp` to support external style encapsulation as well as the encapsulation mode
// for usage tracking.
const urls = styleUrls.map(
(value) =>
value + '?ngcomp' + (encapsulationId ? '=' + encodeURIComponent(encapsulationId) : ''),
value +
'?ngcomp' +
(encapsulationId ? '=' + encodeURIComponent(encapsulationId) : '') +
'&e=' +
definition.encapsulation,
);

return urls;
Expand Down
19 changes: 18 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,23 @@ 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.
// The ShadowDOM renderer uses an alternative execution path for component styles that
// does not use the SharedStylesHost that other encapsulation modes leverage. Much like
// the manual addition of embedded styles directly above, any external stylesheets
// must be manually added here to ensure ShadowDOM components are correctly styled.
// TODO: Consider reworking the DOM Renderers to consolidate style handling.
const styleUrls = component.getExternalStyles?.();
Comment thread
clydin marked this conversation as resolved.
Outdated
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