Prechádzať zdrojové kódy

LibWebView: Add some helpers for "Copy email/telephone" functionality

Other browsers have a nice little feature where right-clicking on a
`mailto` or `tel` link will let you copy the email address or telephone
number, instead of the full URL. So let's do that!
Sam Atkins 1 rok pred
rodič
commit
c8247e5737

+ 22 - 0
Userland/Libraries/LibWebView/URL.cpp

@@ -160,4 +160,26 @@ Optional<URLParts> break_url_into_parts(StringView url_string)
     return {};
 }
 
+URLType url_type(URL const& url)
+{
+    if (url.scheme() == "mailto"sv)
+        return URLType::Email;
+    if (url.scheme() == "tel"sv)
+        return URLType::Telephone;
+    return URLType::Other;
+}
+
+String url_text_to_copy(URL const& url)
+{
+    auto url_text = MUST(url.to_string());
+
+    if (url.scheme() == "mailto"sv)
+        return MUST(url_text.substring_from_byte_offset("mailto:"sv.length()));
+
+    if (url.scheme() == "tel"sv)
+        return MUST(url_text.substring_from_byte_offset("tel:"sv.length()));
+
+    return url_text;
+}
+
 }

+ 9 - 0
Userland/Libraries/LibWebView/URL.h

@@ -28,4 +28,13 @@ struct URLParts {
 };
 Optional<URLParts> break_url_into_parts(StringView url);
 
+// These are both used for the "right-click -> copy FOO" interaction for links.
+enum class URLType {
+    Email,
+    Telephone,
+    Other,
+};
+URLType url_type(URL const&);
+String url_text_to_copy(URL const&);
+
 }