use skyscraper::html::{self, grammar::document_builder::DocumentBuilder};
use crate::test_framework;
#[test]
fn table_basic_structure_with_implicit_tbody() {
let text = r#"<html><head></head><body><table><tr><td>cell</td></tr></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| {
table.add_element("tbody", |tbody| {
tbody.add_element("tr", |tr| {
tr.add_element("td", |td| td.add_text("cell"))
})
})
})
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_with_explicit_tbody() {
let text = r#"<html><head></head><body><table><tbody></tbody></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| table.add_element("tbody", |tbody| tbody))
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_with_thead_tfoot() {
let text =
r#"<html><head></head><body><table><thead></thead><tfoot></tfoot></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| {
table
.add_element("thead", |thead| thead)
.add_element("tfoot", |tfoot| tfoot)
})
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_comment() {
let text =
r#"<html><head></head><body><table><!-- table comment --></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| {
table.add_comment(" table comment ")
})
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_end_tag_closes_table() {
let text = r#"<html><head></head><body><table></table><p>after</p></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| table)
.add_element("p", |p| p.add_text("after"))
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_whitespace_preserved() {
let text = r#"<html><head></head><body><table> </table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| table.add_text(" "))
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_text_foster_parented() {
let text = r#"<html><head></head><body><table>hello</table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_text("hello")
.add_element("table", |table| table)
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_ignores_unexpected_end_tags() {
let text = r#"<html><head></head><body><table></body></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| table)
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_style_uses_in_head_rules() {
let text =
r#"<html><head></head><body><table><style>td{color:red}</style></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| {
table.add_element("style", |style| style.add_text("td{color:red}"))
})
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_script_uses_in_head_rules() {
let text =
r#"<html><head></head><body><table><script>var x=1;</script></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| {
table.add_element("script", |script| script.add_text("var x=1;"))
})
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_empty() {
let text = r#"<html><head></head><body><table></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| body.add_element("table", |table| table))
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_caption() {
let text =
r#"<html><head></head><body><table><caption>Title</caption></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| {
table.add_element("caption", |caption| caption.add_text("Title"))
})
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_colgroup() {
let text =
r#"<html><head></head><body><table><colgroup></colgroup></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| {
table.add_element("colgroup", |cg| cg)
})
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_td_implicitly_creates_tbody() {
let text = r#"<html><head></head><body><table><td>data</td></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| {
table.add_element("tbody", |tbody| {
tbody.add_element("tr", |tr| {
tr.add_element("td", |td| td.add_text("data"))
})
})
})
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_eof() {
let text = r#"<html><head></head><body><table>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| body.add_element("table", |table| table))
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_whitespace_newlines_preserved() {
let text = "<html><head></head><body><table>\n\t</table></body></html>";
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| table.add_text("\n\t"))
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_hidden_input() {
let text = r#"<html><head></head><body><table><input type="hidden" name="token" value="abc"></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| {
table.add_element("input", |input| {
input
.add_attribute_str("type", "hidden")
.add_attribute_str("name", "token")
.add_attribute_str("value", "abc")
})
})
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_non_hidden_input_foster_parented() {
let text = r#"<html><head></head><body><table><input type="text"></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("input", |input| {
input.add_attribute_str("type", "text")
})
.add_element("table", |table| table)
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_element_foster_parented() {
let text =
r#"<html><head></head><body><table><div>foster me</div></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("div", |div| div.add_text("foster me"))
.add_element("table", |table| table)
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_multiple_elements_foster_parented() {
let text = r#"<html><head></head><body><table><p>first</p><p>second</p></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("p", |p| p.add_text("first"))
.add_element("p", |p| p.add_text("second"))
.add_element("table", |table| table)
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_foster_parented_after_row() {
let text = r#"<html><head></head><body><table><tr><td>A</td></tr><p>B</p></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("p", |p| p.add_text("B"))
.add_element("table", |table| {
table.add_element("tbody", |tbody| {
tbody.add_element("tr", |tr| {
tr.add_element("td", |td| td.add_text("A"))
})
})
})
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_bold_text_foster_parented() {
let text =
r#"<html><head></head><body><table><b>bold</b></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("b", |b| b.add_text("bold"))
.add_element("table", |table| table)
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_mixed_text_and_element_foster_parented() {
let text =
r#"<html><head></head><body><table>a<b>bold</b>c</table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_text("a")
.add_element("b", |b| b.add_text("bold"))
.add_text("c")
.add_element("table", |table| table)
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_anchor_foster_parented() {
let text = r#"<html><head></head><body><table><a href="x">link</a></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("a", |a| {
a.add_attribute_str("href", "x").add_text("link")
})
.add_element("table", |table| table)
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}
#[test]
fn table_nested_table_closes_first() {
let text = r#"<html><head></head><body><table><table></table></table></body></html>"#;
let document = html::parse(text).unwrap();
let expected = DocumentBuilder::new()
.add_element("html", |html| {
html.add_element("head", |head| head)
.add_element("body", |body| {
body.add_element("table", |table| table)
.add_element("table", |table| table)
})
})
.build()
.unwrap();
assert!(test_framework::compare_documents(expected, document, true));
}