Skip to content

Commit

Permalink
wasm: blob url support
Browse files Browse the repository at this point in the history
  • Loading branch information
skyf0l committed Apr 10, 2023
1 parent 7047669 commit ea4f343
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/into_url.rs
Expand Up @@ -21,6 +21,16 @@ pub trait IntoUrlSealed {

impl IntoUrlSealed for Url {
fn into_url(self) -> crate::Result<Url> {
// 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 {
Expand Down Expand Up @@ -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");
}
}
}

0 comments on commit ea4f343

Please sign in to comment.