diff --git a/src/anchor_permanence.rs b/src/anchor_permanence.rs index a8a1c144..a350b8d8 100644 --- a/src/anchor_permanence.rs +++ b/src/anchor_permanence.rs @@ -87,57 +87,64 @@ mod tests { use super::*; use crate::dom_utils; use crate::parser::{parse_document_async, tests::serialize_for_test}; + use std::io; #[tokio::test] - async fn removes_script_from_head() { - let document = parse_document_async(r#" + async fn removes_script_from_head() -> io::Result<()> { + let parsed = parse_document_async(r#"

-"#.as_bytes()).await.unwrap(); +"#.as_bytes()).await?; + let document = parsed.document().clone(); let mut processor = Processor::new(); dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); processor.apply().unwrap(); let serialized = serialize_for_test(&[document]); assert!(!serialized.contains("text/required-ids")); + Ok(()) } #[tokio::test] - async fn no_script_present_noop() { - let document = parse_document_async( + async fn no_script_present_noop() -> io::Result<()> { + let parsed = parse_document_async( r#" "# .as_bytes(), ) - .await - .unwrap(); + .await?; + let document = parsed.document().clone(); let before = serialize_for_test(&[document.clone()]); let mut processor = Processor::new(); dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); processor.apply().unwrap(); assert_eq!(before, serialize_for_test(&[document])); + Ok(()) } #[tokio::test] - async fn whitespace_splitting() { + async fn whitespace_splitting() -> io::Result<()> { // Includes indentation, multiple spaces, and newlines in the script content. - let document = parse_document_async(r#"
-"#.as_bytes()).await.unwrap(); +"#.as_bytes()).await?; + let document = parsed.document().clone(); let mut processor = Processor::new(); dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); processor.apply().unwrap(); let serialized = serialize_for_test(&[document]); assert!(!serialized.contains("text/required-ids")); + Ok(()) } #[tokio::test] - async fn errors_on_missing_ids() { - let document = parse_document_async(r#" + async fn errors_on_missing_ids() -> io::Result<()> { + let parsed = parse_document_async(r#"
-"#.as_bytes()).await.unwrap(); +"#.as_bytes()).await?; + let document = parsed.document().clone(); let mut processor = Processor::new(); dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); let err = processor.apply().expect_err("expected missing IDs error"); @@ -145,15 +152,17 @@ mod tests { err.to_string() .contains("Missing required IDs for anchor permanence: bar, baz") ); + Ok(()) } #[tokio::test] #[should_panic(expected = "multiple required-ids scripts encountered")] async fn panics_on_multiple_required_ids_scripts() { - let document = parse_document_async(r#" + let parsed = parse_document_async(r#"
"#.as_bytes()).await.unwrap(); + let document = parsed.document().clone(); let mut processor = Processor::new(); dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); } diff --git a/src/annotate_attributes.rs b/src/annotate_attributes.rs index e47ad417..dccfc9b4 100644 --- a/src/annotate_attributes.rs +++ b/src/annotate_attributes.rs @@ -311,7 +311,7 @@ mod tests { // before and after the attributes table, to demonstrate that this is // not sensitive to which order they occur in (i.e., these could be // reordered in the HTML spec). - let document = parse_document_async( + let parsed = parse_document_async( r#"

The a element

@@ -333,6 +333,7 @@ mod tests {
href "#.trim().as_bytes()).await?; + let document = parsed.document().clone(); let mut proc = Processor::new(); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); proc.apply().await?; @@ -368,7 +369,7 @@ mod tests { async fn test_variant() -> io::Result<()> { // This checks that and work correctly. // i.e., the variant description is used where requested - let document = parse_document_async( + let parsed = parse_document_async( r#"

The a element

@@ -386,6 +387,7 @@ mod tests {
href "#.trim().as_bytes()).await?; + let document = parsed.document().clone(); let mut proc = Processor::new(); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); proc.apply().await?; @@ -415,7 +417,7 @@ mod tests { #[tokio::test] async fn test_special_semantics() -> io::Result<()> { // Checks that the special rules for using : instead of an em dash work. - let document = parse_document_async( + let parsed = parse_document_async( r#"

The a element

@@ -428,6 +430,7 @@ mod tests { nameaAnchor name "#.trim().as_bytes()).await?; + let document = parsed.document().clone(); let mut proc = Processor::new(); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); proc.apply().await?; @@ -451,7 +454,7 @@ mod tests { #[tokio::test] async fn test_special_semantics_multiple() -> io::Result<()> { // Checks that the special rules for joining any special semantics with a ; work. - let document = parse_document_async( + let parsed = parse_document_async( r#"

The a element

@@ -465,6 +468,7 @@ mod tests { nameaName of the anchor "#.trim().as_bytes()).await?; + let document = parsed.document().clone(); let mut proc = Processor::new(); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); proc.apply().await?; @@ -490,7 +494,7 @@ mod tests { async fn test_identical_links() -> io::Result<()> { // This checks the same identifier can be linked multiple times without // repeating the description. - let document = parse_document_async( + let parsed = parse_document_async( r#"

The img element

@@ -508,6 +512,7 @@ mod tests { widthimg; videoHorizontal dimension "#.trim().as_bytes()).await?; + let document = parsed.document().clone(); let mut proc = Processor::new(); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); proc.apply().await?; diff --git a/src/boilerplate.rs b/src/boilerplate.rs index 4e6ded28..35a0d34b 100644 --- a/src/boilerplate.rs +++ b/src/boilerplate.rs @@ -166,10 +166,11 @@ mod tests { "enEnglish", ) .await?; - let document = parse_document_async( + let parsed = parse_document_async( "
".as_bytes(), ) .await?; + let document = parsed.document().clone(); let mut proc = Processor::new(boilerplate_dir.path(), Path::new(".")); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); proc.apply().await?; @@ -188,10 +189,11 @@ mod tests { "data:text/html,Hello, world!", ) .await?; - let document = parse_document_async( + let parsed = parse_document_async( "\">hello".as_bytes(), ) .await?; + let document = parsed.document().clone(); let mut proc = Processor::new(boilerplate_dir.path(), Path::new(".")); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); proc.apply().await?; @@ -208,9 +210,10 @@ mod tests { tokio::fs::write(example_dir.path().join("ex1"), "first").await?; tokio::fs::write(example_dir.path().join("ex2"), "second").await?; tokio::fs::write(example_dir.path().join("ignored"), "bad").await?; - let document = + let parsed = parse_document_async("
EXAMPLE ex1
\nEXAMPLE ex2  

EXAMPLE ignored

".as_bytes()) .await?; + let document = parsed.document().clone(); let mut proc = Processor::new(Path::new("."), example_dir.path()); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); proc.apply().await?; @@ -229,7 +232,8 @@ mod tests { "
EXAMPLE ../foo
", ]; for example in bad_path_examples { - let document = parse_document_async(example.as_bytes()).await?; + let parsed = parse_document_async(example.as_bytes()).await?; + let document = parsed.document().clone(); let mut proc = Processor::new(Path::new("."), Path::new(".")); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); let result = proc.apply().await; diff --git a/src/interface_index.rs b/src/interface_index.rs index 62eb45d4..c1892630 100644 --- a/src/interface_index.rs +++ b/src/interface_index.rs @@ -186,7 +186,7 @@ mod tests { #[tokio::test] async fn test_two_interfaces_in_one_block() -> io::Result<()> { - let document = parse_document_async( + let parsed = parse_document_async( r#"

@@ -199,6 +199,7 @@ INSERT INTERFACES HERE
             .as_bytes(),
         )
         .await?;
+        let document = parsed.document().clone();
         let mut proc = Processor::new();
         dom_utils::scan_dom(&document, &mut |h| proc.visit(h));
         proc.apply()?;
@@ -216,7 +217,7 @@ interface HTMLBlinkElement { ... }
 
     #[tokio::test]
     async fn test_two_interfaces_in_separate_blocks() -> io::Result<()> {
-        let document = parse_document_async(
+        let parsed = parse_document_async(
             r#"
 
 

@@ -231,6 +232,7 @@ INSERT INTERFACES HERE
             .as_bytes(),
         )
         .await?;
+        let document = parsed.document().clone();
         let mut proc = Processor::new();
         dom_utils::scan_dom(&document, &mut |h| proc.visit(h));
         proc.apply()?;
@@ -250,7 +252,7 @@ interface HTMLBlinkElement { ... }
 
     #[tokio::test]
     async fn interface_with_partial() -> io::Result<()> {
-        let document = parse_document_async(
+        let parsed = parse_document_async(
             r#"
 
 

@@ -265,6 +267,7 @@ INSERT INTERFACES HERE
             .as_bytes(),
         )
         .await?;
+        let document = parsed.document().clone();
         let mut proc = Processor::new();
         dom_utils::scan_dom(&document, &mut |h| proc.visit(h));
         proc.apply()?;
@@ -284,7 +287,7 @@ partial interface HTMLMarqueeElement io::Result<()> {
-        let document = parse_document_async(
+        let parsed = parse_document_async(
             r#"
 
 

@@ -298,6 +301,7 @@ INSERT INTERFACES HERE
             .as_bytes(),
         )
         .await?;
+        let document = parsed.document().clone();
         let mut proc = Processor::new();
         dom_utils::scan_dom(&document, &mut |h| proc.visit(h));
         proc.apply()?;
@@ -316,7 +320,7 @@ partial interface HTMLMarqueeElement io::Result<()> {
-        let document = parse_document_async(
+        let parsed = parse_document_async(
             r#"
 
 

@@ -329,6 +333,7 @@ INSERT INTERFACES HERE
             .as_bytes(),
         )
         .await?;
+        let document = parsed.document().clone();
         let mut proc = Processor::new();
         dom_utils::scan_dom(&document, &mut |h| proc.visit(h));
         proc.apply()?;
@@ -346,7 +351,7 @@ partial interface HTMLMarqueeElement io::Result<()> {
-        let document = parse_document_async(
+        let parsed = parse_document_async(
             r#"
 
 INSERT INTERFACES HERE
@@ -358,6 +363,7 @@ interface HTMLMarqueeElement { ... }
             .as_bytes(),
         )
         .await?;
+        let document = parsed.document().clone();
         let mut proc = Processor::new();
         dom_utils::scan_dom(&document, &mut |h| proc.visit(h));
         proc.apply()?;
@@ -376,7 +382,8 @@ interface HTMLMarqueeElement { ... }
 
     #[tokio::test]
     async fn no_marker() -> io::Result<()> {
-        let document = parse_document_async("".as_bytes()).await?;
+        let parsed = parse_document_async("".as_bytes()).await?;
+        let document = parsed.document().clone();
         let mut proc = Processor::new();
         dom_utils::scan_dom(&document, &mut |h| proc.visit(h));
         let result = proc.apply();
@@ -386,11 +393,12 @@ interface HTMLMarqueeElement { ... }
 
     #[tokio::test]
     async fn duplicate_marker() -> io::Result<()> {
-        let document = parse_document_async(
+        let parsed = parse_document_async(
             "
INSERT INTERFACES HERE
INSERT INTERFACES HERE
" .as_bytes(), ) .await?; + let document = parsed.document().clone(); let mut proc = Processor::new(); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); let result = proc.apply(); @@ -400,7 +408,7 @@ interface HTMLMarqueeElement { ... } #[tokio::test] async fn duplicate_dfn() -> io::Result<()> { - let document = parse_document_async( + let parsed = parse_document_async( r#"

@@ -411,6 +419,7 @@ interface HTMLMarqueeElement { ... }
             .as_bytes(),
         )
         .await?;
+        let document = parsed.document().clone();
         let mut proc = Processor::new();
         dom_utils::scan_dom(&document, &mut |h| proc.visit(h));
         let result = proc.apply();
diff --git a/src/main.rs b/src/main.rs
index a3ad9085..e31b71cd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -51,7 +51,8 @@ async fn run_preprocess() -> io::Result<()> {
     // Because parsing can jump around the tree a little, it's most reasonable
     // to just parse the whole document before doing any processing. Even for
     // the HTML standard, this doesn't take too long.
-    let document = parser::parse_document_async(tokio::io::stdin()).await?;
+    let parsed = parser::parse_document_async(tokio::io::stdin()).await?;
+    let document = parsed.document().clone();
 
     let mut boilerplate = boilerplate::Processor::new(cache_dir.clone(), source_dir.join("demos"));
     let mut represents = represents::Processor::new();
@@ -92,7 +93,8 @@ async fn run_preprocess() -> io::Result<()> {
 
 // The steps and considerations here are similar to run_preprocess.
 async fn run_postprocess() -> io::Result<()> {
-    let document = parser::parse_document_async(tokio::io::stdin()).await?;
+    let parsed = parser::parse_document_async(tokio::io::stdin()).await?;
+    let document = parsed.document().clone();
 
     let mut anchor_permanence = anchor_permanence::Processor::new();
 
diff --git a/src/parser.rs b/src/parser.rs
index 9b5d1b0e..a9cc5311 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -65,12 +65,12 @@ pub async fn parse_fragment_async(
     Ok(new_children)
 }
 
-pub async fn parse_document_async(r: R) -> io::Result {
+pub async fn parse_document_async(r: R) -> io::Result {
     let parser = driver::parse_document(RcDomWithLineNumbers::default(), create_error_opts());
     let dom = parse_internal_async(parser, r).await?;
     dom.create_error_from_parse_errors()?;
 
-    Ok(dom.document().clone())
+    Ok(dom)
 }
 
 fn create_error_opts() -> ParseOpts {
@@ -120,7 +120,8 @@ pub(crate) mod tests {
         // we're in. This is important because of the special rules
         // surrounding, e.g., tables. If you change this to use the body as context,
         // no element at all is emitted.
-        let document = parse_document_async("
".as_bytes()).await?; + let parsed = parse_document_async("
".as_bytes()).await?; + let document = parsed.document().clone(); let body = document.children.borrow()[1].children.borrow()[1].clone(); assert!(body.is_html_element(&local_name!("body"))); let table = body.children.borrow()[0].clone(); @@ -176,7 +177,8 @@ pub(crate) mod tests { #[tokio::test] async fn test_fragment_error_line_number() -> io::Result<()> { - let document = parse_document_async("".as_bytes()).await?; + let parsed = parse_document_async("".as_bytes()).await?; + let document = parsed.document().clone(); let body = document.children.borrow()[1].children.borrow()[1].clone(); assert!(body.is_html_element(&local_name!("body"))); let result = parse_fragment_async( @@ -194,7 +196,8 @@ pub(crate) mod tests { #[tokio::test] async fn test_fragment_error_exact() -> io::Result<()> { - let document = parse_document_async("".as_bytes()).await?; + let parsed = parse_document_async("".as_bytes()).await?; + let document = parsed.document().clone(); let body = document.children.borrow()[1].children.borrow()[1].clone(); assert!(body.is_html_element(&local_name!("body"))); let result = parse_fragment_async("&asdf;".as_bytes(), &body).await; diff --git a/src/rcdom_with_line_numbers.rs b/src/rcdom_with_line_numbers.rs index ccb4800e..11cdcce2 100644 --- a/src/rcdom_with_line_numbers.rs +++ b/src/rcdom_with_line_numbers.rs @@ -17,6 +17,13 @@ pub struct RcDomWithLineNumbers { current_line: Cell, } +#[cfg(test)] +impl std::fmt::Debug for RcDomWithLineNumbers { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("RcDomWithLineNumbers") + } +} + impl RcDomWithLineNumbers { // Expose out the document and errors from the inner RcDom pub fn document(&self) -> &Handle { diff --git a/src/represents.rs b/src/represents.rs index 73b24d94..fc29152d 100644 --- a/src/represents.rs +++ b/src/represents.rs @@ -128,7 +128,8 @@ mod tests { #[tokio::test] async fn test_represents() -> io::Result<()> { // Uses can occur either before or after. - let document = parse_document_async("

The chair element represents a seat\nat a table.

".as_bytes()).await?; + let parsed = parse_document_async("

The chair element represents a seat\nat a table.

".as_bytes()).await?; + let document = parsed.document().clone(); let mut proc = Processor::new(); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); proc.apply()?; @@ -142,7 +143,8 @@ mod tests { #[tokio::test] async fn test_represents_undefined() -> io::Result<()> { // Uses can occur either before or after. - let document = parse_document_async("

The chair element represents a seat\nat a table.

".as_bytes()).await?; + let parsed = parse_document_async("

The chair element represents a seat\nat a table.

".as_bytes()).await?; + let document = parsed.document().clone(); let mut proc = Processor::new(); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); let result = proc.apply(); diff --git a/src/self_link.rs b/src/self_link.rs index a10cb105..887092bd 100644 --- a/src/self_link.rs +++ b/src/self_link.rs @@ -97,10 +97,11 @@ mod tests { use super::*; use crate::dom_utils; use crate::parser::{parse_document_async, tests::serialize_for_test}; + use std::io; #[tokio::test] - async fn test_add_self_link() { - let document = parse_document_async( + async fn test_add_self_link() -> io::Result<()> { + let parsed = parse_document_async( r##"

@@ -110,12 +111,12 @@ mod tests { "## .as_bytes(), ) - .await - .unwrap(); + .await?; + let document = parsed.document().clone(); let mut processor = Processor::new(); dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); - processor.apply().unwrap(); + processor.apply()?; assert_eq!( serialize_for_test(&[document]), @@ -126,85 +127,90 @@ mod tests {
"## ); + Ok(()) } #[tokio::test] - async fn test_add_self_link_details() { - let document = parse_document_async( + async fn test_add_self_link_details() -> io::Result<()> { + let parsed = parse_document_async( r##"
Foo
"## .as_bytes(), ) - .await - .unwrap(); + .await?; + let document = parsed.document().clone(); let mut processor = Processor::new(); dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); - processor.apply().unwrap(); + processor.apply()?; assert_eq!( serialize_for_test(&[document]), r##"
Foo
"## ); + Ok(()) } #[tokio::test] - async fn test_add_self_link_details_no_summary() { - let document = parse_document_async( + async fn test_add_self_link_details_no_summary() -> io::Result<()> { + let parsed = parse_document_async( r##"
"##.as_bytes(), ) - .await - .unwrap(); + .await?; + let document = parsed.document().clone(); let mut processor = Processor::new(); dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); let result = processor.apply(); assert!(result.is_err()); + Ok(()) } #[tokio::test] - async fn test_add_self_link_already_present() { - let document = parse_document_async( + async fn test_add_self_link_already_present() -> io::Result<()> { + let parsed = parse_document_async( r##"
"## .as_bytes(), ) - .await - .unwrap(); + .await?; + let document = parsed.document().clone(); let mut processor = Processor::new(); dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); - processor.apply().unwrap(); + processor.apply()?; assert_eq!( serialize_for_test(&[document]), r##"
"## ); + Ok(()) } #[tokio::test] - async fn test_url_encoding() { - let document = parse_document_async( + async fn test_url_encoding() -> io::Result<()> { + let parsed = parse_document_async( r##"
"## .as_bytes(), ) - .await - .unwrap(); + .await?; + let document = parsed.document().clone(); let mut processor = Processor::new(); dom_utils::scan_dom(&document, &mut |h| processor.visit(h)); - processor.apply().unwrap(); + processor.apply()?; assert_eq!( serialize_for_test(&[document]), r##"
"## ); + Ok(()) } } diff --git a/src/tag_omission.rs b/src/tag_omission.rs index d0895ec3..ecba0198 100644 --- a/src/tag_omission.rs +++ b/src/tag_omission.rs @@ -203,7 +203,7 @@ mod tests { #[tokio::test] async fn test_simple() -> io::Result<()> { - let document = parse_document_async( + let parsed = parse_document_async( r#"

Optional tags

@@ -252,6 +252,7 @@ mod tests { .as_bytes(), ) .await?; + let document = parsed.document().clone(); let mut proc = Processor::new(); dom_utils::scan_dom(&document, &mut |h| proc.visit(h)); proc.apply()?;