|
| 1 | +//! Postprocess step for ensuring anchor permanence: see |
| 2 | +//! https://whatwg.org/working-mode#anchors. |
| 3 | +//! |
| 4 | +//! Scans for the `<script type="text/required-ids">` element, which lists |
| 5 | +//! (whitespace-separated) IDs that must appear somewhere in the document. |
| 6 | +//! After verifying that all listed IDs are present, removes the script element. |
| 7 | +
|
| 8 | +use crate::dom_utils::NodeHandleExt; |
| 9 | +use html5ever::{QualName, local_name, ns}; |
| 10 | +use markup5ever_rcdom::Handle; |
| 11 | +use std::collections::HashSet; |
| 12 | + |
| 13 | +pub struct Processor { |
| 14 | + required_ids: HashSet<String>, |
| 15 | + script_node: Option<Handle>, |
| 16 | +} |
| 17 | + |
| 18 | +impl Processor { |
| 19 | + pub fn new() -> Self { |
| 20 | + Self { |
| 21 | + required_ids: HashSet::new(), |
| 22 | + script_node: None, |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + pub fn visit(&mut self, node: &Handle) { |
| 27 | + // Capture and parse the <script type="text/required-ids"> element exactly once. |
| 28 | + if node.is_html_element(&local_name!("script")) { |
| 29 | + const TYPE: QualName = QualName { |
| 30 | + prefix: None, |
| 31 | + ns: ns!(), |
| 32 | + local: local_name!("type"), |
| 33 | + }; |
| 34 | + if node.get_attribute(&TYPE).as_deref() == Some("text/required-ids") { |
| 35 | + assert!( |
| 36 | + self.script_node.is_none(), |
| 37 | + "multiple required-ids scripts encountered" |
| 38 | + ); |
| 39 | + self.script_node = Some(node.clone()); |
| 40 | + // Gather all text within the script and split on any ASCII whitespace. |
| 41 | + let content = node.text_content(); |
| 42 | + for id_token in content.split_ascii_whitespace() { |
| 43 | + if !id_token.is_empty() { |
| 44 | + self.required_ids.insert(id_token.to_string()); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // For elements with an id attribute, mark the ID as seen. |
| 51 | + if self.required_ids.is_empty() { |
| 52 | + return; |
| 53 | + } |
| 54 | + const ID_QN: QualName = QualName { |
| 55 | + prefix: None, |
| 56 | + ns: ns!(), |
| 57 | + local: local_name!("id"), |
| 58 | + }; |
| 59 | + if let Some(id) = node.get_attribute(&ID_QN) { |
| 60 | + self.required_ids.remove(id.as_ref()); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + pub fn apply(self) -> std::io::Result<()> { |
| 65 | + if !self.required_ids.is_empty() { |
| 66 | + let mut missing: Vec<_> = self.required_ids.into_iter().collect(); |
| 67 | + missing.sort(); |
| 68 | + return Err(std::io::Error::new( |
| 69 | + std::io::ErrorKind::InvalidData, |
| 70 | + format!( |
| 71 | + "Missing required IDs for anchor permanence: {}", |
| 72 | + missing.join(", ") |
| 73 | + ), |
| 74 | + )); |
| 75 | + } |
| 76 | + |
| 77 | + // Remove the script element (if present) after verification. |
| 78 | + if let Some(script) = self.script_node { |
| 79 | + script.remove(); |
| 80 | + } |
| 81 | + Ok(()) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(test)] |
| 86 | +mod tests { |
| 87 | + use super::*; |
| 88 | + use crate::dom_utils; |
| 89 | + use crate::parser::{parse_document_async, tests::serialize_for_test}; |
| 90 | + |
| 91 | + #[tokio::test] |
| 92 | + async fn removes_script_from_head() { |
| 93 | + let document = parse_document_async(r#"<!DOCTYPE html> |
| 94 | +<html><head><script type="text/required-ids">a b c</script></head><body><div id="a"></div><p id="b"></p><section id="c"></section></body></html> |
| 95 | +"#.as_bytes()).await.unwrap(); |
| 96 | + let mut processor = Processor::new(); |
| 97 | + dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); |
| 98 | + processor.apply().unwrap(); |
| 99 | + let serialized = serialize_for_test(&[document]); |
| 100 | + assert!(!serialized.contains("text/required-ids")); |
| 101 | + } |
| 102 | + |
| 103 | + #[tokio::test] |
| 104 | + async fn no_script_present_noop() { |
| 105 | + let document = parse_document_async( |
| 106 | + r#"<!DOCTYPE html> |
| 107 | +<html><head></head><body></body></html> |
| 108 | +"# |
| 109 | + .as_bytes(), |
| 110 | + ) |
| 111 | + .await |
| 112 | + .unwrap(); |
| 113 | + let before = serialize_for_test(&[document.clone()]); |
| 114 | + let mut processor = Processor::new(); |
| 115 | + dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); |
| 116 | + processor.apply().unwrap(); |
| 117 | + assert_eq!(before, serialize_for_test(&[document])); |
| 118 | + } |
| 119 | + |
| 120 | + #[tokio::test] |
| 121 | + async fn whitespace_splitting() { |
| 122 | + // Includes indentation, multiple spaces, and newlines in the script content. |
| 123 | + let document = parse_document_async(r#"<!DOCTYPE html><html><head><script type="text/required-ids"> |
| 124 | + foo bar |
| 125 | + baz |
| 126 | + qux |
| 127 | +</script></head><body><div id="foo"></div><div id="bar"></div><div id="baz"></div><div id="qux"></div></body></html> |
| 128 | +"#.as_bytes()).await.unwrap(); |
| 129 | + let mut processor = Processor::new(); |
| 130 | + dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); |
| 131 | + processor.apply().unwrap(); |
| 132 | + let serialized = serialize_for_test(&[document]); |
| 133 | + assert!(!serialized.contains("text/required-ids")); |
| 134 | + } |
| 135 | + |
| 136 | + #[tokio::test] |
| 137 | + async fn errors_on_missing_ids() { |
| 138 | + let document = parse_document_async(r#"<!DOCTYPE html> |
| 139 | +<html><head><script type="text/required-ids">foo bar baz</script></head><body><div id="foo"></div></body></html> |
| 140 | +"#.as_bytes()).await.unwrap(); |
| 141 | + let mut processor = Processor::new(); |
| 142 | + dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); |
| 143 | + let err = processor.apply().expect_err("expected missing IDs error"); |
| 144 | + assert!( |
| 145 | + err.to_string() |
| 146 | + .contains("Missing required IDs for anchor permanence: bar, baz") |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + #[tokio::test] |
| 151 | + #[should_panic(expected = "multiple required-ids scripts encountered")] |
| 152 | + async fn panics_on_multiple_required_ids_scripts() { |
| 153 | + let document = parse_document_async(r#"<!DOCTYPE html><html><head> |
| 154 | +<script type="text/required-ids">a b</script> |
| 155 | +<script type="text/required-ids">c d</script> |
| 156 | +</head><body><div id="a"></div><div id="b"></div><div id="c"></div><div id="d"></div></body></html>"#.as_bytes()).await.unwrap(); |
| 157 | + let mut processor = Processor::new(); |
| 158 | + dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); |
| 159 | + } |
| 160 | +} |
0 commit comments