diff --git a/indexer/editable_map_object.cpp b/indexer/editable_map_object.cpp index 56a26b7194..e9f819de2e 100644 --- a/indexer/editable_map_object.cpp +++ b/indexer/editable_map_object.cpp @@ -52,6 +52,24 @@ size_t PushMwmLanguages(StringUtf8Multilang const & names, vector 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(".")) diff --git a/indexer/indexer_tests/editable_map_object_test.cpp b/indexer/indexer_tests/editable_map_object_test.cpp index c9db74a865..7df4730871 100644 --- a/indexer/indexer_tests/editable_map_object_test.cpp +++ b/indexer/indexer_tests/editable_map_object_test.cpp @@ -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)