From a4381734038d4451774cae2bf79b2171945b6760 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sun, 15 Feb 2026 12:24:53 +0000 Subject: [PATCH] Change the behaviour of chatmarkdownhelper to only work when a single character is added. This is because if pasting a link for example * or _ could be treated as formatting. We now assume that any pasted text is done as plain text and is wanted as is. --- autotests/chatmarkdownhelpertest.qml | 8 ++-- src/libneochat/chatmarkdownhelper.cpp | 56 +++++++++++---------------- src/libneochat/chatmarkdownhelper.h | 2 +- 3 files changed, 27 insertions(+), 39 deletions(-) diff --git a/autotests/chatmarkdownhelpertest.qml b/autotests/chatmarkdownhelpertest.qml index a3519f529..30dd44122 100644 --- a/autotests/chatmarkdownhelpertest.qml +++ b/autotests/chatmarkdownhelpertest.qml @@ -159,15 +159,15 @@ TestCase { compare(chatMarkdownHelper.checkText("test"), true); compare(chatMarkdownHelper.checkFormats([]), true); textEdit.insert(4, "**b"); - compare(chatMarkdownHelper.checkText("testb"), true); - compare(chatMarkdownHelper.checkFormats([RichFormat.Bold]), true); + compare(chatMarkdownHelper.checkText("test**b"), true); + compare(chatMarkdownHelper.checkFormats([]), true); textEdit.clear(); textEdit.insert(0, "test"); compare(chatMarkdownHelper.checkText("test"), true); compare(chatMarkdownHelper.checkFormats([]), true); textEdit.insert(2, "**b"); - compare(chatMarkdownHelper.checkText("tebst"), true); - compare(chatMarkdownHelper.checkFormats([RichFormat.Bold]), true); + compare(chatMarkdownHelper.checkText("te**bst"), true); + compare(chatMarkdownHelper.checkFormats([]), true); } } diff --git a/src/libneochat/chatmarkdownhelper.cpp b/src/libneochat/chatmarkdownhelper.cpp index ffaf865cc..91d021b5b 100644 --- a/src/libneochat/chatmarkdownhelper.cpp +++ b/src/libneochat/chatmarkdownhelper.cpp @@ -188,17 +188,12 @@ void ChatMarkdownHelper::checkMarkdown(int position, int charsRemoved, int chars // This can happen when formatting is applied. if (charsAdded == charsRemoved) { return; - } - auto cursor = m_textItem->textCursor(); - if (cursor.isNull()) { + } else if (charsRemoved > charsAdded || charsAdded - charsRemoved > 1) { + updatePosition(std::max(0, position - charsRemoved + charsAdded)); return; } - if (charsRemoved > charsAdded) { - updatePosition(std::max(0, position - charsRemoved + charsAdded)); - } - - checkMarkdownForward(charsAdded - charsRemoved); + checkMarkdownForward(); } void ChatMarkdownHelper::updatePosition(int position) @@ -226,40 +221,33 @@ void ChatMarkdownHelper::updatePosition(int position) } } -void ChatMarkdownHelper::checkMarkdownForward(int charsAdded) +void ChatMarkdownHelper::checkMarkdownForward() { - if (charsAdded <= 0) { - return; - } auto cursor = m_textItem->textCursor(); if (cursor.isNull()) { return; } - for (auto i = 1; i <= charsAdded; ++i) { - cursor.setPosition(m_startPos); - const auto atBlockStart = cursor.atBlockStart(); - cursor.setPosition(m_endPos, QTextCursor::KeepAnchor); - const auto currentMarkdown = cursor.selectedText(); - cursor.setPosition(m_endPos); - cursor.setPosition(m_endPos + 1, QTextCursor::KeepAnchor); - const auto nextChar = cursor.selectedText(); + cursor.setPosition(m_startPos); + const auto atBlockStart = cursor.atBlockStart(); + cursor.setPosition(m_endPos, QTextCursor::KeepAnchor); + const auto currentMarkdown = cursor.selectedText(); + cursor.setPosition(m_endPos); + cursor.setPosition(m_endPos + 1, QTextCursor::KeepAnchor); + const auto nextChar = cursor.selectedText(); - const auto result = checkSequence(currentMarkdown, nextChar, atBlockStart); - if (!result) { - ++m_startPos; - m_endPos = m_startPos; - continue; - ; - } - if (!*result) { - ++m_endPos; - continue; - ; - } - - complete(); + const auto result = checkSequence(currentMarkdown, nextChar, atBlockStart); + if (!result) { + ++m_startPos; + m_endPos = m_startPos; + return; } + if (!*result) { + ++m_endPos; + return; + } + + complete(); } void ChatMarkdownHelper::complete() diff --git a/src/libneochat/chatmarkdownhelper.h b/src/libneochat/chatmarkdownhelper.h index 3b33c541a..5842943af 100644 --- a/src/libneochat/chatmarkdownhelper.h +++ b/src/libneochat/chatmarkdownhelper.h @@ -42,6 +42,6 @@ private: void checkMarkdown(int position, int charsRemoved, int charsAdded); void updatePosition(int position); - void checkMarkdownForward(int charsAdded); + void checkMarkdownForward(); void complete(); };