Debian Codegraph @

extra_test.go

raw symbols SCIP
1 package goldmark_test
2
3 import (
4 "bytes"
5 "os"
6 "strconv"
7 "strings"
8 "testing"
9 "time"
10
11 . "github.com/yuin/goldmark"
12 "github.com/yuin/goldmark/ast"
13 "github.com/yuin/goldmark/parser"
14 "github.com/yuin/goldmark/renderer/html"
15 "github.com/yuin/goldmark/testutil"
16 "github.com/yuin/goldmark/text"
17 )
18
19 var testTimeoutMultiplier = 1.0
20
21 func init() {
22 m, err := strconv.ParseFloat(os.Getenv("GOLDMARK_TEST_TIMEOUT_MULTIPLIER"), 64)
23 if err == nil {
24 testTimeoutMultiplier = m
25 }
26 }
27
28 func TestExtras(t *testing.T) {
29 markdown := New(WithRendererOptions(
30 html.WithXHTML(),
31 html.WithUnsafe(),
32 ))
33 testutil.DoTestCaseFile(markdown, "_test/extra.txt", t, testutil.ParseCliCaseArg()...)
34 }
35
36 func TestEndsWithNonSpaceCharacters(t *testing.T) {
37 markdown := New(WithRendererOptions(
38 html.WithXHTML(),
39 html.WithUnsafe(),
40 ))
41 source := []byte("```\na\n```")
42 var b bytes.Buffer
43 err := markdown.Convert(source, &b)
44 if err != nil {
45 t.Error(err.Error())
46 }
47 if b.String() != "<pre><code>a\n</code></pre>\n" {
48 t.Errorf("%s \n---------\n %s", source, b.String())
49 }
50 }
51
52 func TestWindowsNewLine(t *testing.T) {
53 markdown := New(WithRendererOptions(
54 html.WithXHTML(),
55 ))
56 source := []byte("a \r\nb\n")
57 var b bytes.Buffer
58 err := markdown.Convert(source, &b)
59 if err != nil {
60 t.Error(err.Error())
61 }
62 if b.String() != "<p>a<br />\nb</p>\n" {
63 t.Errorf("%s\n---------\n%s", source, b.String())
64 }
65
66 source = []byte("a\\\r\nb\r\n")
67 var b2 bytes.Buffer
68 err = markdown.Convert(source, &b2)
69 if err != nil {
70 t.Error(err.Error())
71 }
72 if b2.String() != "<p>a<br />\nb</p>\n" {
73 t.Errorf("\n%s\n---------\n%s", source, b2.String())
74 }
75 }
76
77 type myIDs struct {
78 }
79
80 func (s *myIDs) Generate(value []byte, kind ast.NodeKind) []byte {
81 return []byte("my-id")
82 }
83
84 func (s *myIDs) Put(value []byte) {
85 }
86
87 func TestAutogeneratedIDs(t *testing.T) {
88 ctx := parser.NewContext(parser.WithIDs(&myIDs{}))
89 markdown := New(WithParserOptions(parser.WithAutoHeadingID()))
90 source := []byte("# Title1\n## Title2")
91 var b bytes.Buffer
92 err := markdown.Convert(source, &b, parser.WithContext(ctx))
93 if err != nil {
94 t.Error(err.Error())
95 }
96 if b.String() != `<h1 id="my-id">Title1</h1>
97 <h2 id="my-id">Title2</h2>
98 ` {
99 t.Errorf("%s\n---------\n%s", source, b.String())
100 }
101 }
102
103 func nowMillis() int64 {
104 // TODO: replace UnixNano to UnixMillis(drops Go1.16 support)
105 return time.Now().UnixNano() / 1000000
106 }
107
108 func TestDeepNestedLabelPerformance(t *testing.T) {
109 if testing.Short() {
110 t.Skip("skipping performance test in short mode")
111 }
112 markdown := New(WithRendererOptions(
113 html.WithXHTML(),
114 html.WithUnsafe(),
115 ))
116
117 started := nowMillis()
118 n := 50000
119 source := []byte(strings.Repeat("[", n) + strings.Repeat("]", n))
120 var b bytes.Buffer
121 _ = markdown.Convert(source, &b)
122 finished := nowMillis()
123 if (finished - started) > int64(5000*testTimeoutMultiplier) {
124 t.Error("Parsing deep nested labels took too long")
125 }
126 }
127
128 func TestManyProcessingInstructionPerformance(t *testing.T) {
129 if testing.Short() {
130 t.Skip("skipping performance test in short mode")
131 }
132 markdown := New(WithRendererOptions(
133 html.WithXHTML(),
134 html.WithUnsafe(),
135 ))
136
137 started := nowMillis()
138 n := 50000
139 source := []byte("a " + strings.Repeat("<?", n))
140 var b bytes.Buffer
141 _ = markdown.Convert(source, &b)
142 finished := nowMillis()
143 if (finished - started) > int64(5000*testTimeoutMultiplier) {
144 t.Error("Parsing processing instructions took too long")
145 }
146 }
147
148 func TestManyCDATAPerformance(t *testing.T) {
149 if testing.Short() {
150 t.Skip("skipping performance test in short mode")
151 }
152 markdown := New(WithRendererOptions(
153 html.WithXHTML(),
154 html.WithUnsafe(),
155 ))
156
157 started := nowMillis()
158 n := 50000
159 source := []byte(strings.Repeat("a <![CDATA[", n))
160 var b bytes.Buffer
161 _ = markdown.Convert(source, &b)
162 finished := nowMillis()
163 if (finished - started) > int64(5000*testTimeoutMultiplier) {
164 t.Error("Parsing processing instructions took too long")
165 }
166 }
167
168 func TestManyDeclPerformance(t *testing.T) {
169 if testing.Short() {
170 t.Skip("skipping performance test in short mode")
171 }
172 markdown := New(WithRendererOptions(
173 html.WithXHTML(),
174 html.WithUnsafe(),
175 ))
176
177 started := nowMillis()
178 n := 50000
179 source := []byte(strings.Repeat("a <!A ", n))
180 var b bytes.Buffer
181 _ = markdown.Convert(source, &b)
182 finished := nowMillis()
183 if (finished - started) > int64(5000*testTimeoutMultiplier) {
184 t.Error("Parsing processing instructions took too long")
185 }
186 }
187
188 func TestManyCommentPerformance(t *testing.T) {
189 if testing.Short() {
190 t.Skip("skipping performance test in short mode")
191 }
192 markdown := New(WithRendererOptions(
193 html.WithXHTML(),
194 html.WithUnsafe(),
195 ))
196
197 started := nowMillis()
198 n := 50000
199 source := []byte(strings.Repeat("a <!-- ", n))
200 var b bytes.Buffer
201 _ = markdown.Convert(source, &b)
202 finished := nowMillis()
203 if (finished - started) > int64(5000*testTimeoutMultiplier) {
204 t.Error("Parsing processing instructions took too long")
205 }
206 }
207
208 func TestDangerousURLStringCase(t *testing.T) {
209 markdown := New()
210
211 source := []byte(`[Basic](javascript:alert('Basic'))
212 [CaseInsensitive](JaVaScRiPt:alert('CaseInsensitive'))
213 `)
214 expected := []byte(`<p><a href="">Basic</a>
215 <a href="">CaseInsensitive</a></p>
216 `)
217 var b bytes.Buffer
218 _ = markdown.Convert(source, &b)
219 if !bytes.Equal(expected, b.Bytes()) {
220 t.Error("Dangerous URL should ignore cases:\n" + string(testutil.DiffPretty(expected, b.Bytes())))
221 }
222 }
223
224 func TestNestedATXHeadingAttributes(t *testing.T) {
225 markdown := New(WithParserOptions(
226 parser.WithAutoHeadingID(),
227 parser.WithAttribute(),
228 ))
229
230 source := []byte(`# Heading {test=[a, simple, "attribute", { with="nested values" }]}`)
231 c := parser.NewContext()
232 n := markdown.Parser().Parse(text.NewReader(source), parser.WithContext(c))
233 heading := n.FirstChild()
234 if heading.Kind() != ast.KindHeading {
235 t.Fatalf("expected first node to be heading, got %s", heading.Kind().String())
236 }
237 tv, ok := heading.Attribute([]byte("test"))
238 if !ok {
239 t.Fatal("expected to find attribute 'test'")
240 }
241 wv := tv.([]any)[3].(parser.Attributes)
242 _, ok = wv.Find([]byte("with"))
243 if !ok {
244 t.Fatal("expected to find nested attribute 'with'")
245 }
246
247 }
248
249 func TestDangerousURLWithReferences(t *testing.T) {
250 markdown := New(WithParserOptions(
251 parser.WithAutoHeadingID(),
252 parser.WithAttribute(),
253 ))
254
255 source := []byte(`[click](&#106;avascript:alert(1))`)
256 expected := []byte(`<p><a href="">click</a></p>
257 `)
258 var b bytes.Buffer
259 _ = markdown.Convert(source, &b)
260 if !bytes.Equal(expected, b.Bytes()) {
261 t.Error("Dangerous URL with references should not be rendered:\n" + string(testutil.DiffPretty(expected, b.Bytes())))
262 }
263
264 source = []byte(`<javascript:alert(document.domain)>`)
265 expected = []byte(`<p><a href="">javascript:alert(document.domain)</a></p>
266 `)
267 b.Reset()
268 _ = markdown.Convert(source, &b)
269 if !bytes.Equal(expected, b.Bytes()) {
270 t.Error("Dangerous autolink should not be rendered:\n" + string(testutil.DiffPretty(expected, b.Bytes())))
271 }
272
273 source = []byte(`![alt](javascript:alert(document.domain))`)
274 expected = []byte(`<p><img src="" alt="alt"></p>
275 `)
276 b.Reset()
277 _ = markdown.Convert(source, &b)
278 if !bytes.Equal(expected, b.Bytes()) {
279 t.Error("Dangerous image should not be rendered:\n" + string(testutil.DiffPretty(expected, b.Bytes())))
280 }
281 }