Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/uv-client/src/html.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use tl::HTMLTag;
use tl::{HTMLTag, Parser};
use tracing::{instrument, warn};
use url::Url;

Expand Down Expand Up @@ -44,7 +44,7 @@ impl SimpleHtml {
.iter()
.filter_map(|node| node.as_tag())
.filter(|link| link.name().as_bytes() == b"a")
.map(|link| Self::parse_anchor(link))
.map(|link| Self::parse_anchor(link, dom.parser()))
.collect::<Result<Vec<_>, _>>()?;
// While it has not been positively observed, we sort the files
// to ensure we have a defined ordering. Otherwise, if we rely on
Expand All @@ -70,14 +70,14 @@ impl SimpleHtml {
}

/// Parse a [`File`] from an `<a>` tag.
fn parse_anchor(link: &HTMLTag) -> Result<File, Error> {
fn parse_anchor(link: &HTMLTag, parser: &Parser) -> Result<File, Error> {
// Extract the href.
let href = link
.attributes()
.get("href")
.flatten()
.filter(|bytes| !bytes.as_bytes().is_empty())
.ok_or(Error::MissingHref)?;
.ok_or(Error::MissingHref(link.inner_text(parser).to_string()))?;
let href = std::str::from_utf8(href.as_bytes())?;

// Extract the hash, which should be in the fragment.
Expand Down Expand Up @@ -187,8 +187,8 @@ pub enum Error {
#[error(transparent)]
HtmlParse(#[from] tl::ParseError),

#[error("Missing href attribute on anchor link")]
MissingHref,
#[error("Missing href attribute on anchor link: `{0}`")]
MissingHref(String),

#[error("Expected distribution filename as last path component of URL: {0}")]
MissingFilename(String),
Expand Down
4 changes: 2 additions & 2 deletions crates/uv-client/src/html/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ fn parse_missing_href() {
";
let base = Url::parse("https://download.pytorch.org/whl/jinja2/").unwrap();
let result = SimpleHtml::parse(text, &base).unwrap_err();
insta::assert_snapshot!(result, @"Missing href attribute on anchor link");
insta::assert_snapshot!(result, @"Missing href attribute on anchor link: `Jinja2-3.1.2-py3-none-any.whl`");
}

#[test]
Expand All @@ -436,7 +436,7 @@ fn parse_empty_href() {
"#;
let base = Url::parse("https://download.pytorch.org/whl/jinja2/").unwrap();
let result = SimpleHtml::parse(text, &base).unwrap_err();
insta::assert_snapshot!(result, @"Missing href attribute on anchor link");
insta::assert_snapshot!(result, @"Missing href attribute on anchor link: `Jinja2-3.1.2-py3-none-any.whl`");
}

#[test]
Expand Down
Loading