-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathhtml_renderer_test.go
More file actions
128 lines (112 loc) · 3.27 KB
/
html_renderer_test.go
File metadata and controls
128 lines (112 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package markdown
import (
"io"
"testing"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
func renderHookEmpty(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
return ast.GoToNext, true
}
func TestRenderNodeHookEmpty(t *testing.T) {
tests := []string{
"[foo](gopher://foo.bar)",
"",
"[foo](mailto://bar/)\n",
"",
}
htmlParams := html.RendererOptions{
RenderNodeHook: renderHookEmpty,
}
params := TestParams{
RendererOptions: htmlParams,
}
doTestsParam(t, tests, params)
}
func renderHookCodeBlock(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
_, ok := node.(*ast.CodeBlock)
if !ok {
return ast.GoToNext, false
}
io.WriteString(w, "code_replacement")
return ast.GoToNext, true
}
func TestRenderNodeHookCode(t *testing.T) {
tests := []string{
"a\n```go\ncode\n```\nb",
"<p>a</p>\ncode_replacement\n<p>b</p>\n",
}
opts := html.RendererOptions{
RenderNodeHook: renderHookCodeBlock,
}
params := TestParams{
RendererOptions: opts,
extensions: parser.CommonExtensions,
}
doTestsParam(t, tests, params)
}
func TestTagParagraphCode(t *testing.T) {
tests := []string{
"test",
"<div>test</div>\n",
}
opts := html.RendererOptions{
ParagraphTag: "div",
}
params := TestParams{
RendererOptions: opts,
extensions: parser.CommonExtensions,
}
doTestsParam(t, tests, params)
}
// TestCodeBlockClassCoalescing verifies that when a code block has both
// a language annotation and a custom class attribute, they are merged into
// a single class attribute. See https://github.com/gomarkdown/markdown/issues/209.
func TestCodeBlockClassCoalescing(t *testing.T) {
input := "``` yml\ntext: something\n```\n"
p := parser.NewWithExtensions(parser.FencedCode)
doc := p.Parse([]byte(input))
// Walk the AST and add a custom class to the CodeBlock node.
ast.WalkFunc(doc, func(node ast.Node, entering bool) ast.WalkStatus {
if cb, ok := node.(*ast.CodeBlock); ok {
cb.Attribute = &ast.Attribute{
Classes: [][]byte{[]byte("my-class")},
}
}
return ast.GoToNext
})
renderer := html.NewRenderer(html.RendererOptions{})
got := string(Render(doc, renderer))
// Before the fix, this produced two separate class= attributes:
// <code class="language-yml" class="my-class">
// After the fix, they should be coalesced:
// <code class="language-yml my-class">
expected := `<pre><code class="language-yml my-class">text: something
</code></pre>
`
if got != expected {
t.Errorf("CodeBlock class coalescing failed.\nExpected:\n%s\nGot:\n%s", expected, got)
}
}
func TestRenderNodeHookLinkAttrs(t *testing.T) {
tests := []string{
`[Click Me](gopher://foo.bar "Click Me")`,
`<p><a class="button" href="gopher://foo.bar" target="_blank" title="Click Me">Click Me</a></p>` + "\n",
}
opts := html.RendererOptions{
RenderNodeHook: func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
link, isLink := node.(*ast.Link)
if isLink {
link.AdditionalAttributes = append(link.AdditionalAttributes, `class="button"`)
}
return ast.GoToNext, false
},
}
params := TestParams{
Flags: html.HrefTargetBlank,
RendererOptions: opts,
extensions: parser.CommonExtensions,
}
doTestsParam(t, tests, params)
}