mirror of
https://invent.kde.org/system/dolphin
synced 2024-09-17 15:31:20 +00:00
(search) Fix searching tags with spaces
Summary: Tags containing blank spaces were not handled properly in the search widget. Now we enclose them in quotes and strip the quotes before setting them to the widget. {F7854247} Test Plan: No artifacts when searching tags containing spaces Added test cases to `bin/dolphinquerytest` Reviewers: #dolphin, elvisangelaccio, ngraham, meven Reviewed By: #dolphin, elvisangelaccio, ngraham Subscribers: meven, kfm-devel Tags: #dolphin Differential Revision: https://phabricator.kde.org/D26369
This commit is contained in:
parent
897ce8480c
commit
5778099ece
|
@ -131,9 +131,13 @@ QStringList DolphinFacetsWidget::searchTerms() const
|
||||||
|
|
||||||
if (!m_searchTags.isEmpty()) {
|
if (!m_searchTags.isEmpty()) {
|
||||||
for (auto const &tag : m_searchTags) {
|
for (auto const &tag : m_searchTags) {
|
||||||
|
if (tag.contains(QLatin1Char(' '))) {
|
||||||
|
terms << QStringLiteral("tag:\"%1\"").arg(tag);
|
||||||
|
} else {
|
||||||
terms << QStringLiteral("tag:%1").arg(tag);
|
terms << QStringLiteral("tag:%1").arg(tag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return terms;
|
return terms;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,12 +27,13 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
/** Checks if a given term in the Baloo::Query::searchString() is a special search term.
|
/** Checks if a given term in the Baloo::Query::searchString() is a special search term
|
||||||
* This is a copy of `DolphinFacetsWidget::isSearchTerm()` method.
|
* @return: the specific search token of the term, or an empty QString() if none is found
|
||||||
*/
|
*/
|
||||||
bool isSearchTerm(const QString& term)
|
QString searchTermToken(const QString& term)
|
||||||
{
|
{
|
||||||
static const QLatin1String searchTokens[] {
|
static const QLatin1String searchTokens[] {
|
||||||
|
QLatin1String("filename:"),
|
||||||
QLatin1String("modified>="),
|
QLatin1String("modified>="),
|
||||||
QLatin1String("rating>="),
|
QLatin1String("rating>="),
|
||||||
QLatin1String("tag:"), QLatin1String("tag=")
|
QLatin1String("tag:"), QLatin1String("tag=")
|
||||||
|
@ -40,27 +41,24 @@ namespace {
|
||||||
|
|
||||||
for (const auto &searchToken : searchTokens) {
|
for (const auto &searchToken : searchTokens) {
|
||||||
if (term.startsWith(searchToken)) {
|
if (term.startsWith(searchToken)) {
|
||||||
return true;
|
return searchToken;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString stripQuotes(const QString& text)
|
QString stripQuotes(const QString& text)
|
||||||
{
|
{
|
||||||
QString cleanedText = text;
|
if (text.length() >= 2 && text.at(0) == QLatin1Char('"')
|
||||||
if (!cleanedText.isEmpty() && cleanedText.at(0) == QLatin1Char('"')) {
|
&& text.back() == QLatin1Char('"')) {
|
||||||
cleanedText = cleanedText.mid(1);
|
return text.mid(1, text.size() - 2);
|
||||||
}
|
}
|
||||||
if (!cleanedText.isEmpty() && cleanedText.back() == QLatin1Char('"')) {
|
return text;
|
||||||
cleanedText = cleanedText.mid(0, cleanedText.size() - 1);
|
|
||||||
}
|
|
||||||
return cleanedText;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList splitOutsideQuotes(const QString& text)
|
QStringList splitOutsideQuotes(const QString& text)
|
||||||
{
|
{
|
||||||
const QRegularExpression subTermsRegExp("([^ ]*\"[^\"]*\"|(?<= |^)[^ ]+(?= |$))");
|
const QRegularExpression subTermsRegExp("(\\S*?\"[^\"]*?\"|(?<=\\s|^)\\S+(?=\\s|$))");
|
||||||
auto subTermsMatchIterator = subTermsRegExp.globalMatch(text);
|
auto subTermsMatchIterator = subTermsRegExp.globalMatch(text);
|
||||||
|
|
||||||
QStringList textParts;
|
QStringList textParts;
|
||||||
|
@ -89,25 +87,25 @@ DolphinQuery DolphinQuery::fromBalooSearchUrl(const QUrl& searchUrl)
|
||||||
|
|
||||||
const QStringList subTerms = splitOutsideQuotes(query.searchString());
|
const QStringList subTerms = splitOutsideQuotes(query.searchString());
|
||||||
foreach (const QString& subTerm, subTerms) {
|
foreach (const QString& subTerm, subTerms) {
|
||||||
if (subTerm.startsWith(QLatin1String("filename:"))) {
|
const QString token = searchTermToken(subTerm);
|
||||||
fileName = stripQuotes(subTerm.mid(9));
|
const QString value = stripQuotes(subTerm.mid(token.length()));
|
||||||
if (!fileName.isEmpty()) {
|
|
||||||
|
if (token == QLatin1String("filename:")) {
|
||||||
|
if (!value.isEmpty()) {
|
||||||
|
fileName = value;
|
||||||
model.m_hasFileName = true;
|
model.m_hasFileName = true;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
} else if (isSearchTerm(subTerm)) {
|
} else if (!token.isEmpty()) {
|
||||||
model.m_searchTerms << subTerm;
|
model.m_searchTerms << token + value;
|
||||||
continue;
|
continue;
|
||||||
} else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) {
|
} else if (subTerm == QLatin1String("AND") && subTerm != subTerms.at(0) && subTerm != subTerms.back()) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else if (!value.isEmpty()) {
|
||||||
const QString cleanedTerm = stripQuotes(subTerm);
|
textParts << value;
|
||||||
if (!cleanedTerm.isEmpty()) {
|
|
||||||
textParts << cleanedTerm;
|
|
||||||
model.m_hasContentSearch = true;
|
model.m_hasContentSearch = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (model.m_hasFileName) {
|
if (model.m_hasFileName) {
|
||||||
if (model.m_hasContentSearch) {
|
if (model.m_hasContentSearch) {
|
||||||
|
|
|
@ -41,12 +41,17 @@ private slots:
|
||||||
*/
|
*/
|
||||||
void DolphinSearchBoxTest::testBalooSearchParsing_data()
|
void DolphinSearchBoxTest::testBalooSearchParsing_data()
|
||||||
{
|
{
|
||||||
const QString text = QStringLiteral("abc xyz");
|
const QString text = QStringLiteral("abc");
|
||||||
|
const QString textS = QStringLiteral("abc xyz");
|
||||||
const QString filename = QStringLiteral("filename:\"%1\"").arg(text);
|
const QString filename = QStringLiteral("filename:\"%1\"").arg(text);
|
||||||
|
const QString filenameS = QStringLiteral("filename:\"%1\"").arg(textS);
|
||||||
|
|
||||||
const QString rating = QStringLiteral("rating>=2");
|
const QString rating = QStringLiteral("rating>=2");
|
||||||
const QString modified = QString("modified>=2019-08-07");
|
const QString modified = QStringLiteral("modified>=2019-08-07");
|
||||||
const QString tagA = QString("tag:tagA");
|
|
||||||
const QString tagB = QString("tag:tagB");
|
const QString tag = QStringLiteral("tag:tagA");
|
||||||
|
const QString tagS = QStringLiteral("tag:\"tagB with spaces\""); // in search url
|
||||||
|
const QString tagR = QStringLiteral("tag:tagB with spaces"); // in result term
|
||||||
|
|
||||||
QTest::addColumn<QString>("searchString");
|
QTest::addColumn<QString>("searchString");
|
||||||
QTest::addColumn<QString>("expectedText");
|
QTest::addColumn<QString>("expectedText");
|
||||||
|
@ -56,18 +61,22 @@ void DolphinSearchBoxTest::testBalooSearchParsing_data()
|
||||||
|
|
||||||
// Test for "Content"
|
// Test for "Content"
|
||||||
QTest::newRow("content") << text << text << QStringList() << true << false;
|
QTest::newRow("content") << text << text << QStringList() << true << false;
|
||||||
|
QTest::newRow("content/space") << textS << textS << QStringList() << true << false;
|
||||||
QTest::newRow("content/empty") << "" << "" << QStringList() << false << false;
|
QTest::newRow("content/empty") << "" << "" << QStringList() << false << false;
|
||||||
QTest::newRow("content/singleQuote") << "\"" << "" << QStringList() << false << false;
|
QTest::newRow("content/single_quote") << "\"" << "\"" << QStringList() << true << false;
|
||||||
QTest::newRow("content/doubleQuote") << "\"\"" << "" << QStringList() << false << false;
|
QTest::newRow("content/double_quote") << "\"\"" << "" << QStringList() << false << false;
|
||||||
|
|
||||||
// Test for "FileName"
|
// Test for "FileName"
|
||||||
QTest::newRow("filename") << filename << text << QStringList() << false << true;
|
QTest::newRow("filename") << filename << text << QStringList() << false << true;
|
||||||
|
QTest::newRow("filename/space") << filenameS << textS << QStringList() << false << true;
|
||||||
QTest::newRow("filename/empty") << "filename:" << "" << QStringList() << false << false;
|
QTest::newRow("filename/empty") << "filename:" << "" << QStringList() << false << false;
|
||||||
QTest::newRow("filename/singleQuote") << "filename:\"" << "" << QStringList() << false << false;
|
QTest::newRow("filename/single_quote") << "filename:\"" << "\"" << QStringList() << false << true;
|
||||||
QTest::newRow("filename/doubleQuote") << "filename:\"\"" << "" << QStringList() << false << false;
|
QTest::newRow("filename/double_quote") << "filename:\"\"" << "" << QStringList() << false << false;
|
||||||
|
|
||||||
// Combined content and filename search
|
// Combined content and filename search
|
||||||
QTest::newRow("content+filename") << text + " " + filename << text + " " + filename << QStringList() << true << true;
|
QTest::newRow("content+filename")
|
||||||
|
<< text + " " + filename
|
||||||
|
<< text + " " + filename << QStringList() << true << true;
|
||||||
|
|
||||||
// Test for rating
|
// Test for rating
|
||||||
QTest::newRow("rating") << rating << "" << QStringList({rating}) << false << false;
|
QTest::newRow("rating") << rating << "" << QStringList({rating}) << false << false;
|
||||||
|
@ -80,27 +89,32 @@ void DolphinSearchBoxTest::testBalooSearchParsing_data()
|
||||||
QTest::newRow("modified+filename") << modified + " " + filename << text << QStringList({modified}) << false << true;
|
QTest::newRow("modified+filename") << modified + " " + filename << text << QStringList({modified}) << false << true;
|
||||||
|
|
||||||
// Test for tags
|
// Test for tags
|
||||||
QTest::newRow("tag") << tagA << "" << QStringList({tagA}) << false << false;
|
QTest::newRow("tag") << tag << "" << QStringList({tag}) << false << false;
|
||||||
QTest::newRow("tag/double") << tagA + " " + tagB << "" << QStringList({tagA, tagB}) << false << false;
|
QTest::newRow("tag/space" ) << tagS << "" << QStringList({tagR}) << false << false;
|
||||||
QTest::newRow("tag+content") << tagA + " " + text << text << QStringList({tagA}) << true << false;
|
QTest::newRow("tag/double") << tag + " " + tagS << "" << QStringList({tag, tagR}) << false << false;
|
||||||
QTest::newRow("tag+filename") << tagA + " " + filename << text << QStringList({tagA}) << false << true;
|
QTest::newRow("tag+content") << tag + " " + text << text << QStringList({tag}) << true << false;
|
||||||
|
QTest::newRow("tag+filename") << tag + " " + filename << text << QStringList({tag}) << false << true;
|
||||||
|
|
||||||
// Combined search terms
|
// Combined search terms
|
||||||
|
QTest::newRow("searchTerms")
|
||||||
|
<< rating + " AND " + modified + " AND " + tag + " AND " + tagS
|
||||||
|
<< "" << QStringList({modified, rating, tag, tagR}) << false << false;
|
||||||
|
|
||||||
|
QTest::newRow("searchTerms+content")
|
||||||
|
<< rating + " AND " + modified + " " + text + " " + tag + " AND " + tagS
|
||||||
|
<< text << QStringList({modified, rating, tag, tagR}) << true << false;
|
||||||
|
|
||||||
|
QTest::newRow("searchTerms+filename")
|
||||||
|
<< rating + " AND " + modified + " " + filename + " " + tag + " AND " + tagS
|
||||||
|
<< text << QStringList({modified, rating, tag, tagR}) << false << true;
|
||||||
|
|
||||||
QTest::newRow("allTerms")
|
QTest::newRow("allTerms")
|
||||||
<< rating + " AND " + modified + " AND " + tagA + " AND " + tagB
|
<< text + " " + filename + " " + rating + " AND " + modified + " AND " + tag
|
||||||
<< "" << QStringList({modified, rating, tagA, tagB}) << false << false;
|
<< text + " " + filename << QStringList({modified, rating, tag}) << true << true;
|
||||||
|
|
||||||
QTest::newRow("allTerms+content")
|
QTest::newRow("allTerms/space")
|
||||||
<< rating + " AND " + modified + " " + text + " " + tagA + " AND " + tagB
|
<< textS + " " + filenameS + " " + rating + " AND " + modified + " AND " + tagS
|
||||||
<< text << QStringList({modified, rating, tagA, tagB}) << true << false;
|
<< textS + " " + filenameS << QStringList({modified, rating, tagR}) << true << true;
|
||||||
|
|
||||||
QTest::newRow("allTerms+filename")
|
|
||||||
<< rating + " AND " + modified + " " + filename + " " + tagA + " AND " + tagB
|
|
||||||
<< text << QStringList({modified, rating, tagA, tagB}) << false << true;
|
|
||||||
|
|
||||||
QTest::newRow("allTerms+content+filename")
|
|
||||||
<< text + " " + filename + " " + rating + " AND " + modified + " AND " + tagA + " AND " + tagB
|
|
||||||
<< text + " " + filename << QStringList({modified, rating, tagA, tagB}) << true << true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue