1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

LocationBar: Handle url/search resolution in loadAction instead of WebView::load

This commit is contained in:
David Rosca 2018-01-27 10:28:03 +01:00
parent 6043d38429
commit 2e7436c741
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
5 changed files with 41 additions and 80 deletions

View File

@ -64,7 +64,6 @@ void LocationBarTest::loadActionBasicTest()
action = LocationBar::loadAction("localhost/test/path?x=2"); action = LocationBar::loadAction("localhost/test/path?x=2");
QCOMPARE(action.type, LocationBar::LoadAction::Url); QCOMPARE(action.type, LocationBar::LoadAction::Url);
QEXPECT_FAIL("", "localhost is not treated as full host", Continue);
QCOMPARE(action.loadRequest.url(), QUrl("http://localhost/test/path?x=2")); QCOMPARE(action.loadRequest.url(), QUrl("http://localhost/test/path?x=2"));
action = LocationBar::loadAction("host.com/test/path?x=2"); action = LocationBar::loadAction("host.com/test/path?x=2");
@ -72,12 +71,14 @@ void LocationBarTest::loadActionBasicTest()
QCOMPARE(action.loadRequest.url(), QUrl("http://host.com/test/path?x=2")); QCOMPARE(action.loadRequest.url(), QUrl("http://host.com/test/path?x=2"));
action = LocationBar::loadAction("not-url"); action = LocationBar::loadAction("not-url");
QCOMPARE(action.type, LocationBar::LoadAction::Url); QCOMPARE(action.type, LocationBar::LoadAction::Search);
QCOMPARE(action.loadRequest.url(), QUrl("not-url"));
action = LocationBar::loadAction("not url with spaces"); action = LocationBar::loadAction("not url with spaces");
QCOMPARE(action.type, LocationBar::LoadAction::Search);
action = LocationBar::loadAction("qupzilla:about");
QCOMPARE(action.type, LocationBar::LoadAction::Url); QCOMPARE(action.type, LocationBar::LoadAction::Url);
QCOMPARE(action.loadRequest.url(), QUrl("not url with spaces")); QCOMPARE(action.loadRequest.url(), QUrl("qupzilla:about"));
} }
void LocationBarTest::loadActionBookmarksTest() void LocationBarTest::loadActionBookmarksTest()
@ -95,8 +96,7 @@ void LocationBarTest::loadActionBookmarksTest()
QCOMPARE(action.loadRequest.url(), QUrl("http://kde.org")); QCOMPARE(action.loadRequest.url(), QUrl("http://kde.org"));
action = LocationBar::loadAction("kde-bookmark-notkeyword"); action = LocationBar::loadAction("kde-bookmark-notkeyword");
QCOMPARE(action.type, LocationBar::LoadAction::Url); QCOMPARE(action.type, LocationBar::LoadAction::Search);
QCOMPARE(action.loadRequest.url(), QUrl("kde-bookmark-notkeyword"));
action = LocationBar::loadAction("kde-bookmark"); action = LocationBar::loadAction("kde-bookmark");
QCOMPARE(action.type, LocationBar::LoadAction::Bookmark); QCOMPARE(action.type, LocationBar::LoadAction::Bookmark);
@ -111,13 +111,12 @@ void LocationBarTest::loadActionSearchTest()
engine.url = "http://test/%s"; engine.url = "http://test/%s";
engine.shortcut = "t"; engine.shortcut = "t";
mApp->searchEnginesManager()->addEngine(engine); mApp->searchEnginesManager()->addEngine(engine);
mApp->searchEnginesManager()->setActiveEngine(engine);
LocationBar::LoadAction action; LocationBar::LoadAction action;
action = LocationBar::loadAction("search term"); action = LocationBar::loadAction("search term");
QEXPECT_FAIL("", "loadAction is not handling searching", Continue);
QCOMPARE(action.type, LocationBar::LoadAction::Search); QCOMPARE(action.type, LocationBar::LoadAction::Search);
QEXPECT_FAIL("", "loadAction is not handling searching", Continue);
QCOMPARE(action.loadRequest.url(), QUrl("http://test/search%20term")); QCOMPARE(action.loadRequest.url(), QUrl("http://test/search%20term"));
action = LocationBar::loadAction("t search term"); action = LocationBar::loadAction("t search term");
@ -125,8 +124,8 @@ void LocationBarTest::loadActionSearchTest()
QCOMPARE(action.loadRequest.url(), QUrl("http://test/search%20term")); QCOMPARE(action.loadRequest.url(), QUrl("http://test/search%20term"));
action = LocationBar::loadAction(" ttt-notsearch"); action = LocationBar::loadAction(" ttt-notsearch");
QCOMPARE(action.type, LocationBar::LoadAction::Url); QCOMPARE(action.type, LocationBar::LoadAction::Search);
QCOMPARE(action.loadRequest.url(), QUrl("ttt-notsearch")); QCOMPARE(action.loadRequest.url(), QUrl("http://test/ttt-notsearch"));
} }
int main(int argc, char **argv) int main(int argc, char **argv)

