Skip to content

Commit b51307a

Browse files
authored
[Blazor] Throw error when custom event name matches browserEventName to prevent double-triggering (#64774)
1 parent 38e7d0c commit b51307a

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/Components/Web.JS/src/Rendering/Events/EventTypes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ export function registerCustomEventType(eventName: string, options: EventTypeOpt
2323
throw new Error(`The event '${eventName}' is already registered.`);
2424
}
2525

26+
// When aliasing a browser event, the custom event name must be different from the browser event name
27+
// to avoid double-triggering (once for the browser event, once for the custom event wrapper)
28+
if (options.browserEventName && eventName === options.browserEventName) {
29+
throw new Error(`The custom event '${eventName}' cannot have the same name as its browserEventName '${options.browserEventName}'. Choose a different name for the custom event.`);
30+
}
31+
2632
// If applicable, register this as an alias of the given browserEventName
2733
if (options.browserEventName) {
2834
const aliasGroup = browserEventNamesToAliases.get(options.browserEventName);

src/Components/test/E2ETest/Tests/EventCustomArgsTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ public void CanRegisterCustomEventAndSupplyComplexParams()
170170
line => Assert.Equal("Event with IJSObjectReference received: Hello!", line));
171171
}
172172

173+
[Fact]
174+
public void ThrowsErrorWhenEventNameMatchesBrowserEventName()
175+
{
176+
// Attempt to register a custom event with the same name as the browser event
177+
Browser.Exists(By.Id("register-invalid-same-name-event")).Click();
178+
179+
// Verify that an error is thrown
180+
var errorMessage = Browser.Exists(By.Id("same-name-event-error")).Text;
181+
Assert.Contains("cannot have the same name as its browserEventName", errorMessage);
182+
}
183+
173184
void SendKeysSequentially(IWebElement target, string text)
174185
{
175186
foreach (var c in text)

src/Components/test/testassets/BasicTestApp/EventCustomArgsComponent.razor

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@
6060
onclick="Blazor.registerCustomEventType('custommouseover', { browserEventName: 'mouseover' })">
6161
Register custom mouseover event (which has no corresponding native listener)
6262
</button>
63+
64+
<button id="register-invalid-same-name-event"
65+
onclick="try { Blazor.registerCustomEventType('scrolltop', { browserEventName: 'scrolltop' }); document.getElementById('same-name-event-error').textContent = 'No error thrown'; } catch (error) { document.getElementById('same-name-event-error').textContent = error.message; }">
66+
Try to register event with same name as browserEventName (should throw error)
67+
</button>
68+
</p>
69+
70+
<p>
71+
<span id="same-name-event-error"></span>
6372
</p>
6473

6574
<p>

0 commit comments

Comments
 (0)