From ea4f34312c7980c235b018d0ef43784c91e3ff09 Mon Sep 17 00:00:00 2001 From: Skyf0l Date: Mon, 10 Apr 2023 11:28:29 -0700 Subject: [PATCH] wasm: blob url support --- src/into_url.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/into_url.rs b/src/into_url.rs index ef0db9495..b3e349127 100644 --- a/src/into_url.rs +++ b/src/into_url.rs @@ -21,6 +21,16 @@ pub trait IntoUrlSealed { impl IntoUrlSealed for Url { fn into_url(self) -> crate::Result { + // With blob url the `self.has_host()` check is always false, so we + // remove the `blob:` scheme and check again if the url is valid. + #[cfg(target_arch = "wasm32")] + if self.scheme() == "blob" + && self.path().starts_with("http") // Check if the path starts with http or https to avoid validating a `blob:blob:...`` url. + && self.as_str()[5..].into_url().is_ok() + { + return Ok(self); + } + if self.has_host() { Ok(self) } else { @@ -87,4 +97,24 @@ mod tests { "builder error for url (file:///etc/hosts): URL scheme is not allowed" ); } + + #[test] + fn into_url_blob_scheme() { + let err = "blob:https://example.com".into_url().unwrap_err(); + assert_eq!( + err.to_string(), + "builder error for url (blob:https://example.com): URL scheme is not allowed" + ); + } + + if_wasm! { + use wasm_bindgen_test::*; + + #[wasm_bindgen_test] + fn into_url_blob_scheme_wasm() { + let url = "blob:http://example.com".into_url().unwrap(); + + assert_eq!(url.as_str(), "blob:http://example.com"); + } + } }