Merge pull request #4075 from milchakov/validate_website

[editor] website validation corrections
This commit is contained in:
mgsergio 2016-08-17 17:45:54 +04:00 committed by GitHub
commit d959e2e8d8
2 changed files with 33 additions and 8 deletions

View file

@ -52,6 +52,24 @@ size_t PushMwmLanguages(StringUtf8Multilang const & names, vector<int8_t> const
return count;
}
char const * const kWebsiteProtocols[] = {"http://", "https://"};
size_t const kWebsiteProtocolDefaultIndex = 0;
size_t GetProtocolNameLength(string const & website)
{
for (auto const & protocol : kWebsiteProtocols)
{
if (strings::StartsWith(website, protocol))
return strlen(protocol);
}
return 0;
}
bool IsProtocolSpecified(string const & website)
{
return GetProtocolNameLength(website) > 0;
}
} // namespace
namespace osm
@ -267,12 +285,9 @@ void EditableMapObject::SetEmail(string const & email)
void EditableMapObject::SetWebsite(string website)
{
if (!website.empty() &&
!strings::StartsWith(website, "http://") &&
!strings::StartsWith(website, "https://"))
{
website = "http://" + website;
}
if (!website.empty() && !IsProtocolSpecified(website))
website = kWebsiteProtocols[kWebsiteProtocolDefaultIndex] + website;
m_metadata.Set(feature::Metadata::FMD_WEBSITE, website);
m_metadata.Drop(feature::Metadata::FMD_URL);
}
@ -443,8 +458,13 @@ bool EditableMapObject::ValidateWebsite(string const & site)
if (site.empty())
return true;
auto const startPos = GetProtocolNameLength(site);
if (startPos >= site.size())
return false;
// Site should contain at least one dot but not at the begining/end.
if ('.' == site.front() || '.' == site.back())
if ('.' == site[startPos] || '.' == site.back())
return false;
if (string::npos == site.find("."))

View file

@ -100,13 +100,18 @@ UNIT_TEST(EditableMapObject_ValidateWebsite)
{
TEST(EditableMapObject::ValidateWebsite(""), ());
TEST(EditableMapObject::ValidateWebsite("qwe.rty"), ());
TEST(EditableMapObject::ValidateWebsite("http://websit.e"), ());
TEST(EditableMapObject::ValidateWebsite("https://websit.e"), ());
TEST(!EditableMapObject::ValidateWebsite("qwerty"), ());
TEST(!EditableMapObject::ValidateWebsite(".qwerty"), ());
TEST(!EditableMapObject::ValidateWebsite("qwerty."), ());
TEST(!EditableMapObject::ValidateWebsite(".qwerty."), ());
TEST(!EditableMapObject::ValidateWebsite(".qwerty."), ());
TEST(!EditableMapObject::ValidateWebsite("w..com"), ());
TEST(!EditableMapObject::ValidateWebsite("http://.websit.e"), ());
TEST(!EditableMapObject::ValidateWebsite("https://.websit.e"), ());
TEST(!EditableMapObject::ValidateWebsite("http://"), ());
TEST(!EditableMapObject::ValidateWebsite("https://"), ());
}
UNIT_TEST(EditableMapObject_ValidateEmail)