diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2024-03-01 00:39:50 -0700 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2024-03-26 00:47:37 -0700 |
commit | 0281005a711c3635114ba92f778d0e9c8a89027d (patch) | |
tree | 5705f86f23c9ea20105c1d447b0cf73ba74b73fc /src/gui/text | |
parent | ae8031b5e72032f9e2884c18cd72639acfd0d1a4 (diff) |
QTextMarkdownWriter: escape all backslashes
A literal backslash needs to be doubled so that the parser doesn't treat
it as escaping the following character when the markdown is read back.
In ca4774131b9b8ee40b4d7f5c1ba296af4700207f we tried to limit it to
backslashes that were not already escaped. In case someone really needs
a longer series of backslashes, it's more correct to escape them all;
but this comes with the risk that if they do not get un-escaped by the
markdown parser in some scenario, repeated round-trip saving and loading
could multiply them excessively. So we also add a lot of tests to try
to verify that this is safe.
Task-number: QTBUG-96051
Fixes: QTBUG-122083
Pick-to: 6.7
Change-Id: I64f610d24e99f67ebdc30d5ab5c6cf3985aec5ec
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Diffstat (limited to 'src/gui/text')
-rw-r--r-- | src/gui/text/qtextmarkdownwriter.cpp | 13 |
1 files changed, 4 insertions, 9 deletions
diff --git a/src/gui/text/qtextmarkdownwriter.cpp b/src/gui/text/qtextmarkdownwriter.cpp index e65709bfac..361158e722 100644 --- a/src/gui/text/qtextmarkdownwriter.cpp +++ b/src/gui/text/qtextmarkdownwriter.cpp @@ -316,25 +316,20 @@ static void maybeEscapeFirstChar(QString &s) } /*! \internal - Escape unescaped backslashes. Then escape any special character that stands + Escape all backslashes. Then escape any special character that stands alone or prefixes a "word", including the \c < that starts an HTML tag. https://spec.commonmark.org/0.31.2/#backslash-escapes */ static void escapeSpecialCharacters(QString &s) { - static const QRegularExpression backslashRe(uR"([^\\]\\)"_s); static const QRegularExpression spaceRe(uR"(\s+)"_s); static const QRegularExpression specialRe(uR"([<!*[`&]+[/\w])"_s); + s.replace("\\"_L1, "\\\\"_L1); + int i = 0; while (i >= 0) { - if (int j = s.indexOf(backslashRe, i); j >= 0) { - ++j; // we found some char before the backslash that needs escaping - if (s.size() == j + 1 || s.at(j + 1) != qtmw_Backslash) - s.insert(j, qtmw_Backslash); - i = j + 3; - } - if (int j = s.indexOf(specialRe, i); j >= 0 && (j == 0 || s.at(j - 1) != u'\\')) { + if (int j = s.indexOf(specialRe, i); j >= 0) { s.insert(j, qtmw_Backslash); i = j + 3; } |