Skip to content

Commit

Permalink
do not add 'http://' in fromnt of a link of type 'mailto:'
Browse files Browse the repository at this point in the history
  • Loading branch information
giuspen committed Jan 6, 2025
1 parent db7df2a commit 798bd52
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
8 changes: 3 additions & 5 deletions src/ct/ct_actions_format.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ct_actions_format.cc
*
* Copyright 2009-2024
* Copyright 2009-2025
* Giuseppe Penone <giuspen@gmail.com>
* Evgenii Gurianov <https://github.com/txe>
*
Expand Down Expand Up @@ -532,13 +532,11 @@ bool CtActions::_links_entries_pre_dialog(const Glib::ustring& curr_link, CtLink
// Read Global Links Variables from Dialog
Glib::ustring CtActions::_links_entries_post_dialog(CtLinkEntry& link_entry)
{
Glib::ustring property_value = "";
Glib::ustring property_value;
if (link_entry.type == CtConst::LINK_TYPE_WEBS) {
std::string link_url = link_entry.webs;
if (not link_url.empty()) {
if (link_url.size() < 8 or
(not str::startswith(link_url, "http://") and not str::startswith(link_url, "https://") and not str::startswith_url(link_url.c_str())))
{
if (not str::startswith_url(link_url.c_str())) {
link_url = "http://" + link_url;
}
property_value = CtConst::LINK_TYPE_WEBS + CtConst::CHAR_SPACE + link_url;
Expand Down
4 changes: 2 additions & 2 deletions src/ct/ct_clipboard.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ct_clipboard.cc
*
* Copyright 2009-2024
* Copyright 2009-2025
* Giuseppe Penone <giuspen@gmail.com>
* Evgenii Gurianov <https://github.com/txe>
*
Expand Down Expand Up @@ -544,7 +544,7 @@ void CtClipboard::on_received_to_plain_text(const Gtk::SelectionData& selection_
Gtk::TextIter iter_sel_start = curr_buffer->get_iter_at_offset(start_offset + offset.first);
Gtk::TextIter iter_sel_end = curr_buffer->get_iter_at_offset(start_offset + offset.second);
Glib::ustring link_url = iter_sel_start.get_text(iter_sel_end);
if (not str::startswith(link_url, "htt") and not str::startswith(link_url, "ftp") and not str::startswith_url(link_url.c_str())) {
if (not str::startswith_url(link_url.c_str())) {
link_url = "http://" + link_url;
}
Glib::ustring property_value = "webs " + link_url;
Expand Down
14 changes: 10 additions & 4 deletions src/ct/ct_misc_utils.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ct_misc_utils.cc
*
* Copyright 2009-2024
* Copyright 2009-2025
* Giuseppe Penone <giuspen@gmail.com>
* Evgenii Gurianov <https://github.com/txe>
*
Expand Down Expand Up @@ -434,7 +434,10 @@ bool CtTextIterUtil::startswith(Gtk::TextIter text_iter, const gchar* pChar)

bool str::startswith_url(const gchar* pChar)
{
// look for alpha://anything
// look for mailto:anything or alpha://anything
if (g_str_has_prefix(pChar, "mailto:")) {
return true;
}
int curr_state{0};
while (true) {
gunichar curr_char = g_utf8_get_char(pChar);
Expand Down Expand Up @@ -465,7 +468,10 @@ bool str::startswith_url(const gchar* pChar)

bool CtTextIterUtil::startswith_url(Gtk::TextIter text_iter)
{
// look for alpha://anything
// look for mailto:anything or alpha://anything
if (CtTextIterUtil::startswith(text_iter, "mailto:")) {
return true;
}
int curr_state{0};
while (true) {
const gunichar curr_char = text_iter.get_char();
Expand Down Expand Up @@ -962,7 +968,7 @@ Glib::ustring CtStrUtil::get_accelerator_label(const std::string& accelerator)

std::string CtStrUtil::get_internal_link_from_http_url(std::string link_url)
{
if (str::startswith(link_url, "http")) return "webs " + link_url;
if (str::startswith_url(link_url.c_str())) return "webs " + link_url;
else if (str::startswith(link_url, "file://")) return "file " + Glib::Base64::encode(link_url.substr(7));
else return "webs http://" + link_url;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ct/ct_text_view.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ct_text_view.cc
*
* Copyright 2009-2024
* Copyright 2009-2025
* Giuseppe Penone <giuspen@gmail.com>
* Evgenii Gurianov <https://github.com/txe>
*
Expand Down Expand Up @@ -951,7 +951,7 @@ bool CtTextView::_apply_tag_try_link(Gtk::TextIter iter_end, int offset_cursor)
if (_pCtConfig->urlAutoLink and num_chars > 4 and CtTextIterUtil::startswith_any(iter_start, CtConst::WEB_LINK_STARTERS)) {
get_buffer()->select_range(iter_start, iter_end);
Glib::ustring link_url = get_buffer()->get_text(iter_start, iter_end);
if (not str::startswith(link_url, "htt") and not str::startswith(link_url, "ftp") and not str::startswith_url(link_url.c_str())) {
if (not str::startswith_url(link_url.c_str())) {
link_url = "http://" + link_url;
}
Glib::ustring property_value = CtConst::LINK_TYPE_WEBS + CtConst::CHAR_SPACE + link_url;
Expand Down
8 changes: 7 additions & 1 deletion tests/tests_misc_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* tests_misc_utils.cpp
*
* Copyright 2009-2024
* Copyright 2009-2025
* Giuseppe Penone <giuspen@gmail.com>
* Evgenii Gurianov <https://github.com/txe>
*
Expand Down Expand Up @@ -216,6 +216,12 @@ TEST(MiscUtilsGroup, iter_util__startswith_url)
ASSERT_TRUE(CtTextIterUtil::startswith_url(pTextBuffer->begin()));
ASSERT_TRUE(str::startswith_url(curr_text.c_str()));
}
{
Glib::ustring curr_text{"mailto:ciao"};
pTextBuffer->set_text(curr_text);
ASSERT_TRUE(CtTextIterUtil::startswith_url(pTextBuffer->begin()));
ASSERT_TRUE(str::startswith_url(curr_text.c_str()));
}
{
Glib::ustring curr_text{"ciaociao"};
pTextBuffer->set_text(curr_text);
Expand Down

0 comments on commit 798bd52

Please sign in to comment.