Debian Codegraph @

src/tests/wikilinks.rs

raw
1 use super::*;
2
3 #[test]
4 fn wikilinks_does_not_unescape_html_entities_in_link_label() {
5 html_opts!(
6 [extension.wikilinks_title_after_pipe],
7 "This is [[<script>alert(0)</script>|a <link]]",
8 "<p>This is <a href=\"%3Cscript%3Ealert(0)%3C/script%3E\" data-wikilink=\"true\">a &lt;link</a></p>\n",
9 no_roundtrip,
10 );
11
12 html_opts!(
13 [extension.wikilinks_title_before_pipe],
14 "This is [[a &lt;link|&lt;script&gt;alert(0)&lt;/script&gt;]]",
15 "<p>This is <a href=\"%3Cscript%3Ealert(0)%3C/script%3E\" data-wikilink=\"true\">a &lt;link</a></p>\n",
16 no_roundtrip,
17 );
18 }
19
20 #[test]
21 fn wikilinks_sanitizes_the_href_attribute_case_1() {
22 html_opts!(
23 [extension.wikilinks_title_after_pipe],
24 "[[http:'\"injected=attribute&gt;&lt;img/src=\"0\"onerror=\"alert(0)\"&gt;https://example.com|a]]",
25 "<p><a href=\"http:&#x27;%22injected=attribute%3E%3Cimg/src=%220%22onerror=%22alert(0)%22%3Ehttps://example.com\" data-wikilink=\"true\">a</a></p>\n",
26 );
27
28 html_opts!(
29 [extension.wikilinks_title_before_pipe],
30 "[[a|http:'\"injected=attribute&gt;&lt;img/src=\"0\"onerror=\"alert(0)\"&gt;https://example.com]]",
31 "<p><a href=\"http:&#x27;%22injected=attribute%3E%3Cimg/src=%220%22onerror=%22alert(0)%22%3Ehttps://example.com\" data-wikilink=\"true\">a</a></p>\n",
32 );
33 }
34
35 #[test]
36 fn wikilinks_sanitizes_the_href_attribute_case_2() {
37 html_opts!(
38 [extension.wikilinks_title_after_pipe],
39 "<i>[['\"&gt;&lt;svg&gt;&lt;i/class=gl-show-field-errors&gt;&lt;input/title=\"&lt;script&gt;alert(0)&lt;/script&gt;\"/&gt;&lt;/svg&gt;https://example.com|a]]",
40 "<p><!-- raw HTML omitted --><a href=\"&#x27;%22%3E%3Csvg%3E%3Ci/class=gl-show-field-errors%3E%3Cinput/title=%22%3Cscript%3Ealert(0)%3C/script%3E%22/%3E%3C/svg%3Ehttps://example.com\" data-wikilink=\"true\">a</a></p>\n",
41 );
42
43 html_opts!(
44 [extension.wikilinks_title_before_pipe],
45 "<i>[[a|'\"&gt;&lt;svg&gt;&lt;i/class=gl-show-field-errors&gt;&lt;input/title=\"&lt;script&gt;alert(0)&lt;/script&gt;\"/&gt;&lt;/svg&gt;https://example.com]]",
46 "<p><!-- raw HTML omitted --><a href=\"&#x27;%22%3E%3Csvg%3E%3Ci/class=gl-show-field-errors%3E%3Cinput/title=%22%3Cscript%3Ealert(0)%3C/script%3E%22/%3E%3C/svg%3Ehttps://example.com\" data-wikilink=\"true\">a</a></p>\n",
47 );
48 }
49
50 #[test]
51 fn wikilinks_title_escape_chars() {
52 html_opts!(
53 [
54 extension.wikilinks_title_before_pipe,
55 render.escaped_char_spans
56 ],
57 "[[Name \\[of\\] page|http://example.com]]",
58 "<p><a href=\"http://example.com\" data-wikilink=\"true\">Name <span data-escaped-char>[</span>of<span data-escaped-char>]</span> page</a></p>\n",
59 no_roundtrip,
60 );
61 }
62
63 #[test]
64 fn wikilinks_supersedes_relaxed_autolinks() {
65 html_opts!(
66 [
67 extension.wikilinks_title_after_pipe,
68 parse.relaxed_autolinks
69 ],
70 "[[http://example.com]]",
71 "<p><a href=\"http://example.com\" data-wikilink=\"true\">http://example.com</a></p>\n",
72 );
73
74 html_opts!(
75 [
76 extension.wikilinks_title_before_pipe,
77 parse.relaxed_autolinks
78 ],
79 "[[http://example.com]]",
80 "<p><a href=\"http://example.com\" data-wikilink=\"true\">http://example.com</a></p>\n",
81 );
82 }
83
84 #[test]
85 fn wikilinks_only_url_in_tables() {
86 html_opts!(
87 [extension.wikilinks_title_after_pipe, extension.table],
88 concat!("| header |\n", "| ------- |\n", "| [[url]] |\n",),
89 concat!(
90 "<table>\n",
91 "<thead>\n",
92 "<tr>\n",
93 "<th>header</th>\n",
94 "</tr>\n",
95 "</thead>\n",
96 "<tbody>\n",
97 "<tr>\n",
98 "<td><a href=\"url\" data-wikilink=\"true\">url</a></td>\n",
99 "</tr>\n",
100 "</tbody>\n",
101 "</table>\n",
102 ),
103 );
104
105 html_opts!(
106 [extension.wikilinks_title_before_pipe, extension.table],
107 concat!("| header |\n", "| ------- |\n", "| [[url]] |\n",),
108 concat!(
109 "<table>\n",
110 "<thead>\n",
111 "<tr>\n",
112 "<th>header</th>\n",
113 "</tr>\n",
114 "</thead>\n",
115 "<tbody>\n",
116 "<tr>\n",
117 "<td><a href=\"url\" data-wikilink=\"true\">url</a></td>\n",
118 "</tr>\n",
119 "</tbody>\n",
120 "</table>\n",
121 ),
122 );
123 }
124
125 #[test]
126 fn wikilinks_full_in_tables_not_supported() {
127 html_opts!(
128 [extension.wikilinks_title_after_pipe, extension.table],
129 concat!("| header |\n", "| ------- |\n", "| [[url|link label]] |\n",),
130 concat!(
131 "<table>\n",
132 "<thead>\n",
133 "<tr>\n",
134 "<th>header</th>\n",
135 "</tr>\n",
136 "</thead>\n",
137 "<tbody>\n",
138 "<tr>\n",
139 "<td>[[url</td>\n",
140 "</tr>\n",
141 "</tbody>\n",
142 "</table>\n",
143 ),
144 );
145
146 html_opts!(
147 [extension.wikilinks_title_before_pipe, extension.table],
148 concat!("| header |\n", "| ------- |\n", "| [[link label|url]] |\n",),
149 concat!(
150 "<table>\n",
151 "<thead>\n",
152 "<tr>\n",
153 "<th>header</th>\n",
154 "</tr>\n",
155 "</thead>\n",
156 "<tbody>\n",
157 "<tr>\n",
158 "<td>[[link label</td>\n",
159 "</tr>\n",
160 "</tbody>\n",
161 "</table>\n",
162 ),
163 );
164 }
165
166 #[test]
167 fn wikilinks_exceeds_label_limit() {
168 let long_label = format!("[[{:b<1100}]]", "a");
169 let expected = format!("<p>{}</p>\n", long_label);
170
171 html_opts!(
172 [extension.wikilinks_title_after_pipe],
173 &long_label,
174 &expected,
175 );
176 }
177
178 #[test]
179 fn wikilinks_autolinker_ignored() {
180 html_opts!(
181 [extension.wikilinks_title_after_pipe, extension.autolink],
182 "[[http://example.com]]",
183 "<p><a href=\"http://example.com\" data-wikilink=\"true\">http://example.com</a></p>\n",
184 );
185
186 html_opts!(
187 [extension.wikilinks_title_before_pipe, extension.autolink],
188 "[[http://example.com]]",
189 "<p><a href=\"http://example.com\" data-wikilink=\"true\">http://example.com</a></p>\n",
190 );
191 }
192
193 #[test]
194 fn sourcepos() {
195 assert_ast_match!(
196 [extension.wikilinks_title_after_pipe],
197 "This [[http://example.com|link label]] that\n",
198 (document (1:1-1:43) [
199 (paragraph (1:1-1:43) [
200 (text (1:1-1:5) "This ")
201 (wikilink (1:6-1:38) [
202 (text (1:27-1:36) "link label")
203 ])
204 (text (1:39-1:43) " that")
205 ])
206 ])
207 );
208
209 assert_ast_match!(
210 [extension.wikilinks_title_before_pipe],
211 "This [[link label|http://example.com]] that\n",
212 (document (1:1-1:43) [
213 (paragraph (1:1-1:43) [
214 (text (1:1-1:5) "This ")
215 (wikilink (1:6-1:38) [
216 (text (1:8-1:17) "link label")
217 ])
218 (text (1:39-1:43) " that")
219 ])
220 ])
221 );
222
223 assert_ast_match!(
224 [extension.wikilinks_title_before_pipe],
225 "This [[http://example.com]] that\n",
226 (document (1:1-1:32) [
227 (paragraph (1:1-1:32) [
228 (text (1:1-1:5) "This ")
229 (wikilink (1:6-1:27) [
230 (text (1:8-1:25) "http://example.com")
231 ])
232 (text (1:28-1:32) " that")
233 ])
234 ])
235 );
236
237 assert_ast_match!(
238 [extension.wikilinks_title_before_pipe],
239 "This [[link\\[label|http://example.com]] that\n",
240 (document (1:1-1:44) [
241 (paragraph (1:1-1:44) [
242 (text (1:1-1:5) "This ")
243 (wikilink (1:6-1:39) [
244 (text (1:8-1:18) "link[label")
245 ])
246 (text (1:40-1:44) " that")
247 ])
248 ])
249 );
250 }