From d6488d6c8ef6d351b392fcecebba056e6a4bfb87 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Sat, 1 Nov 2025 23:31:45 +0100 Subject: [PATCH] fixed escaping of formatting characters see #757 --- src/utils/textstyleparser.go | 5 +++++ src/utils/textstyleparser_test.go | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/utils/textstyleparser.go b/src/utils/textstyleparser.go index 8df7f57..9b267c9 100644 --- a/src/utils/textstyleparser.go +++ b/src/utils/textstyleparser.go @@ -141,12 +141,14 @@ func (l *TextstyleParser) Parse() (string, []string) { l.next() if prevChar == EscapeCharacter { prevChar = c + l.fullString += "**" continue } l.handleToken(BoldBegin, Bold) } else { //Italic if prevChar == EscapeCharacter { prevChar = c + l.fullString += "*" continue } l.handleToken(ItalicBegin, Italic) @@ -155,18 +157,21 @@ func (l *TextstyleParser) Parse() (string, []string) { l.next() if prevChar == EscapeCharacter { prevChar = c + l.fullString += "||" continue } l.handleToken(SpoilerBegin, Spoiler) } else if c == '~' { if prevChar == EscapeCharacter { prevChar = c + l.fullString += "~" continue } l.handleToken(StrikethroughBegin, Strikethrough) } else if c == '`' { if prevChar == EscapeCharacter { prevChar = c + l.fullString += "`" continue } l.handleToken(MonoSpaceBegin, Monospace) diff --git a/src/utils/textstyleparser_test.go b/src/utils/textstyleparser_test.go index eb8dfe2..480a1fd 100644 --- a/src/utils/textstyleparser_test.go +++ b/src/utils/textstyleparser_test.go @@ -1,7 +1,9 @@ package utils -import "testing" -import "reflect" +import ( + "reflect" + "testing" +) func expectMessageEqual(t *testing.T, message1 string, message2 string) { if message1 != message2 { @@ -123,27 +125,35 @@ func TestBoldTextInsideSpoiler(t *testing.T) { func TestEscapeAsterisks(t *testing.T) { textstyleParser := NewTextstyleParser("\\*escaped text\\*") message, signalCliFormatStrings := textstyleParser.Parse() - expectMessageEqual(t, message, "escaped text") + expectMessageEqual(t, message, "*escaped text*") expectFormatStringsEqual(t, signalCliFormatStrings, []string{}) } func TestEscapeAsterisks1(t *testing.T) { textstyleParser := NewTextstyleParser("\\**escaped text\\**") message, signalCliFormatStrings := textstyleParser.Parse() - expectMessageEqual(t, message, "escaped text") + expectMessageEqual(t, message, "**escaped text**") expectFormatStringsEqual(t, signalCliFormatStrings, []string{}) } func TestEscapeBackticks(t *testing.T) { textstyleParser := NewTextstyleParser("\\`escaped text\\`") message, signalCliFormatStrings := textstyleParser.Parse() - expectMessageEqual(t, message, "escaped text") + expectMessageEqual(t, message, "`escaped text`") expectFormatStringsEqual(t, signalCliFormatStrings, []string{}) } func TestEscapeTilde(t *testing.T) { textstyleParser := NewTextstyleParser("\\~escaped text\\~") message, signalCliFormatStrings := textstyleParser.Parse() - expectMessageEqual(t, message, "escaped text") + expectMessageEqual(t, message, "~escaped text~") expectFormatStringsEqual(t, signalCliFormatStrings, []string{}) } + +func TestEscapeNew(t *testing.T) { + textstyleParser := NewTextstyleParser("Test \\** \\* \\~ Escape") + message, signalCliFormatStrings := textstyleParser.Parse() + expectMessageEqual(t, message, "Test ** * ~ Escape") + expectFormatStringsEqual(t, signalCliFormatStrings, []string{}) + +}