Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/render3/view/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,12 @@ function setupSelectorMatchedInputsOrOutputs<T>(
let bindings: NodeInputBindings | NodeOutputBindings;
if (mode === BindingType.Inputs) {
bindings = tNode.inputs ??= {};
setShadowStylingInputFlags(tNode, publicName);
} else {
bindings = tNode.outputs ??= {};
}
bindings[publicName] ??= [];
bindings[publicName].push(directiveIndex);
setShadowStylingInputFlags(tNode, publicName);
}
}
}
Expand All @@ -331,12 +331,12 @@ function setupHostDirectiveInputsOrOutputs(
let bindings: HostDirectiveInputs | HostDirectiveOutputs;
if (mode === BindingType.Inputs) {
bindings = tNode.hostDirectiveInputs ??= {};
setShadowStylingInputFlags(tNode, publicName);
} else {
bindings = tNode.hostDirectiveOutputs ??= {};
}
bindings[publicName] ??= [];
bindings[publicName].push(directiveIndex, initialName);
setShadowStylingInputFlags(tNode, publicName);
}
}
}
Expand Down
122 changes: 122 additions & 0 deletions packages/core/test/acceptance/styling_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {
computed,
Directive,
ElementRef,
EventEmitter,
HostBinding,
Input,
Output,
Renderer2,
signal,
ViewChild,
Expand Down Expand Up @@ -612,6 +614,126 @@ describe('styling', () => {
expect(div.getAttribute('shadow-style')).toEqual('color: red; width: 100px;');
});

it('should not treat outputs named class or style as shadowing [class] and [style]', async () => {
@Directive({
selector: '[dir-with-styling-outputs]',
})
class DirectiveWithStylingOutputs {
@Output('class') classOutput = new EventEmitter<string>();
@Output('style') styleOutput = new EventEmitter<string>();
}

@Component({
template: `
<div
dir-with-styling-outputs
class="s1"
style="color: red;"
[class]="classBinding()"
[style]="styleBinding()"
></div>
`,
imports: [DirectiveWithStylingOutputs],
})
class Cmp {
classBinding = signal('d1');
styleBinding = signal('width: 10px;');
}

const fixture = TestBed.createComponent(Cmp);
await fixture.whenStable();
const div = fixture.nativeElement.querySelector('div');
expectClass(div).toEqual({'s1': true, 'd1': true});
expectStyle(div).toEqual({'color': 'red', 'width': '10px'});

fixture.componentInstance.classBinding.set('d2');
fixture.componentInstance.styleBinding.set('width: 20px;');
await fixture.whenStable();
expectClass(div).toEqual({'s1': true, 'd2': true});
expectStyle(div).toEqual({'color': 'red', 'width': '20px'});
});

it('should deliver events from an output named class while [class] is bound', async () => {
@Directive({
selector: '[dir-with-class-output]',
})
class DirectiveWithClassOutput {
@Output('class') classOutput = new EventEmitter<string>();
}

@Component({
template: `
<div
dir-with-class-output
class="s1"
[class]="classBinding()"
(class)="received.push($event)"
></div>
`,
imports: [DirectiveWithClassOutput],
})
class Cmp {
@ViewChild(DirectiveWithClassOutput) dir!: DirectiveWithClassOutput;
classBinding = signal('d1');
received: string[] = [];
}

const fixture = TestBed.createComponent(Cmp);
await fixture.whenStable();
fixture.componentInstance.dir.classOutput.emit('e1');

expect(fixture.componentInstance.received).toEqual(['e1']);
expectClass(fixture.nativeElement.querySelector('div')).toEqual({'s1': true, 'd1': true});
});

it('should not treat a host directive output exposed as class as shadowing [class]', async () => {
@Directive()
class HostDir {
@Output() changed = new EventEmitter<string>();
}

@Directive({
selector: '[dir-exposing-class-output]',
hostDirectives: [{directive: HostDir, outputs: ['changed: class']}],
})
class DirectiveExposingClassOutput {}

@Component({
template: `<div dir-exposing-class-output class="s1" [class]="classBinding()"></div>`,
imports: [DirectiveExposingClassOutput],
})
class Cmp {
classBinding = signal('d1');
}

const fixture = TestBed.createComponent(Cmp);
await fixture.whenStable();
expectClass(fixture.nativeElement.querySelector('div')).toEqual({'s1': true, 'd1': true});
});

it('should bind [class] as input to directive that also has an output named class', async () => {
@Directive({
selector: '[dir-shadows-class-input]',
})
class DirectiveShadowsClassInput {
@Input('class') klass = '';
@Output('class') classOutput = new EventEmitter<string>();
}

@Component({
template: `<div dir-shadows-class-input class="s1" [class]="'d1'"></div>`,
imports: [DirectiveShadowsClassInput],
})
class Cmp {
@ViewChild(DirectiveShadowsClassInput) dir!: DirectiveShadowsClassInput;
}

const fixture = TestBed.createComponent(Cmp);
await fixture.whenStable();
expect(fixture.nativeElement.querySelector('div').className).toEqual('s1');
expect(fixture.componentInstance.dir.klass).toEqual('s1 d1');
});

Comment thread
Flarette marked this conversation as resolved.
Resolved
it('should prevent circular ExpressionChangedAfterItHasBeenCheckedError on shadow inputs', () => {
@Directive({
selector: '[dir-shadows-class-input]',
Expand Down
Loading