Skip to content

Commit de947ab

Browse files
authored
Document possible replacements of the base URL (servo#926)
1 parent 8b8431b commit de947ab

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

url/src/lib.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ pub struct ParseOptions<'a> {
219219

220220
impl<'a> ParseOptions<'a> {
221221
/// Change the base URL
222+
///
223+
/// See the notes of [`Url::join`] for more details about how this base is considered
224+
/// when parsing.
222225
pub fn base_url(mut self, new: Option<&'a Url>) -> Self {
223226
self.base_url = new;
224227
self
@@ -370,24 +373,42 @@ impl Url {
370373
///
371374
/// The inverse of this is [`make_relative`].
372375
///
373-
/// Note: a trailing slash is significant.
376+
/// # Notes
377+
///
378+
/// - A trailing slash is significant.
374379
/// Without it, the last path component is considered to be a “file” name
375-
/// to be removed to get at the “directory” that is used as the base:
380+
/// to be removed to get at the “directory” that is used as the base.
381+
/// - A [scheme relative special URL](https://url.spec.whatwg.org/#scheme-relative-special-url-string)
382+
/// as input replaces everything in the base URL after the scheme.
383+
/// - An absolute URL (with a scheme) as input replaces the whole base URL (even the scheme).
376384
///
377385
/// # Examples
378386
///
379387
/// ```rust
380388
/// use url::Url;
381389
/// # use url::ParseError;
382390
///
391+
/// // Base without a trailing slash
383392
/// # fn run() -> Result<(), ParseError> {
384393
/// let base = Url::parse("https://example.net/a/b.html")?;
385394
/// let url = base.join("c.png")?;
386395
/// assert_eq!(url.as_str(), "https://example.net/a/c.png"); // Not /a/b.html/c.png
387396
///
397+
/// // Base with a trailing slash
388398
/// let base = Url::parse("https://example.net/a/b/")?;
389399
/// let url = base.join("c.png")?;
390400
/// assert_eq!(url.as_str(), "https://example.net/a/b/c.png");
401+
///
402+
/// // Input as scheme relative special URL
403+
/// let base = Url::parse("https://alice.com/a")?;
404+
/// let url = base.join("//eve.com/b")?;
405+
/// assert_eq!(url.as_str(), "https://eve.com/b");
406+
///
407+
/// // Input as absolute URL
408+
/// let base = Url::parse("https://alice.com/a")?;
409+
/// let url = base.join("http://eve.com/b")?;
410+
/// assert_eq!(url.as_str(), "http://eve.com/b"); // http instead of https
411+
391412
/// # Ok(())
392413
/// # }
393414
/// # run().unwrap();

0 commit comments

Comments
 (0)