use node_html_parser::parse;
#[test]
fn issue_218_attribute_quote_and_updates() {
let html = "<html>\n <div id=\"_\" title='\"world\"' onClick='alert(\"hello\")' color=\"red\">nochange</div>\n <div id=\"e\" title='\"world\"' color=\"red\">expected</div>\n <div id=\"a\" title='\"world\"' onClick='alert(\"hello\")' color=\"red\">actual</div>\n</html>";
let mut root = parse(html);
{
let e = root.get_element_by_id_mut("e").expect("#e exists");
e.set_attribute("onClick", "alert('hello')");
}
{
let a = root.get_element_by_id_mut("a").expect("#a exists");
a.remove_attribute("color");
a.set_attribute("title", "\"replaced\"");
}
let serialized = root.to_string();
println!("serialized=\n{}", serialized);
assert!(serialized.contains("<div id=\"e\" title=\""world"\" color=\"red\" onClick=\"alert('hello')\">expected</div>"));
let a_el = root.query_selector("#a").unwrap();
let a_html = a_el.outer_html();
assert!(a_html.starts_with("<div "));
assert!(a_html.contains("id=\"a\""));
assert!(a_html.contains("title=\""replaced"\""));
assert!(a_html.contains("onClick=\"alert("hello")\""));
assert!(a_html.ends_with(">actual</div>"));
}
#[test]
fn issue_218_escape_newlines() {
let root = parse("<p></p>");
let mut p = root.query_selector("p").unwrap().clone();
p.set_attribute("a", "1\n2");
assert_eq!(p.get_attr("a").unwrap(), "1\n2");
assert_eq!(p.outer_html(), "<p a=\"1\n2\"></p>");
}