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
16 changes: 8 additions & 8 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use crate::url::{
clean_url, create_data_url, is_url_and_has_protocol, resolve_url, EMPTY_IMAGE_DATA_URL,
};

const FAVICON_VALUES: &[&str] = &["icon", "shortcut icon"];
const WHITESPACES: &[char] = &[' ', '\t', '\n', '\x0c', '\r']; // ASCII whitespaces

#[derive(PartialEq, Eq)]
pub enum LinkType {
Alternate,
Expand All @@ -32,14 +35,11 @@ pub enum LinkType {
Stylesheet,
}

struct SrcSetItem<'a> {
path: &'a str,
descriptor: &'a str, // Width or pixel density descriptor
pub struct SrcSetItem<'a> {
pub path: &'a str,
pub descriptor: &'a str, // Width or pixel density descriptor
}

const FAVICON_VALUES: &[&str] = &["icon", "shortcut icon"];
const WHITESPACES: &[char] = &[' ', '\t', '\n', '\x0c', '\r']; // ASCII whitespaces

pub fn add_favicon(document: &Handle, favicon_data_url: String) -> RcDom {
let mut buf: Vec<u8> = Vec::new();
serialize(
Expand Down Expand Up @@ -394,6 +394,8 @@ pub fn parse_srcset(srcset: &str) -> Vec<SrcSetItem> {
while i < partials.len() {
let partial = partials[i];

Copy link

Copilot AI Mar 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The repositioning of the loop counter increment may be unclear to readers; consider adding a comment explaining why the increment is done before processing the current partial to aid future maintainers.

Suggested change
// Increment the loop counter before processing the current partial
// to handle cases where the partial is split and reinserted into the list.

Copilot uses AI. Check for mistakes.
i += 1;

// Skip empty strings
if partial.is_empty() {
continue;
Expand Down Expand Up @@ -435,8 +437,6 @@ pub fn parse_srcset(srcset: &str) -> Vec<SrcSetItem> {
path = None;
descriptor = None;
}

i += 1;
}

// Final attempt to process what was found
Expand Down
1 change: 1 addition & 0 deletions tests/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod get_node_name;
mod has_favicon;
mod is_favicon;
mod parse_link_type;
mod parse_srcset;
mod serialize_document;
mod set_node_attr;
mod walk_and_embed_assets;
27 changes: 27 additions & 0 deletions tests/html/parse_srcset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝

#[cfg(test)]
mod passing {
use monolith::html::{parse_srcset, SrcSetItem};

#[test]
fn three_items_with_width_descriptors_and_newlines() {
let srcset = r#"https://some-site.com/width/600/https://media2.some-site.com/2021/07/some-image-073362.jpg 600w,
https://some-site.com/width/960/https://media2.some-site.com/2021/07/some-image-073362.jpg 960w,
https://some-site.com/width/1200/https://media2.some-site.com/2021/07/some-image-073362.jpg 1200w"#;
let srcset_items: Vec<SrcSetItem> = parse_srcset(srcset);

assert_eq!(srcset_items.len(), 3);
assert_eq!(srcset_items[0].path, "https://some-site.com/width/600/https://media2.some-site.com/2021/07/some-image-073362.jpg");
assert_eq!(srcset_items[0].descriptor, "600w");
assert_eq!(srcset_items[1].path, "https://some-site.com/width/960/https://media2.some-site.com/2021/07/some-image-073362.jpg");
assert_eq!(srcset_items[1].descriptor, "960w");
assert_eq!(srcset_items[2].path, "https://some-site.com/width/1200/https://media2.some-site.com/2021/07/some-image-073362.jpg");
assert_eq!(srcset_items[2].descriptor, "1200w");
}
}