Skip to content

Commit bb19cfb

Browse files
syldiumkashike
authored andcommitted
Backport GH-487
Keep child components when joining text components Fixes KyoriPowered/adventure-text-minimessage#181 Test with children at different positions in the component Add a link to the compaction issue Co-authored-by: Riley Park <[email protected]> Use TextComponentImpl directly instead of the factory method
1 parent 66b593d commit bb19cfb

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

api/src/main/java/net/kyori/adventure/text/ComponentCompaction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static Component compact(final @NotNull Component self, final @Nullable Style pa
106106
final Style childStyle = child.style().merge(childParentStyle, Style.Merge.Strategy.IF_ABSENT_ON_TARGET);
107107
final Style neighborStyle = neighbor.style().merge(childParentStyle, Style.Merge.Strategy.IF_ABSENT_ON_TARGET);
108108

109-
if (child instanceof TextComponent && neighbor instanceof TextComponent && childStyle.equals(neighborStyle)) {
109+
if (child.children().isEmpty() && child instanceof TextComponent && neighbor instanceof TextComponent && childStyle.equals(neighborStyle)) {
110110
final Component combined = joinText((TextComponent) child, (TextComponent) neighbor);
111111

112112
// replace the child and its neighbor with the single, combined component
@@ -167,6 +167,6 @@ static Component compact(final @NotNull Component self, final @Nullable Style pa
167167
}
168168

169169
private static TextComponent joinText(final TextComponent one, final TextComponent two) {
170-
return Component.text(one.content() + two.content(), one.style());
170+
return new TextComponentImpl(two.children(), one.style(), one.content() + two.content());
171171
}
172172
}

api/src/test/java/net/kyori/adventure/text/ComponentCompactingTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,28 @@ void testStyleMatchingGrandchild() {
289289

290290
assertEquals(expectedCompact, notCompact.compact());
291291
}
292+
293+
// https://github.com/KyoriPowered/adventure-text-minimessage/issues/181
294+
@Test
295+
void testJoinTextWithChildren() {
296+
final Component expectedCompact = text().append(
297+
text('2', NamedTextColor.AQUA),
298+
text().content("-").append(
299+
text('3', NamedTextColor.BLUE),
300+
text('4', NamedTextColor.DARK_BLUE)
301+
),
302+
text("end")
303+
).build();
304+
final Component notCompact = text().append(
305+
text('2', NamedTextColor.AQUA),
306+
text('-'),
307+
text().append(
308+
text('3', NamedTextColor.BLUE),
309+
text('4', NamedTextColor.DARK_BLUE)
310+
),
311+
text("end")
312+
).build();
313+
314+
assertEquals(expectedCompact, notCompact.compact());
315+
}
292316
}

0 commit comments

Comments
 (0)