Skip to content

fix: component flattener not popping styles in correct order #1255

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

Open
wants to merge 1 commit into
base: main/4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ final class ComponentFlattenerImpl implements ComponentFlattener {
private static final class StackEntry {
final Component component;
final int depth;
final int stylesToPop;

StackEntry(final Component component, final int depth) {
StackEntry(final Component component, final int depth, final int stylesToPop) {
this.component = component;
this.depth = depth;
this.stylesToPop = stylesToPop;
}
}

Expand All @@ -104,7 +106,7 @@ private void flatten0(final @NotNull Component input, final @NotNull FlattenerLi
final Deque<Style> styleStack = new ArrayDeque<>();

// Push the starting component.
componentStack.push(new StackEntry(input, depth));
componentStack.push(new StackEntry(input, depth, 1));

while (!componentStack.isEmpty()) {
final StackEntry entry = componentStack.pop();
Expand All @@ -130,12 +132,19 @@ private void flatten0(final @NotNull Component input, final @NotNull FlattenerLi
// Push any children onto the stack in reverse order so they are popped in the right order.
final List<Component> children = component.children();
for (int i = children.size() - 1; i >= 0; i--) {
componentStack.push(new StackEntry(children.get(i), currentDepth + 1));
if (i == children.size() - 1) {
// The last child is responsible for popping all the parents' styles.
componentStack.push(new StackEntry(children.get(i), currentDepth + 1, entry.stylesToPop + 1));
} else {
componentStack.push(new StackEntry(children.get(i), currentDepth + 1, 1));
}
}
} else {
// If there are no children, we pop the latest style to go back "up" the tree.
final Style style = styleStack.pop();
listener.popStyle(style);
// If there are no children, we pop the latest N styles to go back "up" the tree.
for (int i = entry.stylesToPop; i > 0; i--) {
final Style style = styleStack.pop();
listener.popStyle(style);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.kyori.adventure.text.NBTComponent;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.TranslationArgument;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
Expand All @@ -52,6 +53,7 @@ static class TrackingFlattener implements FlattenerListener {
int pushCount;
int popCount;
final List<Style> pushedStyles = new ArrayList<>();
final List<Style> poppedStyles = new ArrayList<>();
final List<String> strings = new ArrayList<>();

@Override
Expand All @@ -68,6 +70,7 @@ public void component(final @NotNull String text) {
@Override
public void popStyle(final @NotNull Style style) {
this.popCount++;
this.poppedStyles.add(style);
}

public TrackingFlattener assertBalanced() {
Expand All @@ -90,6 +93,11 @@ public TrackingFlattener assertStyles(final Style... styles) {
assertIterableEquals(Arrays.asList(styles), this.pushedStyles);
return this;
}

public TrackingFlattener assertPoppedStyles(final Style... styles) {
assertIterableEquals(Arrays.asList(styles), this.poppedStyles);
return this;
}
}

static class CancellingFlattener extends TrackingFlattener {
Expand Down Expand Up @@ -154,6 +162,40 @@ void testComplex() {
.assertContents("Hi there my", " blue ", "friend");
}

@Test
void testComplexNested() {
final Component input = Component.text()
.content("Hi there my")
.append(Component.text(" clickable ")
.clickEvent(ClickEvent.copyToClipboard("some text"))
.append(
Component.text("and bold ")
.decorate(TextDecoration.BOLD)
.append(Component.text("red ", NamedTextColor.RED)))
.append(
Component.text("and blue ", NamedTextColor.BLUE)))
.append(Component.text("friend"))
.build();

this.testFlatten(ComponentFlattener.basic(), input).assertBalanced()
.assertPushesAndPops(6)
.assertStyles(
Style.empty(),
Style.style(ClickEvent.copyToClipboard("some text")),
Style.style(TextDecoration.BOLD),
Style.style(NamedTextColor.RED),
Style.style(NamedTextColor.BLUE),
Style.empty())
.assertPoppedStyles(
Style.style(NamedTextColor.RED),
Style.style(TextDecoration.BOLD),
Style.style(NamedTextColor.BLUE),
Style.style(ClickEvent.copyToClipboard("some text")),
Style.empty(),
Style.empty())
.assertContents("Hi there my", " clickable ", "and bold ", "red ", "and blue ", "friend");
}

@Test
void testTranslatable() {
this.testFlatten(ComponentFlattener.basic(), Component.translatable("adventure.test.key"))
Expand Down