| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // The document page draws structure around escaped text, and nothing in a |
| 9 | // document can become markup on the page. |
| 10 | // |
| 11 | // MUTATION-TESTED 2026-10-30, each reverted: inline() writing the text |
| 12 | // before a link, or after the last one, unescaped → "<script"; code fences |
| 13 | // unescaped → "<b>"; property values unescaped → "<i>". The URL pattern |
| 14 | // stopping at quotes is tidiness, not a guard: with quotes admitted the |
| 15 | // link is still escaped into its attribute, and that mutation stays green |
| 16 | // for the right reason. |
| 17 | func TestADocumentRendersAsADocumentAndNeverAsMarkup(t *testing.T) { |
| 18 | // PRESENCE: the structures are drawn. |
| 19 | for in, want := range map[string]string{ |
| 20 | "Name: Bill\nEmail: bill@example.com": "<dl rst-doc-props><dt>Name</dt><dd>Bill</dd><dt>Email</dt><dd>bill@example.com</dd></dl>", |
| 21 | "- one\n - two": `<ul rst-doc-list><li>one</li><li rst-depth="1">two</li></ul>`, |
| 22 | "1. first\n2. second": "<ol rst-doc-list><li>first</li><li>second</li></ol>", |
| 23 | "- [x] done": "<li rst-doc-todo>☑ done</li>", |
| 24 | "> careful": "<blockquote rst-doc-quote>careful</blockquote>", |
| 25 | "```\nx = 1\n```": "<pre rst-doc-code><code>x = 1</code></pre>", |
| 26 | "| a | b |\n|---|---|\n| 1 | 2 |": "<thead><tr><th>a</th><th>b</th></tr></thead><tbody><tr><td>1</td><td>2</td></tr></tbody>", |
| 27 | "See https://example.com/wiki.": `<a href="https://example.com/wiki" rel="noopener noreferrer nofollow">https://example.com/wiki</a>.`, |
| 28 | "12 Main Street\nDublin 2": "<p>12 Main Street<br>Dublin 2</p>", |
| 29 | "A **bold** claim": "<strong>bold</strong>", |
| 30 | "Note: one line is a paragraph, not a list": "<p>Note: one line is a paragraph, not a list</p>", |
| 31 | } { |
| 32 | if got := string(renderPassage(in)); !strings.Contains(got, want) { |
| 33 | t.Errorf("renderPassage(%q) = %q, want it to contain %q", in, got, want) |
| 34 | } |
| 35 | } |
| 36 | // ABSENCE: nothing a document says becomes markup. |
| 37 | for _, in := range []string{ |
| 38 | "<script>alert(1)</script>", |
| 39 | "<script>x</script> before https://example.com and after", |
| 40 | "See https://example.com/\"onmouseover=alert(1) here", |
| 41 | "javascript:alert(1)", |
| 42 | "", |
| 43 | "[click](javascript:alert(1))", |
| 44 | "```\n<b>x</b>\n```", |
| 45 | "Name: <i>Bill</i>\nRole: x", |
| 46 | "| <img src=x> | b |", |
| 47 | "- <svg onload=alert(1)>", |
| 48 | "> <iframe>", |
| 49 | } { |
| 50 | got := string(renderPassage(in)) |
| 51 | for _, bad := range []string{"<script", "<img", "<svg", "<iframe", "<b>", "<i>", `href="javascript`, "onmouseover=\"", `"onmouseover`} { |
| 52 | if strings.Contains(got, bad) { |
| 53 | t.Errorf("renderPassage(%q) = %q: %q reached the page", in, got, bad) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |