-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathHtmlUtilsTests.cs
More file actions
337 lines (295 loc) · 10.1 KB
/
HtmlUtilsTests.cs
File metadata and controls
337 lines (295 loc) · 10.1 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
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
using NUnit.Framework;
using System.IO;
using SIL.Core;
using SIL.IO;
using static System.IO.Path;
using static System.StringComparison;
namespace SIL.Tests.Html
{
[TestFixture]
public class HtmlUtilsTests
{
private const string htmlWithNoHead = @"<html>
<body>
<h3>Stuff</h3>
<p>Here is something you need to know.</p>
</body>
</html>";
private const string htmlWithSelfClosingEmptyHead = @"<html>
<head/>
<body>
<h3>Stuff</h3>
<p>Here is something you need to know.</p>
</body>
</html>";
private const string htmlWithNonEmptyHeadButNoTarget = @"<html>
<head><meta charset='UTF-8' /></head>
<body>
<h3>Stuff</h3>
<p>Here is something you need to know.</p>
</body>
</html>";
[TestCase(@"<html>
<head><base target=""_blank"" rel=""noopener noreferrer""></head>
<body>
<p>Good</p>
</body>
</html>")]
[TestCase(@"<html>
<head><base target=""_blank""></head>
<body>
<p>Stuff.</p>
</body>
</html>")]
[TestCase(@"<html>
<head><base target=""_self""></head>
<body>
<p>Stuff.</p>
</body>
</html>")]
public void HasBaseTarget_Yes_ReturnsTrue(string html)
{
Assert.That(HtmlUtils.HasBaseTarget(html), Is.True);
}
[TestCase(htmlWithNoHead)]
[TestCase(htmlWithSelfClosingEmptyHead)]
[TestCase(htmlWithNonEmptyHeadButNoTarget)]
public void HasBaseTarget_No_ReturnsFalse(string html)
{
Assert.That(HtmlUtils.HasBaseTarget(html), Is.False);
}
[TestCase("")]
[TestCase(null)]
public void HandleMissingLinkTargets_EmptyHtml_ReturnsNull(string html)
{
var result = HtmlUtils.HandleMissingLinkTargets(html);
Assert.IsNull(result);
}
[TestCase(htmlWithNoHead)]
[TestCase(htmlWithSelfClosingEmptyHead)]
[TestCase(htmlWithNonEmptyHeadButNoTarget)]
public void HandleMissingLinkTargets_HtmlWithNoLinks_ReturnsNull(string html)
{
var result = HtmlUtils.HandleMissingLinkTargets(html);
Assert.IsNull(result);
}
[TestCase(true)]
[TestCase(false)]
public void HandleMissingLinkTargets_ExternalLinkWithoutTarget_AddsBaseTarget(
bool useDoubleQuotes)
{
var html = @"<html>
<head></head>
<body>
<a href='http://example.com'>External</a>
</body>
</html>";
if (useDoubleQuotes)
html = html.Replace("'", @"""");
var origBody = html.Substring(html.IndexOf("<body>", Ordinal));
var result = HtmlUtils.HandleMissingLinkTargets(html);
Assert.That(HtmlUtils.HasBaseTarget(result), Is.True);
Assert.That(result.Substring(result.IndexOf("<body>", Ordinal)),
Is.EqualTo(origBody));
}
[TestCase(true)]
[TestCase(false)]
public void HandleMissingLinkTargets_ExternalLinkWithTarget_ReturnsNull(
bool useDoubleQuotes)
{
var html = @"<html>
<head></head>
<body>
<a href='https://example.com' target='_blank'>External</a>
</body>
</html>";
if (useDoubleQuotes)
html = html.Replace("'", @"""");
var result = HtmlUtils.HandleMissingLinkTargets(html);
Assert.That(result, Is.Null, "No need to modify; explicit target already present");
}
[TestCase(true)]
[TestCase(false)]
public void HandleMissingLinkTargets_OnlyInternalLinks_ReturnsNull(
bool useDoubleQuotes)
{
var html = @"<html>
<head></head>
<body>
<a href='#section1'>Internal</a>
<a href='mailto:someone@example.com'>Email</a>
<h4 id='section1'>This is the internal section</h4>
<p>You jumped here using an internal anchor link.</p>
</body>
</html>";
if (useDoubleQuotes)
html = html.Replace("'", @"""");
var result = HtmlUtils.HandleMissingLinkTargets(html);
Assert.That(result, Is.Null, "No need to modify; all links are internal/special");
}
[Test]
public void HandleMissingLinkTargets_InternalAnchor_GetsTargetSelf()
{
var html = @"<html><head></head><body><a href=""#section1"">Jump</a> and
<a href='http://example.com'>Go</a>!</body></html>";
var origTail = html.Substring(html.IndexOf("Jump</a> and", Ordinal));
var result = HtmlUtils.HandleMissingLinkTargets(html);
Assert.That(HtmlUtils.HasBaseTarget(result), Is.True);
Assert.That(result, Does.Contain(@"<a href=""#section1"" target=""_self"">Jump</a>"));
Assert.That(result, Does.EndWith(origTail));
}
[Test]
public void HandleMissingLinkTargets_Mailto_GetsTargetSelf()
{
var html = @"<html><head></head><body><a href='http://example.com'>Go</a> here to
<a href='mailto:someone@example.com'>Email</a> someone.</body></html>";
int bodyStart = html.IndexOf("<body>", Ordinal);
var origBodyStart = html.Substring(bodyStart,
html.IndexOf("<a href='mailto", Ordinal) - bodyStart);
var result = HtmlUtils.HandleMissingLinkTargets(html);
Assert.That(HtmlUtils.HasBaseTarget(result), Is.True);
Assert.That(result, Does.Contain(origBodyStart));
Assert.That(result, Does.EndWith("<a href='mailto:someone@example.com' " +
@"target=""_self"">Email</a> someone.</body></html>"));
}
[TestCase(@"href=""#internal"" target=""_self""")]
[TestCase(@"target=""_self"" href=""#internal""")]
[TestCase(@"target='_self' href=""#internal""")]
[TestCase(@"href='#internal' target=""_self""")]
public void HandleMissingLinkTargets_InternalLink_WithTargetAlready_OnlyBaseTargetAdded(
string internalLinkAttributes)
{
var html = $"<html><head></head><body><a {internalLinkAttributes}>Stay</a>" +
@"alert as you <a href=""www.example.com"">walk</a>.</body></html>";
var origBody = html.Substring(html.IndexOf("<body>", Ordinal));
var result = HtmlUtils.HandleMissingLinkTargets(html);
Assert.That(HtmlUtils.HasBaseTarget(result), Is.True);
Assert.That(result.Substring(result.IndexOf("<body>", Ordinal)),
Is.EqualTo(origBody));
}
[TestCase("www.example.com")]
[TestCase("http://www.example.com")]
[TestCase("https://www.example.com")]
public void IsExternalHref_IsExternal_ReturnsTrue(string href)
{
Assert.That(HtmlUtils.IsExternalHref(href), Is.True);
}
[TestCase("#internal")]
[TestCase("mailto:someone@example.com")]
[TestCase("tel:8008008000")]
[TestCase("")]
[TestCase(null)]
public void IsExternalHref_IsNotExternal_ReturnsFalse(string href)
{
Assert.That(HtmlUtils.IsExternalHref(href), Is.False);
}
[TestCase(htmlWithNoHead)]
[TestCase(htmlWithSelfClosingEmptyHead)]
[TestCase(htmlWithNonEmptyHeadButNoTarget)]
public void InjectBaseTarget_Missing_AddsHeadElementWithBaseTarget(string html)
{
var result = HtmlUtils.InjectBaseTarget(html);
Assert.That(HtmlUtils.HasBaseTarget(result), Is.True);
}
[TestCase("")]
[TestCase(@"<p><a name=""gumby""/></p>")]
[TestCase(@"<p><a href=""https://www.example.com""/></p>")]
public void InjectBaseTarget_AlreadyHasBaseTarget_ReturnsOriginalHtml(string body)
{
var html = $"<html><head><base target='_blank'></head><body>{body}</body></html>";
var result = HtmlUtils.InjectBaseTarget(html);
Assert.That(result, Is.EqualTo(html));
}
}
[TestFixture]
public class HtmlUtilsCreatePatchedTempHtmlFileTests
{
private string _testDir;
private TempFile _modifiedHtml;
private string _htmlPath;
[SetUp]
public void Setup()
{
_modifiedHtml = TempFile.WithFilenameInTempFolder("about.html");
_testDir = GetDirectoryName(_modifiedHtml.Path);
_htmlPath = _modifiedHtml.Path;
}
[TearDown]
public void Teardown()
{
_modifiedHtml.Dispose();
}
[TestCase("")]
[TestCase("./")]
[TestCase(" ")]
public void SimpleCssLink_AssetCopied(string prefix)
{
const string cssName = "style.css";
var cssPath = Combine(_testDir, cssName);
File.WriteAllText(cssPath, "body { background: black; }");
var html = $@"<html><head>
<link rel=""stylesheet"" href=""{prefix}{cssName}""></head><body>hello</body></html>";
File.WriteAllText(_htmlPath, html);
using var tempFile = HtmlUtils.CreatePatchedTempHtmlFile(html, _htmlPath);
var tempDir = GetDirectoryName(tempFile.Path);
Assert.That(tempFile.Path, Does.Exist);
Assert.That(Combine(tempDir, cssName), Does.Exist);
}
[TestCase("")]
[TestCase("./")]
[TestCase(" ")]
[TestCase(" ./")]
public void MultipleSimpleAssets_AllCopied(string prefix)
{
const string cssName = "style.css";
const string jsName = "script.js";
const string logoName = "logo.png";
File.WriteAllText(Combine(_testDir, cssName), "css");
File.WriteAllText(Combine(_testDir, jsName), "js");
File.WriteAllText(Combine(_testDir, logoName), "png");
var html = $@"<html><head>
<link rel=""stylesheet"" href = ""{prefix}{cssName}"">
<script src=""{prefix}{jsName}""></script>
</head><body><img src=""{prefix}{logoName}""></body></html>";
File.WriteAllText(_htmlPath, html);
using var tempFile = HtmlUtils.CreatePatchedTempHtmlFile(html, _htmlPath);
var tempDir = GetDirectoryName(tempFile.Path);
Assert.That(File.Exists(Combine(tempDir, cssName)), Is.True);
Assert.That(File.Exists(Combine(tempDir, jsName)), Is.True);
Assert.That(File.Exists(Combine(tempDir, logoName)), Is.True);
}
[Test]
public void ExternalLinks_Ignored()
{
const string html = @"<html><head>
<link rel=""stylesheet"" href=""https://example.com/style.css"">
</head><body>hello</body></html>";
File.WriteAllText(_htmlPath, html);
using var tempFile = HtmlUtils.CreatePatchedTempHtmlFile(html, _htmlPath);
var tempDir = GetDirectoryName(tempFile.Path);
Assert.That(File.Exists(tempFile.Path), Is.True);
Assert.That(Directory.GetFiles(tempDir).Length, Is.EqualTo(1),
"Should not attempt to copy external resources.");
}
/// <summary>
/// Since we're purposefully trying to keep things simple by ignoring relative/
/// subdirectory assets, this test ensures that we don't attempt to copy them.
/// </summary>
[TestCase(@"\")]
[TestCase("/")]
public void SubdirectoryAsset_NotCopied(string slash)
{
var assetsDir = Combine(_testDir, "assets");
Directory.CreateDirectory(assetsDir);
File.WriteAllText(Combine(assetsDir, "style.css"), "should not copy");
var html = $@"<html><head>
<link rel=""stylesheet"" href=""assets{slash}style.css"">
</head><body>hello</body></html>";
File.WriteAllText(_htmlPath, html);
using var tempFile = HtmlUtils.CreatePatchedTempHtmlFile(html, _htmlPath);
var tempDir = GetDirectoryName(tempFile.Path);
Assert.That(Directory.GetFiles(tempDir).Length, Is.EqualTo(1),
"Subdirectory assets should not be copied.");
}
}
}