Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions include/aotextarea.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AOTextArea : public QTextBrowser {
void append_chatmessage(QString p_name, QString p_message,
QString p_name_colour, QString p_color = QString());
void append_error(QString p_message);
QString closetags(QString html);

private:
const QRegularExpression url_parser_regex = QRegularExpression("\\b(https?://\\S+\\.\\S+)\\b");
Expand Down
35 changes: 32 additions & 3 deletions src/aotextarea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ void AOTextArea::append_chatmessage(QString p_name, QString p_message,
p_message += " ";
}

QString result = p_message.toHtmlEscaped()
.replace("\n", "<br>")
.replace(url_parser_regex, "<a href='\\1'>\\1</a>");
QString result = this->closetags(p_message);
result = result.replace("\n", "<br>").replace(url_parser_regex, "<a href='\\1'>\\1</a>");
Comment on lines +35 to +36
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For url parse regex we'll need to not do that if it's already handled via html


if (!p_color.isEmpty()) {
result = "<font color=" + p_color + ">" + result + "</font>";
Expand Down Expand Up @@ -81,3 +80,33 @@ void AOTextArea::auto_scroll(QTextCursor old_cursor, int old_scrollbar_value,
this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum());
}
}

QString AOTextArea::closetags(QString html)
{
QRegExp opened_regex("(<([a-z]+)(?: .*)?(?<![/|/ ])>)");
QRegExp closed_regex("(</([a-z]+)>)");

opened_regex.indexIn(html);
closed_regex.indexIn(html);
QStringList opentags = opened_regex.capturedTexts();
QStringList closetags = closed_regex.capturedTexts();

int size_opentags = opentags.count();
for (int i = 1; i <= size_opentags; i++) {
if (i <= closetags.count()) {
QString expected_opentag = closetags.at(i - 1);
expected_opentag = expected_opentag.replace("</", "<").replace(">", "");
if (!opentags.at(size_opentags - i).startsWith(expected_opentag)) {
QString expected_closetag = opentags.at(size_opentags - i);
expected_closetag = expected_closetag.replace("<", "</");
html = html + expected_closetag;
}
}
else {
QString expected_closetag = opentags.at(size_opentags - i);
expected_closetag = expected_closetag.replace("<", "</");
html = html + expected_closetag;
}
}
return html;
Copy link
Owner

@Crystalwarrior Crystalwarrior Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a qDebug() << html and none of the tags are actually closed Seems to be a centering specific issue actually. After </center> a <br> is needed

brave_6zHHsjtm6M

To properly stop centering, it should be <br><center>msg</center><br>
image

}