From 7284c9339449a5f022c4ba3adf40335447cb5007 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Wed, 24 Feb 2021 04:41:15 +0200 Subject: Bookmarks: Looking up site icons Bookmark icons apply to site roots instead of domains. A root includes the user directory if one is found in the URL. --- src/bookmarks.c | 35 +++++++++++++++++------------------ src/gmutil.c | 26 +++++++++++++++++++++----- src/gmutil.h | 1 + 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/bookmarks.c b/src/bookmarks.c index 60048d36..91280f3c 100644 --- a/src/bookmarks.c +++ b/src/bookmarks.c @@ -254,27 +254,26 @@ iChar siteIcon_Bookmarks(const iBookmarks *d, const iString *url) { if (isEmpty_String(url)) { return 0; } - iChar icon = 0; - iRangecc hostName = urlHost_String(url); - const size_t hostSize = size_Range(&hostName); - size_t matchingSize = iInvalidSize; /* we'll pick the shortest matching */ + static iRegExp *tagPattern_; + if (!tagPattern_) { + tagPattern_ = new_RegExp("\\busericon\\b", caseSensitive_RegExpOption); + } + const iRangecc urlRoot = urlRoot_String(url); + size_t matchingSize = iInvalidSize; /* we'll pick the shortest matching */ + iChar icon = 0; lock_Mutex(d->mtx); iConstForEach(Hash, i, &d->bookmarks) { const iBookmark *bm = (const iBookmark *) i.value; - iUrl parts; - init_Url(&parts, &bm->url); - if (!hasTag_Bookmark(bm, "usericon")) { /* TODO: Inefficient! RegExp rebuilt every time. */ - continue; - } - if (size_Range(&hostName) == size_Range(&parts.host) && - iCmpStrNCase(hostName.start, parts.host.start, hostSize) == 0) { - const size_t n = size_String(&bm->url); - if (n > size_String(url)) { - continue; /* Bookmark must be higher up in the path tree. */ - } - if (n < matchingSize && bm->icon) { - matchingSize = n; - icon = bm->icon; + iRegExpMatch m; + init_RegExpMatch(&m); + if (bm->icon && matchString_RegExp(tagPattern_, &bm->tags, &m)) { + const iRangecc bmRoot = urlRoot_String(&bm->url); + if (equalRangeCase_Rangecc(urlRoot, bmRoot)) { + const size_t n = size_String(&bm->url); + if (n < matchingSize) { + matchingSize = n; + icon = bm->icon; + } } } } diff --git a/src/gmutil.c b/src/gmutil.c index 1edc41ae..2b40367d 100644 --- a/src/gmutil.c +++ b/src/gmutil.c @@ -153,20 +153,36 @@ iRangecc urlHost_String(const iString *d) { } iRangecc urlUser_String(const iString *d) { - iRegExp *userPats[2] = { new_RegExp("~([^/?]+)", 0), - new_RegExp("/users/([^/?]+)", caseInsensitive_RegExpOption) }; + static iRegExp *userPats_[2]; + if (!userPats_[0]) { + userPats_[0] = new_RegExp("~([^/?]+)", 0); + userPats_[1] = new_RegExp("/users/([^/?]+)", caseInsensitive_RegExpOption); + } iRegExpMatch m; init_RegExpMatch(&m); iRangecc found = iNullRange; - iForIndices(i, userPats) { - if (matchString_RegExp(userPats[i], d, &m)) { + iForIndices(i, userPats_) { + if (matchString_RegExp(userPats_[i], d, &m)) { found = capturedRange_RegExpMatch(&m, 1); } - iRelease(userPats[i]); } return found; } +iRangecc urlRoot_String(const iString *d) { + const char *rootEnd; + const iRangecc user = urlUser_String(d); + if (!isEmpty_Range(&user)) { + rootEnd = user.end; + } + else { + iUrl parts; + init_Url(&parts, d); + rootEnd = parts.path.start; + } + return (iRangecc){ constBegin_String(d), rootEnd }; +} + static iBool isAbsolutePath_(iRangecc path) { return isAbsolute_Path(collect_String(urlDecode_String(collect_String(newRange_String(path))))); } diff --git a/src/gmutil.h b/src/gmutil.h index de8c1e65..b2cee61a 100644 --- a/src/gmutil.h +++ b/src/gmutil.h @@ -104,6 +104,7 @@ void init_Url (iUrl *, const iString *text); iRangecc urlScheme_String (const iString *); iRangecc urlHost_String (const iString *); iRangecc urlUser_String (const iString *); +iRangecc urlRoot_String (const iString *); const iString * absoluteUrl_String (const iString *, const iString *urlMaybeRelative); iBool isLikelyUrl_String (const iString *); void punyEncodeUrlHost_String(iString *); -- cgit v1.2.3