Skip to content

Commit 5039a7e

Browse files
committed
feat(platform-browser): Fixes IsolatedShadowDom encapsulation method
Test fixes
1 parent 21f3ef4 commit 5039a7e

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {DOCUMENT} from '@angular/common';
9+
import {DOCUMENT, isPlatformServer} from '@angular/common';
1010
import {
1111
APP_ID,
1212
CSP_NONCE,
@@ -123,7 +123,7 @@ export class SharedStylesHost implements OnDestroy {
123123
@Inject(CSP_NONCE) @Optional() private readonly nonce?: string | null,
124124
// Cannot remove it due to backward compatibility
125125
// (it seems some TGP targets might be calling this constructor directly).
126-
@Inject(PLATFORM_ID) platformId: object = {},
126+
@Inject(PLATFORM_ID) private readonly platformId: object = {},
127127
) {
128128
addServerStyles(doc, appId, this.inline, this.external);
129129
this.hosts.add(doc.head);
@@ -236,17 +236,12 @@ export class SharedStylesHost implements OnDestroy {
236236
if (this.nonce) {
237237
element.setAttribute('nonce', this.nonce);
238238
}
239-
240-
const styleElements = (host as Element).querySelectorAll('style, link[rel="stylesheet"]');
241-
const lastStyleElement =
242-
styleElements.length > 0 ? styleElements[styleElements.length - 1] : null;
243-
244-
if (lastStyleElement) {
245-
host.insertBefore(element, lastStyleElement.nextSibling);
246-
} else {
247-
host.insertBefore(element, host.firstChild);
239+
// The `ng-app-id` attribute is used on the server to identify which styles were
240+
// created by the server renderer.
241+
if (isPlatformServer(this.platformId) && host === this.doc.head) {
242+
element.setAttribute(APP_ID_ATTRIBUTE_NAME, this.appId);
248243
}
249-
244+
host.appendChild(element);
250245
return element;
251246
}
252247

packages/platform-browser/test/dom/dom_renderer_spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ describe('DefaultDomRendererV2', () => {
137137
expect(window.getComputedStyle(shadow).color).toEqual('rgb(255, 0, 0)');
138138

139139
const emulated = fixture.debugElement.query(By.css('.emulated')).nativeElement;
140-
expect(window.getComputedStyle(emulated).color).toEqual('rgb(255, 0, 0)');
140+
expect(window.getComputedStyle(emulated).color).toEqual('rgb(0, 0, 255)');
141141

142142
const none = fixture.debugElement.query(By.css('.none')).nativeElement;
143-
expect(window.getComputedStyle(none).color).toEqual('rgb(255, 0, 0)');
143+
expect(window.getComputedStyle(none).color).toEqual('rgb(0, 255, 0)');
144144
});
145145

146146
it('child components of shadow components should inherit browser defaults rather than their component styles', () => {
@@ -153,10 +153,10 @@ describe('DefaultDomRendererV2', () => {
153153
expect(window.getComputedStyle(shadow).backgroundColor).toEqual('rgba(0, 0, 0, 0)');
154154

155155
const emulated = fixture.debugElement.query(By.css('.emulated')).nativeElement;
156-
expect(window.getComputedStyle(emulated).backgroundColor).toEqual('rgba(0, 0, 0, 0)');
156+
expect(window.getComputedStyle(emulated).backgroundColor).toEqual('rgb(0, 0, 255)');
157157

158158
const none = fixture.debugElement.query(By.css('.none')).nativeElement;
159-
expect(window.getComputedStyle(none).backgroundColor).toEqual('rgba(0, 0, 0, 0)');
159+
expect(window.getComputedStyle(none).backgroundColor).toEqual('rgb(0, 255, 0)');
160160
});
161161

162162
it('shadow components should not be polluted by child components styles when using IsolatedShadowDom', () => {

0 commit comments

Comments
 (0)