Skip to content

Commit f1a0632

Browse files
benbraouvicb
authored andcommitted
feat(core): set preserveWhitespaces to false by default (#22046)
Fixes #22027 PR Close #22046
1 parent d241532 commit f1a0632

File tree

13 files changed

+34
-30
lines changed

13 files changed

+34
-30
lines changed

aio/content/guide/aot-compiler.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ You can control your app compilation by providing template compiler options in t
9292
},
9393
"angularCompilerOptions": {
9494
"fullTemplateTypeCheck": true,
95-
"preserveWhitespaces": false,
95+
"preserveWhitespaces": true,
9696
...
9797
}
9898
}
@@ -234,9 +234,7 @@ done manually.
234234
### *preserveWhitespaces*
235235

236236
This option tells the compiler whether to remove blank text nodes from compiled templates.
237-
This option is `true` by default.
238-
239-
*Note*: It is recommended to set this explicitly to `false` as it emits smaller template factory modules and might be set to `false` by default in the future.
237+
As of v6, this option is `false` by default, which results in smaller emitted template factory modules.
240238

241239
### *allowEmptyCodegenFiles*
242240

packages/bazel/src/ng_module.bzl

+1-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ def _ngc_tsconfig(ctx, files, srcs, **kwargs):
9191
"enableSummariesForJit": True,
9292
"fullTemplateTypeCheck": ctx.attr.type_check,
9393
# FIXME: wrong place to de-dupe
94-
"expectedOut": depset([o.path for o in expected_outs]).to_list(),
95-
"preserveWhitespaces": False,
94+
"expectedOut": depset([o.path for o in expected_outs]).to_list()
9695
}
9796
})
9897

packages/compiler-cli/src/transformers/api.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ export interface CompilerOptions extends ts.CompilerOptions {
148148
// How to handle missing messages
149149
i18nInMissingTranslations?: 'error'|'warning'|'ignore';
150150

151-
// Whether to remove blank text nodes from compiled templates. It is `true` by default
152-
// in Angular 5 and will be re-visited in Angular 6.
151+
// Whether to remove blank text nodes from compiled templates. It is `false` by default starting
152+
// from Angular 6.
153153
preserveWhitespaces?: boolean;
154154

155155
/** generate all possible generated files */

packages/compiler/src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ export class CompilerConfig {
4646
}
4747

4848
export function preserveWhitespacesDefault(
49-
preserveWhitespacesOption: boolean | null, defaultSetting = true): boolean {
49+
preserveWhitespacesOption: boolean | null, defaultSetting = false): boolean {
5050
return preserveWhitespacesOption === null ? defaultSetting : preserveWhitespacesOption;
5151
}

packages/compiler/test/config_spec.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {MissingTranslationStrategy} from '@angular/core';
10-
import {CompilerConfig} from '../src/config';
10+
import {CompilerConfig, preserveWhitespacesDefault} from '../src/config';
1111

1212
{
1313
describe('compiler config', () => {
@@ -16,4 +16,13 @@ import {CompilerConfig} from '../src/config';
1616
expect(config.missingTranslation).toEqual(MissingTranslationStrategy.Error);
1717
});
1818
});
19+
20+
describe('preserveWhitespacesDefault', () => {
21+
it('should return the default `false` setting when no preserveWhitespacesOption are provided',
22+
() => { expect(preserveWhitespacesDefault(null)).toEqual(false); });
23+
it('should return the preserveWhitespacesOption when provided as a parameter', () => {
24+
expect(preserveWhitespacesDefault(true)).toEqual(true);
25+
expect(preserveWhitespacesDefault(false)).toEqual(false);
26+
});
27+
});
1928
}

packages/core/src/metadata/directives.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -693,13 +693,13 @@ export interface Component extends Directive {
693693
* - text nodes are left as-is inside HTML tags where whitespaces are significant (ex. `<pre>`,
694694
* `<textarea>`).
695695
*
696-
* Described transformations can (potentially) influence DOM nodes layout so the
697-
* `preserveWhitespaces` option is `true` be default (no whitespace removal).
698-
* In Angular 5 you need to opt-in for whitespace removal (but we might revisit the default
699-
* setting in Angular 6 or later). If you want to change the default setting for all components
700-
* in your application you can use the `preserveWhitespaces` option of the AOT compiler.
696+
* Described transformations may (potentially) influence DOM nodes layout. However, the impact
697+
* should so be minimal. That's why starting from Angular 6, the
698+
* `preserveWhitespaces` option is `false` by default (whitespace removal).
699+
* If you want to change the default setting for all components in your application you can use
700+
* the `preserveWhitespaces` option of the AOT compiler.
701701
*
702-
* Even if you decide to opt-in for whitespace removal there are ways of preserving whitespaces
702+
* Even with the default behavior of whitespace removal, there are ways of preserving whitespaces
703703
* in certain fragments of a template. You can either exclude entire DOM sub-tree by using the
704704
* `ngPreserveWhitespaces` attribute, ex.:
705705
*

packages/core/test/debug/debug_node_spec.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ class TestApp {
191191
it('should list all child nodes', () => {
192192
fixture = TestBed.createComponent(ParentComp);
193193
fixture.detectChanges();
194-
195-
// The root component has 3 elements and 2 text node children.
196-
expect(fixture.debugElement.childNodes.length).toEqual(5);
194+
expect(fixture.debugElement.childNodes.length).toEqual(3);
197195
});
198196

199197
it('should list all component child elements', () => {

packages/core/test/linker/integration_spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ function declareTests({useJit}: {useJit: boolean}) {
12801280
fixture.detectChanges();
12811281
expect(fixture.nativeElement)
12821282
.toHaveText(
1283-
'Default Interpolation\nCustom Interpolation A\nCustom Interpolation B (Default Interpolation)');
1283+
'Default InterpolationCustom Interpolation ACustom Interpolation B (Default Interpolation)');
12841284
});
12851285
});
12861286

