|
| 1 | +use html2rdf::parse; |
| 2 | +use oxiri::Iri; |
| 3 | +use oxrdf::Graph; |
| 4 | + |
| 5 | +mod utils; |
| 6 | + |
| 7 | +fn base() -> Iri<String> { |
| 8 | + Iri::parse("http://example.test/".to_string()).unwrap() |
| 9 | +} |
| 10 | + |
| 11 | +fn parse_html(html: &str) -> (Graph, Graph) { |
| 12 | + let mut output_graph = Graph::new(); |
| 13 | + let mut processor_graph = Graph::new(); |
| 14 | + parse(html, base(), &mut output_graph, &mut processor_graph).unwrap(); |
| 15 | + (output_graph, processor_graph) |
| 16 | +} |
| 17 | + |
| 18 | +#[test] |
| 19 | +fn invalid_blank_node_suffix_emits_warning() { |
| 20 | + let html = r#"<!DOCTYPE html> |
| 21 | + <html> |
| 22 | + <head><title>test</title></head> |
| 23 | + <body> |
| 24 | + <div about="[_:invalid blank]" property="http://example.test/p">value</div> |
| 25 | + </body> |
| 26 | + </html>"#; |
| 27 | + |
| 28 | + let (_output, processor) = parse_html(html); |
| 29 | + insta::assert_snapshot!(utils::serialize_graph(processor, base().as_str()), @r#" |
| 30 | + @base <http://example.test/> . |
| 31 | + @prefix rdf: <//www.w3.org/1999/02/22-rdf-syntax-ns#> . |
| 32 | + @prefix rdfa: <//www.w3.org/ns/rdfa#> . |
| 33 | + @prefix dc: <//purl.org/dc/terms/> . |
| 34 | + _:c14n0 a rdfa:UnresolvedCurie ; |
| 35 | + dc:description "Invalid CURIE: [_:invalid blank] (invalid blank node suffix: `invalid blank`)" . |
| 36 | + "#); |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn undefined_prefix_in_safe_curie_emits_warning() { |
| 41 | + let html = r#"<!DOCTYPE html> |
| 42 | + <html> |
| 43 | + <head><title>test</title></head> |
| 44 | + <body> |
| 45 | + <div about="[undefined:thing]" property="http://example.test/p">value</div> |
| 46 | + </body> |
| 47 | + </html>"#; |
| 48 | + |
| 49 | + let (_output, processor) = parse_html(html); |
| 50 | + insta::assert_snapshot!(utils::serialize_graph(processor, base().as_str()), @r#" |
| 51 | + @base <http://example.test/> . |
| 52 | + @prefix rdf: <//www.w3.org/1999/02/22-rdf-syntax-ns#> . |
| 53 | + @prefix rdfa: <//www.w3.org/ns/rdfa#> . |
| 54 | + @prefix dc: <//purl.org/dc/terms/> . |
| 55 | + _:c14n0 a rdfa:UnresolvedCurie ; |
| 56 | + dc:description "Invalid CURIE: [undefined:thing] (no such prefix defined)" . |
| 57 | + "#); |
| 58 | +} |
| 59 | + |
| 60 | +#[test] |
| 61 | +fn invalid_iri_from_curie_expansion_emits_warning() { |
| 62 | + let html = r#"<!DOCTYPE html> |
| 63 | + <html> |
| 64 | + <head><title>test</title></head> |
| 65 | + <body prefix="bad: http://example.test/invalid{iri}/"> |
| 66 | + <div about="[bad:thing]" property="http://example.test/p">value</div> |
| 67 | + </body> |
| 68 | + </html>"#; |
| 69 | + |
| 70 | + let (_output, processor) = parse_html(html); |
| 71 | + insta::assert_snapshot!(utils::serialize_graph(processor, base().as_str()), @r#" |
| 72 | + @base <http://example.test/> . |
| 73 | + @prefix rdf: <//www.w3.org/1999/02/22-rdf-syntax-ns#> . |
| 74 | + @prefix rdfa: <//www.w3.org/ns/rdfa#> . |
| 75 | + @prefix dc: <//purl.org/dc/terms/> . |
| 76 | + _:c14n0 a rdfa:UnresolvedCurie ; |
| 77 | + dc:description "Invalid CURIE: [bad:thing] (expanded to invalid IRI value <http://example.test/invalid{iri}/thing>)" . |
| 78 | + "#); |
| 79 | +} |
| 80 | + |
| 81 | +#[test] |
| 82 | +fn empty_safe_curie_silently_ignored() { |
| 83 | + let html = r#"<!DOCTYPE html> |
| 84 | + <html> |
| 85 | + <head><title>test</title></head> |
| 86 | + <body> |
| 87 | + <div about="[]" property="http://example.test/p">value</div> |
| 88 | + </body> |
| 89 | + </html>"#; |
| 90 | + |
| 91 | + let (_output, processor) = parse_html(html); |
| 92 | + insta::assert_snapshot!(utils::serialize_graph(processor, base().as_str()), @""); |
| 93 | +} |
| 94 | + |
| 95 | +#[test] |
| 96 | +fn missing_default_prefix_in_safe_curie_silently_ignored() { |
| 97 | + let html = r#"<!DOCTYPE html> |
| 98 | + <html> |
| 99 | + <head><title>test</title></head> |
| 100 | + <body> |
| 101 | + <div about="[:thing]" property="http://example.test/p">value</div> |
| 102 | + </body> |
| 103 | + </html>"#; |
| 104 | + |
| 105 | + let (_output, processor) = parse_html(html); |
| 106 | + insta::assert_snapshot!(utils::serialize_graph(processor, base().as_str()), @""); |
| 107 | +} |
0 commit comments