/
test_html.rb
99 lines (83 loc) · 3.92 KB
/
test_html.rb
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
require "helper"
class IntegrationTestHtml < Loofah::TestCase
context "html fragment" do
context "#to_s" do
it "includes header tags (like style)" do
html = "<style>foo</style><div>bar</div>"
expected = "<style>foo</style><div>bar</div>"
assert_equal(expected, Loofah.fragment(html).to_s)
# assumption check is that Nokogiri does the same
assert_equal(expected, Nokogiri::HTML4::DocumentFragment.parse(html).to_s)
assert_equal(expected, Nokogiri::HTML5::DocumentFragment.parse(html).to_s)
end
end
context "#text" do
it "includes header tags (like style)" do
html = "<style>foo</style><div>bar</div>"
expected = "foobar"
assert_equal(expected, Loofah.fragment(html).text)
# assumption check is that Nokogiri does the same
assert_equal(expected, Nokogiri::HTML4::DocumentFragment.parse(html).text)
assert_equal(expected, Nokogiri::HTML5::DocumentFragment.parse(html).text)
end
it "does not include cdata tags (like comments)" do
html = "<div>bar<!-- comment1 --></div><!-- comment2 -->"
expected = "bar"
assert_equal(expected, Loofah.fragment(html).text)
# assumption check is that Nokogiri does the same
assert_equal(expected, Nokogiri::HTML4::DocumentFragment.parse(html).text)
assert_equal(expected, Nokogiri::HTML5::DocumentFragment.parse(html).text)
end
end
context "#to_text" do
it "add newlines before and after html4 block elements" do
html = Loofah.fragment "<div>tweedle<h1>beetle</h1>bottle<span>puddle</span>paddle<div>battle</div>muddle</div>"
assert_equal "\ntweedle\nbeetle\nbottlepuddlepaddle\nbattle\nmuddle\n", html.to_text
end
it "add newlines before and after html5 block elements" do
html = Loofah.fragment "<div>tweedle<section>beetle</section>bottle<span>puddle</span>paddle<div>battle</div>muddle</div>"
assert_equal "\ntweedle\nbeetle\nbottlepuddlepaddle\nbattle\nmuddle\n", html.to_text
end
it "remove extraneous whitespace" do
html = Loofah.fragment "<div>tweedle\n\n\t\n\s\nbeetle</div>"
assert_equal "\ntweedle\n\nbeetle\n", html.to_text
end
it "replaces <br> with newlines" do
html = Loofah.fragment("hello<div>first line<br>second line</div>goodbye")
assert_equal("hello\nfirst line\nsecond line\ngoodbye", html.to_text)
end
end
context "with an `encoding` arg" do
it "sets the parent document's encoding to accordingly" do
html = Loofah.fragment "<style>foo</style><div>bar</div>", "US-ASCII"
assert_equal "US-ASCII", html.document.encoding
end
end
end
context "html document" do
context "#text" do
it "not include head tags (like style)" do
html = Loofah.document "<style>foo</style><div>bar</div>"
assert_equal "bar", html.text
end
end
context "#to_text" do
it "add newlines before and after html4 block elements" do
html = Loofah.document "<div>tweedle<h1>beetle</h1>bottle<span>puddle</span>paddle<div>battle</div>muddle</div>"
assert_equal "\ntweedle\nbeetle\nbottlepuddlepaddle\nbattle\nmuddle\n", html.to_text
end
it "add newlines before and after html5 block elements" do
html = Loofah.document "<div>tweedle<section>beetle</section>bottle<span>puddle</span>paddle<div>battle</div>muddle</div>"
assert_equal "\ntweedle\nbeetle\nbottlepuddlepaddle\nbattle\nmuddle\n", html.to_text
end
it "remove extraneous whitespace" do
html = Loofah.document "<div>tweedle\n\n\t\n\s\nbeetle</div>"
assert_equal "\ntweedle\n\nbeetle\n", html.to_text
end
it "replaces <br> with newlines" do
html = Loofah.document("<body>hello<div>first line<br>second line</div>goodbye</body>")
assert_equal("hello\nfirst line\nsecond line\ngoodbye", html.to_text)
end
end
end
end