dotnet / dotnet/roslyn

EventHandlerAttribute: enableStopPropagation and enablePreventDefault are swapped

Open
#85,535 2 comments 1 reaction 1 assignee Claimed by @chsienki View on GitHub
Area-Razor Area-Razor-Compiler
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

### Summary
The parameters `enableStopPropagation` and `enablePreventDefault` of the EventHandlerAttribute are swapped by the razor compiler, so that setting `enableStopPropagation = true` actually enables `preventDefault`, and vice versa.

### Steps to reproduce
I added the following class to my project in order to add support for the "transitionend" event to blazor:
```
[EventHandler("ontransitionend", typeof(EventArgs), enableStopPropagation: true, enablePreventDefault: false)]
public static class EventHandlers { }
```
When I set `@ontransitionend=SomeCallback` on a div in one of my components, this is recognized immediately by the compiler, according to the syntax highlighting.
However, when I try to set `@ontransitionend:stopPropagation` on another div, this is not recognized (the whole string was red like an html-attribute). When I then start my app, I get an exception complaining about a malformed string, i.e. the unprocessed "@" in the html-attribute name.
Furthermore, adding `@ontransitionend:preventDefault` is recognized and handled by the compiler, when it should be disabled. Swapping the values for both parameters results in the exact opposite behavior.

### Possible cause
https://github.com/dotnet/razor/blob/056b3bede2afd12405a60295513e09c5fbaa2d7f/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/EventHandlerTagHelperDescriptorProvider.cs#L74-L75
In EventHandlerTagHelperDescriptorProvider, the constructor arguments of the EventHandlerAttribute are accessed in the wrong order. The variable `enablePreventDefault` is being assigned the argument with index 2, when that actually is the `enableStopPropagation` parameter; the same goes for the `enableStopPropagation` variable being assigned the argument with index 3.

As far as I can see, this is the only place that accesses the values of these constructor arguments or the properties they are assigned to.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.