Skip to content
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

Properly escape quotes with leading spaces #2363

Merged
merged 4 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 11 additions & 8 deletions src/main/java/net/dv8tion/jda/api/utils/MarkdownSanitizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -554,22 +554,27 @@ public String compute(@Nonnull String sequence)
{
Checks.notNull(sequence, "Input");
StringBuilder builder = new StringBuilder();
String end = handleQuote(sequence, false);
String end = handleQuote(sequence);
if (end != null) return end;

boolean onlySpacesSinceNewLine = true;
for (int i = 0; i < sequence.length();)
{
int nextRegion = getRegion(i, sequence);
char c = sequence.charAt(i);
boolean isNewLine = c == '\n';
boolean isSpace = c == ' ';
onlySpacesSinceNewLine = isNewLine || (onlySpacesSinceNewLine && isSpace);

if (nextRegion == NORMAL)
{
if (sequence.charAt(i) == '\n' && i + 1 < sequence.length())
builder.append(sequence.charAt(i++));
if ((isNewLine || (isSpace && onlySpacesSinceNewLine)) && i < sequence.length())
{
String result = handleQuote(sequence.substring(i + 1), true);
String result = handleQuote(sequence.substring(i));
if (result != null)
return builder.append(result).toString();
}

builder.append(sequence.charAt(i++));
continue;
}

Expand All @@ -588,7 +593,7 @@ public String compute(@Nonnull String sequence)
return builder.toString();
}

private String handleQuote(@Nonnull String sequence, boolean newline)
private String handleQuote(@Nonnull String sequence)
{
// Special handling for quote
if (!isIgnored(QUOTE) && quote.matcher(sequence).matches())
Expand All @@ -599,8 +604,6 @@ private String handleQuote(@Nonnull String sequence, boolean newline)
StringBuilder builder = new StringBuilder(compute(sequence.substring(start + 2)));
if (strategy == SanitizationStrategy.ESCAPE)
builder.insert(0, "\\> ");
if (newline)
builder.insert(0, '\n');
return builder.toString();

}
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/MarkdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public void testQuote()
Assertions.assertEquals("Hello\nWorld", markdown.compute(">>> Hello\nWorld"));
Assertions.assertEquals("Hello\nWorld", markdown.compute(">>>\nHello\nWorld"));
Assertions.assertEquals("Hello > World", markdown.compute(">>>\nHello > World"));
Assertions.assertEquals("Hello\n World", markdown.compute("Hello\n > World"));
}
}

Expand Down Expand Up @@ -261,6 +262,7 @@ public void testQuote()
Assertions.assertEquals(">>> Hello\nWorld", markdown.compute(">>> Hello\nWorld"));
Assertions.assertEquals(">>>\nHello\nWorld", markdown.compute(">>>\nHello\nWorld"));
Assertions.assertEquals(">>>\nHello > World", markdown.compute(">>>\nHello > World"));
Assertions.assertEquals("Hello\n > World", markdown.compute("Hello\n > World"));
}
}

Expand Down Expand Up @@ -389,6 +391,7 @@ public void testQuote()
Assertions.assertEquals("\\>>>\nHello\nWorld", markdown.compute(">>>\nHello\nWorld"));
Assertions.assertEquals("\\>>>\nHello > World", markdown.compute(">>>\nHello > World"));
Assertions.assertEquals("\\> \\_Hello \n\\> World\\_", markdown.compute("> _Hello \n> World_"));
Assertions.assertEquals("Hello\n \\> World", markdown.compute("Hello\n > World"));
}
}

Expand Down Expand Up @@ -459,5 +462,6 @@ public void testQuote()
Assertions.assertEquals("\\> Hello World", MarkdownSanitizer.escape("\\> Hello World", true));
Assertions.assertEquals("\\>\\>\\> Hello World", MarkdownSanitizer.escape("\\>\\>\\> Hello World", true));
Assertions.assertEquals("Hello > World", MarkdownSanitizer.escape("Hello > World"));
Assertions.assertEquals("Hello\n \\> World", MarkdownSanitizer.escape("Hello\n > World"));
}
}