Skip to content

Commit 8c440c2

Browse files
Copilotegil
andcommitted
Improve comments in FindFormById method
Co-authored-by: egil <[email protected]>
1 parent ff0de7b commit 8c440c2

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/bunit/EventDispatchExtensions/TriggerEventDispatchExtensions.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,20 @@ private static bool TryGetParentFormElementSpecialCase(
205205

206206
private static IHtmlFormElement? FindFormById(IElement element, string formId)
207207
{
208-
// First try the owner's GetElementById
208+
// First try the owner's GetElementById (most efficient if it works)
209209
var formByOwner = element.Owner?.GetElementById(formId) as IHtmlFormElement;
210210
if (formByOwner is not null)
211211
{
212212
return formByOwner;
213213
}
214214

215-
// If that didn't work, traverse up to find a common ancestor and search its children
216-
// Start from the parent
215+
// If GetElementById didn't work (which can happen with Blazor's incremental DOM rendering),
216+
// traverse up the DOM tree to find a common ancestor and search its children
217+
// This handles cases where the button and form are siblings or in different subtrees
217218
var current = element.Parent as IElement;
218219
while (current is not null)
219220
{
220-
// Search children of current element for the form
221+
// Search children of current element for the form with matching ID
221222
foreach (var child in current.Children)
222223
{
223224
if ((child.Id == formId || child.GetAttribute("id") == formId) && child is IHtmlFormElement htmlForm)
@@ -226,7 +227,7 @@ private static bool TryGetParentFormElementSpecialCase(
226227
}
227228
}
228229

229-
// Move up to parent
230+
// Move up to parent to widen the search
230231
current = current.Parent as IElement;
231232
}
232233

0 commit comments

Comments
 (0)