use skyscraper::html::{self, grammar::document_builder::DocumentBuilder};
use crate::test_framework;
#[test]
fn noscript_in_head_basic() {
let text = r#"<html><head><noscript><style>body{color:red}</style></noscript></head><body></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| {
head.add_element("noscript", |noscript| {
noscript.add_element("style", |style| style.add_text("body{color:red}"))
})
})
.add_element("body", |body| body)
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn noscript_in_head_with_link() {
let text = r#"<html><head><noscript><link rel="stylesheet" href="style.css"></noscript></head><body></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| {
head.add_element("noscript", |noscript| {
noscript.add_element("link", |link| {
link.add_attribute_str("rel", "stylesheet")
.add_attribute_str("href", "style.css")
})
})
})
.add_element("body", |body| body)
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn noscript_in_head_with_meta() {
let text = r#"<html><head><noscript><meta http-equiv="refresh" content="0;url=fallback.html"></noscript></head><body></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| {
head.add_element("noscript", |noscript| {
noscript.add_element("meta", |meta| {
meta.add_attribute_str("http-equiv", "refresh")
.add_attribute_str("content", "0;url=fallback.html")
})
})
})
.add_element("body", |body| body)
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn noscript_in_head_unexpected_start_tag_causes_reprocess() {
let text = r#"<html><head><noscript><div>hello</div></noscript></head><body></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head.add_element("noscript", |ns| ns))
.add_element("body", |body| {
body.add_element("div", |div| div.add_text("hello"))
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn noscript_end_tag_returns_to_in_head() {
let text =
r#"<html><head><noscript></noscript><title>Test</title></head><body></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| {
head.add_element("noscript", |ns| ns)
.add_element("title", |title| title.add_text("Test"))
})
.add_element("body", |body| body)
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn noscript_in_head_ignores_head_start_tag() {
let text = r#"<html><head><noscript><head></noscript></head><body></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head.add_element("noscript", |ns| ns))
.add_element("body", |body| body)
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn noscript_in_head_ignores_nested_noscript_start_tag() {
let text = r#"<html><head><noscript><noscript></noscript></noscript></head><body></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head.add_element("noscript", |ns| ns))
.add_element("body", |body| body)
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn noscript_in_head_comment() {
let text =
r#"<html><head><noscript><!-- a comment --></noscript></head><body></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| {
head.add_element("noscript", |ns| ns.add_comment(" a comment "))
})
.add_element("body", |body| body)
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn noscript_in_head_multiple_elements() {
let text = r#"<html><head><noscript><link rel="stylesheet" href="a.css"><style>body{}</style><link rel="stylesheet" href="b.css"></noscript></head><body></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| {
head.add_element("noscript", |ns| {
ns.add_element("link", |link| {
link.add_attribute_str("rel", "stylesheet")
.add_attribute_str("href", "a.css")
})
.add_element("style", |style| style.add_text("body{}"))
.add_element("link", |link| {
link.add_attribute_str("rel", "stylesheet")
.add_attribute_str("href", "b.css")
})
})
})
.add_element("body", |body| body)
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn noscript_in_head_eof() {
let text = r#"<html><head><noscript>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head.add_element("noscript", |ns| ns))
.add_element("body", |body| body)
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn noscript_in_head_br_end_tag() {
let text = r#"<html><head><noscript></br></noscript></head><body></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head.add_element("noscript", |ns| ns))
.add_element("body", |body| body.add_element("br", |br| br))
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}