Skip to content

Commit b0a81d7

Browse files
committed
Remove idna dependency on wasm32, and fix test
1 parent d924a2c commit b0a81d7

File tree

7 files changed

+59
-25
lines changed

7 files changed

+59
-25
lines changed

url/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ wasm-bindgen-test = "0.3"
2626

2727
[dependencies]
2828
form_urlencoded = { version = "1.2.1", path = "../form_urlencoded" }
29-
idna = { version = "0.5.0", path = "../idna" }
3029
percent-encoding = { version = "2.3.1", path = "../percent_encoding" }
3130
serde = { version = "1.0", optional = true, features = ["derive"] }
3231

32+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
33+
idna = { version = "0.5.0", path = "../idna" }
34+
35+
[target.'cfg(target_arch = "wasm32")'.dependencies]
36+
web-sys = { version = "0.3.65", features = ["Url"] }
37+
3338
[features]
3439
default = []
3540
# Enable to use the #[debugger_visualizer] attribute. This feature requires Rust >= 1.71.

url/src/host.rs

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,7 @@ impl Host<String> {
8989
return Err(ParseError::EmptyHost);
9090
}
9191

92-
let is_invalid_domain_char = |c| {
93-
matches!(
94-
c,
95-
'\0'..='\u{001F}'
96-
| ' '
97-
| '#'
98-
| '%'
99-
| '/'
100-
| ':'
101-
| '<'
102-
| '>'
103-
| '?'
104-
| '@'
105-
| '['
106-
| '\\'
107-
| ']'
108-
| '^'
109-
| '\u{007F}'
110-
| '|'
111-
)
112-
};
113-
114-
if domain.find(is_invalid_domain_char).is_some() {
92+
if domain.find(Self::is_invalid_domain_char).is_some() {
11593
Err(ParseError::InvalidDomainCharacter)
11694
} else if ends_in_a_number(&domain) {
11795
let address = parse_ipv4addr(&domain)?;
@@ -161,10 +139,50 @@ impl Host<String> {
161139
}
162140
}
163141

142+
#[cfg(not(target_arch="wasm32"))]
164143
/// convert domain with idna
165144
fn domain_to_ascii(domain: &str) -> Result<String, ParseError> {
166145
idna::domain_to_ascii(domain).map_err(Into::into)
167146
}
147+
148+
#[cfg(target_arch="wasm32")]
149+
/// convert domain with idna
150+
fn domain_to_ascii(domain: &str) -> Result<String, ParseError> {
151+
// Url throws an error on empty hostnames
152+
if domain.is_empty() {
153+
return Err(ParseError::EmptyHost);
154+
}
155+
// Url returns strange results for invalid domain chars
156+
if domain.contains(Self::is_invalid_domain_char) {
157+
return Err(ParseError::InvalidDomainCharacter);
158+
}
159+
let u = web_sys::Url::new(&format!("http://{domain}")).map_err(|_| {
160+
ParseError::IdnaError
161+
})?;
162+
Ok(u.host())
163+
}
164+
165+
fn is_invalid_domain_char(c: char) -> bool {
166+
matches!(
167+
c,
168+
'\0'..='\u{001F}'
169+
| ' '
170+
| '#'
171+
| '%'
172+
| '/'
173+
| ':'
174+
| '<'
175+
| '>'
176+
| '?'
177+
| '@'
178+
| '['
179+
| '\\'
180+
| ']'
181+
| '^'
182+
| '\u{007F}'
183+
| '|'
184+
)
185+
}
168186
}
169187

170188
impl<S: AsRef<str>> fmt::Display for Host<S> {

url/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,7 @@ impl Url {
12281228
self.port.or_else(|| parser::default_port(self.scheme()))
12291229
}
12301230

1231+
#[cfg(not(target_arch = "wasm32"))]
12311232
/// Resolve a URL’s host and port number to `SocketAddr`.
12321233
///
12331234
/// If the URL has the default port number of a scheme that is unknown to this library,
@@ -1237,6 +1238,8 @@ impl Url {
12371238
///
12381239
/// If the host is a domain, it is resolved using the standard library’s DNS support.
12391240
///
1241+
/// **Note:** this feature is not available on `target_arch = "wasm32"`.
1242+
///
12401243
/// # Examples
12411244
///
12421245
/// ```no_run

url/src/origin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ impl Origin {
8585
}
8686
}
8787

88+
#[cfg(not(target_arch = "wasm32"))]
89+
// FIXME: "There used to also be a Unicode serialization of an origin. However, it was never widely adopted."
8890
/// <https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin>
8991
pub fn unicode_serialization(&self) -> String {
9092
match *self {

url/src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ simple_enum_error! {
8787
Overflow => "URLs more than 4 GB are not supported",
8888
}
8989

90+
#[cfg(not(target_arch = "wasm32"))]
9091
impl From<::idna::Errors> for ParseError {
9192
fn from(_: ::idna::Errors) -> ParseError {
9293
ParseError::IdnaError

url/src/quirks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub fn domain_to_ascii(domain: &str) -> String {
6565
}
6666
}
6767

68+
#[cfg(not(target_arch = "wasm32"))]
6869
/// https://url.spec.whatwg.org/#dom-url-domaintounicode
6970
pub fn domain_to_unicode(domain: &str) -> String {
7071
match Host::parse(domain) {

url/tests/unit.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ fn test_origin_opaque() {
597597
}
598598

599599
#[test]
600+
#[cfg(not(target_arch = "wasm32"))]
600601
fn test_origin_unicode_serialization() {
601602
let data = [
602603
("http://😅.com", "http://😅.com"),
@@ -771,7 +772,9 @@ fn test_set_href() {
771772

772773
#[test]
773774
fn test_domain_encoding_quirks() {
774-
use url::quirks::{domain_to_ascii, domain_to_unicode};
775+
use url::quirks::domain_to_ascii;
776+
#[cfg(not(target_arch = "wasm32"))]
777+
use url::quirks::domain_to_unicode;
775778

776779
let data = [
777780
("http://example.com", "", ""),
@@ -782,6 +785,7 @@ fn test_domain_encoding_quirks() {
782785

783786
for url in &data {
784787
assert_eq!(domain_to_ascii(url.0), url.1);
788+
#[cfg(not(target_arch = "wasm32"))]
785789
assert_eq!(domain_to_unicode(url.0), url.2);
786790
}
787791
}

0 commit comments

Comments
 (0)