diff --git a/packages/compiler-cli/test/compliance/r3_view_compiler_styling_spec.ts b/packages/compiler-cli/test/compliance/r3_view_compiler_styling_spec.ts index 344ee23667..a621c347c4 100644 --- a/packages/compiler-cli/test/compliance/r3_view_compiler_styling_spec.ts +++ b/packages/compiler-cli/test/compliance/r3_view_compiler_styling_spec.ts @@ -649,6 +649,30 @@ describe('compiler compliance: styling', () => { expectEmit(result.source, template, 'Incorrect template'); }); + + it('should not create instructions for empty style bindings', () => { + const files = { + app: { + 'spec.ts': ` + import {Component, NgModule} from '@angular/core'; + + @Component({ + selector: 'my-component', + template: \`
\` + }) + export class MyComponent { + } + + @NgModule({declarations: [MyComponent]}) + export class MyModule {} + ` + } + }; + + const result = compile(files, angularFiles); + expect(result.source).not.toContain('elementStyling'); + }); + }); describe('[class]', () => { @@ -802,6 +826,30 @@ describe('compiler compliance: styling', () => { const result = compile(files, angularFiles); expectEmit(result.source, template, 'Incorrect template'); }); + + it('should not create instructions for empty class bindings', () => { + const files = { + app: { + 'spec.ts': ` + import {Component, NgModule} from '@angular/core'; + + @Component({ + selector: 'my-component', + template: \`\` + }) + export class MyComponent { + } + + @NgModule({declarations: [MyComponent]}) + export class MyModule {} + ` + } + }; + + const result = compile(files, angularFiles); + expect(result.source).not.toContain('elementStyling'); + }); + }); describe('[style] mixed with [class]', () => { diff --git a/packages/compiler/src/render3/view/styling_builder.ts b/packages/compiler/src/render3/view/styling_builder.ts index ceee817f20..33c7d7ffbe 100644 --- a/packages/compiler/src/render3/view/styling_builder.ts +++ b/packages/compiler/src/render3/view/styling_builder.ts @@ -10,6 +10,7 @@ import {AttributeMarker} from '../../core'; import {AST, BindingType, Interpolation} from '../../expression_parser/ast'; import * as o from '../../output/output_ast'; import {ParseSourceSpan} from '../../parse_util'; +import {isEmptyExpression} from '../../template_parser/template_parser'; import * as t from '../r3_ast'; import {Identifiers as R3} from '../r3_identifiers'; @@ -160,7 +161,10 @@ export class StylingBuilder { registerStyleInput( name: string, isMapBased: boolean, value: AST, sourceSpan: ParseSourceSpan, - unit?: string|null): BoundStylingEntry { + unit?: string|null): BoundStylingEntry|null { + if (isEmptyExpression(value)) { + return null; + } const {property, hasOverrideFlag, unit: bindingUnit} = parseProperty(name); const entry: BoundStylingEntry = { name: property, @@ -180,7 +184,10 @@ export class StylingBuilder { } registerClassInput(name: string, isMapBased: boolean, value: AST, sourceSpan: ParseSourceSpan): - BoundStylingEntry { + BoundStylingEntry|null { + if (isEmptyExpression(value)) { + return null; + } const {property, hasOverrideFlag} = parseProperty(name); const entry: BoundStylingEntry = {name: property, value, sourceSpan, hasOverrideFlag, unit: null}; diff --git a/packages/compiler/src/template_parser/template_parser.ts b/packages/compiler/src/template_parser/template_parser.ts index 2251272104..f7af554933 100644 --- a/packages/compiler/src/template_parser/template_parser.ts +++ b/packages/compiler/src/template_parser/template_parser.ts @@ -899,7 +899,7 @@ export function removeSummaryDuplicates