Skip to content

Commit

Permalink
Add GetExtensionOrDomain()
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake-Madden committed Apr 14, 2024
1 parent 227d356 commit 1791153
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
46 changes: 46 additions & 0 deletions src/util/fileutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,49 @@ wxString CreateNewFileName(const wxString& filePath)

return wxString{};
}

//---------------------------------------------------
wxString GetExtensionOrDomain(const wxString& url)
{
if (url.empty())
{
return wxString{};
}

const size_t lastSlash = url.rfind(L'/');
const size_t startPos = ((lastSlash != wxString::npos) ? (lastSlash + 1) : 0);

// Any sort of page with a query.
// Note that some pages are malformed and missing the variable assignment,
// so only look for the initial query (i.e., the '?') and go back from there.
if (const size_t queryPos = url.find(L'?', startPos); queryPos != wxString::npos)
{
// might be a JS, CSS, or other extension, so get the real extension
// in front of the query...
const wxFileName fn(url.substr(startPos, queryPos - startPos));
if (fn.HasExt())
{
return fn.GetExt();
}
// sometimes, the "webpage" name is simply "js" or something like that,
// so treat it as such if it has a query being passed to it
else if (fn.GetName().CmpNoCase(L"js") == 0 || fn.GetName().CmpNoCase(L"css") == 0)
{
return fn.GetName();
}
}
else
{
if (lastSlash == url.length() - 1)
{
return wxString{};
}
const wxFileName fn(url.substr(startPos));
if (fn.HasExt())
{
return fn.GetExt();
}
}

return wxString{};
}
5 changes: 5 additions & 0 deletions src/util/fileutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ bool MoveDirectory(const wxString& fromDirectory, const wxString& toDirectory);
[[nodiscard]]
wxString CreateNewFileName(const wxString& filePath);

/** @returns The extension (or simply domain) from an URL.
@param url The URL to parse.*/
[[nodiscard]]
wxString GetExtensionOrDomain(const wxString& url);

/** @}*/

#endif //__FILE_UTIL_H__
32 changes: 28 additions & 4 deletions tests/gui-tests/fileutiltests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,41 @@
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include "../../src/util/fileutil.h"

TEST_CASE("Web page extensions", "[file]")
{
CHECK(GetExtensionOrDomain(L"").empty());
CHECK(GetExtensionOrDomain(L"/").empty());
CHECK(GetExtensionOrDomain(L"//").empty());
CHECK(GetExtensionOrDomain(L"business").empty());
CHECK(GetExtensionOrDomain(L"/business").empty());
CHECK(GetExtensionOrDomain(L"org").empty());

CHECK(GetExtensionOrDomain(L"business.org") == wxString{ "org" });
CHECK(GetExtensionOrDomain(L"/business.org") == wxString{ "org" });
CHECK(GetExtensionOrDomain(L"www.mycompany.com/business.doc") ==
wxString{ "doc" });
CHECK(GetExtensionOrDomain(L"www.mycompany.com/business").empty());

CHECK(GetExtensionOrDomain(L"www.mycompany.com/business.doc?5145") == wxString{ "doc" });
CHECK(GetExtensionOrDomain(L"www.mycompany.com/business.js?5145") == wxString{ "js" });
CHECK(GetExtensionOrDomain(L"www.mycompany.com/business.js?id=501") == wxString{ "js" });
CHECK(GetExtensionOrDomain(L"www.mycompany.com/business.js?id=501;l=en") == wxString{ "js" });
CHECK(GetExtensionOrDomain(L"www.mycompany.com/business.js.php?id=501;l=en") == wxString{ "php" });
CHECK(GetExtensionOrDomain(L"www.mycompany.com/js?5") == wxString{ "js" });
CHECK(GetExtensionOrDomain(L"www.mycompany.com/css?en") == wxString{ "css" });
}

TEST_CASE("Parse title from file name", "[parse-title]")
{
SECTION("Local File")
{
CHECK(ParseTitleFromFileName(L"c:\\files\\file.txt") == wxT("file"));
CHECK(ParseTitleFromFileName(L"/users/files/file.txt") == wxT("file"));
CHECK(ParseTitleFromFileName(L"c:\\files\\file.txt") == wxString("file"));
CHECK(ParseTitleFromFileName(L"/users/files/file.txt") == wxString("file"));
}
SECTION("Local File Illegal Chars")
{
CHECK(ParseTitleFromFileName(L"c:\\files\\fi?l*e.txt") == wxT("file"));
CHECK(ParseTitleFromFileName(L"/users/files/fi?l*e.txt") == wxT("file"));
CHECK(ParseTitleFromFileName(L"c:\\files\\fi?l*e.txt") == wxString("file"));
CHECK(ParseTitleFromFileName(L"/users/files/fi?l*e.txt") == wxString("file"));
}
SECTION("Url Ending With Slash")
{
Expand Down

0 comments on commit 1791153

Please sign in to comment.