Skip to content

Commit

Permalink
fix handling of unicode characters in URL
Browse files Browse the repository at this point in the history
  • Loading branch information
amodm committed Feb 19, 2022
1 parent d09eeae commit 11789dd
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ keywords = ["webbrowser", "browser"]
license = "MIT OR Apache-2.0"
edition = "2018"

[dependencies]
url = "2"

[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
version = "0.3.36"
features = [
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if webbrowser::open("http://github.com").is_ok() {

| Platform | Supported | Browsers | Test status |
|----------|-----------|----------|-------------|
| macos || default + [others](https://docs.rs/webbrowser/latest/webbrowser/enum.Browser.html) |(unencoded non-ascii URLs currently fail on Github, but work locally, so YMMV) |
| macos || default + [others](https://docs.rs/webbrowser/latest/webbrowser/enum.Browser.html) ||
| windows || default only ||
| linux/*bsd || default only (respects $BROWSER env var, so can be used with other browsers) ||
| android || default only ||
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! | Platform | Supported | Browsers | Test status |
//! |----------|-----------|----------|-------------|
//! | macos | ✅ | default + [others](https://docs.rs/webbrowser/latest/webbrowser/enum.Browser.html) | ✅ (unencoded non-ascii URLs currently fail on Github, but work locally, so YMMV) |
//! | macos | ✅ | default + [others](https://docs.rs/webbrowser/latest/webbrowser/enum.Browser.html) | ✅ |
//! | windows | ✅ | default only | ✅ |
//! | linux/*bsd | ✅ | default only (respects $BROWSER env var, so can be used with other browsers) | ✅ |
//! | android | ✅ | default only | ✅ |
Expand Down Expand Up @@ -177,7 +177,11 @@ pub fn open(url: &str) -> Result<()> {
/// }
/// ```
pub fn open_browser(browser: Browser, url: &str) -> Result<()> {
os::open_browser_internal(browser, url)
let url_s: String = match url::Url::parse(url) {
Ok(u) => u.as_str().into(),
Err(_) => url.into(),
};
os::open_browser_internal(browser, &url_s)
}

#[test]
Expand Down
7 changes: 6 additions & 1 deletion src/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ use common::from_status;

/// Deal with opening of browsers on Mac OS X, using `open` command
#[inline]
pub fn open_browser_internal(browser: Browser, url: &str) -> Result<()> {
pub fn open_browser_internal(browser: Browser, url_raw: &str) -> Result<()> {
let url_s: String = match url::Url::parse(url_raw) {
Ok(u) => u.as_str().into(),
Err(_) => url_raw.into(),
};
let url = &url_s;
let mut cmd = Command::new("open");
match browser {
Browser::Default => from_status(cmd.arg(url).status()),
Expand Down
2 changes: 1 addition & 1 deletion tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ pub async fn check_request_received(browser: Browser, uri: String) {
#[allow(dead_code)]
pub async fn check_browser(browser: Browser, platform: &str) {
check_request_received(browser, format!("/{}", platform)).await;
check_request_received(browser, format!("/{}/nonascii", platform)).await;
check_request_received(browser, format!("/{}/😀😀😀", platform)).await;
}
10 changes: 3 additions & 7 deletions tests/test_macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ mod common;
mod tests {
const TEST_PLATFORM: &str = "macos";

use super::common::{check_browser, check_request_received};
use super::common::check_browser;
use webbrowser::Browser;

#[actix_rt::test]
async fn test_open_default() {
// we've replaced check_browser with check_request_received as UTF-8 test fails currently
// check_browser(Browser::Default, TEST_PLATFORM).await;
check_request_received(Browser::Default, format!("/{}", TEST_PLATFORM)).await;
check_browser(Browser::Default, TEST_PLATFORM).await;
}

#[actix_rt::test]
async fn test_open_safari() {
// we've replaced check_browser with check_request_received as UTF-8 test fails currently
// check_browser(Browser::Safari, TEST_PLATFORM).await;
check_request_received(Browser::Safari, format!("/{}", TEST_PLATFORM)).await;
check_browser(Browser::Safari, TEST_PLATFORM).await;
}

#[actix_rt::test]
Expand Down

0 comments on commit 11789dd

Please sign in to comment.