blob: f3385252658187c09e1adafe5272450ca5c3351b (
plain) (
blame)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
import Foundation
import Testing
@testable import Hutch
struct ReadmeViewTests {
@Test
func sanitizedReadmeLinkURLStringRejectsUnexpectedSchemes() {
#expect(sanitizedReadmeLinkURLString("javascript:alert(1)") == nil)
#expect(sanitizedReadmeLinkURLString("file:///tmp/readme") == nil)
#expect(sanitizedReadmeLinkURLString("data:text/html;base64,SGVsbG8=") == nil)
}
@Test
func processInlineDropsUnsafeMarkdownLinks() {
let rendered = processInline("[click me](javascript:alert)")
#expect(rendered == "click me")
#expect(!rendered.contains("href="))
#expect(!rendered.contains("javascript:"))
}
@Test
func sanitizedReadmeLinkURLStringAllowsExpectedDestinations() {
#expect(sanitizedReadmeLinkURLString("https://example.com/docs?q=1") == "https://example.com/docs?q=1")
#expect(sanitizedReadmeLinkURLString("mailto:[email protected]") == "mailto:[email protected]")
#expect(sanitizedReadmeLinkURLString("#readme") == "#readme")
}
}
struct MarkdownRenderingTests {
@Test
func markdownOrderedList() {
let html = markdownToHTML("1. First\n2. Second")
#expect(html.contains("<ol>"))
#expect(html.contains("<li>"))
}
@Test
func markdownBlockquote() {
let html = markdownToHTML("> This is a quote")
#expect(html.contains("<blockquote>"))
}
@Test
func markdownTable() {
let input = "| A | B |\n|---|---|\n| 1 | 2 |"
let html = markdownToHTML(input)
#expect(html.contains("<table>"))
#expect(html.contains("<th>"))
}
@Test
func markdownTableAlignment() {
let input = "| Left | Center | Right |\n|:-----|:------:|------:|\n| a | b | c |"
let html = markdownToHTML(input)
#expect(html.contains("text-align: left;"))
#expect(html.contains("text-align: center;"))
#expect(html.contains("text-align: right;"))
}
@Test
func markdownStrikethrough() {
let html = markdownToHTML("~~deleted~~")
#expect(html.contains("<del>"))
}
@Test
func markdownDeepHeadings() {
let html = markdownToHTML("#### Level 4")
#expect(html.contains("<h4>"))
}
@Test
func markdownHardWrapNormalization() {
let html = markdownToHTML("line one\nline two")
#expect(!html.contains("line one\nline two"))
#expect(html.contains("line one"))
#expect(html.contains("line two"))
}
@Test
func markdownSoftBreakIsSpace() {
let html = markdownToHTML("word one\nword two")
#expect(html.contains("word one word two") || (html.contains("word one") && html.contains("word two")))
#expect(!html.contains("<br>"))
}
@Test
func markdownUnsafeLinkDropped() {
let html = markdownToHTML("[click](javascript:alert(1))")
#expect(!html.contains("href="))
#expect(!html.contains("javascript:"))
}
@Test
func markdownImageRenders() {
let html = markdownToHTML("")
#expect(html.contains("<img src=\"https://example.com/logo.png\" alt=\"logo\">"))
#expect(!html.contains(#"\"#))
}
@Test
func markdownLinkedImageRendersAnchor() {
let html = markdownToHTML("[](https://example.com/build)")
#expect(html.contains("<a href=\"https://example.com/build\">"))
#expect(html.contains("<img src=\"https://example.com/badge.png\" alt=\"badge\">"))
#expect(!html.contains(#"\"#))
}
@Test
func markdownInlineCodeEscaping() {
let html = processInline("`<b>`")
#expect(html.contains("<code>"))
#expect(html.contains("<b>"))
#expect(!html.contains("<b>"))
}
@Test
func markdownPlainEmailAutolinks() {
let html = processInline("Contact hello@cleberg.net")
#expect(html.contains(#"href="mailto:[email protected]""#))
#expect(html.contains(">hello@cleberg.net</a>"))
}
@Test
func markdownImageQueryStringPreservesAmpersands() {
let html = processInline("")
#expect(html.contains("metric=security_rating"))
#expect(!html.contains("amp;metric"))
#expect(html.contains("<img"))
}
@Test
func markdownLinkedImagesWithQueryStringsRenderAllImages() {
let html = markdownToHTML("""
[](https://builds.sr.ht/~ccleberg/Hutch?)
[](https://sonarcloud.io/summary/new_code?id=ccleberg_Hutch)
[](https://sonarcloud.io/summary/new_code?id=ccleberg_Hutch)
""")
#expect(html.contains("Hutch.svg"))
#expect(html.contains("metric=security_rating"))
#expect(html.contains("metric=reliability_rating"))
#expect(!html.contains("amp;amp;"))
}
@Test
func markdownAllowsSafeInlineHTML() {
let html = processInline(#"<strong>Bold</strong> <a href="https://example.com">Link</a>"#)
#expect(html.contains("<strong>Bold</strong>"))
#expect(html.contains(#"<a href="https://example.com">Link</a>"#))
}
@Test
func markdownProtectedTokensDoNotLeak() {
let html = processInline(#"<a href="https://example.com">Back to top</a> `yoshi [ARG] <FILE>`"#)
#expect(!html.contains("ZZPROTECTED"))
#expect(html.contains(#"<a href="https://example.com">Back to top</a>"#))
#expect(html.contains("<code>yoshi [ARG] <FILE></code>"))
}
}
struct OrgRenderingTests {
@Test
func orgDeepHeading() {
let html = orgToHTML("**** Level 4 Heading")
#expect(html.contains("<h4>"))
}
@Test
func orgCommentLinesIgnored() {
let html = orgToHTML("# This is a comment\nNormal text")
#expect(!html.contains("This is a comment"))
#expect(html.contains("Normal text"))
}
@Test
func orgStrikethrough() {
let html = orgToHTML("+deleted text+")
#expect(html.contains("<del>"))
}
@Test
func orgExampleBlock() {
let html = orgToHTML("#+begin_example\nhello world\n#+end_example")
#expect(html.contains("<pre><code>"))
#expect(html.contains("hello world"))
}
@Test
func orgTitleKeyword() {
let html = orgToHTML("#+TITLE: My Document\nBody text")
#expect(html.contains("org-title"))
#expect(html.contains("My Document"))
}
@Test
func orgItalicDoesNotMatchURLPaths() {
let html = orgToHTML("[[https://example.com/path/to/file]]")
#expect(!html.contains("<em>"))
}
@Test
func orgWrappedBulletNormalizesLines() {
let html = orgToHTML("- First line\n continues here")
#expect(html.contains("<li>First line continues here</li>"))
}
@Test
func orgHeaderKeywordsIgnored() {
let html = orgToHTML("""
#+OPTIONS: toc:nil
#+PROPERTY: header-args :results output
Body text
""")
#expect(!html.contains("#+OPTIONS"))
#expect(!html.contains("#+PROPERTY"))
#expect(html.contains("<p>Body text</p>"))
}
@Test
func orgVerseBlock() {
let html = orgToHTML("""
#+begin_verse
There is a line.
And an indented line.
#+end_verse
""")
#expect(html.contains(#"<blockquote class="org-verse">"#))
#expect(html.contains("There is a line."))
#expect(html.contains("And an indented line."))
#expect(!html.contains("#+begin_verse"))
}
@Test
func orgNamedBlockRendersCaption() {
let html = orgToHTML("""
#+CAPTION: Build output
#+NAME: build-log
#+begin_example
hello world
#+end_example
""")
#expect(html.contains(#"<figure class="org-block" id="build-log">"#))
#expect(html.contains("<figcaption>Build output</figcaption>"))
#expect(!html.contains("#+CAPTION"))
#expect(!html.contains("#+NAME"))
}
@Test
func orgLinkedImageRenders() {
let html = orgToHTML("[[https://example.com][[https://img.cleberg.net/apps/hutch/screenshots/ipad/01_patch.jpg]]]")
#expect(html.contains(#"<a href="https://example.com">"#))
#expect(html.contains(#"<img src="https://img.cleberg.net/apps/hutch/screenshots/ipad/01_patch.jpg" alt="">"#))
}
@Test
func orgLinkedRelativeImageRenders() {
let html = orgToHTML(
"[[https://example.com/docs][[./images/badge.svg]]]",
imageURLResolver: { source in
source == "./images/badge.svg" ? "https://git.sr.ht/~ccleberg/Hutch/blob/HEAD/images/badge.svg" : nil
}
)
#expect(html.contains(#"<a href="https://example.com/docs">"#))
#expect(html.contains(#"<img src="https://git.sr.ht/~ccleberg/Hutch/blob/HEAD/images/badge.svg" alt="">"#))
}
@Test
func orgNestedBulletListRendersNestedMarkup() {
let html = orgToHTML("""
* Lists
** Unordered
- First bullet
- Second bullet
- Third bullet with /italic/ and *bold*
- Bullet with wrapped
continuation line
- Bullet with nested list
- Nested child one
- Nested child two
- Bullet with inline code ~let x = 1~
""")
#expect(html.contains("<ul>"))
#expect(html.contains("<li>Bullet with nested list\n<ul>"))
#expect(html.contains("<li>Nested child one</li>"))
#expect(html.contains("<li>Nested child two</li>"))
#expect(html.contains("<li>Bullet with wrapped continuation line</li>"))
}
@Test
func orgTableAlignmentRendersStyles() {
let html = orgToHTML("""
| Left | Center | Right |
|:-----+:-----:+------:|
| a | b | c |
| 1 | 2 | 3 |
""")
#expect(html.contains("text-align: left;"))
#expect(html.contains("text-align: center;"))
#expect(html.contains("text-align: right;"))
}
}
struct RepositoryAssetURLTests {
@Test
func repositoryAssetURLPercentEncodesImagePaths() {
let url = resolveRepositoryAssetURL(
"images/My Logo.png",
owner: "~ccleberg",
repositoryName: "Hutch",
readmePath: "README.md"
)
#expect(url == "https://git.sr.ht/~ccleberg/Hutch/blob/HEAD/images/My%20Logo.png")
}
}
|