@@ -160,6 +160,8 @@ impl Url {
160
160
161
161
/// Returns whether or not the URL can be parsed or not.
162
162
///
163
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-canparse)
164
+ ///
163
165
/// ```
164
166
/// use ada_url::Url;
165
167
/// assert!(Url::can_parse("https://ada-url.github.io/ada", None));
@@ -180,6 +182,16 @@ impl Url {
180
182
}
181
183
}
182
184
185
+ /// Return the origin of this URL
186
+ ///
187
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-origin)
188
+ ///
189
+ /// ```
190
+ /// use ada_url::Url;
191
+ ///
192
+ /// let mut url = Url::parse("blob:https://example.com/foo", None).expect("Invalid URL");
193
+ /// assert_eq!(url.origin(), "https://example.com");
194
+ /// ```
183
195
pub fn origin ( & mut self ) -> & str {
184
196
unsafe {
185
197
let out = ffi:: ada_get_origin ( self . url ) ;
@@ -188,6 +200,8 @@ impl Url {
188
200
}
189
201
}
190
202
203
+ /// Return the parsed version of the URL with all components.
204
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-href)
191
205
pub fn href ( & self ) -> & str {
192
206
unsafe { ffi:: ada_get_href ( self . url ) } . as_str ( )
193
207
}
@@ -196,6 +210,16 @@ impl Url {
196
210
unsafe { ffi:: ada_set_href ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
197
211
}
198
212
213
+ /// Return the username for this URL as a percent-encoded ASCII string.
214
+ ///
215
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-username)
216
+ ///
217
+ /// ```
218
+ /// use ada_url::Url;
219
+ ///
220
+ /// let url = Url::parse("ftp://rms:[email protected] ", None).expect("Invalid URL");
221
+ /// assert_eq!(url.username(), "rms");
222
+ /// ```
199
223
pub fn username ( & self ) -> & str {
200
224
unsafe { ffi:: ada_get_username ( self . url ) } . as_str ( )
201
225
}
@@ -204,6 +228,16 @@ impl Url {
204
228
unsafe { ffi:: ada_set_username ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
205
229
}
206
230
231
+ /// Return the password for this URL, if any, as a percent-encoded ASCII string.
232
+ ///
233
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-password)
234
+ ///
235
+ /// ```
236
+ /// use ada_url::Url;
237
+ ///
238
+ /// let url = Url::parse("ftp://rms:[email protected] ", None).expect("Invalid URL");
239
+ /// assert_eq!(url.password(), "secret123");
240
+ /// ```
207
241
pub fn password ( & self ) -> & str {
208
242
unsafe { ffi:: ada_get_password ( self . url ) } . as_str ( )
209
243
}
@@ -212,6 +246,19 @@ impl Url {
212
246
unsafe { ffi:: ada_set_password ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
213
247
}
214
248
249
+ /// Return the port number for this URL, or an empty string.
250
+ ///
251
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-port)
252
+ ///
253
+ /// ```
254
+ /// use ada_url::Url;
255
+ ///
256
+ /// let url = Url::parse("https://example.com", None).expect("Invalid URL");
257
+ /// assert_eq!(url.port(), "");
258
+ ///
259
+ /// let url = Url::parse("https://example.com:8080", None).expect("Invalid URL");
260
+ /// assert_eq!(url.port(), "8080");
261
+ /// ```
215
262
pub fn port ( & self ) -> & str {
216
263
unsafe { ffi:: ada_get_port ( self . url ) } . as_str ( )
217
264
}
@@ -220,6 +267,23 @@ impl Url {
220
267
unsafe { ffi:: ada_set_port ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
221
268
}
222
269
270
+ /// Return this URL’s fragment identifier, or an empty string.
271
+ /// A fragment is the part of the URL with the # symbol.
272
+ /// The fragment is optional and, if present, contains a fragment identifier that identifies
273
+ /// a secondary resource, such as a section heading of a document.
274
+ /// In HTML, the fragment identifier is usually the id attribute of a an element that is
275
+ /// scrolled to on load. Browsers typically will not send the fragment portion of a URL to the
276
+ /// server.
277
+ ///
278
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-hash)
279
+ ///
280
+ /// ```
281
+ /// use ada_url::Url;
282
+ ///
283
+ /// let url = Url::parse("https://example.com/data.csv#row=4", None).expect("Invalid URL");
284
+ /// assert_eq!(url.hash(), "#row=4");
285
+ /// assert!(url.has_hash());
286
+ /// ```
223
287
pub fn hash ( & self ) -> & str {
224
288
unsafe { ffi:: ada_get_hash ( self . url ) } . as_str ( )
225
289
}
@@ -228,6 +292,16 @@ impl Url {
228
292
unsafe { ffi:: ada_set_hash ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
229
293
}
230
294
295
+ /// Return the parsed representation of the host for this URL with an optional port number.
296
+ ///
297
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-host)
298
+ ///
299
+ /// ```
300
+ /// use ada_url::Url;
301
+ ///
302
+ /// let url = Url::parse("https://127.0.0.1:8080/index.html", None).expect("Invalid URL");
303
+ /// assert_eq!(url.host(), "127.0.0.1:8080");
304
+ /// ```
231
305
pub fn host ( & self ) -> & str {
232
306
unsafe { ffi:: ada_get_host ( self . url ) } . as_str ( )
233
307
}
@@ -236,6 +310,20 @@ impl Url {
236
310
unsafe { ffi:: ada_set_host ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
237
311
}
238
312
313
+ /// Return the parsed representation of the host for this URL. Non-ASCII domain labels are
314
+ /// punycode-encoded per IDNA if this is the host of a special URL, or percent encoded for
315
+ /// non-special URLs.
316
+ ///
317
+ /// Hostname does not contain port number.
318
+ ///
319
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-hostname)
320
+ ///
321
+ /// ```
322
+ /// use ada_url::Url;
323
+ ///
324
+ /// let url = Url::parse("https://127.0.0.1:8080/index.html", None).expect("Invalid URL");
325
+ /// assert_eq!(url.hostname(), "127.0.0.1");
326
+ /// ```
239
327
pub fn hostname ( & self ) -> & str {
240
328
unsafe { ffi:: ada_get_hostname ( self . url ) } . as_str ( )
241
329
}
@@ -244,6 +332,16 @@ impl Url {
244
332
unsafe { ffi:: ada_set_hostname ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
245
333
}
246
334
335
+ /// Return the path for this URL, as a percent-encoded ASCII string.
336
+ ///
337
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-pathname)
338
+ ///
339
+ /// ```
340
+ /// use ada_url::Url;
341
+ ///
342
+ /// let url = Url::parse("https://example.com/api/versions?page=2", None).expect("Invalid URL");
343
+ /// assert_eq!(url.pathname(), "/api/versions");
344
+ /// ```
247
345
pub fn pathname ( & self ) -> & str {
248
346
unsafe { ffi:: ada_get_pathname ( self . url ) } . as_str ( )
249
347
}
@@ -252,6 +350,19 @@ impl Url {
252
350
unsafe { ffi:: ada_set_pathname ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
253
351
}
254
352
353
+ /// Return this URL’s query string, if any, as a percent-encoded ASCII string.
354
+ ///
355
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-search)
356
+ ///
357
+ /// ```
358
+ /// use ada_url::Url;
359
+ ///
360
+ /// let url = Url::parse("https://example.com/products?page=2", None).expect("Invalid URL");
361
+ /// assert_eq!(url.search(), "?page=2");
362
+ ///
363
+ /// let url = Url::parse("https://example.com/products", None).expect("Invalid URL");
364
+ /// assert_eq!(url.search(), "");
365
+ /// ```
255
366
pub fn search ( & self ) -> & str {
256
367
unsafe { ffi:: ada_get_search ( self . url ) } . as_str ( )
257
368
}
@@ -260,6 +371,16 @@ impl Url {
260
371
unsafe { ffi:: ada_set_search ( self . url , input. as_ptr ( ) . cast ( ) , input. len ( ) ) }
261
372
}
262
373
374
+ /// Return the scheme of this URL, lower-cased, as an ASCII string with the ‘:’ delimiter.
375
+ ///
376
+ /// For more information, read [WHATWG URL spec](https://url.spec.whatwg.org/#dom-url-protocol)
377
+ ///
378
+ /// ```
379
+ /// use ada_url::Url;
380
+ ///
381
+ /// let url = Url::parse("file:///tmp/foo", None).expect("Invalid URL");
382
+ /// assert_eq!(url.protocol(), "file:");
383
+ /// ```
263
384
pub fn protocol ( & self ) -> & str {
264
385
unsafe { ffi:: ada_get_protocol ( self . url ) } . as_str ( )
265
386
}
0 commit comments