Skip to content

Commit 03ad5e8

Browse files
committed
fix parsing of srcset with whitespaces
1 parent bfac165 commit 03ad5e8

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/html.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ use crate::url::{
2222
clean_url, create_data_url, is_url_and_has_protocol, resolve_url, EMPTY_IMAGE_DATA_URL,
2323
};
2424

25+
const FAVICON_VALUES: &[&str] = &["icon", "shortcut icon"];
26+
const WHITESPACES: &[char] = &[' ', '\t', '\n', '\x0c', '\r']; // ASCII whitespaces
27+
2528
#[derive(PartialEq, Eq)]
2629
pub enum LinkType {
2730
Alternate,
@@ -32,14 +35,11 @@ pub enum LinkType {
3235
Stylesheet,
3336
}
3437

35-
struct SrcSetItem<'a> {
36-
path: &'a str,
37-
descriptor: &'a str, // Width or pixel density descriptor
38+
pub struct SrcSetItem<'a> {
39+
pub path: &'a str,
40+
pub descriptor: &'a str, // Width or pixel density descriptor
3841
}
3942

40-
const FAVICON_VALUES: &[&str] = &["icon", "shortcut icon"];
41-
const WHITESPACES: &[char] = &[' ', '\t', '\n', '\x0c', '\r']; // ASCII whitespaces
42-
4343
pub fn add_favicon(document: &Handle, favicon_data_url: String) -> RcDom {
4444
let mut buf: Vec<u8> = Vec::new();
4545
serialize(
@@ -394,6 +394,8 @@ pub fn parse_srcset(srcset: &str) -> Vec<SrcSetItem> {
394394
while i < partials.len() {
395395
let partial = partials[i];
396396

397+
i += 1;
398+
397399
// Skip empty strings
398400
if partial.is_empty() {
399401
continue;
@@ -435,8 +437,6 @@ pub fn parse_srcset(srcset: &str) -> Vec<SrcSetItem> {
435437
path = None;
436438
descriptor = None;
437439
}
438-
439-
i += 1;
440440
}
441441

442442
// Final attempt to process what was found

tests/html/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod get_node_name;
1010
mod has_favicon;
1111
mod is_favicon;
1212
mod parse_link_type;
13+
mod parse_srcset;
1314
mod serialize_document;
1415
mod set_node_attr;
1516
mod walk_and_embed_assets;

tests/html/parse_srcset.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// ██████╗ █████╗ ███████╗███████╗██╗███╗ ██╗ ██████╗
2+
// ██╔══██╗██╔══██╗██╔════╝██╔════╝██║████╗ ██║██╔════╝
3+
// ██████╔╝███████║███████╗███████╗██║██╔██╗ ██║██║ ███╗
4+
// ██╔═══╝ ██╔══██║╚════██║╚════██║██║██║╚██╗██║██║ ██║
5+
// ██║ ██║ ██║███████║███████║██║██║ ╚████║╚██████╔╝
6+
// ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝
7+
8+
#[cfg(test)]
9+
mod passing {
10+
use monolith::html::{parse_srcset, SrcSetItem};
11+
12+
#[test]
13+
fn three_items_with_width_descriptors_and_newlines() {
14+
let srcset = r#"https://some-site.com/width/600/https://media2.some-site.com/2021/07/some-image-073362.jpg 600w,
15+
https://some-site.com/width/960/https://media2.some-site.com/2021/07/some-image-073362.jpg 960w,
16+
https://some-site.com/width/1200/https://media2.some-site.com/2021/07/some-image-073362.jpg 1200w"#;
17+
let srcset_items: Vec<SrcSetItem> = parse_srcset(srcset);
18+
19+
assert_eq!(srcset_items.len(), 3);
20+
assert_eq!(srcset_items[0].path, "https://some-site.com/width/600/https://media2.some-site.com/2021/07/some-image-073362.jpg");
21+
assert_eq!(srcset_items[0].descriptor, "600w");
22+
assert_eq!(srcset_items[1].path, "https://some-site.com/width/960/https://media2.some-site.com/2021/07/some-image-073362.jpg");
23+
assert_eq!(srcset_items[1].descriptor, "960w");
24+
assert_eq!(srcset_items[2].path, "https://some-site.com/width/1200/https://media2.some-site.com/2021/07/some-image-073362.jpg");
25+
assert_eq!(srcset_items[2].descriptor, "1200w");
26+
}
27+
}

0 commit comments

Comments
 (0)