use facet_testhelpers::test;
use hotmeal::{NodePath, NodeRef, Patch, StrTendril};
#[allow(unused_imports)]
use hotmeal::{debug, trace};
use html5ever::LocalName;
use smallvec::smallvec;
fn t(s: &str) -> StrTendril {
StrTendril::from(s)
}
#[test]
fn test_minimal_repro() {
let html = t("<html><body><div></div></body></html>");
let mut doc = hotmeal::parse(&html);
let patches = vec![Patch::InsertElement {
at: NodeRef(NodePath(smallvec![0, 1])),
tag: LocalName::from("p"),
attrs: vec![],
children: vec![],
detach_to_slot: None,
}];
let result = doc.apply_patches(patches);
trace!(?result, "Result");
assert!(result.is_ok(), "should be able to insert at end");
}
#[test]
fn test_move_within_same_parent() {
let html = t("<html><body><div>1</div><div>2</div></body></html>");
let mut doc = hotmeal::parse(&html);
let patches = vec![Patch::Move {
from: NodeRef(NodePath(smallvec![0, 0])),
to: NodeRef(NodePath(smallvec![0, 1])),
detach_to_slot: None,
}];
let result = doc.apply_patches(patches);
trace!(?result, "Result");
trace!(html = %doc.to_html(), "HTML");
}
#[test]
fn test_img_alt_attribute_preservation() {
let old_html = t(r#"<html><body><img><img src="" alt=""><img src=""></body></html>"#);
let new_html = t(r#"<html><body><img src="" alt=""><img src=""></body></html>"#);
let mut old = hotmeal::parse(&old_html);
let new = hotmeal::parse(&new_html);
let patches = hotmeal::diff(&old, &new).expect("diff failed");
trace!(?patches, "Patches");
old.apply_patches(patches).expect("apply failed");
let result = old.to_html();
trace!(result = %result, "Result");
assert!(
!result.contains(r#"<img src="" alt=""><img src="" alt="">"#),
"Second img should not have alt attribute, got: {}",
result
);
assert!(
result.contains(r#"<img src="" alt=""><img src="">"#),
"Expected <img src=\"\" alt=\"\"><img src=\"\">, got: {}",
result
);
}
#[test]
fn test_textarea_content() {
let a = t("<html><body>\n$</body></html>");
let b = t("<html><body><textarea>\n\n\n\n</body></html></textarea></body></html>");
trace!(?a, "Parsing a");
let doc_a = hotmeal::parse(&a);
trace!(doc_a_html = %doc_a.to_html(), "doc_a HTML");
trace!(?b, "Parsing b");
let doc_b = hotmeal::parse(&b);
trace!(doc_b_html = %doc_b.to_html(), "doc_b HTML");
trace!("Computing diff...");
let patches = hotmeal::diff(&doc_a, &doc_b).expect("diff should succeed");
trace!(?patches, "Patches");
let mut patched = doc_a.clone();
patched
.apply_patches(patches)
.expect("apply should succeed");
trace!(patched = %patched.to_html(), "Patched");
trace!(expected = %doc_b.to_html(), "Expected");
assert_eq!(
patched.to_html(),
doc_b.to_html(),
"Patched HTML should match target"
);
}
#[test]
fn test_text_to_s_element() {
let a = t("<html><body>'</body></html>");
let b = t("<html><body><s =</body></html>");
trace!(?a, "Parsing a");
let doc_a = hotmeal::parse(&a);
trace!(doc_a_html = %doc_a.to_html(), "doc_a HTML");
trace!(?b, "Parsing b");
let doc_b = hotmeal::parse(&b);
trace!(doc_b_html = %doc_b.to_html(), "doc_b HTML");
trace!("Computing diff...");
let patches = hotmeal::diff(&doc_a, &doc_b).expect("diff should succeed");
trace!(?patches, "Patches");
let mut patched = doc_a.clone();
patched
.apply_patches(patches)
.expect("apply should succeed");
trace!(patched = %patched.to_html(), "Patched");
trace!(expected = %doc_b.to_html(), "Expected");
assert_eq!(
patched.to_html(),
doc_b.to_html(),
"Patched HTML should match target"
);
}
#[test]
fn test_diff_incomplete_doctype_vs_empty() {
let a = t("<!");
let b = t("");
trace!(?a, "Parsing a");
let doc_a = hotmeal::parse(&a);
trace!(doc_a_html = %doc_a.to_html(), "doc_a HTML");
trace!(doc_a_body = ?doc_a.body(), "doc_a.body()");
trace!(?b, "Parsing b");
let doc_b = hotmeal::parse(&b);
trace!(doc_b_html = %doc_b.to_html(), "doc_b HTML");
trace!(doc_b_body = ?doc_b.body(), "doc_b.body()");
trace!("Computing diff...");
let patches = hotmeal::diff(&doc_a, &doc_b).expect("diff should succeed");
trace!(?patches, "Patches");
let mut patched = doc_a.clone();
patched
.apply_patches(patches)
.expect("apply should succeed");
assert_eq!(
patched.to_body_html(),
doc_b.to_body_html(),
"Patched body content should match target body content"
);
}