@@ -1792,7 +1792,7 @@ function declareTests({useJit}: {useJit: boolean}) {
17921792
const f = TestBed.configureTestingModule({declarations: [MyCmp]}).createComponent(MyCmp);
17931793
f.detectChanges();
17941794

1795-
expect(f.nativeElement.childNodes.length).toBe(3);
1795+
expect(f.nativeElement.childNodes.length).toBe(2);
17961796
}));
17971797

17981798
it('should not remove whitespaces when explicitly requested not to do so', async(() => {

packages/examples/common/ngComponentOutlet/ts/e2e_test/ngComponentOutlet_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('ngComponentOutlet', () => {
2929
it('should render complete', () => {
3030
browser.get(URL);
3131
waitForElement('ng-component-outlet-complete-example');
32-
expect(element.all(by.css('complete-component')).getText()).toEqual(['Complete: Ahoj Svet!']);
32+
expect(element.all(by.css('complete-component')).getText()).toEqual(['Complete: AhojSvet!']);
3333
});
3434

3535
it('should render other module', () => {

packages/examples/common/ngIf/ts/e2e_test/ngIf_spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ describe('ngIf', () => {
4848
browser.get(URL);
4949
waitForElement(comp);
5050
expect(element.all(by.css(comp)).get(0).getText())
51-
.toEqual('hide Switch Primary show = true\nPrimary text to show');
51+
.toEqual('hideSwitch Primary show = true\nPrimary text to show');
5252
element.all(by.css(comp + ' button')).get(1).click();
5353
expect(element.all(by.css(comp)).get(0).getText())
54-
.toEqual('hide Switch Primary show = true\nSecondary text to show');
54+
.toEqual('hideSwitch Primary show = true\nSecondary text to show');
5555
element.all(by.css(comp + ' button')).get(0).click();
5656
expect(element.all(by.css(comp)).get(0).getText())
57-
.toEqual('show Switch Primary show = false\nAlternate text while primary text is hidden');
57+
.toEqual('showSwitch Primary show = false\nAlternate text while primary text is hidden');
5858
});
5959
});
6060

packages/platform-browser-dynamic/test/testing_public_browser_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class BadTemplateUrl {
123123
TestBed.compileComponents().then(() => {
124124
const componentFixture = TestBed.createComponent(ExternalTemplateComp);
125125
componentFixture.detectChanges();
126-
expect(componentFixture.nativeElement.textContent).toEqual('from external template\n');
126+
expect(componentFixture.nativeElement.textContent).toEqual('from external template');
127127
});
128128
}),
129129
10000); // Long timeout here because this test makes an actual ResourceLoader request, and

packages/platform-browser/test/testing_public_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ class CompWithUrlTemplate {
307307
it('should allow to createSync components with templateUrl after explicit async compilation',
308308
() => {
309309
const fixture = TestBed.createComponent(CompWithUrlTemplate);
310-
expect(fixture.nativeElement).toHaveText('from external template\n');
310+
expect(fixture.nativeElement).toHaveText('from external template');
311311
});
312312
});
313313

packages/router/test/integration.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1618,20 +1618,20 @@ describe('Integration', () => {
16181618

16191619
router.navigateByUrl('/');
16201620
advance(fixture);
1621-
expect(fixture.nativeElement).toHaveText(' ');
1621+
expect(fixture.nativeElement).toHaveText('');
16221622
const cmp = fixture.componentInstance;
16231623

16241624
cmp.show = true;
16251625
advance(fixture);
16261626

1627-
expect(fixture.nativeElement).toHaveText('link ');
1627+
expect(fixture.nativeElement).toHaveText('link');
16281628
const native = fixture.nativeElement.querySelector('a');
16291629

16301630
expect(native.getAttribute('href')).toEqual('/simple');
16311631
native.click();
16321632
advance(fixture);
16331633

1634-
expect(fixture.nativeElement).toHaveText('link simple');
1634+
expect(fixture.nativeElement).toHaveText('linksimple');
16351635
})));
16361636

16371637
it('should support query params and fragments',

0 commit comments

Comments
 (0)