Skip to content

Commit 24c26c4

Browse files
committed
fix(elements): added flag to adhere to style encapsulation
Added new API to let ShadowDom encapsulation to be isolated and avoid injection of shared styles (no UT merged)
1 parent 69a0025 commit 24c26c4

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

packages/platform-browser/src/dom/dom_renderer.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`;
3434
*/
3535
const REMOVE_STYLES_ON_COMPONENT_DESTROY_DEFAULT = true;
3636

37+
/**
38+
* The default value for the `ISOLATED_SHADOW_DOM` DI token.
39+
*/
40+
const ISOLATED_SHADOW_DOM_DEFAULT = false;
41+
3742
/**
3843
* A [DI token](guide/glossary#di-token "DI token definition") that indicates whether styles
3944
* of destroyed components should be removed from DOM.
@@ -47,6 +52,20 @@ export const REMOVE_STYLES_ON_COMPONENT_DESTROY =
4752
factory: () => REMOVE_STYLES_ON_COMPONENT_DESTROY_DEFAULT,
4853
});
4954

55+
/**
56+
* A [DI token](guide/glossary#di-token "DI token definition") that indicates whether the style
57+
* of components that are using ShadowDom as encapsulation must remain isolated from other
58+
* components instances styles and/or global styles.
59+
*
60+
* By default, the value is set to `false`.
61+
* @publicApi
62+
*/
63+
export const ISOLATED_SHADOW_DOM =
64+
new InjectionToken<boolean>(ngDevMode ? 'IsolatedShadowDom' : '', {
65+
providedIn: 'root',
66+
factory: () => ISOLATED_SHADOW_DOM_DEFAULT,
67+
});
68+
5069
export function shimContentAttribute(componentShortId: string): string {
5170
return CONTENT_ATTR.replace(COMPONENT_REGEX, componentShortId);
5271
}
@@ -71,6 +90,7 @@ export class DomRendererFactory2 implements RendererFactory2, OnDestroy {
7190
private readonly sharedStylesHost: SharedStylesHost,
7291
@Inject(APP_ID) private readonly appId: string,
7392
@Inject(REMOVE_STYLES_ON_COMPONENT_DESTROY) private removeStylesOnCompDestroy: boolean,
93+
@Inject(ISOLATED_SHADOW_DOM) private readonly isolatedShadowDom: boolean,
7494
@Inject(DOCUMENT) private readonly doc: Document,
7595
@Inject(PLATFORM_ID) readonly platformId: Object,
7696
readonly ngZone: NgZone,
@@ -123,7 +143,7 @@ export class DomRendererFactory2 implements RendererFactory2, OnDestroy {
123143
break;
124144
case ViewEncapsulation.ShadowDom:
125145
return new ShadowDomRenderer(
126-
eventManager, sharedStylesHost, element, type, doc, ngZone, this.nonce,
146+
eventManager, this.isolatedShadowDom ? null : sharedStylesHost, element, type, doc, ngZone, this.nonce,
127147
platformIsServer);
128148
default:
129149
renderer = new NoneEncapsulationDomRenderer(
@@ -358,7 +378,7 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
358378

359379
constructor(
360380
eventManager: EventManager,
361-
private sharedStylesHost: SharedStylesHost,
381+
private readonly sharedStylesHost: SharedStylesHost | null,
362382
private hostEl: any,
363383
component: RendererType2,
364384
doc: Document,
@@ -374,7 +394,8 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
374394
this.shadowRoot.innerHTML = "";
375395
}
376396

377-
this.sharedStylesHost.addHost(this.shadowRoot);
397+
this.sharedStylesHost?.addHost(this.shadowRoot);
398+
378399
const styles = shimStylesContent(component.id, component.styles);
379400

380401
for (const style of styles) {
@@ -407,7 +428,7 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
407428
}
408429

409430
override destroy() {
410-
this.sharedStylesHost.removeHost(this.shadowRoot);
431+
this.sharedStylesHost?.removeHost(this.shadowRoot);
411432
}
412433
}
413434

packages/platform-browser/src/platform-browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export {Meta, MetaDefinition} from './browser/meta';
7474
export {Title} from './browser/title';
7575
export {disableDebugTools, enableDebugTools} from './browser/tools/tools';
7676
export {By} from './dom/debug/by';
77-
export {REMOVE_STYLES_ON_COMPONENT_DESTROY} from './dom/dom_renderer';
77+
export {REMOVE_STYLES_ON_COMPONENT_DESTROY, ISOLATED_SHADOW_DOM} from './dom/dom_renderer';
7878
export {EVENT_MANAGER_PLUGINS, EventManager, EventManagerPlugin} from './dom/events/event_manager';
7979
export {HAMMER_GESTURE_CONFIG, HAMMER_LOADER, HammerGestureConfig, HammerLoader, HammerModule} from './dom/events/hammer_gestures';
8080
export {DomSanitizer, SafeHtml, SafeResourceUrl, SafeScript, SafeStyle, SafeUrl, SafeValue} from './security/dom_sanitization_service';

0 commit comments

Comments
 (0)