oxedyne/fe2o3/fe2o3_text/tests/html_page.rs
20.1 KiB, 49 runs
created by r1870400018:70665, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The page constructs of an encyclopaedia article -- figures, definition lists, captioned tables with |
| 2 | //! row headers, classed spans and divisions -- read from HTML into the document tree and written back |
| 3 | //! out as HTML of the same structure. |
| 4 | |
| 5 | use oxedyne_fe2o3_text::doc::{ |
| 6 | html, |
| 7 | markdown, |
| 8 | policy, |
| 9 | Align, |
| 10 | Attrs, |
| 11 | Block, |
| 12 | Cell, |
| 13 | Doc, |
| 14 | Inline, |
| 15 | Term, |
| 16 | djot, |
| 17 | }; |
| 18 | use oxedyne_fe2o3_text::doc::html::read; |
| 19 | |
| 20 | use oxedyne_fe2o3_core::prelude::*; |
| 21 | |
| 22 | fn t(s: &str) -> Inline { |
| 23 | Inline::Text(s.to_string()) |
| 24 | } |
| 25 | |
| 26 | fn class(c: &str) -> Attrs { |
| 27 | Attrs { id: None, classes: vec![c.to_string()], pairs: Vec::new() } |
| 28 | } |
| 29 | |
| 30 | fn keep() -> read::Opts { |
| 31 | read::Opts { keep_classed: true } |
| 32 | } |
| 33 | |
| 34 | fn parse_kept(src: &str) -> Outcome<Doc> { |
| 35 | Ok(Doc { blocks: res!(read::parse_with(src, &keep())) }) |
| 36 | } |
| 37 | |
| 38 | /// HTML as a comparison sees it: each tag's attributes in sorted order, every run of whitespace one |
| 39 | /// space, and none between two tags. |
| 40 | fn norm(src: &str) -> String { |
| 41 | let mut out = String::new(); |
| 42 | let mut rest = src; |
| 43 | while let Some(at) = rest.find('<') { |
| 44 | out.push_str(&rest[..at]); |
| 45 | let end = match rest[at..].find('>') { |
| 46 | Some(k) => at + k, |
| 47 | None => break, |
| 48 | }; |
| 49 | out.push_str(&tag(&rest[at + 1..end])); |
| 50 | rest = &rest[end + 1..]; |
| 51 | } |
| 52 | out.push_str(rest); |
| 53 | // Collapse whitespace, then drop what stands between two tags. |
| 54 | let mut flat = String::new(); |
| 55 | let mut ws = false; |
| 56 | for c in out.chars() { |
| 57 | if c.is_whitespace() { |
| 58 | ws = true; |
| 59 | } else { |
| 60 | if ws && !flat.is_empty() { |
| 61 | flat.push(' '); |
| 62 | } |
| 63 | ws = false; |
| 64 | flat.push(c); |
| 65 | } |
| 66 | } |
| 67 | flat.replace("> <", "><") |
| 68 | } |
| 69 | |
| 70 | /// One tag with its attributes sorted. Quoted values in the fixtures hold no `>` and no spaces that |
| 71 | /// matter beyond the one in a style, which the split below keeps inside its quotes. |
| 72 | fn tag(inner: &str) -> String { |
| 73 | let mut parts: Vec<String> = Vec::new(); |
| 74 | let mut cur = String::new(); |
| 75 | let mut q = false; |
| 76 | for c in inner.chars() { |
| 77 | match c { |
| 78 | '"' => { q = !q; cur.push(c); } |
| 79 | c if c.is_whitespace() && !q => { |
| 80 | if !cur.is_empty() { |
| 81 | parts.push(std::mem::take(&mut cur)); |
| 82 | } |
| 83 | } |
| 84 | _ => cur.push(c), |
| 85 | } |
| 86 | } |
| 87 | if !cur.is_empty() { |
| 88 | parts.push(cur); |
| 89 | } |
| 90 | if parts.is_empty() { |
| 91 | return "<>".to_string(); |
| 92 | } |
| 93 | let name = parts.remove(0); |
| 94 | parts.sort(); |
| 95 | let mut s = format!("<{}", name); |
| 96 | for p in parts { |
| 97 | s.push(' '); |
| 98 | s.push_str(&p); |
| 99 | } |
| 100 | s.push('>'); |
| 101 | s |
| 102 | } |
| 103 | |
| 104 | /// The input as the writer is expected to give it back: a `num` cell's column alignment comes out as |
| 105 | /// the logical style the writer has always used for `Align::End`, and an `i` as the `em` the tree |
| 106 | /// reads it to. |
| 107 | fn expected(src: &str) -> String { |
| 108 | norm(&src |
| 109 | .replace("class=\"num\"", "style=\"text-align: end\"") |
| 110 | .replace("<i>", "<em>") |
| 111 | .replace("</i>", "</em>")) |
| 112 | } |
| 113 | |
| 114 | // The fixtures, each the HTML of one construct as the pages write it. |
| 115 | const FIGURE: &str = "<figure class=\"fig\">\n<img src=\"img/paw.jpg\" alt=\"Dog offering a paw\">\n\ |
| 116 | <figcaption><span class=\"fig-caption\">Hachikō giving a paw.</span> \ |
| 117 | <span class=\"credit\">Unknown author, public domain</span></figcaption>\n</figure>"; |
| 118 | const DL_WRAPPED: &str = "<dl class=\"terms\">\n<div><dt>Jaw joint</dt><dd>The dentary meets the squamosal.</dd></div>\n\ |
| 119 | <div><dt>Middle ear</dt><dd>Three bones.</dd></div>\n</dl>"; |
| 120 | const DL_BARE: &str = "<dl class=\"terms\"><dt>Jaw joint</dt><dd>The dentary meets the squamosal.</dd>\ |
| 121 | <dt>Middle ear</dt><dd>Three bones.</dd></dl>"; |
| 122 | const DL_TWO_DEFS: &str = "<dl><dt>Bark</dt><dd>A tree's skin.</dd><dd>A dog's call.</dd></dl>"; |
| 123 | const ROW_HEADS: &str = "<table>\n<caption>Facts</caption>\n<tbody>\n\ |
| 124 | <tr><th scope=\"row\">Breed</th><td>Akita</td></tr>\n\ |
| 125 | <tr><th scope=\"row\">Sex</th><td>Male</td></tr>\n</tbody>\n</table>"; |
| 126 | const NUM_ALL: &str = "<table><tbody><tr><td>a</td><td class=\"num\">1</td></tr>\ |
| 127 | <tr><td>b</td><td class=\"num\">2</td></tr></tbody></table>"; |
| 128 | const NUM_SOME: &str = "<table><tbody><tr><td>a</td><td class=\"num\">1</td></tr>\ |
| 129 | <tr><td>b</td><td>two</td></tr></tbody></table>"; |
| 130 | const SUP_SUB: &str = "<p>H<sub>2</sub>O and E = mc<sup>2</sup></p>"; |
| 131 | const NAMED: &str = "<div class=\"table-wrap\"><div><p>Inner.</p></div><p>After.</p></div>\ |
| 132 | <p>A <span class=\"id\">1375772297</span> and <span>bare</span> span.</p>"; |
| 133 | const SPAN_ID: &str = "<p><span id=\"x\" lang=\"ja\">名</span></p>"; |
| 134 | const HOSTILE: &str = "<figure class=\"fig\"><img src=\"https://tracker.example/x.png\" alt=\"A picture\">\ |
| 135 | <figcaption><a href=\"javascript:alert(1)\">Click</a></figcaption></figure>\ |
| 136 | <dl class=\"terms\"><dt><a href=\"javascript:x\">Term</a></dt><dd>Def <sup>1</sup></dd></dl>\ |
| 137 | <table><caption><a href=\"javascript:y\">Cap</a></caption><tr><th scope=\"row\">A</th><td>1</td></tr></table>"; |
| 138 | const COUNTED: &str = "<figure><img src=\"a.png\" alt=\"x\"><figcaption>Two words</figcaption></figure>\ |
| 139 | <dl><dt>Term</dt><dd>One def.</dd></dl>\ |
| 140 | <table><caption>A caption</caption><tr><td>cell</td></tr></table>"; |
| 141 | const HANDLER: &str = "<p><span class=\"hl\" onmouseover=\"alert(1)\" style=\"color:red\">x</span></p>"; |
| 142 | const CREDIT: &str = "<p><span id=\"a\" class=\"credit evil\" lang=\"ja\">x</span></p>"; |
| 143 | |
| 144 | #[test] |
| 145 | fn test_a_figure_holds_its_image_and_caption_00() -> Outcome<()> { |
| 146 | let src = FIGURE; |
| 147 | let doc = res!(parse_kept(src)); |
| 148 | assert_eq!(doc.blocks, vec![Block::Figure { |
| 149 | attrs: class("fig"), |
| 150 | content: vec![Block::Para(vec![Inline::Image { |
| 151 | src: "img/paw.jpg".to_string(), |
| 152 | alt: "Dog offering a paw".to_string(), |
| 153 | attrs: Attrs::default(), |
| 154 | }])], |
| 155 | caption: vec![ |
| 156 | Inline::Span { attrs: class("fig-caption"), content: vec![t("Hachikō giving a paw.")] }, |
| 157 | t(" "), |
| 158 | Inline::Span { attrs: class("credit"), content: vec![t("Unknown author, public domain")] }, |
| 159 | ], |
| 160 | }]); |
| 161 | assert_eq!(norm(&html::render(&doc)), norm(src)); |
| 162 | // Without the option the figure stays a figure, and only the names go. |
| 163 | let plain = res!(html::parse(src)); |
| 164 | match &plain.blocks[0] { |
| 165 | Block::Figure { attrs, caption, .. } => { |
| 166 | assert!(attrs.is_empty()); |
| 167 | assert_eq!(caption, &vec![t("Hachikō giving a paw. Unknown author, public domain")]); |
| 168 | } |
| 169 | other => panic!("expected a figure, got {:?}", other), |
| 170 | } |
| 171 | Ok(()) |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn test_a_definition_list_reads_wrapped_and_bare_groups_01() -> Outcome<()> { |
| 176 | let wrapped = DL_WRAPPED; |
| 177 | let bare = DL_BARE; |
| 178 | let want = vec![Block::Terms { |
| 179 | attrs: class("terms"), |
| 180 | items: vec![ |
| 181 | Term { |
| 182 | term: vec![t("Jaw joint")], |
| 183 | defs: vec![vec![Block::Para(vec![t("The dentary meets the squamosal.")])]], |
| 184 | }, |
| 185 | Term { |
| 186 | term: vec![t("Middle ear")], |
| 187 | defs: vec![vec![Block::Para(vec![t("Three bones.")])]], |
| 188 | }, |
| 189 | ], |
| 190 | }]; |
| 191 | assert_eq!(res!(parse_kept(wrapped)).blocks, want); |
| 192 | assert_eq!(res!(parse_kept(bare)).blocks, want); |
| 193 | // The writer gives the wrapped form, which is the one a stylesheet can lay out by group. |
| 194 | assert_eq!(norm(&html::render(&Doc { blocks: want })), norm(wrapped)); |
| 195 | Ok(()) |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn test_a_term_may_carry_several_definitions_02() -> Outcome<()> { |
| 200 | let doc = res!(html::parse(DL_TWO_DEFS)); |
| 201 | match &doc.blocks[0] { |
| 202 | Block::Terms { items, .. } => { |
| 203 | assert_eq!(items.len(), 1); |
| 204 | assert_eq!(items[0].defs.len(), 2); |
| 205 | } |
| 206 | other => panic!("expected terms, got {:?}", other), |
| 207 | } |
| 208 | let out = html::render(&doc); |
| 209 | assert!(out.contains("<dd>A tree's skin.</dd><dd>A dog's call.</dd>"), "got: {}", out); |
| 210 | Ok(()) |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn test_a_table_keeps_its_caption_and_row_headers_03() -> Outcome<()> { |
| 215 | let src = ROW_HEADS; |
| 216 | let doc = res!(html::parse(src)); |
| 217 | match &doc.blocks[0] { |
| 218 | Block::Table { head, rows, caption, .. } => { |
| 219 | // A column of `th scope="row"` names rows, not columns, so the table has no head. |
| 220 | assert!(head.is_none(), "row headers were read as a header row"); |
| 221 | assert_eq!(caption, &vec![t("Facts")]); |
| 222 | assert_eq!(rows[0].0[0], Cell { content: vec![t("Breed")], header: true }); |
| 223 | assert_eq!(rows[0].0[1], Cell::new(vec![t("Akita")])); |
| 224 | } |
| 225 | other => panic!("expected a table, got {:?}", other), |
| 226 | } |
| 227 | assert_eq!(norm(&html::render(&doc)), norm(src)); |
| 228 | Ok(()) |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | fn test_a_num_column_aligns_to_the_end_04() -> Outcome<()> { |
| 233 | let all = res!(html::parse(NUM_ALL)); |
| 234 | match &all.blocks[0] { |
| 235 | Block::Table { cols, .. } => assert_eq!(cols, &vec![Align::None, Align::End]), |
| 236 | other => panic!("expected a table, got {:?}", other), |
| 237 | } |
| 238 | // A column only some of whose cells say `num` has no one alignment to give, and is given none. |
| 239 | let some = res!(html::parse(NUM_SOME)); |
| 240 | match &some.blocks[0] { |
| 241 | Block::Table { cols, .. } => assert_eq!(cols, &vec![Align::None, Align::None]), |
| 242 | other => panic!("expected a table, got {:?}", other), |
| 243 | } |
| 244 | Ok(()) |
| 245 | } |
| 246 | |
| 247 | #[test] |
| 248 | fn test_sup_and_sub_are_read_and_written_05() -> Outcome<()> { |
| 249 | let src = SUP_SUB; |
| 250 | let doc = res!(html::parse(src)); |
| 251 | assert_eq!(doc.blocks, vec![Block::Para(vec![ |
| 252 | t("H"), |
| 253 | Inline::Sub(vec![t("2")]), |
| 254 | t("O and E = mc"), |
| 255 | Inline::Sup(vec![t("2")]), |
| 256 | ])]); |
| 257 | assert_eq!(norm(&html::render(&doc)), norm(src)); |
| 258 | Ok(()) |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn test_keep_classed_keeps_only_named_spans_and_divs_06() -> Outcome<()> { |
| 263 | let src = NAMED; |
| 264 | // By default both are erased, as they always were. |
| 265 | let plain = res!(html::parse(src)); |
| 266 | assert_eq!(plain.blocks, vec![ |
| 267 | Block::Para(vec![t("Inner.")]), |
| 268 | Block::Para(vec![t("After.")]), |
| 269 | Block::Para(vec![t("A 1375772297 and bare span.")]), |
| 270 | ]); |
| 271 | // Kept, the named ones stay, and an unnamed `div` inside a named one closes only itself. |
| 272 | let kept = res!(parse_kept(src)); |
| 273 | assert_eq!(kept.blocks, vec![ |
| 274 | Block::Div { |
| 275 | attrs: class("table-wrap"), |
| 276 | content: vec![Block::Para(vec![t("Inner.")]), Block::Para(vec![t("After.")])], |
| 277 | }, |
| 278 | Block::Para(vec![ |
| 279 | t("A "), |
| 280 | Inline::Span { attrs: class("id"), content: vec![t("1375772297")] }, |
| 281 | t(" and bare span."), |
| 282 | ]), |
| 283 | ]); |
| 284 | // An id names an element as well as a class does, and other attributes travel as pairs. |
| 285 | let id = res!(parse_kept(SPAN_ID)); |
| 286 | assert_eq!(id.blocks, vec![Block::Para(vec![Inline::Span { |
| 287 | attrs: Attrs { |
| 288 | id: Some("x".to_string()), |
| 289 | classes: Vec::new(), |
| 290 | pairs: vec![("lang".to_string(), "ja".to_string())], |
| 291 | }, |
| 292 | content: vec![t("名")], |
| 293 | }])]); |
| 294 | Ok(()) |
| 295 | } |
| 296 | |
| 297 | #[test] |
| 298 | fn test_the_policy_walks_every_new_node_07() -> Outcome<()> { |
| 299 | let src = HOSTILE; |
| 300 | let doc = policy::apply(&res!(parse_kept(src)), &policy::Policy::default()); |
| 301 | let out = html::render(&doc); |
| 302 | assert!(!out.contains("javascript"), "a refused destination survived: {}", out); |
| 303 | assert!(!out.contains("tracker.example"), "a remote image survived: {}", out); |
| 304 | assert!(!out.contains("class="), "a stranger's names survived: {}", out); |
| 305 | for words in ["A picture", "Click", "Term", "Def <sup>1</sup>", "Cap", "<th scope=\"row\">A</th>"] { |
| 306 | assert!(out.contains(words), "lost {:?}: {}", words, out); |
| 307 | } |
| 308 | // A trusting policy keeps the names. |
| 309 | let trusted = html::render(&policy::apply(&res!(parse_kept(src)), &policy::Policy::trusting())); |
| 310 | assert!(trusted.contains("<figure class=\"fig\">"), "got: {}", trusted); |
| 311 | Ok(()) |
| 312 | } |
| 313 | |
| 314 | #[test] |
| 315 | fn test_the_new_nodes_are_counted_and_written_as_markdown_08() -> Outcome<()> { |
| 316 | let doc = res!(parse_kept(COUNTED)); |
| 317 | // The alt text, the caption, the term and definition, the table's caption and cell. |
| 318 | assert_eq!(doc.word_count(), 1 + 2 + 1 + 2 + 2 + 1); |
| 319 | let md = markdown::write::render(&doc); |
| 320 | for words in ["", "Two words", "**Term**", "One def.", "A caption", "| cell |"] { |
| 321 | assert!(md.contains(words), "lost {:?}: {}", words, md); |
| 322 | } |
| 323 | Ok(()) |
| 324 | } |
| 325 | |
| 326 | /// The body of an article, modelled on the Hachikō and Mammal pages: sections' content as the editor |
| 327 | /// stores it, each construct as those pages write it. |
| 328 | /// |
| 329 | /// One thing the pages write is not carried, and the fixture leaves it out: the `section` around each |
| 330 | /// part, which the application splits a page by before it reads one. An `img`'s `class`, `width` and |
| 331 | /// `height` are carried, as the image's [`Attrs`]. |
| 332 | const ARTICLE: &str = r#" |
| 333 | <h2>Life story</h2> |
| 334 | <p>Hachikō, a creamy-white Akita, was born in mid-November 1923, at a farm in Ōdate, Akita Prefecture, Japan. His father was Ōshinai-yama-gō (<i>'Ōshinai mountain'</i>).</p> |
| 335 | <figure class="fig"> |
| 336 | <img src="img/paw.jpg" width="896" height="654" alt="Dog offering a paw"> |
| 337 | <figcaption><span class="fig-caption">Hachikō giving a paw.</span> <span class="credit">Unknown author, public domain</span></figcaption> |
| 338 | </figure> |
| 339 | <figure class="fig"> |
| 340 | <img class="diagram" src="img/skull.png" width="568" height="260" alt="Diagram of a synapsid skull with one temporal opening"> |
| 341 | <figcaption><span class="fig-caption">The original synapsid skull structure contains one temporal opening.</span> <span class="credit">Preto(m), CC BY-SA 3.0</span></figcaption> |
| 342 | </figure> |
| 343 | <p>Text from <a href="https://en.wikipedia.org/w/index.php?title=Hachik%C5%8D&oldid=1375772297">Hachikō</a>, revision <span class="id">1375772297</span>, CO<sub>2</sub> and m<sup>2</sup>.</p> |
| 344 | <h2>Facts</h2> |
| 345 | <div class="table-wrap"> |
| 346 | <table> |
| 347 | <tbody> |
| 348 | <tr><th scope="row">Breed</th><td>Akita</td></tr> |
| 349 | <tr><th scope="row">Cause of death</th><td>Terminal carcinosarcoma and <i>Dirofilaria immitis</i></td></tr> |
| 350 | </tbody> |
| 351 | </table> |
| 352 | </div> |
| 353 | <div class="table-wrap"> |
| 354 | <table> |
| 355 | <caption>Counts of mammal species</caption> |
| 356 | <thead> |
| 357 | <tr><th scope="col">Source</th><th scope="col" class="num">Year</th><th scope="col" class="num">Species</th></tr> |
| 358 | </thead> |
| 359 | <tbody> |
| 360 | <tr><td><i>Mammal Species of the World</i></td><td class="num">2006</td><td class="num">5,416</td></tr> |
| 361 | <tr><td>IUCN Global Mammal Assessment</td><td class="num">2008</td><td class="num">5,488</td></tr> |
| 362 | </tbody> |
| 363 | </table> |
| 364 | </div> |
| 365 | <h2>Places</h2> |
| 366 | <dl class="terms"> |
| 367 | <div><dt>Ōdate, Akita Prefecture</dt><dd>Hachikō's hometown. A statue stands in front of Ōdate Station.</dd></div> |
| 368 | <div><dt>Shibuya Station, Tokyo</dt><dd>The second statue, unveiled on August 15, 1948, still stands.</dd></div> |
| 369 | </dl> |
| 370 | <blockquote> |
| 371 | <p>A quoted line, with <strong>strength</strong> and <em>emphasis</em>.</p> |
| 372 | </blockquote> |
| 373 | <h2>References</h2> |
| 374 | <ol> |
| 375 | <li>Itoh, Mayumi (2017). <i>Hachikō: Solving Twenty Mysteries about the Most Famous Dog in Japan</i>. pp. 22–23.</li> |
| 376 | <li>“How many species of mammals are there?”. <i>Journal of Mammalogy</i>. 99 (1): 1–14.</li> |
| 377 | </ol> |
| 378 | "#; |
| 379 | |
| 380 | #[test] |
| 381 | fn test_an_article_body_round_trips_09() -> Outcome<()> { |
| 382 | let doc = res!(parse_kept(ARTICLE)); |
| 383 | let out = html::render(&doc); |
| 384 | assert_eq!(norm(&out), expected(ARTICLE), "\n--- rendered ---\n{}", out); |
| 385 | // And the tree is a fixed point: reading what was written gives the tree back. |
| 386 | assert_eq!(res!(parse_kept(&out)), doc); |
| 387 | // The diagram's names are in the tree, not merely passed through as text. |
| 388 | let diagram = Attrs { |
| 389 | id: None, |
| 390 | classes: vec!["diagram".to_string()], |
| 391 | pairs: vec![ |
| 392 | ("width".to_string(), "568".to_string()), |
| 393 | ("height".to_string(), "260".to_string()), |
| 394 | ], |
| 395 | }; |
| 396 | let found = doc.blocks.iter().any(|b| match b { |
| 397 | Block::Figure { content, .. } => content.iter().any(|p| match p { |
| 398 | Block::Para(run) => run.iter().any(|i| match i { |
| 399 | Inline::Image { attrs, .. } => *attrs == diagram, |
| 400 | _ => false, |
| 401 | }), |
| 402 | _ => false, |
| 403 | }), |
| 404 | _ => false, |
| 405 | }); |
| 406 | assert!(found, "the diagram's class and size were not carried: {:?}", doc.blocks); |
| 407 | // The plain reader still reads an image as its source and alt text alone. |
| 408 | let plain = res!(read::parse("<p><img class=\"diagram\" src=\"a.png\" width=\"5\" alt=\"x\"></p>")); |
| 409 | assert_eq!(plain, vec![Block::Para(vec![Inline::Image { |
| 410 | src: "a.png".to_string(), |
| 411 | alt: "x".to_string(), |
| 412 | attrs: Attrs::default(), |
| 413 | }])]); |
| 414 | Ok(()) |
| 415 | } |
| 416 | |
| 417 | #[test] |
| 418 | fn test_the_writer_refuses_script_and_style_keys_10() -> Outcome<()> { |
| 419 | let span = |k: &str| Doc { blocks: vec![Block::Para(vec![Inline::Span { |
| 420 | attrs: Attrs { |
| 421 | id: None, |
| 422 | classes: vec!["hl".to_string()], |
| 423 | pairs: vec![(k.to_string(), "alert(1)".to_string())], |
| 424 | }, |
| 425 | content: vec![t("x")], |
| 426 | }])] }; |
| 427 | // Whatever a trusting policy lets through, the writer will not write a handler or a style. |
| 428 | for k in ["onmouseover", "OnClick", "style", "x y", "data\"a", "class", "id", "1a"] { |
| 429 | let out = html::render(&policy::apply(&span(k), &policy::Policy::trusting())); |
| 430 | assert!(!out.contains("alert"), "{:?} was written: {}", k, out); |
| 431 | assert!(out.contains("<span class=\"hl\">x</span>"), "got: {}", out); |
| 432 | } |
| 433 | let ok = html::render(&span("data-k")); |
| 434 | assert!(ok.contains("data-k=\"alert(1)\""), "a plain key was refused: {}", ok); |
| 435 | // Read from HTML, the handler never reaches the page either. |
| 436 | let read = res!(parse_kept(HANDLER)); |
| 437 | let out = html::render(&read); |
| 438 | assert!(!out.contains("alert") && !out.contains("color"), "got: {}", out); |
| 439 | Ok(()) |
| 440 | } |
| 441 | |
| 442 | #[test] |
| 443 | fn test_the_policy_allows_attributes_by_kind_11() -> Outcome<()> { |
| 444 | let doc = res!(parse_kept(CREDIT)); |
| 445 | let mut p = policy::Policy::default(); |
| 446 | p.classes = policy::AllowList::Only(vec!["credit".to_string()]); |
| 447 | let out = html::render(&policy::apply(&doc, &p)); |
| 448 | assert!(out.contains("<span class=\"credit\">x</span>"), "got: {}", out); |
| 449 | p.ids = true; |
| 450 | p.pairs = policy::AllowList::Only(vec!["lang".to_string()]); |
| 451 | let out = html::render(&policy::apply(&doc, &p)); |
| 452 | assert!(out.contains("id=\"a\"") && out.contains("lang=\"ja\"") && !out.contains("evil"), "got: {}", out); |
| 453 | // Nothing permitted, and the span is unwrapped. |
| 454 | let out = html::render(&policy::apply(&doc, &policy::Policy::default())); |
| 455 | assert!(out.contains("<p>x</p>"), "got: {}", out); |
| 456 | Ok(()) |
| 457 | } |
| 458 | |
| 459 | /// Every fixture above, read keeping its names, written as Djot and read back: the tree an editor |
| 460 | /// opens must be the tree that was stored. |
| 461 | #[test] |
| 462 | fn test_the_corpus_round_trips_through_djot_12() -> Outcome<()> { |
| 463 | let corpus = [ |
| 464 | FIGURE, DL_WRAPPED, DL_BARE, DL_TWO_DEFS, ROW_HEADS, NUM_ALL, NUM_SOME, SUP_SUB, NAMED, |
| 465 | SPAN_ID, HOSTILE, COUNTED, HANDLER, CREDIT, ARTICLE, |
| 466 | ]; |
| 467 | for (n, src) in corpus.iter().enumerate() { |
| 468 | res!(via_djot(&res!(parse_kept(src)), &fmt!("fixture {}", n))); |
| 469 | } |
| 470 | // The row headers are written in their short form. |
| 471 | let text = djot::render(&res!(parse_kept(ROW_HEADS))); |
| 472 | assert!(text.starts_with("{heads=\"rows\"}\n"), "got:\n{}", text); |
| 473 | Ok(()) |
| 474 | } |
| 475 | |
| 476 | /// The bodies of the Zoapedia pages, read where the application keeps them, through the same path. |
| 477 | /// Ignored by default, since the pages live outside this repository. |
| 478 | #[test] |
| 479 | #[ignore] |
| 480 | fn test_the_zoapedia_page_bodies_round_trip_through_djot_13() -> Outcome<()> { |
| 481 | let home = res!(std::env::var("HOME")); |
| 482 | let dir = std::path::Path::new(&home).join("usr/code/web/apps/zoapedia/tools/pages"); |
| 483 | let mut n = 0; |
| 484 | for entry in res!(std::fs::read_dir(&dir)) { |
| 485 | let path = res!(entry).path(); |
| 486 | if path.extension().map_or(true, |e| e != "json") { |
| 487 | continue; |
| 488 | } |
| 489 | let json = res!(std::fs::read_to_string(&path)); |
| 490 | let body = res!(json_field(&json, "body")); |
| 491 | let doc = res!(parse_kept(&body)); |
| 492 | res!(via_djot(&doc, &fmt!("{}", path.display()))); |
| 493 | n += 1; |
| 494 | } |
| 495 | assert_eq!(n, 25, "expected the 25 pages in {}", dir.display()); |
| 496 | Ok(()) |
| 497 | } |
| 498 | |
| 499 | fn via_djot(doc: &Doc, name: &str) -> Outcome<()> { |
| 500 | let text = djot::render(doc); |
| 501 | let back = res!(djot::parse(&text)); |
| 502 | if back != *doc { |
| 503 | let at = doc.blocks.iter().zip(&back.blocks).position(|(a, b)| a != b).unwrap_or(0); |
| 504 | return Err(err!("{}: the Djot round trip changed the tree at block {}.\n--- djot ---\n{}\n\ |
| 505 | --- want ---\n{:?}\n--- got ---\n{:?}", |
| 506 | name, at, text, doc.blocks.get(at), back.blocks.get(at); Invalid, Output)); |
| 507 | } |
| 508 | Ok(()) |
| 509 | } |
| 510 | |
| 511 | /// The string value of a top-level field of a JSON object, its escapes decoded: enough of JSON to |
| 512 | /// read a page file, and no more. |
| 513 | fn json_field(json: &str, key: &str) -> Outcome<String> { |
| 514 | let bad = || err!("No string field {:?} could be read.", key; Invalid, Input); |
| 515 | let at = res!(json.find(&fmt!("\"{}\"", key)).ok_or_else(bad)); |
| 516 | let rest = json[at + key.len() + 2..].trim_start(); |
| 517 | let rest = res!(rest.strip_prefix(':').ok_or_else(bad)).trim_start(); |
| 518 | let mut chars = res!(rest.strip_prefix('"').ok_or_else(bad)).chars(); |
| 519 | let mut out = String::new(); |
| 520 | let mut high: Option<u32> = None; // a high surrogate awaiting its pair |
| 521 | while let Some(c) = chars.next() { |
| 522 | match c { |
| 523 | '"' => return Ok(out), |
| 524 | '\\' => match res!(chars.next().ok_or_else(bad)) { |
| 525 | 'n' => out.push('\n'), |
| 526 | 't' => out.push('\t'), |
| 527 | 'r' => out.push('\r'), |
| 528 | 'b' => out.push('\u{8}'), |
| 529 | 'f' => out.push('\u{c}'), |
| 530 | 'u' => { |
| 531 | let hex: String = (0..4).filter_map(|_| chars.next()).collect(); |
| 532 | let v = res!(u32::from_str_radix(&hex, 16)); |
| 533 | let code = match (high.take(), v) { |
| 534 | (None, 0xD800..=0xDBFF) => { |
| 535 | high = Some(v); |
| 536 | continue; |
| 537 | } |
| 538 | (Some(h), 0xDC00..=0xDFFF) => 0x10000 + ((h - 0xD800) << 10) + (v - 0xDC00), |
| 539 | (_, v) => v, |
| 540 | }; |
| 541 | out.push(res!(char::from_u32(code).ok_or_else(bad))); |
| 542 | } |
| 543 | other => out.push(other), |
| 544 | }, |
| 545 | c => out.push(c), |
| 546 | } |
| 547 | } |
| 548 | Err(bad()) |
| 549 | } |