Skip to content

Commit

Permalink
fix(text-minimessage): Be more lenient with input when stripping/esca…
Browse files Browse the repository at this point in the history
…ping tags

Fixes GH-799
  • Loading branch information
zml2008 committed Nov 9, 2022
1 parent 288a9e0 commit bc2c627
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Expand Up @@ -98,7 +98,7 @@ private void processTokens(final @NotNull StringBuilder sb, final @NotNull Conte

private void processTokens(final @NotNull StringBuilder sb, final @NotNull String richMessage, final @NotNull ContextImpl context, final BiConsumer<Token, StringBuilder> tagHandler) {
final TagResolver combinedResolver = TagResolver.resolver(this.tagResolver, context.extraTags());
final List<Token> root = TokenParser.tokenize(richMessage);
final List<Token> root = TokenParser.tokenize(richMessage, true);
for (final Token token : root) {
switch (token.type()) {
case TEXT:
Expand Down
Expand Up @@ -86,7 +86,7 @@ public static RootNode parse(
final boolean strict
) throws ParsingException {
// collect tokens...
final List<Token> tokens = tokenize(message);
final List<Token> tokens = tokenize(message, false);

// then build the tree!
return buildTree(tagProvider, tagNameChecker, tokens, message, originalMessage, strict);
Expand All @@ -109,7 +109,7 @@ public static String resolvePreProcessTags(final String message, final TagProvid
lastResult = result;
final StringResolvingMatchedTokenConsumer stringTokenResolver = new StringResolvingMatchedTokenConsumer(lastResult, provider);

parseString(lastResult, stringTokenResolver);
parseString(lastResult, false, stringTokenResolver);
result = stringTokenResolver.result();
passes++;
} while (passes < MAX_DEPTH && !lastResult.equals(result));
Expand All @@ -121,12 +121,13 @@ public static String resolvePreProcessTags(final String message, final TagProvid
* Tokenize a minimessage string into a list of tokens.
*
* @param message the minimessage string to parse
* @param lenient whether to allow section symbols (for escaping/stripping/non-actual-parse stuff only)
* @return the root tokens
* @since 4.10.0
*/
public static List<Token> tokenize(final String message) {
public static List<Token> tokenize(final String message, final boolean lenient) {
final TokenListProducingMatchedTokenConsumer listProducer = new TokenListProducingMatchedTokenConsumer(message);
parseString(message, listProducer);
parseString(message, lenient, listProducer);
final List<Token> tokens = listProducer.result();
parseSecondPass(message, tokens);
return tokens;
Expand All @@ -142,10 +143,11 @@ enum FirstPassState {
* Parses a string, providing information on matched tokens to the matched token consumer.
*
* @param message the message
* @param lenient whether to allow section symbols
* @param consumer the consumer
* @since 4.10.0
*/
public static void parseString(final String message, final MatchedTokenConsumer<?> consumer) {
public static void parseString(final String message, final boolean lenient, final MatchedTokenConsumer<?> consumer) {
FirstPassState state = FirstPassState.NORMAL;
// If the current state is escaped then the next character is skipped
boolean escaped = false;
Expand All @@ -158,7 +160,7 @@ public static void parseString(final String message, final MatchedTokenConsumer<
final int length = message.length();
for (int i = 0; i < length; i++) {
final int codePoint = message.codePointAt(i);
if (codePoint == '§' && i + 1 < length) {
if (!lenient && codePoint == '§' && i + 1 < length) {
final int nextChar = Character.toLowerCase(message.codePointAt(i + 1));
// Only throw an exception if the next character is actually going to make a legacy color code
if ((nextChar >= '0' && nextChar <= '9')
Expand Down
Expand Up @@ -82,7 +82,7 @@ public void accept(final int start, final int end, final @NotNull TokenType toke

// we might care if it's a valid tag!
if (TagInternals.sanitizeAndCheckValidTagName(tag)) {
final List<Token> tokens = tokenize(match);
final List<Token> tokens = tokenize(match, false);
final List<TagPart> parts = new ArrayList<>();
final List<Token> childs = tokens.isEmpty() ? null : tokens.get(0).childTokens();
if (childs != null) {
Expand Down
Expand Up @@ -157,6 +157,15 @@ void testEscapeParse() {
assertEquals(expected, PlainTextComponentSerializer.plainText().serialize(comp));
}

// https://github.com/KyoriPowered/adventure/issues/799
@Test
void testEscapeIgnoresSectionSigns() {
final String input = "Hello, read §64 for <red> information";
final String expected = "Hello, read §64 for \\<red> information";

assertEquals(expected, PARSER.escapeTags(input));
}

@Test
void testNiceMix() {
final String input = "<yellow><test> random <bold>stranger</bold><click:run_command:test command><underlined><red>click here</click><blue> to <b>FEEL</underlined> it";
Expand Down

0 comments on commit bc2c627

Please sign in to comment.