All checks were successful
check / check (pull_request) Successful in 31s
A preformatted block is not wrapped by the readers, so a table with long cells (the data retention table of the services page) became unreadable. A wide table with a header row is now a heading for each row and a bullet for each other cell, named by its column; without a header, one bullet per row. Refs #10
151 lines
7.5 KiB
Go
151 lines
7.5 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package gemtext
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// identity leaves every address as it is.
|
|
func identity(href string) (string, error) { return href, nil }
|
|
|
|
// convert converts html with addresses left as they are and no blocks.
|
|
func convert(t *testing.T, html string) string {
|
|
t.Helper()
|
|
got, err := Convert(html, Options{Link: identity})
|
|
if err != nil {
|
|
t.Fatalf("Convert(%q) error = %v", html, err)
|
|
}
|
|
return got
|
|
}
|
|
|
|
// TestConvert checks the gemtext of every kind of element the site uses.
|
|
func TestConvert(t *testing.T) {
|
|
tests := []struct {
|
|
name, html, want string
|
|
}{
|
|
{"headings", `<h1>Title</h1><h2 id="a">Sub</h2><h4>Deep</h4>`, "# Title\n\n## Sub\n\n### Deep\n"},
|
|
{"hero with line breaks", `<h1 class="hero">Technology.<br>Freedom.<br><span class="accent">Dissent.</span></h1>`, "# Technology. Freedom. Dissent.\n"},
|
|
{"links go after the paragraph",
|
|
`<p>Read the <a href="/a.html">first</a> and <strong>the <a href="https://x.org/b">second</a></strong> page.</p>`,
|
|
"Read the first and the second page.\n=> /a.html first\n=> https://x.org/b second\n"},
|
|
{"the same link once", `<p><a href="/a">A</a> and <a href="/a">A</a> again.</p>`, "A and A again.\n=> /a A\n"},
|
|
{"a paragraph that is only a link", `<p><a href="/x.html">All news</a></p>`, "=> /x.html All news\n"},
|
|
{"line breaks", `<p>One<br>Two<br />Three</p>`, "One\nTwo\nThree\n"},
|
|
{"whitespace is collapsed", "<p>a\n b\t c</p>", "a b c\n"},
|
|
{"entities", `<p>Tom & Jerry © <ok></p>`, "Tom & Jerry © <ok>\n"},
|
|
{"unordered list", `<ul><li>One</li><li>Two <a href="/c">link</a></li></ul>`, "* One\n* Two link\n=> /c link\n"},
|
|
{"a list of only links has no bullets", `<ul><li><a href="/a">A</a></li><li><a href="/b">B</a></li></ul>`, "=> /a A\n=> /b B\n"},
|
|
{"nested list is flattened", `<ul><li>A<ul><li>A1</li></ul></li><li>B</li></ul>`, "* A\n* A1\n* B\n"},
|
|
{"ordered list", `<ol><li>First</li><li>Second</li></ol>`, "* 1. First\n* 2. Second\n"},
|
|
{"quote", `<blockquote><p>One</p><p>Two <a href="/x">x</a></p></blockquote>`, "> One\n>\n> Two x\n=> /x x\n"},
|
|
{"note is a quote", `<p class="note" role="note">Careful</p>`, "> Careful\n"},
|
|
{"preformatted", "<pre><code>line1\n line2\n```\n</code></pre>", "```\nline1\n line2\n ```\n```\n"},
|
|
{"table with a header",
|
|
`<table class="data"><caption>Data</caption><thead><tr><th>Service</th><th>Kept</th></tr></thead><tbody><tr><td data-label="Service">Mail</td><td>30 days</td></tr><tr><td>Chat</td><td>1 year</td></tr></tbody></table>`,
|
|
"Data\n```\nService Kept\n------- -------\nMail 30 days\nChat 1 year\n```\n"},
|
|
{"table without a header", `<table><tr><td>a</td><td>bb</td></tr><tr><td>ccc</td><td>d</td></tr></table>`, "```\na bb\nccc d\n```\n"},
|
|
{"table links", `<table><tr><td><a href="/m">Mail</a></td><td>x</td></tr></table>`, "```\nMail x\n```\n=> /m Mail\n"},
|
|
{"image with a description", `<p><img src="/logo.svg" alt="kmalloc"></p>`, "=> /logo.svg kmalloc\n"},
|
|
{"image without a description is skipped", `<p><img src="/logo.svg" alt=""></p>`, ""},
|
|
{"lines that look like gemtext are protected", `<p># no</p><p>=> no</p><p>* no</p><p>> no</p>`, " # no\n\n => no\n\n * no\n\n > no\n"},
|
|
{"hidden and scripted things are skipped", `<script>x</script><style>y</style><p hidden>z</p><p aria-hidden="true">w</p><svg><path/></svg><p>kept</p>`, "kept\n"},
|
|
{"containers are transparent", `<div><section><p>A</p></section></div><div>Loose <em>text</em></div>`, "A\n\nLoose text\n"},
|
|
{"time and span are inline", `<p><time datetime="2026-11-14">14 November 2026</time> <span>by</span> squid</p>`, "14 November 2026 by squid\n"},
|
|
{"empty input", ``, ""},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := convert(t, tt.html); got != tt.want {
|
|
t.Errorf("Convert() =\n%q\nwant\n%q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestConvertLinkMapping checks that Link rewrites and drops addresses, and that its error stops the conversion.
|
|
func TestConvertLinkMapping(t *testing.T) {
|
|
link := func(href string) (string, error) {
|
|
switch {
|
|
case strings.HasPrefix(href, "#"):
|
|
return "", nil
|
|
case href == "/broken.html":
|
|
return "", errors.New("no such page")
|
|
}
|
|
target, _, _ := strings.Cut(href, "#")
|
|
return strings.Replace(target, ".html", ".gmi", 1), nil
|
|
}
|
|
got, err := Convert(`<p>See <a href="/a.html#x">a</a> and <a href="#top">top</a>.</p>`, Options{Link: link})
|
|
if err != nil || got != "See a and top.\n=> /a.gmi a\n" {
|
|
t.Errorf("Convert() = %q, %v", got, err)
|
|
}
|
|
_, err = Convert(`<p><a href="/broken.html">x</a></p>`, Options{Link: link})
|
|
if err == nil || !strings.Contains(err.Error(), "/broken.html") || !strings.Contains(err.Error(), "no such page") {
|
|
t.Errorf("error = %v, want one that names the address and the cause", err)
|
|
}
|
|
_, err = Convert(`<p><img src="/broken.html" alt="pic"></p>`, Options{Link: link})
|
|
if err == nil {
|
|
t.Error("a broken image address is not an error")
|
|
}
|
|
}
|
|
|
|
// TestConvertBlocks checks the placeholders of generated blocks.
|
|
func TestConvertBlocks(t *testing.T) {
|
|
block := func(name string) (string, error) {
|
|
if name != "news_list" {
|
|
return "", errors.New("unknown block " + name)
|
|
}
|
|
return "=> /a 2026-01-01 A\n=> /b 2025-12-31 B", nil
|
|
}
|
|
got, err := Convert(`<p>intro</p><gemblock name="news_list"></gemblock><p>end</p>`, Options{Link: identity, Block: block})
|
|
if err != nil || got != "intro\n\n=> /a 2026-01-01 A\n=> /b 2025-12-31 B\n\nend\n" {
|
|
t.Errorf("Convert() = %q, %v", got, err)
|
|
}
|
|
if _, err := Convert(`<gemblock name="other"></gemblock>`, Options{Link: identity, Block: block}); err == nil {
|
|
t.Error("an unknown block is not an error")
|
|
}
|
|
if _, err := Convert(`<gemblock name="news_list"></gemblock>`, Options{Link: identity}); err == nil {
|
|
t.Error("a block without a Block function is not an error")
|
|
}
|
|
}
|
|
|
|
// TestConvertRejectsBrokenHTML checks that markup that cannot be read is an error.
|
|
func TestConvertRejectsBrokenHTML(t *testing.T) {
|
|
if _, err := Convert(`<p>unclosed`, Options{Link: identity}); err == nil || !strings.Contains(err.Error(), "HTML") {
|
|
t.Errorf("error = %v, want one about the HTML", err)
|
|
}
|
|
}
|
|
|
|
// TestTextAndLink checks the helpers that write single lines.
|
|
func TestTextAndLink(t *testing.T) {
|
|
if got := Text(" two \n words "); got != "two words" {
|
|
t.Errorf("Text() = %q", got)
|
|
}
|
|
if got := Text("# not a heading"); got != " # not a heading" {
|
|
t.Errorf("Text() = %q, want a leading space", got)
|
|
}
|
|
if got := Link("/a.gmi", " A page "); got != "=> /a.gmi A page" {
|
|
t.Errorf("Link() = %q", got)
|
|
}
|
|
if got := Link("/a.gmi", ""); got != "=> /a.gmi" {
|
|
t.Errorf("Link() without a label = %q", got)
|
|
}
|
|
}
|
|
|
|
// TestConvertWideTable checks that a table that does not fit in 80 columns becomes a list of records, a
|
|
// heading for the first cell of each row and a bullet for each other cell, named by its column.
|
|
func TestConvertWideTable(t *testing.T) {
|
|
long := strings.Repeat("word ", 20) + "end"
|
|
html := `<table><caption>Data</caption><thead><tr><th>Service</th><th>Kept</th><th>Seen</th></tr></thead>` +
|
|
`<tbody><tr><td><a href="/m">Mail</a></td><td>` + long + `</td><td>all</td></tr><tr><td>Chat</td><td></td><td>some</td></tr></tbody></table>`
|
|
want := "Data\n\n### Mail\n* Kept: " + long + "\n* Seen: all\n\n### Chat\n* Seen: some\n=> /m Mail\n"
|
|
if got := convert(t, html); got != want {
|
|
t.Errorf("Convert() =\n%q\nwant\n%q", got, want)
|
|
}
|
|
noHeader := `<table><tr><td>a</td><td>` + long + `</td></tr><tr><td>b</td><td>c</td></tr></table>`
|
|
if got, want := convert(t, noHeader), "* a | "+long+"\n* b | c\n"; got != want {
|
|
t.Errorf("Convert() without a header =\n%q\nwant\n%q", got, want)
|
|
}
|
|
}
|