Fix bug in inline_css_str_to_map

This commit is contained in:
Kenneth Gitere 2021-04-14 18:07:39 +03:00
parent 2762bc5086
commit d6cbbe405b

View file

@ -799,6 +799,7 @@ impl Readability {
state = State::ReadProp; state = State::ReadProp;
decl.1 = Some(token.trim().to_string()); decl.1 = Some(token.trim().to_string());
tokens.push(decl.clone()); tokens.push(decl.clone());
decl = (None, None);
token.clear(); token.clear();
} else { } else {
token.push(c); token.push(c);
@ -819,11 +820,18 @@ impl Readability {
} }
} }
if !token.is_empty() { if !token.is_empty() {
decl.1 = Some(token.trim().to_string()); match state {
tokens.push(decl); State::ReadVal => {
decl.1 = Some(token.trim().to_string());
tokens.push(decl);
}
_ => (),
}
} }
tokens tokens
.into_iter() .into_iter()
.filter(|tok_pair| tok_pair.0.is_some() && tok_pair.1.is_some())
.map(|tok_pair| (tok_pair.0.unwrap(), tok_pair.1.unwrap())) .map(|tok_pair| (tok_pair.0.unwrap(), tok_pair.1.unwrap()))
.collect() .collect()
} }
@ -2460,12 +2468,24 @@ mod test {
css_map.insert("align-items".to_string(), "center".to_string()); css_map.insert("align-items".to_string(), "center".to_string());
css_map.insert("border".to_string(), "2px solid black".to_string()); css_map.insert("border".to_string(), "2px solid black".to_string());
let css_str_to_vec = Readability::inline_css_str_to_map(css_str); let css_str_to_map = Readability::inline_css_str_to_map(css_str);
assert_eq!(css_map, css_str_to_vec); assert_eq!(css_map, css_str_to_map);
let mut css_map = HashMap::new(); let mut css_map = HashMap::new();
css_map.insert("color".to_string(), "red".to_string()); css_map.insert("color".to_string(), "red".to_string());
css_map.insert("background-image".to_string(), "url('data:image/jpeg;base64,/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAgADBP/')".to_string()); css_map.insert("background-image".to_string(), "url('data:image/jpeg;base64,/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAgADBP/')".to_string());
assert_eq!(css_map, Readability::inline_css_str_to_map("color: red;background-image: url('data:image/jpeg;base64,/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAgADBP/')")); assert_eq!(css_map, Readability::inline_css_str_to_map("color: red;background-image: url('data:image/jpeg;base64,/wgARCAALABQDASIAAhEBAxEB/8QAFwABAQEBAAAAAAAAAAAAAAAAAgADBP/')"));
let empty_map = HashMap::new();
assert_eq!(empty_map, Readability::inline_css_str_to_map(" \n \t \r"));
assert_eq!(empty_map, Readability::inline_css_str_to_map("color"));
let mut css_map = HashMap::new();
css_map.insert("color".to_string(), "red".to_string());
css_map.insert("height".to_string(), "300px".to_string());
assert_eq!(
css_map,
Readability::inline_css_str_to_map("color: red;height: 300px;width")
);
} }
#[test] #[test]