-
Notifications
You must be signed in to change notification settings - Fork 0
/
domparser_test.go
402 lines (335 loc) · 14.9 KB
/
domparser_test.go
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package readability
import (
"slices"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var baseTestCase = `<html><body><p>Some text and <a class="someclass" href="#">a link</a></p>` +
`<div id="foo">With a <script>With < fancy " characters in it because` +
`</script> that is fun.<span>And another node to make it harder</span></div><form><input type="text"/><input type="number"/>Here\'s a form</form></body></html>`
func TestDecodeHTML(t *testing.T) {
// https://www.toptal.com/designers/htmlarrows/symbols/
testCases := []struct {
input string
want string
}{
{
input: "§",
want: "§",
},
{
input: "§",
want: "§",
},
{
input: "❦",
want: "❦",
},
{
input: "❦",
want: "❦",
},
{
input: `With < fancy " characters in it because`,
want: `With < fancy " characters in it because`,
},
}
for _, tc := range testCases {
got, err := decodeHTML(tc.input)
assert.NoError(t, err)
if got != tc.want {
t.Errorf("got %v want %v", got, tc.want)
}
}
}
func TestJSDOM_Functionality(t *testing.T) {
t.Run("should work for basic operations using the parent child hierarchy and innerHTML", func(t *testing.T) {
baseDoc := newDOMParser().parse(baseTestCase, "http://fakehost/")
assert.Equal(t, 1, len(baseDoc.ChildNodes))
assert.Equal(t, 10, len(baseDoc.getElementsByTagName("*")))
var foo = baseDoc.GetElementById("foo")
assert.Equal(t, "body", foo.ParentNode.LocalName)
assert.Equal(t, baseDoc.Body, foo.ParentNode)
assert.Equal(t, baseDoc.Body.ParentNode, baseDoc.DocumentElement)
assert.Equal(t, 3, len(baseDoc.Body.ChildNodes))
var generatedHTML = baseDoc.getElementsByTagName("p")[0].GetInnerHTML()
assert.Equal(t, `Some text and <a class="someclass" href="#">a link</a>`, generatedHTML)
var scriptNode = baseDoc.getElementsByTagName("script")[0]
generatedHTML = scriptNode.GetInnerHTML()
assert.Equal(t, `With < fancy " characters in it because`, generatedHTML)
assert.Equal(t, `With < fancy " characters in it because`, scriptNode.GetTextContent())
})
t.Run("should have basic URI information", func(t *testing.T) {
baseDoc := newDOMParser().parse(baseTestCase, "http://fakehost/")
assert.Equal(t, "http://fakehost/", baseDoc.DocumentURI)
assert.Equal(t, "http://fakehost/", baseDoc.getBaseURI())
})
t.Run("should deal with script tags", func(t *testing.T) {
// Check our script parsing worked:
baseDoc := newDOMParser().parse(baseTestCase, "http://fakehost/")
var scripts = baseDoc.getElementsByTagName("script")
assert.Equal(t, 1, len(scripts))
assert.Equal(t, "With < fancy \" characters in it because", scripts[0].GetTextContent())
})
t.Run("should have working sibling/first+lastChild properties", func(t *testing.T) {
baseDoc := newDOMParser().parse(baseTestCase, "http://fakehost/")
var foo = baseDoc.GetElementById("foo")
assert.Equal(t, foo.PreviousSibling.NextSibling, foo)
assert.Equal(t, foo.NextSibling.PreviousSibling, foo)
assert.Equal(t, foo.NextSibling, foo.NextElementSibling)
assert.Equal(t, foo.PreviousSibling, foo.PreviousElementSibling)
var beforeFoo = foo.PreviousSibling
var afterFoo = foo.NextSibling
assert.Equal(t, baseDoc.Body.LastChild(), afterFoo)
assert.Equal(t, baseDoc.Body.FirstChild(), beforeFoo)
})
t.Run("should have working removeChild and appendChild functionality", func(t *testing.T) {
baseDoc := newDOMParser().parse(baseTestCase, "http://fakehost/")
var foo = baseDoc.GetElementById("foo")
var beforeFoo = foo.PreviousSibling
var afterFoo = foo.NextSibling
var removedFoo, err = foo.ParentNode.RemoveChild(foo)
assert.NoError(t, err)
assert.Equal(t, foo, removedFoo)
assert.Nil(t, foo.ParentNode)
assert.Nil(t, foo.PreviousSibling)
assert.Nil(t, foo.NextSibling)
assert.Nil(t, foo.PreviousElementSibling)
assert.Nil(t, foo.NextElementSibling)
assert.Equal(t, "p", beforeFoo.LocalName)
assert.Equal(t, beforeFoo.NextSibling, afterFoo)
assert.Equal(t, afterFoo.PreviousSibling, beforeFoo)
assert.Equal(t, beforeFoo.NextElementSibling, afterFoo)
assert.Equal(t, afterFoo.PreviousElementSibling, beforeFoo)
assert.Equal(t, 2, len(baseDoc.Body.ChildNodes))
baseDoc.Body.AppendChild(foo)
assert.Equal(t, 3, len(baseDoc.Body.ChildNodes))
assert.Equal(t, afterFoo.NextSibling, foo)
assert.Equal(t, foo.PreviousSibling, afterFoo)
assert.Equal(t, afterFoo.NextElementSibling, foo)
assert.Equal(t, foo.PreviousElementSibling, afterFoo)
// This should reorder back to sanity:
baseDoc.Body.AppendChild(afterFoo)
assert.Equal(t, foo.PreviousSibling, beforeFoo)
assert.Equal(t, foo.NextSibling, afterFoo)
assert.Equal(t, foo.PreviousElementSibling, beforeFoo)
assert.Equal(t, foo.NextElementSibling, afterFoo)
assert.Equal(t, foo.PreviousSibling.NextSibling, foo)
assert.Equal(t, foo.NextSibling.PreviousSibling, foo)
assert.Equal(t, foo.NextSibling, foo.NextElementSibling)
assert.Equal(t, foo.PreviousSibling, foo.PreviousElementSibling)
})
t.Run("should handle attributes", func(t *testing.T) {
baseDoc := newDOMParser().parse(baseTestCase, "http://fakehost/")
var link = baseDoc.getElementsByTagName("a")[0]
assert.Equal(t, "#", link.GetAttribute("href"))
assert.Equal(t, link.GetClassName(), link.GetAttribute("class"))
var foo = baseDoc.GetElementById("foo")
assert.Equal(t, foo.GetAttribute("id"), foo.GetId())
})
t.Run("should have a working replaceChild", func(t *testing.T) {
baseDoc := newDOMParser().parse(baseTestCase, "http://fakehost/")
var parent = baseDoc.getElementsByTagName("div")[0]
var p = baseDoc.createElementNode("p")
p.SetAttribute("id", "my-replaced-kid")
var childCount = len(parent.ChildNodes)
var childElCount = len(parent.Children)
for i := 0; i < len(parent.ChildNodes); i++ {
var replacedNode = parent.ChildNodes[i]
var replacedAnElement = replacedNode.NodeType == elementNode
var oldNext = replacedNode.NextSibling
var oldNextEl = replacedNode.NextElementSibling
var oldPrev = replacedNode.PreviousSibling
var oldPrevEl = replacedNode.PreviousElementSibling
parent.ReplaceChild(p, replacedNode)
// Check siblings and parents on both nodes were set:
assert.Equal(t, p.NextSibling, oldNext)
assert.Equal(t, p.PreviousSibling, oldPrev)
assert.Equal(t, p.ParentNode, parent)
assert.Nil(t, replacedNode.ParentNode)
assert.Nil(t, replacedNode.NextSibling)
assert.Nil(t, replacedNode.PreviousSibling)
// if the old node was an element, element siblings should now be null
if replacedAnElement {
assert.Nil(t, replacedNode.NextElementSibling)
assert.Nil(t, replacedNode.PreviousElementSibling)
}
// Check the siblings were updated
if oldNext != nil {
assert.Equal(t, oldNext.PreviousSibling, p)
}
if oldPrev != nil {
assert.Equal(t, oldPrev.NextSibling, p)
}
// check the array was updated
assert.Equal(t, parent.ChildNodes[i], p)
// Now check element properties/lists:
var kidElementIndex = slices.IndexFunc(parent.Children, func(n *Node) bool {
return n == p
})
// should be in the list:
assert.NotEqual(t, -1, kidElementIndex)
if kidElementIndex > 0 {
assert.Equal(t, parent.Children[kidElementIndex-1], p.PreviousElementSibling)
assert.Equal(t, p.PreviousElementSibling.NextElementSibling, p)
} else {
assert.Nil(t, p.PreviousElementSibling)
}
if kidElementIndex < len(parent.Children)-1 {
assert.Equal(t, parent.Children[kidElementIndex+1], p.NextElementSibling)
assert.Equal(t, p.NextElementSibling.PreviousElementSibling, p)
} else {
assert.Nil(t, p.NextElementSibling)
}
if replacedAnElement {
assert.Equal(t, oldNextEl, p.NextElementSibling)
assert.Equal(t, oldPrevEl, p.PreviousElementSibling)
}
assert.Equal(t, childCount, len(parent.ChildNodes))
if replacedAnElement {
assert.Equal(t, childElCount, len(parent.Children))
} else {
assert.Equal(t, childElCount+1, len(parent.Children))
}
parent.ReplaceChild(replacedNode, p)
assert.Equal(t, oldNext, replacedNode.NextSibling)
assert.Equal(t, oldNextEl, replacedNode.NextElementSibling)
assert.Equal(t, oldPrev, replacedNode.PreviousSibling)
assert.Equal(t, oldPrevEl, replacedNode.PreviousElementSibling)
if replacedNode.NextSibling != nil {
assert.Equal(t, replacedNode.NextSibling.PreviousSibling, replacedNode)
}
if replacedNode.PreviousSibling != nil {
assert.Equal(t, replacedNode.PreviousSibling.NextSibling, replacedNode)
}
if replacedAnElement {
if replacedNode.PreviousElementSibling != nil {
assert.Equal(t, replacedNode.PreviousElementSibling.NextElementSibling, replacedNode)
}
if replacedNode.NextElementSibling != nil {
assert.Equal(t, replacedNode.NextElementSibling.PreviousElementSibling, replacedNode)
}
}
}
})
}
func TestHTML_Escaping(t *testing.T) {
var baseStr = "<p>Hello, everyone & all their friends, <this> is a " test with ' quotes.</p>"
var doc = newDOMParser().parse(baseStr, "")
var p = doc.getElementsByTagName("p")[0]
var txtNode = p.FirstChild()
t.Run("should handle encoding HTML correctly", func(t *testing.T) {
// This /should/ just be cached straight from reading it:
assert.Equal(t, baseStr, "<p>"+p.GetInnerHTML()+"</p>")
assert.Equal(t, baseStr, "<p>"+txtNode.GetInnerHTML()+"</p>")
})
t.Run("should have decoded correctly", func(t *testing.T) {
// This /should/ just be cached straight from reading it:
assert.Equal(t, "Hello, everyone & all their friends, <this> is a \" test with ' quotes.", p.GetTextContent())
assert.Equal(t, "Hello, everyone & all their friends, <this> is a \" test with ' quotes.", txtNode.GetTextContent())
})
t.Run("should handle updates via textContent correctly", func(t *testing.T) {
// Because the initial tests might be based on cached innerHTML values,
// let's manipulate via textContent in order to test that it alters
// the innerHTML correctly.
txtNode.SetTextContent(txtNode.GetTextContent() + " ")
txtNode.SetTextContent(strings.TrimSpace(txtNode.GetTextContent()))
var expectedHTML = strings.NewReplacer(`"`, `"`, `'`, `'`).Replace(baseStr)
assert.Equal(t, expectedHTML, "<p>"+txtNode.GetInnerHTML()+"</p>")
assert.Equal(t, expectedHTML, "<p>"+p.GetInnerHTML()+"</p>")
})
}
func TestScript_Parsing(t *testing.T) {
t.Run("should strip ?-based comments within script tags", func(t *testing.T) {
var html = `<script><?Silly test <img src="test"></script>`
var doc = newDOMParser().parse(html, "")
assert.Equal(t, "SCRIPT", doc.FirstChild().TagName)
assert.Equal(t, "", doc.FirstChild().GetTextContent())
assert.Equal(t, 0, len(doc.FirstChild().Children))
assert.Equal(t, 0, len(doc.FirstChild().ChildNodes))
})
t.Run("should strip !-based comments within script tags", func(t *testing.T) {
var html = `<script><!--Silly test > <script src="foo.js"></script>--></script>`
var doc = newDOMParser().parse(html, "")
assert.Equal(t, "SCRIPT", doc.FirstChild().TagName)
assert.Equal(t, "", doc.FirstChild().GetTextContent())
assert.Equal(t, 0, len(doc.FirstChild().Children))
assert.Equal(t, 0, len(doc.FirstChild().ChildNodes))
})
t.Run("should strip any other nodes within script tags", func(t *testing.T) {
var html = `<script><div>Hello, I'm not really in a </div></script>`
var doc = newDOMParser().parse(html, "")
assert.Equal(t, "SCRIPT", doc.FirstChild().TagName)
assert.Equal(t, `<div>Hello, I'm not really in a </div>`, doc.FirstChild().GetTextContent())
assert.Equal(t, 0, len(doc.FirstChild().Children))
assert.Equal(t, 1, len(doc.FirstChild().ChildNodes))
})
t.Run("should strip any other invalid script nodes within script tags", func(t *testing.T) {
var html = `<script><script src="foo.js"></script></script>`
var doc = newDOMParser().parse(html, "")
assert.Equal(t, "SCRIPT", doc.FirstChild().TagName)
assert.Equal(t, "<script src=\"foo.js\"></script>", doc.FirstChild().GetTextContent())
assert.Equal(t, 0, len(doc.FirstChild().Children))
assert.Equal(t, 1, len(doc.FirstChild().ChildNodes))
})
t.Run("should not be confused by partial closing tags", func(t *testing.T) {
var html = "<script>var x = '<script>Hi<' + '/script>';</script>"
var doc = newDOMParser().parse(html, "")
assert.Equal(t, "SCRIPT", doc.FirstChild().TagName)
assert.Equal(t, "var x = '<script>Hi<' + '/script>';", doc.FirstChild().GetTextContent())
assert.Equal(t, 0, len(doc.FirstChild().Children))
assert.Equal(t, 1, len(doc.FirstChild().ChildNodes))
})
}
func TestTagName_LocalName_Handling(t *testing.T) {
t.Run("should lowercase tag names", func(t *testing.T) {
var html = "<DIV><svG><clippath/></svG></DIV>"
var doc = newDOMParser().parse(html, "")
assert.Equal(t, "DIV", doc.FirstChild().TagName)
assert.Equal(t, "div", doc.FirstChild().LocalName)
assert.Equal(t, "SVG", doc.FirstChild().FirstChild().TagName)
assert.Equal(t, "svg", doc.FirstChild().FirstChild().LocalName)
assert.Equal(t, "CLIPPATH", doc.FirstChild().FirstChild().FirstChild().TagName)
assert.Equal(t, "clippath", doc.FirstChild().FirstChild().FirstChild().LocalName)
})
}
func TestRecovery_From_SelfClosing_Tags_That_Have_Close_Tags(t *testing.T) {
t.Run("should handle delayed closing of a tag", func(t *testing.T) {
var html = "<div><input><p>I'm in an input</p></input></div>"
var doc = newDOMParser().parse(html, "")
assert.Equal(t, "div", doc.FirstChild().LocalName)
assert.Equal(t, 1, len(doc.FirstChild().ChildNodes))
assert.Equal(t, "input", doc.FirstChild().FirstChild().LocalName)
assert.Equal(t, 1, len(doc.FirstChild().FirstChild().ChildNodes))
assert.Equal(t, "p", doc.FirstChild().FirstChild().FirstChild().LocalName)
})
}
func TestBaseURI_Parsing(t *testing.T) {
t.Run("should handle various types of relative and absolute base URIs", func(t *testing.T) {
var checkBase = func(base, expectedResult string) {
var html = "<html><head><base href='" + base + "'></base></head><body/></html>"
var doc = newDOMParser().parse(html, "http://fakehost/some/dir/")
assert.Equal(t, expectedResult, doc.getBaseURI())
}
checkBase("relative/path", "http://fakehost/some/dir/relative/path")
checkBase("/path", "http://fakehost/path")
checkBase("http://absolute/", "http://absolute/")
checkBase("//absolute/path", "http://absolute/path")
})
}
func TestNamespace_Workarounds(t *testing.T) {
t.Run("should handle random namespace information in the serialized DOM", func(t *testing.T) {
var html = "<a0:html><a0:body><a0:DIV><a0:svG><a0:clippath/></a0:svG></a0:DIV></a0:body></a0:html>"
var doc = newDOMParser().parse(html, "")
var div = doc.getElementsByTagName("div")[0]
assert.Equal(t, "DIV", div.TagName)
assert.Equal(t, "div", div.LocalName)
assert.Equal(t, "SVG", div.FirstChild().TagName)
assert.Equal(t, "svg", div.FirstChild().LocalName)
assert.Equal(t, "CLIPPATH", div.FirstChild().FirstChild().TagName)
assert.Equal(t, "clippath", div.FirstChild().FirstChild().LocalName)
assert.Equal(t, doc.FirstChild(), doc.DocumentElement)
assert.Equal(t, doc.DocumentElement.FirstChild(), doc.Body)
})
}