#![cfg(feature = "metadata")]
use super::{KeptScripts, strip_script_and_style_tags, strip_script_and_style_tags_with_kept_scripts};
const OPEN: &str = r#"<script type="application/ld+json">"#;
struct Script {
as_written: String,
attributes: Vec<(String, Option<String>)>,
}
fn scripts(page: &str, kept_scripts: KeptScripts) -> Vec<Script> {
let mut dom = tl::parse(page, tl::ParserOptions::default()).expect("the page");
let kept_scripts = kept_scripts.with_owner_marks_taken(&mut dom).expect("the owner marks");
let parser = dom.parser();
let mut scripts = Vec::new();
for (node, slot) in dom.nodes().iter().enumerate() {
let Some(tag) = slot.as_tag() else {
continue;
};
if tag.name().as_bytes() != b"script" {
continue;
}
let node = u32::try_from(node).expect("the id of a node");
let text = tag.inner_text(parser);
let mut attributes: Vec<(String, Option<String>)> = tag
.attributes()
.iter()
.map(|(name, value)| (name.into_owned(), value.map(std::borrow::Cow::into_owned)))
.collect();
attributes.sort();
scripts.push(Script {
as_written: kept_scripts.as_written(node, text.trim()).to_owned(),
attributes,
});
}
scripts
}
fn type_only() -> Vec<(String, Option<String>)> {
vec![("type".to_string(), Some("application/ld+json".to_string()))]
}
#[test]
fn the_text_of_a_kept_script_is_given_as_written() {
for body in [
r#"{"a":"?a=1&b=2 & & &amp;"}"#,
r#"{"a":"a < b"}"#,
r#"{"a":"a < b"}"#,
r#"{"a":"a & b & c < d < e &lt; f"}"#,
"{\"a\":\"<p>x</p>\",\r\n\"b\":\"<\"}",
] {
let html = format!("<p>a</p>{OPEN} {body}\n</script><p>b</p>");
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
let open = if body.contains('<') || body.contains("<") {
r#"<script data-h2m-script-0="h2m_script_body_0_end" type="application/ld+json">"#
} else {
OPEN
};
let in_tree = page
.strip_prefix(&format!("<p>a</p>{open}"))
.and_then(|rest| rest.strip_suffix("</script><p>b</p>"))
.expect("the kept script");
assert!(!in_tree.contains('<'), "{body:?}: one text for the parser");
let found = scripts(&page, kept_scripts);
assert_eq!(found.len(), 1, "{body:?}");
assert_eq!(found[0].as_written, body, "{body:?}");
assert_eq!(found[0].attributes, type_only(), "{body:?}: no owner mark stays");
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
let found = scripts(&page.replace("\r\n", "\n"), kept_scripts);
assert_eq!(found[0].as_written, body, "{body:?}: a repaired page");
}
}
#[test]
fn the_record_of_a_kept_script_goes_with_its_owner_mark() {
let lt = r#"{"a":"<"}"#;
let reference = r#"{"a":"<"}"#;
let other = r#"{"b":"<"}"#;
let bodies = [lt, reference, other, reference];
let mut html = String::new();
for body in bodies {
html.push_str(OPEN);
html.push_str(body);
html.push_str("</script>");
}
let cases: [&[usize]; 9] = [
&[0, 1, 2, 3],
&[2, 0, 1, 3],
&[3, 2, 1, 0],
&[1, 2, 3],
&[0, 2, 3],
&[1],
&[0],
&[3, 0],
&[],
];
for kept in cases {
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
let elements: Vec<&str> = page.split_inclusive("</script>").collect();
assert_eq!(elements.len(), bodies.len());
let left: String = kept.iter().map(|at| elements[*at]).collect();
let found = scripts(&left, kept_scripts);
let as_written: Vec<&str> = found.iter().map(|script| script.as_written.as_str()).collect();
let expected: Vec<&str> = kept.iter().map(|at| bodies[*at]).collect();
assert_eq!(as_written, expected, "{kept:?}");
for script in &found {
assert_eq!(script.attributes, type_only(), "{kept:?}: no owner mark stays");
}
}
}
#[test]
fn an_attribute_of_the_author_with_the_name_of_an_owner_mark_stays() {
let html = format!(
"<script type=\"application/ld+json\" DATA-H2M-SCRIPT-0=\"x\" data-h2m-script-2>{{\"a\":\"<\"}}</script>\
<p data-h2m-script-3=\"y\">text</p>{OPEN}{{\"a\":\"<\"}}</script>"
);
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
assert_eq!(
page,
"<script data-h2m-script-1=\"h2m_script_body_1_end\" type=\"application/ld+json\" \
DATA-H2M-SCRIPT-0=\"x\" data-h2m-script-2>{\"a\":\"<\"}</script>\
<p data-h2m-script-3=\"y\">text</p>\
<script data-h2m-script-4=\"h2m_script_body_4_end\" type=\"application/ld+json\">{\"a\":\"<\"}</script>"
);
let found = scripts(&page, kept_scripts);
assert_eq!(found.len(), 2);
assert_eq!(found[0].as_written, r#"{"a":"<"}"#);
assert_eq!(
found[0].attributes,
[
("data-h2m-script-0".to_string(), Some("x".to_string())),
("data-h2m-script-2".to_string(), None),
("type".to_string(), Some("application/ld+json".to_string())),
]
);
assert_eq!(found[1].as_written, r#"{"a":"<"}"#);
assert_eq!(found[1].attributes, type_only());
}
#[test]
fn a_name_with_a_sign_or_a_zero_before_its_number_is_no_owner_mark() {
for name in [
"data-h2m-script-+0",
"data-h2m-script-00",
"data-h2m-script-0x",
"data-h2m-script-",
] {
let html = format!(
"{OPEN}{{\"a\":\"<\"}}</script>\
<script type=\"application/ld+json\" {name}=\"x\">{{\"a\":\"b\"}}</script>"
);
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
let found = scripts(&page, kept_scripts);
assert_eq!(found.len(), 2, "{name}");
assert_eq!(found[0].as_written, r#"{"a":"<"}"#, "{name}");
assert_eq!(found[1].as_written, r#"{"a":"b"}"#, "{name}");
let mut expected = type_only();
expected.push((name.to_string(), Some("x".to_string())));
expected.sort();
assert_eq!(found[1].attributes, expected, "{name}");
}
}
#[test]
fn a_script_with_no_owner_mark_reads_as_the_tree_has_it() {
let html = format!("{OPEN}{{\"a\":\"<\"}}</script>");
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
let other_value = page.replace("h2m_script_body_0_end", "h2m_script_body_0_end ");
assert_ne!(other_value, page);
let found = scripts(&other_value, kept_scripts);
assert_eq!(found[0].as_written, r#"{"a":"<"}"#);
assert_eq!(found[0].attributes.len(), 2);
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
assert_eq!(scripts(&page, kept_scripts)[0].as_written, r#"{"a":"<"}"#);
}
#[test]
fn an_owner_mark_in_the_text_of_the_tree_is_taken_out() {
let html = format!("{OPEN}{{\"a\":\"<\"}}</script><p>b</p>{OPEN}{{\"a\":\"<\"}}</script>");
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
assert_eq!(page.matches("data-h2m-script-").count(), 2);
let as_text = page
.replacen("<script", "<textarea>x data-h2m-script-7 <script", 1)
.replacen("</script>", "</script></textarea>", 1);
let mut dom = tl::parse(&as_text, tl::ParserOptions::default()).expect("the page");
kept_scripts.with_owner_marks_taken(&mut dom).expect("the owner marks");
let texts: Vec<String> = dom
.nodes()
.iter()
.filter_map(tl::Node::as_raw)
.map(|text| text.as_utf8_str().into_owned())
.collect();
let attributes = &OPEN["<script".len()..];
let expected = format!("x data-h2m-script-7 <script{attributes}{{\"a\":\"<\"}}</script>");
assert!(texts.contains(&expected), "{texts:?}");
assert_eq!(
texts.iter().filter(|text| text.contains("data-h2m-script-")).count(),
1,
"{texts:?}"
);
}
#[test]
fn the_second_pass_uses_no_number_that_the_cleaned_page_has() {
let html = format!("<p data-h2m-script-1>a</p>{OPEN}{{\"a\":\"<\"}}</script>");
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
let marked = |mark: usize| {
format!(
"<p data-h2m-script-1>a</p><script data-h2m-script-{mark}=\"h2m_script_body_{mark}_end\" \
type=\"application/ld+json\">{{\"a\":\"<\"}}</script>"
)
};
assert_eq!(page, marked(0));
let cleaned =
format!("<script data-h2m-script-0=\"h2m_script_body_0_end\">x</script><i DATA-H2M-SCRIPT-2>b</i>{page}");
let second = kept_scripts
.reserving_owner_names_of(&cleaned)
.expect("the first pass wrote a mark");
let (page, kept_scripts) =
crate::converter::utility::preprocessing::strip_script_and_style_tags_for_second_pass(&html, second);
assert_eq!(page, marked(3));
assert_eq!(scripts(&page, kept_scripts)[0].as_written, r#"{"a":"<"}"#);
}
#[test]
fn a_pass_with_no_owner_mark_asks_for_no_second_pass() {
let html = format!("{OPEN}{{\"a\":\"b\"}}</script><p data-h2m-script-0>a</p>");
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
assert_eq!(page, html);
assert!(kept_scripts.reserving_owner_names_of(&page).is_none());
}
#[test]
fn the_pass_over_a_repaired_page_writes_no_owner_mark() {
let html = format!("{OPEN}{{\"a\":\"<\"}}</script>{OPEN}{{\"a\":\"<\"}}</script>");
assert!(!strip_script_and_style_tags(&html).contains("data-h2m-script-"));
let (page, _) = strip_script_and_style_tags_with_kept_scripts(&html);
assert_eq!(page.matches("data-h2m-script-").count(), 2);
assert_eq!(strip_script_and_style_tags(&page), page);
}
#[test]
fn a_kept_script_whose_text_the_tree_holds_as_written_has_no_owner_mark() {
let html = format!("<p>a</p>{OPEN}{{\"a\":\"b & c\"}}</script>");
let (page, kept_scripts) = strip_script_and_style_tags_with_kept_scripts(&html);
assert_eq!(page, html);
assert_eq!(scripts(&page, kept_scripts)[0].as_written, r#"{"a":"b & c"}"#);
}