-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Fix Blazor crash when interactive component is in layout with enhanced nav #66007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f13842
Fix Blazor crash when interactive component is in layout with enhance…
maraf 8ceece8
Merge branch 'main' into maraf/fix-64722-enhanced-nav-layout-crash
maraf 030835e
Merge remote-tracking branch 'origin/main' into maraf/fix-64722-enhan…
Copilot 68fedb6
Address PR review feedback on enhanced nav test
maraf 8cd54f4
Clean up orphaned logical siblings before insert to preserve DOM order
maraf a3870b8
Fix stale element reference in E2E test
maraf 21c156e
Wait for InteractiveAuto interactivity before clicking counter in E2E…
maraf 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import { expect, test, describe } from '@jest/globals'; | ||
| import { | ||
| toLogicalElement, | ||
| insertLogicalChild, | ||
| getLogicalChildrenArray, | ||
| getLogicalChild, | ||
| LogicalElement, | ||
| } from '../src/Rendering/LogicalElements'; | ||
|
|
||
| describe('insertLogicalChild', () => { | ||
| test('should insert child at the correct position when no orphans exist', () => { | ||
| const parent = document.createElement('div'); | ||
| const existingChild = document.createElement('span'); | ||
| parent.appendChild(existingChild); | ||
| const logicalParent = toLogicalElement(parent, true); | ||
|
|
||
| const newChild = document.createElement('p'); | ||
| insertLogicalChild(newChild, logicalParent, 0); | ||
|
|
||
| // New child should be inserted before the existing span in the DOM | ||
| expect(parent.childNodes[0]).toBe(newChild); | ||
| expect(parent.childNodes[1]).toBe(existingChild); | ||
|
|
||
| // Logical children should reflect the correct order | ||
| const children = getLogicalChildrenArray(logicalParent); | ||
| expect(children.length).toBe(2); | ||
| expect(children[0] as any as Node).toBe(newChild); | ||
| expect(children[1] as any as Node).toBe(existingChild); | ||
| }); | ||
|
|
||
| test('should remove orphaned sibling at the reference position and insert in correct DOM order', () => { | ||
| // Set up: parent with [orphanComment, connectedSpan] in logical children | ||
| const parent = document.createElement('div'); | ||
| const orphanComment = document.createComment('orphan'); | ||
| const connectedSpan = document.createElement('span'); | ||
|
|
||
| // Add both to the DOM initially so toLogicalElement picks them up | ||
| parent.appendChild(orphanComment); | ||
| parent.appendChild(connectedSpan); | ||
| const logicalParent = toLogicalElement(parent, true); | ||
|
|
||
| // Now orphan the comment by removing it from the DOM (simulates discoverWebAssemblyOptions) | ||
| parent.removeChild(orphanComment); | ||
|
|
||
| // Insert a new element at index 0 — the orphan is at position 0 | ||
| const newChild = document.createElement('p'); | ||
| insertLogicalChild(newChild, logicalParent, 0); | ||
|
|
||
| // The orphan should have been cleaned up from logical children | ||
| const children = getLogicalChildrenArray(logicalParent); | ||
| expect(children).not.toContain(orphanComment as any); | ||
|
|
||
| // New child should be before the connected span in the DOM | ||
| expect(parent.childNodes[0]).toBe(newChild); | ||
| expect(parent.childNodes[1]).toBe(connectedSpan); | ||
|
|
||
| // Logical children: [newChild, connectedSpan] | ||
| expect(children.length).toBe(2); | ||
| expect(children[0] as any as Node).toBe(newChild); | ||
| expect(children[1] as any as Node).toBe(connectedSpan); | ||
| }); | ||
|
|
||
| test('should adjust insertion index when orphans precede the target position', () => { | ||
| // Set up: parent with [orphan1, orphan2, spanA, spanB] in logical children | ||
| const parent = document.createElement('div'); | ||
| const orphan1 = document.createComment('orphan1'); | ||
| const orphan2 = document.createComment('orphan2'); | ||
| const spanA = document.createElement('span'); | ||
| const spanB = document.createElement('em'); | ||
|
|
||
| parent.appendChild(orphan1); | ||
| parent.appendChild(orphan2); | ||
| parent.appendChild(spanA); | ||
| parent.appendChild(spanB); | ||
| const logicalParent = toLogicalElement(parent, true); | ||
|
|
||
| // Orphan the first two comments | ||
| parent.removeChild(orphan1); | ||
| parent.removeChild(orphan2); | ||
|
|
||
| // Insert at logical index 2 (which, after cleanup of 2 orphans, becomes index 0) | ||
| // This should insert BEFORE spanA | ||
| const newChild = document.createElement('p'); | ||
| insertLogicalChild(newChild, logicalParent, 2); | ||
|
|
||
| const children = getLogicalChildrenArray(logicalParent); | ||
|
|
||
| // Orphans should be removed | ||
| expect(children).not.toContain(orphan1 as any); | ||
| expect(children).not.toContain(orphan2 as any); | ||
|
|
||
| // Logical children: [newChild, spanA, spanB] | ||
| expect(children.length).toBe(3); | ||
| expect(children[0] as any as Node).toBe(newChild); | ||
| expect(children[1] as any as Node).toBe(spanA); | ||
| expect(children[2] as any as Node).toBe(spanB); | ||
|
|
||
| // DOM order should match | ||
| expect(parent.childNodes[0]).toBe(newChild); | ||
| expect(parent.childNodes[1]).toBe(spanA); | ||
| expect(parent.childNodes[2]).toBe(spanB); | ||
| }); | ||
|
|
||
| test('should append when all siblings after orphan cleanup are before insertion point', () => { | ||
| // Set up: parent with [connectedSpan, orphanComment] in logical children | ||
| const parent = document.createElement('div'); | ||
| const connectedSpan = document.createElement('span'); | ||
| const orphanComment = document.createComment('orphan'); | ||
|
|
||
| parent.appendChild(connectedSpan); | ||
| parent.appendChild(orphanComment); | ||
| const logicalParent = toLogicalElement(parent, true); | ||
|
|
||
| // Orphan the comment | ||
| parent.removeChild(orphanComment); | ||
|
|
||
| // Insert at index 1 — after cleanup, there's only 1 sibling so index 1 means append | ||
| const newChild = document.createElement('p'); | ||
| insertLogicalChild(newChild, logicalParent, 1); | ||
|
|
||
| const children = getLogicalChildrenArray(logicalParent); | ||
| expect(children).not.toContain(orphanComment as any); | ||
|
|
||
| // Logical children: [connectedSpan, newChild] | ||
| expect(children.length).toBe(2); | ||
| expect(children[0] as any as Node).toBe(connectedSpan); | ||
| expect(children[1] as any as Node).toBe(newChild); | ||
|
|
||
| // DOM order | ||
| expect(parent.childNodes[0]).toBe(connectedSpan); | ||
| expect(parent.childNodes[1]).toBe(newChild); | ||
| }); | ||
|
|
||
| test('should handle multiple orphans interspersed with connected nodes', () => { | ||
| const parent = document.createElement('div'); | ||
| const orphan1 = document.createComment('orphan1'); | ||
| const spanA = document.createElement('span'); | ||
| const orphan2 = document.createComment('orphan2'); | ||
| const spanB = document.createElement('em'); | ||
|
|
||
| parent.appendChild(orphan1); | ||
| parent.appendChild(spanA); | ||
| parent.appendChild(orphan2); | ||
| parent.appendChild(spanB); | ||
| const logicalParent = toLogicalElement(parent, true); | ||
|
|
||
| // Orphan both comments | ||
| parent.removeChild(orphan1); | ||
| parent.removeChild(orphan2); | ||
|
|
||
| // Insert at index 1 (between orphan1 and spanA originally). | ||
| // After cleanup of orphan1 (index 0, before target) → index becomes 0. | ||
| // orphan2 at original index 2 is also removed but doesn't affect adjusted index. | ||
| // So we insert at index 0 before spanA. | ||
| const newChild = document.createElement('p'); | ||
| insertLogicalChild(newChild, logicalParent, 1); | ||
|
|
||
| const children = getLogicalChildrenArray(logicalParent); | ||
| expect(children.length).toBe(3); | ||
| expect(children[0] as any as Node).toBe(newChild); | ||
| expect(children[1] as any as Node).toBe(spanA); | ||
| expect(children[2] as any as Node).toBe(spanB); | ||
|
|
||
| expect(parent.childNodes[0]).toBe(newChild); | ||
| expect(parent.childNodes[1]).toBe(spanA); | ||
| expect(parent.childNodes[2]).toBe(spanB); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...ets/Components.TestServer/RazorComponents/Pages/EnhancedNav/InteractiveInLayoutHome.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| @page "/nav/interactive-in-layout" | ||
| @layout Components.TestServer.RazorComponents.Shared.EnhancedNavWithInteractiveInLayout | ||
|
|
||
| <PageTitle>Interactive In Layout Home</PageTitle> | ||
|
|
||
| <h1 id="interactive-layout-home">Hello from interactive layout</h1> | ||
| <p>This page tests that enhanced navigation works when the layout contains an interactive component.</p> |
7 changes: 7 additions & 0 deletions
7
...ts/Components.TestServer/RazorComponents/Pages/EnhancedNav/InteractiveInLayoutOther.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| @page "/nav/interactive-in-layout/other" | ||
| @layout Components.TestServer.RazorComponents.Shared.EnhancedNavWithInteractiveInLayout | ||
|
|
||
| <PageTitle>Interactive In Layout Other</PageTitle> | ||
|
|
||
| <h1 id="interactive-layout-other">Other page with interactive layout</h1> | ||
| <p>Navigating back from here to the home page should not cause a JavaScript error.</p> |
12 changes: 12 additions & 0 deletions
12
...ets/Components.TestServer/RazorComponents/Shared/EnhancedNavWithInteractiveInLayout.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| @inherits LayoutComponentBase | ||
| @using TestContentPackage | ||
|
|
||
| <nav> | ||
| <NavLink href="nav/interactive-in-layout">Home (interactive layout)</NavLink> | | ||
| <NavLink href="nav/interactive-in-layout/other">Other (interactive layout)</NavLink> | ||
| </nav> | ||
| <hr /> | ||
| <main> | ||
| @Body | ||
| <InteractiveCounterInLayout /> | ||
| </main> |
26 changes: 26 additions & 0 deletions
26
src/Components/test/testassets/TestContentPackage/InteractiveCounterInLayout.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| @rendermode RenderMode.InteractiveAuto | ||
|
|
||
| <p id="layout-counter"> | ||
| Layout counter: <span id="layout-counter-value">@currentCount</span> | ||
| </p> | ||
| <p>Interactive: <span id="layout-counter-interactive">@_isInteractive</span></p> | ||
| <button id="layout-counter-button" class="btn btn-primary" @onclick="IncrementCount">Increment layout counter</button> | ||
|
|
||
| @code { | ||
| private int currentCount = 0; | ||
| private bool _isInteractive; | ||
|
|
||
| private void IncrementCount() | ||
| { | ||
| currentCount++; | ||
| } | ||
|
|
||
| protected override void OnAfterRender(bool firstRender) | ||
| { | ||
| if (firstRender) | ||
| { | ||
| _isInteractive = true; | ||
| StateHasChanged(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.