Skip to content

Commit 6f3ba7e

Browse files
committed
fix(convert): avoid unicode offset panic in attribute extraction
1 parent 539fa84 commit 6f3ba7e

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

crates/fetchkit/src/convert.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,15 @@ pub fn html_to_text(html: &str) -> String {
414414
/// Extract attribute value from tag
415415
fn extract_attribute(tag: &str, attr: &str) -> Option<String> {
416416
let pattern = format!("{}=", attr);
417-
let tag_lower = tag.to_lowercase();
418-
419-
if let Some(start) = tag_lower.find(&pattern) {
417+
let start = tag
418+
.char_indices()
419+
.find_map(|(idx, _)| {
420+
tag.get(idx..idx + pattern.len())
421+
.filter(|candidate| candidate.eq_ignore_ascii_case(&pattern))
422+
.map(|_| idx)
423+
});
424+
425+
if let Some(start) = start {
420426
let rest = &tag[start + pattern.len()..];
421427
let rest = rest.trim_start();
422428

@@ -1254,6 +1260,10 @@ mod tests {
12541260
extract_attribute("div class=test", "class"),
12551261
Some("test".to_string())
12561262
);
1263+
assert_eq!(
1264+
extract_attribute("a title=\"İİ\" href=x", "href"),
1265+
Some("x".to_string())
1266+
);
12571267
}
12581268

12591269
#[test]

0 commit comments

Comments
 (0)