forked from rubys/nokogumbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-nokogumbo.rb
147 lines (125 loc) · 3.88 KB
/
test-nokogumbo.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
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
$:.unshift('lib')
$:.unshift('ext/nokogumboc')
gem 'minitest'
require 'nokogumbo'
require 'minitest/autorun'
class TestNokogumbo < Minitest::Test
def test_element_text
doc = Nokogiri::HTML5(buffer)
assert_equal "content", doc.at('span').text
end
def test_element_cdata_textarea
doc = Nokogiri::HTML5(buffer)
assert_equal "foo<x>bar", doc.at('textarea').text.strip
end
def test_element_cdata_script
doc = Nokogiri::HTML5.fragment(buffer)
assert_equal true, doc.document.html?
assert_equal "<script> if (a < b) alert(1) </script>", doc.at('script').to_s
end
def test_attr_value
doc = Nokogiri::HTML5(buffer)
assert_equal "utf-8", doc.at('meta')['charset']
end
def test_comment
doc = Nokogiri::HTML5(buffer)
assert_equal " test comment ", doc.xpath('//comment()').text
end
def test_unknown_element
doc = Nokogiri::HTML5(buffer)
assert_equal "main", doc.at('main').name
end
def test_IO
require 'stringio'
doc = Nokogiri::HTML5(StringIO.new(buffer))
assert_equal 'textarea', doc.at('form').element_children.first.name
end
def test_nil
doc = Nokogiri::HTML5(nil)
assert_equal 1, doc.search('body').count
end
if ''.respond_to? 'encoding'
def test_macroman_encoding
mac="<span>\xCA</span>".force_encoding('macroman')
doc = Nokogiri::HTML5(mac)
assert_equal '<span> </span>', doc.at('span').to_xml
end
def test_iso8859_encoding
iso8859="<span>Se\xF1or</span>".force_encoding(Encoding::ASCII_8BIT)
doc = Nokogiri::HTML5(iso8859)
assert_equal '<span>Señor</span>', doc.at('span').to_xml
end
def test_charset_encoding
utf8="<meta charset='utf-8'><span>Se\xC3\xB1or</span>".
force_encoding(Encoding::ASCII_8BIT)
doc = Nokogiri::HTML5(utf8)
assert_equal '<span>Señor</span>', doc.at('span').to_xml
end
def test_bogus_encoding
bogus="<meta charset='bogus'><span>Se\xF1or</span>".
force_encoding(Encoding::ASCII_8BIT)
doc = Nokogiri::HTML5(bogus)
assert_equal '<span>Señor</span>', doc.at('span').to_xml
end
end
def test_html5_doctype
doc = Nokogumbo.parse("<!DOCTYPE html><html></html>")
assert_match /<!DOCTYPE html>/, doc.to_html
end
def test_fragment_head
doc = Nokogiri::HTML5.fragment(buffer[/<head>(.*?)<\/head>/m, 1])
assert_equal "hello world", doc.xpath('title').text
assert_equal "utf-8", doc.xpath('meta').first['charset']
end
def test_fragment_body
doc = Nokogiri::HTML5.fragment(buffer[/<body>(.*?)<\/body>/m, 1])
assert_equal '<span>content</span>', doc.xpath('main/span').to_xml
assert_equal " test comment ", doc.xpath('comment()').text
end
def test_xlink_attribute
source = <<-EOF.gsub(/^ {6}/, '')
<svg xmlns="http://www.w3.org/2000/svg">
<a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#s1"/>
</svg>
EOF
doc = Nokogiri::HTML5.fragment(source)
a = doc.at('a')
assert_equal ["xlink:href", "xmlns:xlink"], a.attributes.keys.sort
end
def test_template
source = <<-EOF.gsub(/^ {6}/, '')
<template id="productrow">
<tr>
<td class="record"></td>
<td></td>
</tr>
</template>
EOF
doc = Nokogiri::HTML5.fragment(source)
template = doc.at('template')
assert_equal "productrow", template['id']
assert_equal "record", template.at('td')['class']
end
private
def buffer
<<-EOF.gsub(/^ /, '')
<html>
<head>
<meta charset="utf-8"/>
<title>hello world</title>
<script> if (a < b) alert(1) </script>
</head>
<body>
<h1>hello world</h1>
<main>
<span>content</span>
</main>
<!-- test comment -->
<form>
<textarea>foo<x>bar</textarea>
</form>
</body>
</html>
EOF
end
end