Skip to content

Commit b3c77e0

Browse files
fix: render markdown content on re-attachment (#7808)
* Initial plan * Fix re-attaching Markdown component content disappearing issue Co-authored-by: sissbruecker <357820+sissbruecker@users.noreply.github.com> * run formatter --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sissbruecker <357820+sissbruecker@users.noreply.github.com> Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
1 parent b769152 commit b3c77e0

File tree

2 files changed

+25
-0
lines changed
  • vaadin-markdown-flow-parent/vaadin-markdown-flow/src

2 files changed

+25
-0
lines changed

vaadin-markdown-flow-parent/vaadin-markdown-flow/src/main/java/com/vaadin/flow/component/markdown/Markdown.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Objects;
1919

20+
import com.vaadin.flow.component.AttachEvent;
2021
import com.vaadin.flow.component.Component;
2122
import com.vaadin.flow.component.HasSize;
2223
import com.vaadin.flow.component.Tag;
@@ -87,6 +88,15 @@ public String getContent() {
8788
return serverContent;
8889
}
8990

91+
@Override
92+
protected void onAttach(AttachEvent attachEvent) {
93+
super.onAttach(attachEvent);
94+
if (serverContent != null) {
95+
clientContent = null;
96+
scheduleContentUpdate();
97+
}
98+
}
99+
90100
private void scheduleContentUpdate() {
91101
getElement().getNode()
92102
.runWhenAttached(ui -> ui.beforeClientResponse(this, ctx -> {

vaadin-markdown-flow-parent/vaadin-markdown-flow/src/test/java/com/vaadin/flow/component/markdown/tests/MarkdownTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ public void testDetach_setContent_attach() {
138138
Assert.assertEquals("**Hello** _World_", markdown.getContent());
139139
}
140140

141+
@Test
142+
public void testReAttachment_contentPreserved() {
143+
// Set content while attached
144+
markdown.setContent("**Hello** _World_");
145+
assertUpdateMarkdownCall(markdown, "**Hello** _World_", false);
146+
147+
// Detach the component
148+
markdown.removeFromParent();
149+
150+
// Re-attach the component - content should be sent to client again
151+
ui.add(markdown);
152+
assertUpdateMarkdownCall(markdown, "**Hello** _World_", false);
153+
Assert.assertEquals("**Hello** _World_", markdown.getContent());
154+
}
155+
141156
private void assertUpdateMarkdownCall(Component component, String content,
142157
boolean isAppend) {
143158
var pendingJavaScriptInvocations = getPendingJavaScriptInvocations();

0 commit comments

Comments
 (0)