View File

@ -283,8 +283,6 @@ void LocationCompleter::indexDeleteRequested(const QModelIndex &index)
LoadRequest LocationCompleter::createLoadRequest(const QModelIndex &index) LoadRequest LocationCompleter::createLoadRequest(const QModelIndex &index)
{ {
LoadRequest request; LoadRequest request;
QString searchString;
SearchEngine searchEngine;
BookmarkItem *bookmark = nullptr; BookmarkItem *bookmark = nullptr;
if (index.data(LocationCompleterModel::HistoryRole).toBool()) { if (index.data(LocationCompleterModel::HistoryRole).toBool()) {
@ -292,8 +290,8 @@ LoadRequest LocationCompleter::createLoadRequest(const QModelIndex &index)
} else if (index.data(LocationCompleterModel::BookmarkRole).toBool()) { } else if (index.data(LocationCompleterModel::BookmarkRole).toBool()) {
bookmark = static_cast<BookmarkItem*>(index.data(LocationCompleterModel::BookmarkItemRole).value<void*>()); bookmark = static_cast<BookmarkItem*>(index.data(LocationCompleterModel::BookmarkItemRole).value<void*>());
} else if (index.data(LocationCompleterModel::SearchSuggestionRole).toBool()) { } else if (index.data(LocationCompleterModel::SearchSuggestionRole).toBool()) {
searchEngine = LocationBar::searchEngine(); const QString text = index.data(LocationCompleterModel::TitleRole).toString();
searchString = index.data(LocationCompleterModel::TitleRole).toString(); request = mApp->searchEnginesManager()->searchResult(LocationBar::searchEngine(), text);
} else if (index.data(LocationCompleterModel::VisitSearchItemRole).toBool()) { } else if (index.data(LocationCompleterModel::VisitSearchItemRole).toBool()) {
const auto action = LocationBar::loadAction(m_originalText); const auto action = LocationBar::loadAction(m_originalText);
switch (action.type) { switch (action.type) {
@ -312,8 +310,6 @@ LoadRequest LocationCompleter::createLoadRequest(const QModelIndex &index)
if (bookmark) { if (bookmark) {
bookmark->updateVisitCount(); bookmark->updateVisitCount();
request = bookmark->url(); request = bookmark->url();
} else if (searchEngine.isValid()) {
request = mApp->searchEnginesManager()->searchResult(searchEngine, searchString);
} }
return request; return request;

View File

@ -29,21 +29,6 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <QTextLayout> #include <QTextLayout>
static bool isUrlOrDomain(const QString &text)
{
QUrl url(text);
if (!url.scheme().isEmpty() && (!url.host().isEmpty() || !url.path().isEmpty())) {
return true;
}
if (text.contains(QL1C('.')) && !text.contains(QL1C(' '))) {
return true;
}
if (text == QL1S("localhost")) {
return true;
}
return false;
}
LocationCompleterDelegate::LocationCompleterDelegate(QObject *parent) LocationCompleterDelegate::LocationCompleterDelegate(QObject *parent)
: QStyledItemDelegate(parent) : QStyledItemDelegate(parent)
, m_rowHeight(0) , m_rowHeight(0)
@ -93,17 +78,12 @@ void LocationCompleterDelegate::paint(QPainter* painter, const QStyleOptionViewI
const bool isVisitSearchItem = index.data(LocationCompleterModel::VisitSearchItemRole).toBool(); const bool isVisitSearchItem = index.data(LocationCompleterModel::VisitSearchItemRole).toBool();
const bool isSearchSuggestion = index.data(LocationCompleterModel::SearchSuggestionRole).toBool(); const bool isSearchSuggestion = index.data(LocationCompleterModel::SearchSuggestionRole).toBool();
bool isWebSearch = false;
LocationBar::LoadAction loadAction; LocationBar::LoadAction loadAction;
bool isWebSearch = isSearchSuggestion;
if (isVisitSearchItem) { if (isVisitSearchItem) {
loadAction = LocationBar::loadAction(m_originalText); loadAction = LocationBar::loadAction(m_originalText);
isWebSearch = loadAction.type == LocationBar::LoadAction::Search; isWebSearch = loadAction.type == LocationBar::LoadAction::Search;
if (!isWebSearch && loadAction.type == LocationBar::LoadAction::Url) {
isWebSearch = !isUrlOrDomain(m_originalText.trimmed());
}
} else if (isSearchSuggestion) {
isWebSearch = true;
} }
// Draw icon // Draw icon

View File

@ -213,6 +213,10 @@ LocationBar::LoadAction LocationBar::loadAction(const QString &text)
const QString &t = text.trimmed(); const QString &t = text.trimmed();
if (t.isEmpty()) {
return action;
}
// Check for Search Engine shortcut // Check for Search Engine shortcut
const int firstSpacePos = t.indexOf(QLatin1Char(' ')); const int firstSpacePos = t.indexOf(QLatin1Char(' '));
if (qzSettings->searchFromAddressBar && firstSpacePos != -1) { if (qzSettings->searchFromAddressBar && firstSpacePos != -1) {
@ -238,27 +242,35 @@ LocationBar::LoadAction LocationBar::loadAction(const QString &text)
return action; return action;
} }
// Otherwise load as url if (!qzSettings->searchFromAddressBar) {
action.type = LoadAction::Url; action.type = LoadAction::Url;
action.loadRequest = QUrl(t);
return action;
}
// One word needs special handling, because QUrl::fromUserInput // Check for one word search
// would convert it to QUrl("http://WORD") if (t != QL1S("localhost")
if (t != QL1S("localhost") && !QzTools::containsSpace(t) && !t.contains(QL1C('.'))) { && !QzTools::containsSpace(t)
action.loadRequest.setUrl(QUrl(t)); && !t.contains(QL1C('.'))
} else { && !t.contains(QL1C(':'))
const QUrl &guessed = QUrl::fromUserInput(t); && !t.contains(QL1C('/'))
if (!guessed.isEmpty()) ) {
action.loadRequest.setUrl(guessed); action.type = LoadAction::Search;
else action.loadRequest = mApp->searchEnginesManager()->searchResult(searchEngine(), t);
action.loadRequest.setUrl(QUrl::fromEncoded(t.toUtf8())); return action;
}
// Otherwise load as url
const QUrl &guessedUrl = QUrl::fromUserInput(t);
if (guessedUrl.isValid()) {
action.type = LoadAction::Url;
action.loadRequest = guessedUrl;
return action;
} }
// Search when creating url failed // Search when creating url failed
if (qzSettings->searchFromAddressBar && !action.loadRequest.isValid()) { action.type = LoadAction::Search;
action.type = LoadAction::Search; action.loadRequest = mApp->searchEnginesManager()->searchResult(searchEngine(), t);
action.loadRequest = mApp->searchEnginesManager()->searchResult(t);
}
return action; return action;
} }

View File

@ -43,7 +43,6 @@
#include <QDesktopServices> #include <QDesktopServices>
#include <QWebEngineHistory> #include <QWebEngineHistory>
#include <QClipboard> #include <QClipboard>
#include <QHostInfo>
#include <QMimeData> #include <QMimeData>
#include <QWebEngineContextMenuData> #include <QWebEngineContextMenuData>
#include <QStackedLayout> #include <QStackedLayout>
@ -203,31 +202,6 @@ void WebView::load(const LoadRequest &request)
if (isUrlValid(reqUrl)) { if (isUrlValid(reqUrl)) {
loadRequest(request); loadRequest(request);
return;
}
// Make sure to correctly load hosts like localhost (eg. without the dot)
if (!reqUrl.isEmpty() &&
reqUrl.scheme().isEmpty() &&
!QzTools::containsSpace(reqUrl.path()) && // See #1622
!reqUrl.path().contains(QL1C('.'))
) {
QUrl u(QSL("http://") + reqUrl.path());
if (u.isValid()) {
// This is blocking...
QHostInfo info = QHostInfo::fromName(u.path());
if (info.error() == QHostInfo::NoError) {
LoadRequest req = request;
req.setUrl(u);
loadRequest(req);
return;
}
}
}
if (qzSettings->searchFromAddressBar) {
const LoadRequest searchRequest = mApp->searchEnginesManager()->searchResult(request.urlString());
loadRequest(searchRequest);
} }
} }