Skip to content

Commit 9636910

Browse files
alan-agius4pkozlowski-opensource
authored andcommitted
refactor(platform-browser): rename ng-app style attribute to ng-app-id (#49424)
`ng-app` is an AngularJS attribute, see https://docs.angularjs.org/api/ng/directive/ngApp. Using this attribute on a non AngularJS element can cause DI issues in AngularJS when running an AngularJS and Angular application on the same page. As such, we avoid such problems the Angular `ng-app` attribute is renamed to `ng-app-id`. PR Close #49424
1 parent 9ab9a2a commit 9636910

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
lines changed

integration/platform-server/e2e/helloworld-spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('Hello world E2E Tests', function() {
1515
// Load the page without waiting for Angular since it is not bootstrapped automatically.
1616
browser.driver.get(browser.baseUrl + 'helloworld');
1717

18-
const style = browser.driver.findElement(by.css('style[ng-app="ng"]'));
18+
const style = browser.driver.findElement(by.css('style[ng-app-id="ng"]'));
1919
expect(style.getText()).not.toBeNull();
2020

2121
// Test the contents from the server.
@@ -28,7 +28,7 @@ describe('Hello world E2E Tests', function() {
2828
expect(element(by.css('div')).getText()).toEqual('Hello world!');
2929

3030
// Make sure the server styles get reused by the client.
31-
expect(element(by.css('style[ng-app="ng"]')).isPresent()).toBeTruthy();
31+
expect(element(by.css('style[ng-app-id="ng"]')).isPresent()).toBeTruthy();
3232
expect(element(by.css('style[ng-style-reused]')).isPresent()).toBeTruthy();
3333
expect(element(by.css('style')).getText()).toBe('');
3434

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {DOCUMENT, isPlatformServer} from '@angular/common';
10-
import {APP_ID, Inject, Injectable, OnDestroy, PLATFORM_ID} from '@angular/core';
9+
import {DOCUMENT} from '@angular/common';
10+
import {APP_ID, Inject, Injectable, OnDestroy} from '@angular/core';
11+
12+
/** The style elements attribute name used to set value of `APP_ID` token. */
13+
const APP_ID_ATTRIBUTE_NAME = 'ng-app-id';
1114

1215
@Injectable()
1316
export class SharedStylesHost implements OnDestroy {
@@ -19,14 +22,12 @@ export class SharedStylesHost implements OnDestroy {
1922
> ();
2023
private readonly hostNodes = new Set<Node>();
2124
private readonly styleNodesInDOM: Map<string, HTMLStyleElement>|null;
22-
private readonly platformIsServer: boolean;
2325

2426
constructor(
25-
@Inject(DOCUMENT) private readonly doc: Document, @Inject(APP_ID) private appId: string,
26-
@Inject(PLATFORM_ID) readonly platformId: object = {}) {
27+
@Inject(DOCUMENT) private readonly doc: Document,
28+
@Inject(APP_ID) private readonly appId: string) {
2729
this.styleNodesInDOM = this.collectServerRenderedStyles();
2830
this.resetHostNodes();
29-
this.platformIsServer = isPlatformServer(platformId);
3031
}
3132

3233
addStyles(styles: string[]): void {
@@ -92,8 +93,8 @@ export class SharedStylesHost implements OnDestroy {
9293
}
9394

9495
private collectServerRenderedStyles(): Map<string, HTMLStyleElement>|null {
95-
const styles =
96-
this.doc.head?.querySelectorAll<HTMLStyleElement>(`style[ng-app="${this.appId}"]`);
96+
const styles = this.doc.head?.querySelectorAll<HTMLStyleElement>(
97+
`style[${APP_ID_ATTRIBUTE_NAME}="${this.appId}"]`);
9798

9899
if (styles?.length) {
99100
const styleMap = new Map<string, HTMLStyleElement>();
@@ -139,12 +140,7 @@ export class SharedStylesHost implements OnDestroy {
139140
} else {
140141
const styleEl = this.doc.createElement('style');
141142
styleEl.textContent = style;
142-
143-
if (this.platformIsServer) {
144-
// This `ng-app` attribute is only required when server rendering for style re-using
145-
// purposes.
146-
styleEl.setAttribute('ng-app', this.appId);
147-
}
143+
styleEl.setAttribute(APP_ID_ATTRIBUTE_NAME, this.appId);
148144

149145
return styleEl;
150146
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
2525
it('should add existing styles to new hosts', () => {
2626
ssh.addStyles(['a {};']);
2727
ssh.addHost(someHost);
28-
expect(someHost.innerHTML).toEqual('<style>a {};</style>');
28+
expect(someHost.innerHTML).toEqual('<style ng-app-id="app-id">a {};</style>');
2929
});
3030

3131
it('should add new styles to hosts', () => {
3232
ssh.addHost(someHost);
3333
ssh.addStyles(['a {};']);
34-
expect(someHost.innerHTML).toEqual('<style>a {};</style>');
34+
expect(someHost.innerHTML).toEqual('<style ng-app-id="app-id">a {};</style>');
3535
});
3636

3737
it('should add styles only once to hosts', () => {
3838
ssh.addStyles(['a {};']);
3939
ssh.addHost(someHost);
4040
ssh.addStyles(['a {};']);
41-
expect(someHost.innerHTML).toEqual('<style>a {};</style>');
41+
expect(someHost.innerHTML).toEqual('<style ng-app-id="app-id">a {};</style>');
4242
});
4343

4444
it('should use the document head as default host', () => {
@@ -49,7 +49,7 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
4949
it('should remove style nodes on destroy', () => {
5050
ssh.addStyles(['a {};']);
5151
ssh.addHost(someHost);
52-
expect(someHost.innerHTML).toEqual('<style>a {};</style>');
52+
expect(someHost.innerHTML).toEqual('<style ng-app-id="app-id">a {};</style>');
5353

5454
ssh.ngOnDestroy();
5555
expect(someHost.innerHTML).toEqual('');

packages/platform-server/test/integration_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ describe('platform-server integration', () => {
556556
});
557557
}));
558558

559-
it('adds styles with ng-app attribute', waitForAsync(() => {
559+
it('adds styles with ng-app-id attribute', waitForAsync(() => {
560560
const platform = platformDynamicServer([{
561561
provide: INITIAL_CONFIG,
562562
useValue: {document: '<html><head></head><body><app></app></body></html>'}
@@ -567,7 +567,7 @@ describe('platform-server integration', () => {
567567
const styles: any[] = head.children as any;
568568
expect(styles.length).toBe(1);
569569
expect(styles[0].textContent).toContain('color: red');
570-
expect(styles[0].getAttribute('ng-app')).toBe('ng');
570+
expect(styles[0].getAttribute('ng-app-id')).toBe('ng');
571571
});
572572
}));
573573

0 commit comments

Comments
 (0)