aboutsummaryrefslogtreecommitdiff
path: root/src/browsertab.cpp
diff options
context:
space:
mode:
authorMike Skec <skec@protonmail.ch>2021-02-14 16:43:04 +1100
committerFelix Queißner <felix@ib-queissner.de>2021-02-14 14:18:12 +0100
commitfd190163feaf80537c6d5ab38f69b19dd41ff899 (patch)
tree22edae7d8caa5b1c0f845aef7b863099b958671c /src/browsertab.cpp
parent49e78d5338e274a28b51b60776c903234d7046cc (diff)
downloadkristall-fd190163feaf80537c6d5ab38f69b19dd41ff899.tar.gz
BrowserTab: finding quotations now works for all characters
Since #150, when typographer quotes were enabled, searching for text with quotation marks using the searchbox was made slightly more difficult as the search pattern would not match each of the different quotation mark types. This patch addresses this by using a regex which contains the search query, and creates a pattern that matches the various quote types.
Diffstat (limited to 'src/browsertab.cpp')
-rw-r--r--src/browsertab.cpp34
1 files changed, 27 insertions, 7 deletions
diff --git a/src/browsertab.cpp b/src/browsertab.cpp
index f6684b5..15205a5 100644
--- a/src/browsertab.cpp
+++ b/src/browsertab.cpp
@@ -1542,6 +1542,26 @@ void BrowserTab::disableClientCertificate()
this->current_identity = CryptoIdentity();
}
+bool BrowserTab::searchBoxFind(QString text, bool backward)
+{
+ // First we escape the query to be suitable to use inside a regex pattern.
+ // https://stackoverflow.com/a/6969486
+ static const QRegularExpression ESCAPE_REGEX = QRegularExpression(R"(([-\/\\^$*+?.()|[\]{}]))");
+ text.replace(ESCAPE_REGEX, "\\\\1");
+
+ // This part allows us to match different types of quotes easily:
+ // ' -> ('|‘|’)
+ // " -> ("|“|”)
+ static const QRegularExpression QUOTES_SINGLE_REGEX = QRegularExpression("'");
+ static const QRegularExpression QUOTES_DOUBLE_REGEX = QRegularExpression("\"");
+ text.replace(QUOTES_SINGLE_REGEX, "('|‘|’)").replace(QUOTES_DOUBLE_REGEX, "(\"|“|”)");
+
+ // Perform search using our new regex
+ return this->ui->text_browser->find(
+ QRegularExpression(text, QRegularExpression::CaseInsensitiveOption),
+ backward ? QTextDocument::FindBackward : QTextDocument::FindFlags());
+}
+
void BrowserTab::on_text_browser_customContextMenuRequested(const QPoint pos)
{
QMenu menu;
@@ -1637,33 +1657,33 @@ void BrowserTab::on_enable_client_cert_button_clicked(bool checked)
void BrowserTab::on_search_box_textChanged(const QString &arg1)
{
this->ui->text_browser->setTextCursor(QTextCursor { this->ui->text_browser->document() });
- this->ui->text_browser->find(arg1);
+ this->searchBoxFind(arg1);
}
void BrowserTab::on_search_box_returnPressed()
{
- this->ui->text_browser->find(this->ui->search_box->text());
+ this->searchBoxFind(this->ui->search_box->text());
}
void BrowserTab::on_search_next_clicked()
{
- if (!this->ui->text_browser->find(this->ui->search_box->text()) &&
+ if (!this->searchBoxFind(this->ui->search_box->text()) &&
this->current_buffer.contains(this->ui->search_box->text().toUtf8()))
{
// Wrap search
this->ui->text_browser->moveCursor(QTextCursor::Start);
- this->ui->text_browser->find(this->ui->search_box->text());
+ this->searchBoxFind(this->ui->search_box->text());
}
}
void BrowserTab::on_search_previous_clicked()
{
- if (!this->ui->text_browser->find(this->ui->search_box->text(), QTextDocument::FindBackward) &&
+ if (!this->searchBoxFind(this->ui->search_box->text(), true) &&
this->current_buffer.contains(this->ui->search_box->text().toUtf8()))
{
// Wrap search
this->ui->text_browser->moveCursor(QTextCursor::End);
- this->ui->text_browser->find(this->ui->search_box->text(), QTextDocument::FindBackward);
+ this->searchBoxFind(this->ui->search_box->text(), true);
}
}
@@ -1672,7 +1692,7 @@ void BrowserTab::on_close_search_clicked()
this->ui->search_bar->setVisible(false);
}
-void BrowserTab::resizeEvent(QResizeEvent * event)
+void BrowserTab::resizeEvent(QResizeEvent *event)
{
this->updatePageMargins();
QWidget::resizeEvent(event);