Skip to content

Commit 76f18c1

Browse files
committed
Remove idna dependency on wasm32, and fix test
1 parent 62cf711 commit 76f18c1

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
@@ -1226,6 +1226,7 @@ impl Url {
12261226
self.port.or_else(|| parser::default_port(self.scheme()))
12271227
}
12281228

1229+
#[cfg(not(target_arch = "wasm32"))]
12291230
/// Resolve a URL’s host and port number to `SocketAddr`.
12301231
///
12311232
/// If the URL has the default port number of a scheme that is unknown to this library,
@@ -1235,6 +1236,8 @@ impl Url {
12351236
///
12361237
/// If the host is a domain, it is resolved using the standard library’s DNS support.
12371238
///
1239+
/// **Note:** this feature is not available on `target_arch = "wasm32"`.
1240+
///
12381241
/// # Examples
12391242
///
12401243
/// ```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
@@ -596,6 +596,7 @@ fn test_origin_opaque() {
596596
}
597597

598598
#[test]
599+
#[cfg(not(target_arch = "wasm32"))]
599600
fn test_origin_unicode_serialization() {
600601
let data = [
601602
("http://😅.com", "http://😅.com"),
@@ -770,7 +771,9 @@ fn test_set_href() {
770771

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

775778
let data = [
776779
("http://example.com", "", ""),
@@ -781,6 +784,7 @@ fn test_domain_encoding_quirks() {
781784

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

0 commit comments

Comments
 (0)