nokogiri 1.6.2.rc1-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of nokogiri might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.autotest +26 -0
- data/.editorconfig +17 -0
- data/.gemtest +0 -0
- data/.travis.yml +25 -0
- data/CHANGELOG.ja.rdoc +857 -0
- data/CHANGELOG.rdoc +880 -0
- data/C_CODING_STYLE.rdoc +33 -0
- data/Gemfile +21 -0
- data/Manifest.txt +371 -0
- data/README.ja.rdoc +112 -0
- data/README.rdoc +180 -0
- data/ROADMAP.md +89 -0
- data/Rakefile +351 -0
- data/STANDARD_RESPONSES.md +47 -0
- data/Y_U_NO_GEMSPEC.md +155 -0
- data/bin/nokogiri +78 -0
- data/build_all +130 -0
- data/dependencies.yml +4 -0
- data/ext/nokogiri/depend +358 -0
- data/ext/nokogiri/extconf.rb +453 -0
- data/ext/nokogiri/html_document.c +170 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +279 -0
- data/ext/nokogiri/html_element_description.h +10 -0
- data/ext/nokogiri/html_entity_lookup.c +32 -0
- data/ext/nokogiri/html_entity_lookup.h +8 -0
- data/ext/nokogiri/html_sax_parser_context.c +116 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/html_sax_push_parser.c +87 -0
- data/ext/nokogiri/html_sax_push_parser.h +9 -0
- data/ext/nokogiri/nokogiri.c +148 -0
- data/ext/nokogiri/nokogiri.h +164 -0
- data/ext/nokogiri/xml_attr.c +94 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +56 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +54 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +577 -0
- data/ext/nokogiri/xml_document.h +23 -0
- data/ext/nokogiri/xml_document_fragment.c +48 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +202 -0
- data/ext/nokogiri/xml_dtd.h +10 -0
- data/ext/nokogiri/xml_element_content.c +123 -0
- data/ext/nokogiri/xml_element_content.h +10 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_element_decl.h +9 -0
- data/ext/nokogiri/xml_encoding_handler.c +79 -0
- data/ext/nokogiri/xml_encoding_handler.h +8 -0
- data/ext/nokogiri/xml_entity_decl.c +110 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +52 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +56 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_libxml2_hacks.c +112 -0
- data/ext/nokogiri/xml_libxml2_hacks.h +12 -0
- data/ext/nokogiri/xml_namespace.c +78 -0
- data/ext/nokogiri/xml_namespace.h +13 -0
- data/ext/nokogiri/xml_node.c +1541 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +467 -0
- data/ext/nokogiri/xml_node_set.h +14 -0
- data/ext/nokogiri/xml_processing_instruction.c +56 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +681 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +161 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +312 -0
- data/ext/nokogiri/xml_sax_parser.h +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +262 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +115 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +205 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +63 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +52 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +307 -0
- data/ext/nokogiri/xml_xpath_context.h +10 -0
- data/ext/nokogiri/xslt_stylesheet.c +270 -0
- data/ext/nokogiri/xslt_stylesheet.h +14 -0
- data/lib/nokogiri.rb +137 -0
- data/lib/nokogiri/2.0/nokogiri.so +0 -0
- data/lib/nokogiri/2.1/nokogiri.so +0 -0
- data/lib/nokogiri/css.rb +27 -0
- data/lib/nokogiri/css/node.rb +52 -0
- data/lib/nokogiri/css/parser.rb +715 -0
- data/lib/nokogiri/css/parser.y +249 -0
- data/lib/nokogiri/css/parser_extras.rb +91 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +152 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +219 -0
- data/lib/nokogiri/decorators/slop.rb +35 -0
- data/lib/nokogiri/html.rb +37 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +333 -0
- data/lib/nokogiri/html/document_fragment.rb +41 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/element_description_defaults.rb +671 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +52 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/html/sax/push_parser.rb +16 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +106 -0
- data/lib/nokogiri/xml.rb +73 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +443 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +279 -0
- data/lib/nokogiri/xml/document_fragment.rb +112 -0
- data/lib/nokogiri/xml/dtd.rb +32 -0
- data/lib/nokogiri/xml/element_content.rb +36 -0
- data/lib/nokogiri/xml/element_decl.rb +13 -0
- data/lib/nokogiri/xml/entity_decl.rb +19 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +982 -0
- data/lib/nokogiri/xml/node/save_options.rb +61 -0
- data/lib/nokogiri/xml/node_set.rb +355 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +98 -0
- data/lib/nokogiri/xml/pp.rb +2 -0
- data/lib/nokogiri/xml/pp/character_data.rb +18 -0
- data/lib/nokogiri/xml/pp/node.rb +56 -0
- data/lib/nokogiri/xml/processing_instruction.rb +8 -0
- data/lib/nokogiri/xml/reader.rb +112 -0
- data/lib/nokogiri/xml/relax_ng.rb +32 -0
- data/lib/nokogiri/xml/sax.rb +4 -0
- data/lib/nokogiri/xml/sax/document.rb +171 -0
- data/lib/nokogiri/xml/sax/parser.rb +123 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
- data/lib/nokogiri/xml/schema.rb +63 -0
- data/lib/nokogiri/xml/syntax_error.rb +47 -0
- data/lib/nokogiri/xml/text.rb +9 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xslt.rb +56 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xsd/xmlparser/nokogiri.rb +102 -0
- data/suppressions/README.txt +1 -0
- data/suppressions/nokogiri_ree-1.8.7.358.supp +61 -0
- data/suppressions/nokogiri_ruby-1.8.7.370.supp +0 -0
- data/suppressions/nokogiri_ruby-1.9.2.320.supp +28 -0
- data/suppressions/nokogiri_ruby-1.9.3.327.supp +28 -0
- data/tasks/nokogiri.org.rb +24 -0
- data/tasks/test.rb +95 -0
- data/test/css/test_nthiness.rb +222 -0
- data/test/css/test_parser.rb +358 -0
- data/test/css/test_tokenizer.rb +198 -0
- data/test/css/test_xpath_visitor.rb +96 -0
- data/test/decorators/test_slop.rb +16 -0
- data/test/files/2ch.html +108 -0
- data/test/files/address_book.rlx +12 -0
- data/test/files/address_book.xml +10 -0
- data/test/files/atom.xml +344 -0
- data/test/files/bar/bar.xsd +4 -0
- data/test/files/bogus.xml +0 -0
- data/test/files/dont_hurt_em_why.xml +422 -0
- data/test/files/encoding.html +82 -0
- data/test/files/encoding.xhtml +84 -0
- data/test/files/exslt.xml +8 -0
- data/test/files/exslt.xslt +35 -0
- data/test/files/foo/foo.xsd +4 -0
- data/test/files/metacharset.html +10 -0
- data/test/files/noencoding.html +47 -0
- data/test/files/po.xml +32 -0
- data/test/files/po.xsd +66 -0
- data/test/files/saml/saml20assertion_schema.xsd +283 -0
- data/test/files/saml/saml20protocol_schema.xsd +302 -0
- data/test/files/saml/xenc_schema.xsd +146 -0
- data/test/files/saml/xmldsig_schema.xsd +318 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -0
- data/test/files/shift_jis_no_charset.html +9 -0
- data/test/files/snuggles.xml +3 -0
- data/test/files/staff.dtd +10 -0
- data/test/files/staff.xml +59 -0
- data/test/files/staff.xslt +32 -0
- data/test/files/test_document_url/bar.xml +2 -0
- data/test/files/test_document_url/document.dtd +4 -0
- data/test/files/test_document_url/document.xml +6 -0
- data/test/files/tlm.html +850 -0
- data/test/files/to_be_xincluded.xml +2 -0
- data/test/files/valid_bar.xml +2 -0
- data/test/files/xinclude.xml +4 -0
- data/test/helper.rb +164 -0
- data/test/html/sax/test_parser.rb +141 -0
- data/test/html/sax/test_parser_context.rb +46 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +619 -0
- data/test/html/test_document_encoding.rb +148 -0
- data/test/html/test_document_fragment.rb +261 -0
- data/test/html/test_element_description.rb +105 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +196 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/namespaces/test_additional_namespaces_in_builder_doc.rb +14 -0
- data/test/namespaces/test_namespaces_in_builder_doc.rb +75 -0
- data/test/namespaces/test_namespaces_in_cloned_doc.rb +31 -0
- data/test/namespaces/test_namespaces_in_created_doc.rb +75 -0
- data/test/namespaces/test_namespaces_in_parsed_doc.rb +66 -0
- data/test/test_convert_xpath.rb +135 -0
- data/test/test_css_cache.rb +45 -0
- data/test/test_encoding_handler.rb +46 -0
- data/test/test_memory_leak.rb +156 -0
- data/test/test_nokogiri.rb +138 -0
- data/test/test_reader.rb +558 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +279 -0
- data/test/xml/node/test_save_options.rb +28 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +382 -0
- data/test/xml/sax/test_parser_context.rb +115 -0
- data/test/xml/sax/test_push_parser.rb +157 -0
- data/test/xml/test_attr.rb +64 -0
- data/test/xml/test_attribute_decl.rb +86 -0
- data/test/xml/test_builder.rb +315 -0
- data/test/xml/test_c14n.rb +161 -0
- data/test/xml/test_cdata.rb +48 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +934 -0
- data/test/xml/test_document_encoding.rb +28 -0
- data/test/xml/test_document_fragment.rb +228 -0
- data/test/xml/test_dtd.rb +187 -0
- data/test/xml/test_dtd_encoding.rb +33 -0
- data/test/xml/test_element_content.rb +56 -0
- data/test/xml/test_element_decl.rb +73 -0
- data/test/xml/test_entity_decl.rb +122 -0
- data/test/xml/test_entity_reference.rb +245 -0
- data/test/xml/test_namespace.rb +95 -0
- data/test/xml/test_node.rb +1155 -0
- data/test/xml/test_node_attributes.rb +113 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_inheritance.rb +32 -0
- data/test/xml/test_node_reparenting.rb +374 -0
- data/test/xml/test_node_set.rb +755 -0
- data/test/xml/test_parse_options.rb +64 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +142 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +129 -0
- data/test/xml/test_syntax_error.rb +12 -0
- data/test/xml/test_text.rb +45 -0
- data/test/xml/test_unparented_node.rb +422 -0
- data/test/xml/test_xinclude.rb +83 -0
- data/test/xml/test_xpath.rb +376 -0
- data/test/xslt/test_custom_functions.rb +133 -0
- data/test/xslt/test_exception_handling.rb +37 -0
- data/test_all +81 -0
- metadata +601 -0
data/test/test_reader.rb
ADDED
@@ -0,0 +1,558 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "helper"
|
3
|
+
|
4
|
+
class TestReader < Nokogiri::TestCase
|
5
|
+
def test_from_io_sets_io_as_source
|
6
|
+
io = File.open SNUGGLES_FILE
|
7
|
+
reader = Nokogiri::XML::Reader.from_io(io)
|
8
|
+
assert_equal io, reader.source
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_empty_element?
|
12
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
13
|
+
<xml><city>Paris</city><state/></xml>
|
14
|
+
eoxml
|
15
|
+
|
16
|
+
results = reader.map do |node|
|
17
|
+
if node.node_type == Nokogiri::XML::Node::ELEMENT_NODE
|
18
|
+
node.empty_element?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
assert_equal [false, false, nil, nil, true, nil], results
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_self_closing?
|
25
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
26
|
+
<xml><city>Paris</city><state/></xml>
|
27
|
+
eoxml
|
28
|
+
|
29
|
+
results = reader.map do |node|
|
30
|
+
if node.node_type == Nokogiri::XML::Node::ELEMENT_NODE
|
31
|
+
node.self_closing?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
assert_equal [false, false, nil, nil, true, nil], results
|
35
|
+
end
|
36
|
+
|
37
|
+
# Issue #831
|
38
|
+
# Make sure that the reader doesn't block reading the entire input
|
39
|
+
def test_reader_blocking
|
40
|
+
rd, wr = IO.pipe()
|
41
|
+
node_out = nil
|
42
|
+
t = Thread.start do
|
43
|
+
reader = Nokogiri::XML::Reader(rd, 'UTF-8')
|
44
|
+
reader.each do |node|
|
45
|
+
node_out = node
|
46
|
+
break
|
47
|
+
end
|
48
|
+
rd.close
|
49
|
+
end
|
50
|
+
sleep(1) # sleep for one second to make sure the reader will actually block for input
|
51
|
+
begin
|
52
|
+
wr.puts "<foo>"
|
53
|
+
wr.puts "<bar/>" * 10000
|
54
|
+
wr.flush
|
55
|
+
rescue Errno::EPIPE
|
56
|
+
end
|
57
|
+
res = t.join(5) # wait 5 seconds for the thread to finish
|
58
|
+
wr.close
|
59
|
+
refute_nil node_out, "Didn't read any nodes, exclude the trivial case"
|
60
|
+
refute_nil res, "Reader blocks trying to read the entire stream"
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_reader_takes_block
|
64
|
+
options = nil
|
65
|
+
Nokogiri::XML::Reader(File.read(XML_FILE), XML_FILE) do |cfg|
|
66
|
+
options = cfg
|
67
|
+
options.nonet.nowarning.dtdattr
|
68
|
+
end
|
69
|
+
assert options.nonet?
|
70
|
+
assert options.nowarning?
|
71
|
+
assert options.dtdattr?
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_nil_raises
|
75
|
+
assert_raises(ArgumentError) {
|
76
|
+
Nokogiri::XML::Reader.from_memory(nil)
|
77
|
+
}
|
78
|
+
assert_raises(ArgumentError) {
|
79
|
+
Nokogiri::XML::Reader.from_io(nil)
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_from_io
|
84
|
+
io = File.open SNUGGLES_FILE
|
85
|
+
reader = Nokogiri::XML::Reader.from_io(io)
|
86
|
+
assert_equal false, reader.default?
|
87
|
+
assert_equal [false, false, false, false, false, false, false],
|
88
|
+
reader.map { |x| x.default? }
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_io
|
92
|
+
io = File.open SNUGGLES_FILE
|
93
|
+
reader = Nokogiri::XML::Reader(io)
|
94
|
+
assert_equal false, reader.default?
|
95
|
+
assert_equal [false, false, false, false, false, false, false],
|
96
|
+
reader.map { |x| x.default? }
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_string_io
|
100
|
+
io = StringIO.new(<<-eoxml)
|
101
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
102
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
103
|
+
</x>
|
104
|
+
eoxml
|
105
|
+
reader = Nokogiri::XML::Reader(io)
|
106
|
+
assert_equal false, reader.default?
|
107
|
+
assert_equal [false, false, false, false, false, false, false],
|
108
|
+
reader.map { |x| x.default? }
|
109
|
+
end
|
110
|
+
|
111
|
+
class ReallyBadIO
|
112
|
+
def read(size)
|
113
|
+
'a' * size ** 10
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class ReallyBadIO4Java
|
118
|
+
def read(size=1)
|
119
|
+
'a' * size ** 10
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_io_that_reads_too_much
|
124
|
+
if Nokogiri.jruby?
|
125
|
+
io = ReallyBadIO4Java.new
|
126
|
+
Nokogiri::XML::Reader(io)
|
127
|
+
else
|
128
|
+
io = ReallyBadIO.new
|
129
|
+
Nokogiri::XML::Reader(io)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_in_memory
|
134
|
+
assert Nokogiri::XML::Reader(<<-eoxml)
|
135
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
136
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
137
|
+
</x>
|
138
|
+
eoxml
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_reader_holds_on_to_string
|
142
|
+
xml = <<-eoxml
|
143
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
144
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
145
|
+
</x>
|
146
|
+
eoxml
|
147
|
+
reader = Nokogiri::XML::Reader(xml)
|
148
|
+
assert_equal xml, reader.source
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_default?
|
152
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
153
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
154
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
155
|
+
</x>
|
156
|
+
eoxml
|
157
|
+
assert_equal false, reader.default?
|
158
|
+
assert_equal [false, false, false, false, false, false, false],
|
159
|
+
reader.map { |x| x.default? }
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_value?
|
163
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
164
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
165
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
166
|
+
</x>
|
167
|
+
eoxml
|
168
|
+
assert_equal false, reader.value?
|
169
|
+
assert_equal [false, true, false, true, false, true, false],
|
170
|
+
reader.map { |x| x.value? }
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_read_error_document
|
174
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
175
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
176
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
177
|
+
<foo>
|
178
|
+
</x>
|
179
|
+
eoxml
|
180
|
+
assert_raises(Nokogiri::XML::SyntaxError) do
|
181
|
+
reader.each { |node| }
|
182
|
+
end
|
183
|
+
assert 1, reader.errors.length
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_attributes?
|
187
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
188
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
189
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
190
|
+
</x>
|
191
|
+
eoxml
|
192
|
+
assert_equal false, reader.attributes?
|
193
|
+
assert_equal [true, false, true, false, true, false, true],
|
194
|
+
reader.map { |x| x.attributes? }
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_attributes
|
198
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
199
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'
|
200
|
+
xmlns='http://mothership.connection.com/'
|
201
|
+
>
|
202
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
203
|
+
</x>
|
204
|
+
eoxml
|
205
|
+
assert_equal({}, reader.attributes)
|
206
|
+
assert_equal [{'xmlns:tenderlove'=>'http://tenderlovemaking.com/',
|
207
|
+
'xmlns'=>'http://mothership.connection.com/'},
|
208
|
+
{}, {"awesome"=>"true"}, {}, {"awesome"=>"true"}, {},
|
209
|
+
{'xmlns:tenderlove'=>'http://tenderlovemaking.com/',
|
210
|
+
'xmlns'=>'http://mothership.connection.com/'}],
|
211
|
+
reader.map { |x| x.attributes }
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_attribute_roundtrip
|
215
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
216
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'
|
217
|
+
xmlns='http://mothership.connection.com/'
|
218
|
+
>
|
219
|
+
<tenderlove:foo awesome='true' size='giant'>snuggles!</tenderlove:foo>
|
220
|
+
</x>
|
221
|
+
eoxml
|
222
|
+
reader.each do |node|
|
223
|
+
node.attributes.each do |key, value|
|
224
|
+
assert_equal value, node.attribute(key)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_attribute_at
|
230
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
231
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
232
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
233
|
+
</x>
|
234
|
+
eoxml
|
235
|
+
assert_nil reader.attribute_at(nil)
|
236
|
+
assert_nil reader.attribute_at(0)
|
237
|
+
assert_equal ['http://tenderlovemaking.com/', nil, 'true', nil, 'true', nil, 'http://tenderlovemaking.com/'],
|
238
|
+
reader.map { |x| x.attribute_at(0) }
|
239
|
+
end
|
240
|
+
|
241
|
+
def test_attribute
|
242
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
243
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
244
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
245
|
+
</x>
|
246
|
+
eoxml
|
247
|
+
assert_nil reader.attribute(nil)
|
248
|
+
assert_nil reader.attribute('awesome')
|
249
|
+
assert_equal [nil, nil, 'true', nil, 'true', nil, nil],
|
250
|
+
reader.map { |x| x.attribute('awesome') }
|
251
|
+
end
|
252
|
+
|
253
|
+
def test_attribute_length
|
254
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
255
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
256
|
+
<tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
|
257
|
+
</x>
|
258
|
+
eoxml
|
259
|
+
assert_equal 0, reader.attribute_count
|
260
|
+
assert_equal [1, 0, 1, 0, 0, 0, 0], reader.map { |x| x.attribute_count }
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_depth
|
264
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
265
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
266
|
+
<tenderlove:foo>snuggles!</tenderlove:foo>
|
267
|
+
</x>
|
268
|
+
eoxml
|
269
|
+
assert_equal 0, reader.depth
|
270
|
+
assert_equal [0, 1, 1, 2, 1, 1, 0], reader.map { |x| x.depth }
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_encoding
|
274
|
+
string = <<-eoxml
|
275
|
+
<awesome>
|
276
|
+
<p xml:lang="en">The quick brown fox jumps over the lazy dog.</p>
|
277
|
+
<p xml:lang="ja">日本語が上手です</p>
|
278
|
+
</awesome>
|
279
|
+
eoxml
|
280
|
+
reader = Nokogiri::XML::Reader.from_memory(string, nil, 'UTF-8')
|
281
|
+
assert_equal ['UTF-8'], reader.map { |x| x.encoding }.uniq
|
282
|
+
end
|
283
|
+
|
284
|
+
def test_xml_version
|
285
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
286
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
287
|
+
<tenderlove:foo>snuggles!</tenderlove:foo>
|
288
|
+
</x>
|
289
|
+
eoxml
|
290
|
+
assert_nil reader.xml_version
|
291
|
+
assert_equal ['1.0'], reader.map { |x| x.xml_version }.uniq
|
292
|
+
end
|
293
|
+
|
294
|
+
def test_lang
|
295
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
296
|
+
<awesome>
|
297
|
+
<p xml:lang="en">The quick brown fox jumps over the lazy dog.</p>
|
298
|
+
<p xml:lang="ja">日本語が上手です</p>
|
299
|
+
</awesome>
|
300
|
+
eoxml
|
301
|
+
assert_nil reader.lang
|
302
|
+
assert_equal [nil, nil, "en", "en", "en", nil, "ja", "ja", "ja", nil, nil],
|
303
|
+
reader.map { |x| x.lang }
|
304
|
+
end
|
305
|
+
|
306
|
+
def test_value
|
307
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
308
|
+
<x xmlns:tenderlove='http://tenderlovemaking.com/'>
|
309
|
+
<tenderlove:foo>snuggles!</tenderlove:foo>
|
310
|
+
</x>
|
311
|
+
eoxml
|
312
|
+
assert_nil reader.value
|
313
|
+
assert_equal [nil, "\n ", nil, "snuggles!", nil, "\n ", nil],
|
314
|
+
reader.map { |x| x.value }
|
315
|
+
end
|
316
|
+
|
317
|
+
def test_prefix
|
318
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
319
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
320
|
+
<edi:foo>hello</edi:foo>
|
321
|
+
</x>
|
322
|
+
eoxml
|
323
|
+
assert_nil reader.prefix
|
324
|
+
assert_equal [nil, nil, "edi", nil, "edi", nil, nil],
|
325
|
+
reader.map { |n| n.prefix }
|
326
|
+
end
|
327
|
+
|
328
|
+
def test_node_type
|
329
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
330
|
+
<x>
|
331
|
+
<y>hello</y>
|
332
|
+
</x>
|
333
|
+
eoxml
|
334
|
+
assert_equal 0, reader.node_type
|
335
|
+
assert_equal [1, 14, 1, 3, 15, 14, 15], reader.map { |n| n.node_type }
|
336
|
+
end
|
337
|
+
|
338
|
+
def test_inner_xml
|
339
|
+
str = "<x><y>hello</y></x>"
|
340
|
+
reader = Nokogiri::XML::Reader.from_memory(str)
|
341
|
+
|
342
|
+
reader.read
|
343
|
+
|
344
|
+
assert_equal "<y>hello</y>", reader.inner_xml
|
345
|
+
end
|
346
|
+
|
347
|
+
def test_outer_xml
|
348
|
+
str = ["<x><y>hello</y></x>", "<y>hello</y>", "hello", "<y/>", "<x/>"]
|
349
|
+
reader = Nokogiri::XML::Reader.from_memory(str.first)
|
350
|
+
|
351
|
+
xml = []
|
352
|
+
reader.map { |node| xml << node.outer_xml }
|
353
|
+
|
354
|
+
assert_equal str, xml
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_outer_xml_with_empty_nodes
|
358
|
+
str = ["<x><y/></x>", "<y/>", "<x/>"]
|
359
|
+
reader = Nokogiri::XML::Reader.from_memory(str.first)
|
360
|
+
|
361
|
+
xml = []
|
362
|
+
reader.map { |node| xml << node.outer_xml }
|
363
|
+
|
364
|
+
assert_equal str, xml
|
365
|
+
end
|
366
|
+
|
367
|
+
def test_state
|
368
|
+
reader = Nokogiri::XML::Reader.from_memory('<foo>bar</bar>')
|
369
|
+
assert reader.state
|
370
|
+
end
|
371
|
+
|
372
|
+
def test_ns_uri
|
373
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
374
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
375
|
+
<edi:foo>hello</edi:foo>
|
376
|
+
</x>
|
377
|
+
eoxml
|
378
|
+
assert_nil reader.namespace_uri
|
379
|
+
assert_equal([nil,
|
380
|
+
nil,
|
381
|
+
"http://ecommerce.example.org/schema",
|
382
|
+
nil,
|
383
|
+
"http://ecommerce.example.org/schema",
|
384
|
+
nil,
|
385
|
+
nil],
|
386
|
+
reader.map { |n| n.namespace_uri })
|
387
|
+
end
|
388
|
+
|
389
|
+
def test_local_name
|
390
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
391
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
392
|
+
<edi:foo>hello</edi:foo>
|
393
|
+
</x>
|
394
|
+
eoxml
|
395
|
+
assert_nil reader.local_name
|
396
|
+
assert_equal(["x", "#text", "foo", "#text", "foo", "#text", "x"],
|
397
|
+
reader.map { |n| n.local_name })
|
398
|
+
end
|
399
|
+
|
400
|
+
def test_name
|
401
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
402
|
+
<x xmlns:edi='http://ecommerce.example.org/schema'>
|
403
|
+
<edi:foo>hello</edi:foo>
|
404
|
+
</x>
|
405
|
+
eoxml
|
406
|
+
assert_nil reader.name
|
407
|
+
assert_equal(["x", "#text", "edi:foo", "#text", "edi:foo", "#text", "x"],
|
408
|
+
reader.map { |n| n.name })
|
409
|
+
end
|
410
|
+
|
411
|
+
def test_base_uri
|
412
|
+
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
|
413
|
+
<x xml:base="http://base.example.org/base/">
|
414
|
+
<link href="link"/>
|
415
|
+
<other xml:base="http://other.example.org/"/>
|
416
|
+
<relative xml:base="relative">
|
417
|
+
<link href="stuff" />
|
418
|
+
</relative>
|
419
|
+
</x>
|
420
|
+
eoxml
|
421
|
+
|
422
|
+
assert_nil reader.base_uri
|
423
|
+
assert_equal(["http://base.example.org/base/",
|
424
|
+
"http://base.example.org/base/",
|
425
|
+
"http://base.example.org/base/",
|
426
|
+
"http://base.example.org/base/",
|
427
|
+
"http://other.example.org/",
|
428
|
+
"http://base.example.org/base/",
|
429
|
+
"http://base.example.org/base/relative",
|
430
|
+
"http://base.example.org/base/relative",
|
431
|
+
"http://base.example.org/base/relative",
|
432
|
+
"http://base.example.org/base/relative",
|
433
|
+
"http://base.example.org/base/relative",
|
434
|
+
"http://base.example.org/base/",
|
435
|
+
"http://base.example.org/base/"],
|
436
|
+
reader.map {|n| n.base_uri })
|
437
|
+
end
|
438
|
+
|
439
|
+
def test_xlink_href_without_base_uri
|
440
|
+
reader = Nokogiri::XML::Reader(<<-eoxml)
|
441
|
+
<x xmlns:xlink="http://www.w3.org/1999/xlink">
|
442
|
+
<link xlink:href="#other">Link</link>
|
443
|
+
<other id="other">Linked Element</other>
|
444
|
+
</x>
|
445
|
+
eoxml
|
446
|
+
|
447
|
+
reader.each do |node|
|
448
|
+
if node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
|
449
|
+
if node.name == 'link'
|
450
|
+
assert_nil node.base_uri
|
451
|
+
end
|
452
|
+
end
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
def test_xlink_href_with_base_uri
|
457
|
+
reader = Nokogiri::XML::Reader(<<-eoxml)
|
458
|
+
<x xml:base="http://base.example.org/base/"
|
459
|
+
xmlns:xlink="http://www.w3.org/1999/xlink">
|
460
|
+
<link xlink:href="#other">Link</link>
|
461
|
+
<other id="other">Linked Element</other>
|
462
|
+
</x>
|
463
|
+
eoxml
|
464
|
+
|
465
|
+
reader.each do |node|
|
466
|
+
if node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
|
467
|
+
assert_equal node.base_uri, "http://base.example.org/base/"
|
468
|
+
end
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
def test_read_from_memory
|
473
|
+
called = false
|
474
|
+
reader = Nokogiri::XML::Reader.from_memory('<foo>bar</foo>')
|
475
|
+
reader.each do |node|
|
476
|
+
called = true
|
477
|
+
assert node
|
478
|
+
end
|
479
|
+
assert called
|
480
|
+
end
|
481
|
+
|
482
|
+
def test_large_document_smoke_test
|
483
|
+
# simply run on a large document to verify that there no GC issues
|
484
|
+
xml = []
|
485
|
+
xml << "<elements>"
|
486
|
+
10000.times { |j| xml << "<element id=\"#{j}\"/>" }
|
487
|
+
xml << "</elements>"
|
488
|
+
xml = xml.join("\n")
|
489
|
+
|
490
|
+
Nokogiri::XML::Reader.from_memory(xml).each do |e|
|
491
|
+
e.attributes
|
492
|
+
end
|
493
|
+
end
|
494
|
+
|
495
|
+
def test_correct_outer_xml_inclusion
|
496
|
+
xml = Nokogiri::XML::Reader.from_io(StringIO.new(<<-eoxml))
|
497
|
+
<root-element>
|
498
|
+
<children>
|
499
|
+
<child n="1">
|
500
|
+
<field>child-1</field>
|
501
|
+
</child>
|
502
|
+
<child n="2">
|
503
|
+
<field>child-2</field>
|
504
|
+
</child>
|
505
|
+
<child n="3">
|
506
|
+
<field>child-3</field>
|
507
|
+
</child>
|
508
|
+
</children>
|
509
|
+
</root-element>
|
510
|
+
eoxml
|
511
|
+
|
512
|
+
nodelengths = []
|
513
|
+
has_child2 = []
|
514
|
+
|
515
|
+
xml.each do |node|
|
516
|
+
if node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT and node.name == "child"
|
517
|
+
nodelengths << node.outer_xml.length
|
518
|
+
has_child2 << !!(node.outer_xml =~ /child-2/)
|
519
|
+
end
|
520
|
+
end
|
521
|
+
|
522
|
+
assert_equal(nodelengths[0], nodelengths[1])
|
523
|
+
assert(has_child2[1])
|
524
|
+
assert(!has_child2[0])
|
525
|
+
end
|
526
|
+
|
527
|
+
def test_correct_inner_xml_inclusion
|
528
|
+
xml = Nokogiri::XML::Reader.from_io(StringIO.new(<<-eoxml))
|
529
|
+
<root-element>
|
530
|
+
<children>
|
531
|
+
<child n="1">
|
532
|
+
<field>child-1</field>
|
533
|
+
</child>
|
534
|
+
<child n="2">
|
535
|
+
<field>child-2</field>
|
536
|
+
</child>
|
537
|
+
<child n="3">
|
538
|
+
<field>child-3</field>
|
539
|
+
</child>
|
540
|
+
</children>
|
541
|
+
</root-element>
|
542
|
+
eoxml
|
543
|
+
|
544
|
+
nodelengths = []
|
545
|
+
has_child2 = []
|
546
|
+
|
547
|
+
xml.each do |node|
|
548
|
+
if node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT and node.name == "child"
|
549
|
+
nodelengths << node.inner_xml.length
|
550
|
+
has_child2 << !!(node.inner_xml =~ /child-2/)
|
551
|
+
end
|
552
|
+
end
|
553
|
+
|
554
|
+
assert_equal(nodelengths[0], nodelengths[1])
|
555
|
+
assert(has_child2[1])
|
556
|
+
assert(!has_child2[0])
|
557
|
+
end
|
558
|
+
end
|