oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1package render_test
2
3import (
4 "strings"
5 "testing"
6
7 "amadan.net/oficina/slides/internal/slides/deck"
8 "amadan.net/oficina/slides/internal/slides/render"
9)
10
11// The editor's stage is served in ProseMirror's own markup, so a late
12// editor binding to a slide already shown replaces it with the same and
13// moves nothing (Astra's finding 3 on #27: a heading's trailing empty
14// paragraphs, joined with <br>, lost their last line, and grew back when
15// the editor mounted). internal/slidestest's TestFontALateEditorMovesNothing
16// holds this to the browser's own ProseMirror; this holds the shapes.
17func TestEditingMarkupIsProseMirrors(t *testing.T) {
18 r := func(s string, marks ...string) deck.Run {
19 run := deck.Run{T: s}
20 for _, m := range marks {
21 switch m {
22 case "b":
23 run.B = true
24 case "i":
25 run.I = true
26 case "c":
27 run.C = true
28 case "h":
29 run.H = true
30 case "a":
31 run.Href = "https://example.com/"
32 }
33 }
34 return run
35 }
36 const br = `<br class="ProseMirror-trailingBreak">`
37 for _, c := range []struct {
38 name string
39 block deck.Block
40 want string
41 }{
42 {"a heading with trailing empty paragraphs", deck.Block{Type: "heading", Text: deck.Text{{r("Title")}, {}, {}}},
43 `<div>Title</div><div>` + br + `</div><div>` + br + `</div></h1>`},
44 {"a heading with no paragraphs", deck.Block{Type: "heading"}, `<div>` + br + `</div></h1>`},
45 {"a text with an empty paragraph between", deck.Block{Type: "text", Text: deck.Text{{r("a")}, {}, {r("b")}}},
46 `<p>a</p><p>` + br + `</p><p>b</p></div>`},
47 {"a list with no items", deck.Block{Type: "list"}, `<li>` + br + `</li></ul>`},
48 {"a code block keeps its lines and marks", deck.Block{Type: "code", Text: deck.Text{{r("x", "b")}, {}}},
49 `<code><div><strong>x</strong></div><div>` + br + `</div></code></pre>`},
50 {"marks nest in the schema's order and stay open", deck.Block{Type: "text", Text: deck.Text{{r("a", "b"), r("b", "b", "i"), r("c", "c", "h", "a")}}},
51 `<p><strong>a<em>b</em></strong><code><mark><a href="https://example.com/">c</a></mark></code></p>`},
52 } {
53 d := &deck.Deck{Meta: deck.Meta{Theme: "paper"}, Slides: []deck.Slide{{ID: "s", Layout: "flow", Template: "blank", Blocks: []deck.Block{func() deck.Block {
54 b := c.block
55 b.ID, b.Region = "b", "body"
56 return b
57 }()}}}}
58 html, err := render.SlideWith(d, 0, render.Desktop, render.Options{Editing: true})
59 if err != nil {
60 t.Fatal(err)
61 }
62 if !strings.Contains(string(html), c.want) {
63 t.Errorf("%s: want %s in\n%s", c.name, c.want, html)
64 }
65 plain, _ := render.SlideWith(d, 0, render.Desktop, render.Options{})
66 if strings.Contains(string(plain), "ProseMirror") {
67 t.Errorf("%s: the reader's markup carries the editor's", c.name)
68 }
69 }
70}
71