use skyscraper::html;
#[test]
fn comment_in_after_head_mode_is_inserted() {
let text = "<html><head></head><!-- after head comment --><body></body></html>";
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<!-- after head comment -->"),
"Comment should be preserved: {output:?}"
);
}
#[test]
fn doctype_in_after_head_mode_is_ignored() {
let text = "<!DOCTYPE html><html><head></head><!DOCTYPE html><body></body></html>";
let document = html::parse(text).unwrap();
let output = document.to_string();
assert_eq!(
output.matches("<!DOCTYPE").count(),
1,
"Only one DOCTYPE should survive; got: {output:?}"
);
}
#[test]
fn html_start_tag_in_after_head_merges_attributes() {
let text = r#"<html><head></head><html lang="en"><body></body></html>"#;
let document = html::parse(text).unwrap();
let output = document.to_string();
assert_eq!(
output.matches("<html").count(),
1,
"Only one <html> should exist; got: {output:?}"
);
assert!(
output.contains(r#"lang="en""#),
"Should contain lang attribute: {output:?}"
);
}
#[test]
fn duplicate_head_start_tag_in_after_head_is_ignored() {
let text = "<html><head></head><head><title>oops</title></head><body></body></html>";
let document = html::parse(text).unwrap();
let output = document.to_string();
assert_eq!(
output.matches("<head").count(),
1,
"Only one <head> should exist; got: {output:?}"
);
}
#[test]
fn unexpected_end_tag_in_after_head_is_ignored() {
let text = "<html><head></head></div><body></body></html>";
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<body></body>"),
"Should still have <body>: {output:?}"
);
}
#[test]
fn body_end_tag_in_after_head_triggers_anything_else() {
let text = "<html><head></head></body></html>";
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<body>"),
"A <body> should be implicitly created: {output:?}"
);
}
#[test]
fn meta_after_head_is_inserted_in_head() {
let text = r#"<html><head></head><meta charset="utf-8"><body></body></html>"#;
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<head><meta"),
"Meta should be inside head: {output:?}"
);
}
#[test]
fn title_after_head_is_inserted_in_head() {
let text = "<html><head></head><title>Late Title</title><body></body></html>";
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<head><title>"),
"Title should be inside head: {output:?}"
);
assert!(
output.contains("Late Title"),
"Title content should be preserved: {output:?}"
);
}
#[test]
fn link_after_head_is_inserted_in_head() {
let text = r#"<html><head></head><link rel="stylesheet" href="a.css"><body></body></html>"#;
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<head><link"),
"Link should be inside head: {output:?}"
);
}
#[test]
fn style_after_head_is_inserted_in_head() {
let text = "<html><head></head><style>body { color: red; }</style><body></body></html>";
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<head><style>"),
"Style should be inside head: {output:?}"
);
}
#[test]
fn base_after_head_is_inserted_in_head() {
let text = r#"<html><head></head><base href="/"><body></body></html>"#;
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<head><base"),
"Base should be inside head: {output:?}"
);
}
#[test]
fn script_after_head_is_inserted_in_head() {
let text = "<html><head></head><script>var x = 1;</script><body></body></html>";
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<head><script>"),
"Script should be inside head: {output:?}"
);
assert!(
output.contains("var x = 1;"),
"Script content should be preserved: {output:?}"
);
}
#[test]
fn noframes_after_head_is_inserted_in_head() {
let text = "<html><head></head><noframes>No frames</noframes><body></body></html>";
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<head><noframes>"),
"Noframes should be inside head: {output:?}"
);
}
#[test]
fn multiple_head_elements_after_head_all_go_in_head() {
let text = r#"<html><head></head><meta charset="utf-8"><title>T</title><link rel="icon" href="f"><body></body></html>"#;
let document = html::parse(text).unwrap();
let output = document.to_string();
assert!(
output.contains("<head><meta"),
"Meta should be inside head: {output:?}"
);
assert!(
output.contains("<title>"),
"Title should be inside head: {output:?}"
);
assert!(
output.contains("<link"),
"Link should be inside head: {output:?}"
);
}