EXT-3122: Added support for URLs without an explicit protocol.

You can now enter URLs like "www.google.com", "secondlife.com", or
"www.google.com/search?q=second%20life" and these will be hyperlinked.
meow-7.2.2
Lynx Linden 2009-12-10 15:33:05 +00:00
parent 9b4d09471c
commit 1ab022df43
3 changed files with 53 additions and 1 deletions

View File

@ -196,6 +196,31 @@ std::string LLUrlEntryHTTPLabel::getUrl(const std::string &string)
return getUrlFromWikiLink(string);
}
//
// LLUrlEntryHTTPNoProtocol Describes generic Urls like www.google.com
//
LLUrlEntryHTTPNoProtocol::LLUrlEntryHTTPNoProtocol()
{
mPattern = boost::regex("(\\bwww\\.\\S+\\.\\S+|\\S+.com\\S*|\\S+.net\\S*|\\S+.edu\\S*|\\S+.org\\S*)",
boost::regex::perl|boost::regex::icase);
mMenuName = "menu_url_http.xml";
mTooltip = LLTrans::getString("TooltipHttpUrl");
}
std::string LLUrlEntryHTTPNoProtocol::getLabel(const std::string &url, const LLUrlLabelCallback &cb)
{
return unescapeUrl(url);
}
std::string LLUrlEntryHTTPNoProtocol::getUrl(const std::string &string)
{
if (string.find("://") == std::string::npos)
{
return "http://" + escapeUrl(string);
}
return escapeUrl(string);
}
//
// LLUrlEntrySLURL Describes generic http: and https: Urls
//

View File

@ -134,6 +134,17 @@ public:
/*virtual*/ std::string getUrl(const std::string &string);
};
///
/// LLUrlEntryHTTPNoProtocol Describes generic Urls like www.google.com
///
class LLUrlEntryHTTPNoProtocol : public LLUrlEntryBase
{
public:
LLUrlEntryHTTPNoProtocol();
/*virtual*/ std::string getLabel(const std::string &url, const LLUrlLabelCallback &cb);
/*virtual*/ std::string getUrl(const std::string &string);
};
///
/// LLUrlEntrySLURL Describes http://slurl.com/... Urls
///

View File

@ -58,6 +58,9 @@ LLUrlRegistry::LLUrlRegistry()
//so it should be registered in the end of list
registerUrl(new LLUrlEntrySL());
registerUrl(new LLUrlEntrySLLabel());
// most common pattern is a URL without any protocol,
// e.g., "secondlife.com"
registerUrl(new LLUrlEntryHTTPNoProtocol());
}
LLUrlRegistry::~LLUrlRegistry()
@ -118,10 +121,23 @@ static bool matchRegex(const char *text, boost::regex regex, U32 &start, U32 &en
return true;
}
static bool stringHasUrl(const std::string &text)
{
// fast heuristic test for a URL in a string. This is used
// to avoid lots of costly regex calls, BUT it needs to be
// kept in sync with the LLUrlEntry regexes we support.
return (text.find("://") != std::string::npos ||
text.find("www.") != std::string::npos ||
text.find(".com") != std::string::npos ||
text.find(".net") != std::string::npos ||
text.find(".edu") != std::string::npos ||
text.find(".org") != std::string::npos);
}
bool LLUrlRegistry::findUrl(const std::string &text, LLUrlMatch &match, const LLUrlLabelCallback &cb)
{
// avoid costly regexes if there is clearly no URL in the text
if (text.find("://") == std::string::npos)
if (! stringHasUrl(text))
{
return false;
}