superfeedr-nokogiri 1.4.0.20091116183308
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +27 -0
- data/CHANGELOG.ja.rdoc +330 -0
- data/CHANGELOG.rdoc +314 -0
- data/Manifest.txt +269 -0
- data/README.ja.rdoc +105 -0
- data/README.rdoc +118 -0
- data/Rakefile +244 -0
- data/bin/nokogiri +49 -0
- data/ext/nokogiri/extconf.rb +145 -0
- data/ext/nokogiri/html_document.c +145 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +272 -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 +92 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/nokogiri.c +89 -0
- data/ext/nokogiri/nokogiri.h +145 -0
- data/ext/nokogiri/xml_attr.c +92 -0
- data/ext/nokogiri/xml_attr.h +9 -0
- data/ext/nokogiri/xml_attribute_decl.c +67 -0
- data/ext/nokogiri/xml_attribute_decl.h +9 -0
- data/ext/nokogiri/xml_cdata.c +54 -0
- data/ext/nokogiri/xml_cdata.h +9 -0
- data/ext/nokogiri/xml_comment.c +52 -0
- data/ext/nokogiri/xml_comment.h +9 -0
- data/ext/nokogiri/xml_document.c +388 -0
- data/ext/nokogiri/xml_document.h +24 -0
- data/ext/nokogiri/xml_document_fragment.c +46 -0
- data/ext/nokogiri/xml_document_fragment.h +10 -0
- data/ext/nokogiri/xml_dtd.c +192 -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_entity_decl.c +97 -0
- data/ext/nokogiri/xml_entity_decl.h +10 -0
- data/ext/nokogiri/xml_entity_reference.c +50 -0
- data/ext/nokogiri/xml_entity_reference.h +9 -0
- data/ext/nokogiri/xml_io.c +31 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_namespace.c +74 -0
- data/ext/nokogiri/xml_namespace.h +12 -0
- data/ext/nokogiri/xml_node.c +1060 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +397 -0
- data/ext/nokogiri/xml_node_set.h +9 -0
- data/ext/nokogiri/xml_processing_instruction.c +54 -0
- data/ext/nokogiri/xml_processing_instruction.h +9 -0
- data/ext/nokogiri/xml_reader.c +593 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +159 -0
- data/ext/nokogiri/xml_relax_ng.h +9 -0
- data/ext/nokogiri/xml_sax_parser.c +286 -0
- data/ext/nokogiri/xml_sax_parser.h +43 -0
- data/ext/nokogiri/xml_sax_parser_context.c +155 -0
- data/ext/nokogiri/xml_sax_parser_context.h +10 -0
- data/ext/nokogiri/xml_sax_push_parser.c +114 -0
- data/ext/nokogiri/xml_sax_push_parser.h +9 -0
- data/ext/nokogiri/xml_schema.c +156 -0
- data/ext/nokogiri/xml_schema.h +9 -0
- data/ext/nokogiri/xml_syntax_error.c +261 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +48 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath.c +53 -0
- data/ext/nokogiri/xml_xpath.h +11 -0
- data/ext/nokogiri/xml_xpath_context.c +239 -0
- data/ext/nokogiri/xml_xpath_context.h +9 -0
- data/ext/nokogiri/xslt_stylesheet.c +131 -0
- data/ext/nokogiri/xslt_stylesheet.h +9 -0
- data/lib/nokogiri.rb +116 -0
- data/lib/nokogiri/css.rb +25 -0
- data/lib/nokogiri/css/generated_parser.rb +646 -0
- data/lib/nokogiri/css/generated_tokenizer.rb +142 -0
- data/lib/nokogiri/css/node.rb +99 -0
- data/lib/nokogiri/css/parser.rb +82 -0
- data/lib/nokogiri/css/parser.y +227 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rex +54 -0
- data/lib/nokogiri/css/xpath_visitor.rb +162 -0
- data/lib/nokogiri/decorators/slop.rb +33 -0
- data/lib/nokogiri/ffi/html/document.rb +28 -0
- data/lib/nokogiri/ffi/html/element_description.rb +85 -0
- data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
- data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
- data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
- data/lib/nokogiri/ffi/libxml.rb +356 -0
- data/lib/nokogiri/ffi/structs/common_node.rb +26 -0
- data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
- data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
- data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
- data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
- data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
- data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
- data/lib/nokogiri/ffi/structs/xml_document.rb +108 -0
- data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
- data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
- data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
- data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
- data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
- data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
- data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
- data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
- data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
- data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
- data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
- data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
- data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +15 -0
- data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
- data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
- data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
- data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +37 -0
- data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
- data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
- data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
- data/lib/nokogiri/ffi/xml/attr.rb +41 -0
- data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
- data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
- data/lib/nokogiri/ffi/xml/comment.rb +18 -0
- data/lib/nokogiri/ffi/xml/document.rb +135 -0
- data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
- data/lib/nokogiri/ffi/xml/dtd.rb +69 -0
- data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
- data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
- data/lib/nokogiri/ffi/xml/entity_decl.rb +27 -0
- data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
- data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
- data/lib/nokogiri/ffi/xml/node.rb +444 -0
- data/lib/nokogiri/ffi/xml/node_set.rb +133 -0
- data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
- data/lib/nokogiri/ffi/xml/reader.rb +227 -0
- data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
- data/lib/nokogiri/ffi/xml/sax/parser.rb +142 -0
- data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
- data/lib/nokogiri/ffi/xml/sax/push_parser.rb +39 -0
- data/lib/nokogiri/ffi/xml/schema.rb +92 -0
- data/lib/nokogiri/ffi/xml/syntax_error.rb +91 -0
- data/lib/nokogiri/ffi/xml/text.rb +18 -0
- data/lib/nokogiri/ffi/xml/xpath.rb +19 -0
- data/lib/nokogiri/ffi/xml/xpath_context.rb +135 -0
- data/lib/nokogiri/ffi/xslt/stylesheet.rb +47 -0
- data/lib/nokogiri/html.rb +35 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +88 -0
- data/lib/nokogiri/html/document_fragment.rb +15 -0
- data/lib/nokogiri/html/element_description.rb +23 -0
- data/lib/nokogiri/html/entity_lookup.rb +13 -0
- data/lib/nokogiri/html/sax/parser.rb +48 -0
- data/lib/nokogiri/html/sax/parser_context.rb +16 -0
- data/lib/nokogiri/syntax_error.rb +4 -0
- data/lib/nokogiri/version.rb +33 -0
- data/lib/nokogiri/version_warning.rb +11 -0
- data/lib/nokogiri/xml.rb +67 -0
- data/lib/nokogiri/xml/attr.rb +14 -0
- data/lib/nokogiri/xml/attribute_decl.rb +18 -0
- data/lib/nokogiri/xml/builder.rb +405 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +131 -0
- data/lib/nokogiri/xml/document_fragment.rb +69 -0
- data/lib/nokogiri/xml/dtd.rb +11 -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 +15 -0
- data/lib/nokogiri/xml/fragment_handler.rb +71 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +665 -0
- data/lib/nokogiri/xml/node/save_options.rb +42 -0
- data/lib/nokogiri/xml/node_set.rb +307 -0
- data/lib/nokogiri/xml/notation.rb +6 -0
- data/lib/nokogiri/xml/parse_options.rb +85 -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 +74 -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 +160 -0
- data/lib/nokogiri/xml/sax/parser.rb +115 -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 +61 -0
- data/lib/nokogiri/xml/syntax_error.rb +38 -0
- data/lib/nokogiri/xml/xpath.rb +10 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xslt.rb +48 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xsd/xmlparser/nokogiri.rb +71 -0
- data/tasks/test.rb +100 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +277 -0
- data/test/css/test_tokenizer.rb +183 -0
- data/test/css/test_xpath_visitor.rb +76 -0
- data/test/ffi/test_document.rb +35 -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/bar/bar.xsd +4 -0
- data/test/files/dont_hurt_em_why.xml +422 -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/po.xml +32 -0
- data/test/files/po.xsd +66 -0
- data/test/files/shift_jis.html +10 -0
- data/test/files/shift_jis.xml +5 -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/tlm.html +850 -0
- data/test/files/valid_bar.xml +2 -0
- data/test/helper.rb +136 -0
- data/test/html/sax/test_parser.rb +64 -0
- data/test/html/sax/test_parser_context.rb +48 -0
- data/test/html/test_builder.rb +164 -0
- data/test/html/test_document.rb +390 -0
- data/test/html/test_document_encoding.rb +77 -0
- data/test/html/test_document_fragment.rb +132 -0
- data/test/html/test_element_description.rb +94 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +228 -0
- data/test/html/test_node_encoding.rb +27 -0
- data/test/test_convert_xpath.rb +135 -0
- data/test/test_css_cache.rb +45 -0
- data/test/test_gc.rb +15 -0
- data/test/test_memory_leak.rb +77 -0
- data/test/test_nokogiri.rb +134 -0
- data/test/test_reader.rb +358 -0
- data/test/test_xslt_transforms.rb +131 -0
- data/test/xml/node/test_save_options.rb +20 -0
- data/test/xml/node/test_subclass.rb +44 -0
- data/test/xml/sax/test_parser.rb +307 -0
- data/test/xml/sax/test_parser_context.rb +56 -0
- data/test/xml/sax/test_push_parser.rb +131 -0
- data/test/xml/test_attr.rb +38 -0
- data/test/xml/test_attribute_decl.rb +82 -0
- data/test/xml/test_builder.rb +167 -0
- data/test/xml/test_cdata.rb +38 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +607 -0
- data/test/xml/test_document_encoding.rb +26 -0
- data/test/xml/test_document_fragment.rb +138 -0
- data/test/xml/test_dtd.rb +82 -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 +83 -0
- data/test/xml/test_entity_reference.rb +21 -0
- data/test/xml/test_namespace.rb +68 -0
- data/test/xml/test_node.rb +889 -0
- data/test/xml/test_node_attributes.rb +34 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_set.rb +531 -0
- data/test/xml/test_parse_options.rb +52 -0
- data/test/xml/test_processing_instruction.rb +30 -0
- data/test/xml/test_reader_encoding.rb +126 -0
- data/test/xml/test_relax_ng.rb +60 -0
- data/test/xml/test_schema.rb +89 -0
- data/test/xml/test_syntax_error.rb +27 -0
- data/test/xml/test_text.rb +30 -0
- data/test/xml/test_unparented_node.rb +381 -0
- data/test/xml/test_xpath.rb +106 -0
- metadata +430 -0
@@ -0,0 +1,34 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class TestNodeAttributes < Nokogiri::TestCase
|
6
|
+
def test_attribute_with_ns
|
7
|
+
doc = Nokogiri::XML <<-eoxml
|
8
|
+
<root xmlns:tlm='http://tenderlovemaking.com/'>
|
9
|
+
<node tlm:foo='bar' foo='baz' />
|
10
|
+
</root>
|
11
|
+
eoxml
|
12
|
+
|
13
|
+
node = doc.at('node')
|
14
|
+
|
15
|
+
assert_equal 'bar',
|
16
|
+
node.attribute_with_ns('foo', 'http://tenderlovemaking.com/').value
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_namespace_key?
|
20
|
+
doc = Nokogiri::XML <<-eoxml
|
21
|
+
<root xmlns:tlm='http://tenderlovemaking.com/'>
|
22
|
+
<node tlm:foo='bar' foo='baz' />
|
23
|
+
</root>
|
24
|
+
eoxml
|
25
|
+
|
26
|
+
node = doc.at('node')
|
27
|
+
|
28
|
+
assert node.namespaced_key?('foo', 'http://tenderlovemaking.com/')
|
29
|
+
assert node.namespaced_key?('foo', nil)
|
30
|
+
assert !node.namespaced_key?('foo', 'foo')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
if RUBY_VERSION =~ /^1\.9/
|
6
|
+
class TestNodeEncoding < Nokogiri::TestCase
|
7
|
+
def setup
|
8
|
+
super
|
9
|
+
@html = Nokogiri::HTML(File.read(HTML_FILE), HTML_FILE)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_get_attribute
|
13
|
+
node = @html.css('a').first
|
14
|
+
assert_equal @html.encoding, node['href'].encoding.name
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_text_encoding_is_utf_8
|
18
|
+
@html = Nokogiri::HTML(File.open(NICH_FILE))
|
19
|
+
assert_equal 'UTF-8', @html.text.encoding.name
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_serialize_encoding_html
|
23
|
+
@html = Nokogiri::HTML(File.open(NICH_FILE))
|
24
|
+
assert_equal @html.encoding.downcase,
|
25
|
+
@html.serialize.encoding.name.downcase
|
26
|
+
|
27
|
+
@doc = Nokogiri::HTML(@html.serialize)
|
28
|
+
assert_equal @html.serialize, @doc.serialize
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_serialize_encoding_xml
|
32
|
+
@xml = Nokogiri::XML(File.open(SHIFT_JIS_XML))
|
33
|
+
assert_equal @xml.encoding.downcase,
|
34
|
+
@xml.serialize.encoding.name.downcase
|
35
|
+
|
36
|
+
@doc = Nokogiri::XML(@xml.serialize)
|
37
|
+
assert_equal @xml.serialize, @doc.serialize
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_encode_special_chars
|
41
|
+
foo = @html.css('a').first.encode_special_chars('foo')
|
42
|
+
assert_equal @html.encoding, foo.encoding.name
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_content
|
46
|
+
node = @html.css('a').first
|
47
|
+
assert_equal @html.encoding, node.content.encoding.name
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_name
|
51
|
+
node = @html.css('a').first
|
52
|
+
assert_equal @html.encoding, node.name.encoding.name
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_path
|
56
|
+
node = @html.css('a').first
|
57
|
+
assert_equal @html.encoding, node.path.encoding.name
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_namespace
|
61
|
+
xml = <<-eoxml
|
62
|
+
<root>
|
63
|
+
<car xmlns:part="http://general-motors.com/">
|
64
|
+
<part:tire>Michelin Model XGV</part:tire>
|
65
|
+
</car>
|
66
|
+
<bicycle xmlns:part="http://schwinn.com/">
|
67
|
+
<part:tire>I'm a bicycle tire!</part:tire>
|
68
|
+
</bicycle>
|
69
|
+
</root>
|
70
|
+
eoxml
|
71
|
+
doc = Nokogiri::XML(xml, nil, 'UTF-8')
|
72
|
+
assert_equal 'UTF-8', doc.encoding
|
73
|
+
n = doc.xpath('//part:tire', { 'part' => 'http://schwinn.com/' }).first
|
74
|
+
assert n
|
75
|
+
assert_equal doc.encoding, n.namespace.href.encoding.name
|
76
|
+
assert_equal doc.encoding, n.namespace.prefix.encoding.name
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_namespace_as_hash
|
80
|
+
xml = <<-eoxml
|
81
|
+
<root>
|
82
|
+
<car xmlns:part="http://general-motors.com/">
|
83
|
+
<part:tire>Michelin Model XGV</part:tire>
|
84
|
+
</car>
|
85
|
+
<bicycle xmlns:part="http://schwinn.com/">
|
86
|
+
<part:tire>I'm a bicycle tire!</part:tire>
|
87
|
+
</bicycle>
|
88
|
+
</root>
|
89
|
+
eoxml
|
90
|
+
doc = Nokogiri::XML(xml, nil, 'UTF-8')
|
91
|
+
assert_equal 'UTF-8', doc.encoding
|
92
|
+
assert n = doc.xpath('//car').first
|
93
|
+
|
94
|
+
n.namespace_definitions.each do |nd|
|
95
|
+
assert_equal doc.encoding, nd.href.encoding.name
|
96
|
+
assert_equal doc.encoding, nd.prefix.encoding.name
|
97
|
+
end
|
98
|
+
|
99
|
+
n.namespaces.each do |k,v|
|
100
|
+
assert_equal doc.encoding, k.encoding.name
|
101
|
+
assert_equal doc.encoding, v.encoding.name
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,531 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class TestNodeSet < Nokogiri::TestCase
|
6
|
+
def setup
|
7
|
+
super
|
8
|
+
@xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
|
9
|
+
@list = @xml.css('employee')
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_remove_attr
|
13
|
+
@list.each { |x| x['class'] = 'blah' }
|
14
|
+
assert_equal @list, @list.remove_attr('class')
|
15
|
+
@list.each { |x| assert_nil x['class'] }
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_add_class
|
19
|
+
assert_equal @list, @list.add_class('bar')
|
20
|
+
@list.each { |x| assert_equal 'bar', x['class'] }
|
21
|
+
|
22
|
+
@list.add_class('bar')
|
23
|
+
@list.each { |x| assert_equal 'bar', x['class'] }
|
24
|
+
|
25
|
+
@list.add_class('baz')
|
26
|
+
@list.each { |x| assert_equal 'bar baz', x['class'] }
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_remove_class_with_no_class
|
30
|
+
assert_equal @list, @list.remove_class('bar')
|
31
|
+
@list.each { |e| assert_nil e['class'] }
|
32
|
+
|
33
|
+
@list.each { |e| e['class'] = '' }
|
34
|
+
assert_equal @list, @list.remove_class('bar')
|
35
|
+
@list.each { |e| assert_nil e['class'] }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_remove_class_single
|
39
|
+
@list.each { |e| e['class'] = 'foo bar' }
|
40
|
+
|
41
|
+
assert_equal @list, @list.remove_class('bar')
|
42
|
+
@list.each { |e| assert_equal 'foo', e['class'] }
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_remove_class_completely
|
46
|
+
@list.each { |e| e['class'] = 'foo' }
|
47
|
+
|
48
|
+
assert_equal @list, @list.remove_class
|
49
|
+
@list.each { |e| assert_nil e['class'] }
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_attribute_set
|
53
|
+
@list.each { |e| assert_nil e['foo'] }
|
54
|
+
|
55
|
+
[ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
|
56
|
+
@list.send(t.first.to_sym, 'foo', t.last)
|
57
|
+
@list.each { |e| assert_equal t.last, e['foo'] }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_attribute_set_with_block
|
62
|
+
@list.each { |e| assert_nil e['foo'] }
|
63
|
+
|
64
|
+
[ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
|
65
|
+
@list.send(t.first.to_sym, 'foo') { |x| t.last }
|
66
|
+
@list.each { |e| assert_equal t.last, e['foo'] }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_attribute_set_with_hash
|
71
|
+
@list.each { |e| assert_nil e['foo'] }
|
72
|
+
|
73
|
+
[ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
|
74
|
+
@list.send(t.first.to_sym, 'foo' => t.last)
|
75
|
+
@list.each { |e| assert_equal t.last, e['foo'] }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_attribute_no_args
|
80
|
+
@list.first['foo'] = 'bar'
|
81
|
+
assert_equal @list.first.attribute('foo'), @list.attribute('foo')
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_search_empty_node_set
|
85
|
+
set = Nokogiri::XML::NodeSet.new(Nokogiri::XML::Document.new)
|
86
|
+
assert_equal 0, set.css('foo').length
|
87
|
+
assert_equal 0, set.xpath('.//foo').length
|
88
|
+
assert_equal 0, set.search('foo').length
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_xpath_with_custom_object
|
92
|
+
set = @xml.xpath('//staff')
|
93
|
+
custom_employees = set.xpath('//*[awesome(.)]', Class.new {
|
94
|
+
def awesome ns
|
95
|
+
ns.select { |n| n.name == 'employee' }
|
96
|
+
end
|
97
|
+
}.new)
|
98
|
+
|
99
|
+
assert_equal @xml.xpath('//employee'), custom_employees
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_css_with_custom_object
|
103
|
+
set = @xml.xpath('//staff')
|
104
|
+
custom_employees = set.css('*:awesome', Class.new {
|
105
|
+
def awesome ns
|
106
|
+
ns.select { |n| n.name == 'employee' }
|
107
|
+
end
|
108
|
+
}.new)
|
109
|
+
|
110
|
+
assert_equal @xml.xpath('//employee'), custom_employees
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_search_with_custom_object
|
114
|
+
set = @xml.xpath('//staff')
|
115
|
+
custom_employees = set.search('//*[awesome(.)]', Class.new {
|
116
|
+
def awesome ns
|
117
|
+
ns.select { |n| n.name == 'employee' }
|
118
|
+
end
|
119
|
+
}.new)
|
120
|
+
|
121
|
+
assert_equal @xml.xpath('//employee'), custom_employees
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_css_searches_match_self
|
125
|
+
html = Nokogiri::HTML("<html><body><div class='a'></div></body></html>")
|
126
|
+
set = html.xpath("/html/body/div")
|
127
|
+
assert_equal set.first, set.css(".a").first
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_search_with_css_matches_self
|
131
|
+
html = Nokogiri::HTML("<html><body><div class='a'></div></body></html>")
|
132
|
+
set = html.xpath("/html/body/div")
|
133
|
+
assert_equal set.first, set.search(".a").first
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_double_equal
|
137
|
+
assert node_set_one = @xml.xpath('//employee')
|
138
|
+
assert node_set_two = @xml.xpath('//employee')
|
139
|
+
|
140
|
+
assert_not_equal node_set_one.object_id, node_set_two.object_id
|
141
|
+
|
142
|
+
assert_equal node_set_one, node_set_two
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_node_set_not_equal_to_string
|
146
|
+
node_set_one = @xml.xpath('//employee')
|
147
|
+
assert_not_equal node_set_one, "asdfadsf"
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_out_of_order_not_equal
|
151
|
+
one = @xml.xpath('//employee')
|
152
|
+
two = @xml.xpath('//employee')
|
153
|
+
two.push two.shift
|
154
|
+
assert_not_equal one, two
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_shorter_is_not_equal
|
158
|
+
node_set_one = @xml.xpath('//employee')
|
159
|
+
node_set_two = @xml.xpath('//employee')
|
160
|
+
node_set_two.delete(node_set_two.first)
|
161
|
+
|
162
|
+
assert_not_equal node_set_one, node_set_two
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_pop
|
166
|
+
set = @xml.xpath('//employee')
|
167
|
+
last = set.last
|
168
|
+
assert_equal last, set.pop
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_shift
|
172
|
+
set = @xml.xpath('//employee')
|
173
|
+
first = set.first
|
174
|
+
assert_equal first, set.shift
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_shift_empty
|
178
|
+
set = Nokogiri::XML::NodeSet.new(@xml)
|
179
|
+
assert_nil set.shift
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_pop_empty
|
183
|
+
set = Nokogiri::XML::NodeSet.new(@xml)
|
184
|
+
assert_nil set.pop
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_first_takes_arguments
|
188
|
+
assert node_set = @xml.xpath('//employee')
|
189
|
+
assert_equal 2, node_set.first(2).length
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_dup
|
193
|
+
assert node_set = @xml.xpath('//employee')
|
194
|
+
dup = node_set.dup
|
195
|
+
assert_equal node_set.length, dup.length
|
196
|
+
node_set.zip(dup).each do |a,b|
|
197
|
+
assert_equal a, b
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_xmlns_is_automatically_registered
|
202
|
+
doc = Nokogiri::XML(<<-eoxml)
|
203
|
+
<root xmlns="http://tenderlovemaking.com/">
|
204
|
+
<foo>
|
205
|
+
<bar/>
|
206
|
+
</foo>
|
207
|
+
</root>
|
208
|
+
eoxml
|
209
|
+
set = doc.css('foo')
|
210
|
+
assert_equal 1, set.css('xmlns|bar').length
|
211
|
+
assert_equal 0, set.css('|bar').length
|
212
|
+
assert_equal 1, set.xpath('//xmlns:bar').length
|
213
|
+
assert_equal 1, set.search('xmlns|bar').length
|
214
|
+
assert_equal 1, set.search('//xmlns:bar').length
|
215
|
+
assert set.at('//xmlns:bar')
|
216
|
+
assert set.at('xmlns|bar')
|
217
|
+
assert set.at('bar')
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_children_has_document
|
221
|
+
set = @xml.root.children
|
222
|
+
assert_instance_of(NodeSet, set)
|
223
|
+
assert_equal @xml, set.document
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_length_size
|
227
|
+
assert node_set = @xml.search('//employee')
|
228
|
+
assert_equal node_set.length, node_set.size
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_to_xml
|
232
|
+
assert node_set = @xml.search('//employee')
|
233
|
+
assert node_set.to_xml
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_inner_html
|
237
|
+
doc = Nokogiri::HTML(<<-eohtml)
|
238
|
+
<html>
|
239
|
+
<body>
|
240
|
+
<div>
|
241
|
+
<a>one</a>
|
242
|
+
</div>
|
243
|
+
<div>
|
244
|
+
<a>two</a>
|
245
|
+
</div>
|
246
|
+
</body>
|
247
|
+
</html>
|
248
|
+
eohtml
|
249
|
+
assert html = doc.css('div').inner_html
|
250
|
+
assert_match '<a>', html
|
251
|
+
end
|
252
|
+
|
253
|
+
def test_at
|
254
|
+
assert node_set = @xml.search('//employee')
|
255
|
+
assert_equal node_set.first, node_set.at(0)
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_percent
|
259
|
+
assert node_set = @xml.search('//employee')
|
260
|
+
assert_equal node_set.first, node_set % 0
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_to_ary
|
264
|
+
assert node_set = @xml.search('//employee')
|
265
|
+
foo = []
|
266
|
+
foo += node_set
|
267
|
+
assert_equal node_set.length, foo.length
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_push
|
271
|
+
node = Nokogiri::XML::Node.new('foo', @xml)
|
272
|
+
node.content = 'bar'
|
273
|
+
|
274
|
+
assert node_set = @xml.search('//employee')
|
275
|
+
node_set.push(node)
|
276
|
+
|
277
|
+
assert node_set.include?(node)
|
278
|
+
end
|
279
|
+
|
280
|
+
def test_delete_with_invalid_argument
|
281
|
+
employees = @xml.search("//employee")
|
282
|
+
positions = @xml.search("//position")
|
283
|
+
|
284
|
+
assert_raises(ArgumentError) { employees.delete(positions) }
|
285
|
+
end
|
286
|
+
|
287
|
+
def test_delete_when_present
|
288
|
+
employees = @xml.search("//employee")
|
289
|
+
wally = employees.first
|
290
|
+
assert employees.include?(wally) # testing setup
|
291
|
+
length = employees.length
|
292
|
+
|
293
|
+
result = employees.delete(wally)
|
294
|
+
assert_equal result, wally
|
295
|
+
assert ! employees.include?(wally)
|
296
|
+
assert length-1, employees.length
|
297
|
+
end
|
298
|
+
|
299
|
+
def test_delete_when_not_present
|
300
|
+
employees = @xml.search("//employee")
|
301
|
+
phb = @xml.search("//position").first
|
302
|
+
assert ! employees.include?(phb) # testing setup
|
303
|
+
length = employees.length
|
304
|
+
|
305
|
+
result = employees.delete(phb)
|
306
|
+
assert_nil result
|
307
|
+
assert length, employees.length
|
308
|
+
end
|
309
|
+
|
310
|
+
def test_unlink
|
311
|
+
xml = Nokogiri::XML.parse(<<-eoxml)
|
312
|
+
<root>
|
313
|
+
<a class='foo bar'>Bar</a>
|
314
|
+
<a class='bar foo'>Bar</a>
|
315
|
+
<a class='bar'>Bar</a>
|
316
|
+
<a>Hello world</a>
|
317
|
+
<a class='baz bar foo'>Bar</a>
|
318
|
+
<a class='bazbarfoo'>Awesome</a>
|
319
|
+
<a class='bazbar'>Awesome</a>
|
320
|
+
</root>
|
321
|
+
eoxml
|
322
|
+
set = xml.xpath('//a')
|
323
|
+
set.unlink
|
324
|
+
set.each do |node|
|
325
|
+
assert !node.parent
|
326
|
+
#assert !node.document
|
327
|
+
assert !node.previous_sibling
|
328
|
+
assert !node.next_sibling
|
329
|
+
end
|
330
|
+
assert_no_match(/Hello world/, xml.to_s)
|
331
|
+
end
|
332
|
+
|
333
|
+
def test_nodeset_search_takes_namespace
|
334
|
+
@xml = Nokogiri::XML.parse(<<-eoxml)
|
335
|
+
<root>
|
336
|
+
<car xmlns:part="http://general-motors.com/">
|
337
|
+
<part:tire>Michelin Model XGV</part:tire>
|
338
|
+
</car>
|
339
|
+
<bicycle xmlns:part="http://schwinn.com/">
|
340
|
+
<part:tire>I'm a bicycle tire!</part:tire>
|
341
|
+
</bicycle>
|
342
|
+
</root>
|
343
|
+
eoxml
|
344
|
+
set = @xml/'root'
|
345
|
+
assert_equal 1, set.length
|
346
|
+
bike_tire = set.search('//bike:tire', 'bike' => "http://schwinn.com/")
|
347
|
+
assert_equal 1, bike_tire.length
|
348
|
+
end
|
349
|
+
|
350
|
+
def test_new_nodeset
|
351
|
+
node_set = Nokogiri::XML::NodeSet.new(@xml)
|
352
|
+
assert_equal(0, node_set.length)
|
353
|
+
node = Nokogiri::XML::Node.new('form', @xml)
|
354
|
+
node_set << node
|
355
|
+
assert_equal(1, node_set.length)
|
356
|
+
assert_equal(node, node_set.last)
|
357
|
+
end
|
358
|
+
|
359
|
+
def test_search_on_nodeset
|
360
|
+
assert node_set = @xml.search('//employee')
|
361
|
+
assert sub_set = node_set.search('.//name')
|
362
|
+
assert_equal(node_set.length, sub_set.length)
|
363
|
+
end
|
364
|
+
|
365
|
+
def test_negative_index_works
|
366
|
+
assert node_set = @xml.search('//employee')
|
367
|
+
assert_equal node_set.last, node_set[-1]
|
368
|
+
end
|
369
|
+
|
370
|
+
def test_large_negative_index_returns_nil
|
371
|
+
assert node_set = @xml.search('//employee')
|
372
|
+
assert_nil(node_set[-1 * (node_set.length + 1)])
|
373
|
+
end
|
374
|
+
|
375
|
+
def test_node_set_fetches_private_data
|
376
|
+
assert node_set = @xml.search('//employee')
|
377
|
+
|
378
|
+
set = node_set
|
379
|
+
assert_equal(set[0], set[0])
|
380
|
+
end
|
381
|
+
|
382
|
+
def test_node_set_returns_0
|
383
|
+
assert node_set = @xml.search('//asdkfjhasdlkfjhaldskfh')
|
384
|
+
assert_equal(0, node_set.length)
|
385
|
+
end
|
386
|
+
|
387
|
+
def test_wrap
|
388
|
+
employees = (@xml/"//employee").wrap("<wrapper/>")
|
389
|
+
assert_equal 'wrapper', employees[0].parent.name
|
390
|
+
assert_equal 'employee', @xml.search("//wrapper").first.children[0].name
|
391
|
+
end
|
392
|
+
|
393
|
+
def test_wrap_preserves_document_structure
|
394
|
+
assert_equal "employeeId",
|
395
|
+
@xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
|
396
|
+
@xml.xpath("//employeeId[text()='EMP0001']").wrap("<wrapper/>")
|
397
|
+
assert_equal "wrapper",
|
398
|
+
@xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
|
399
|
+
end
|
400
|
+
|
401
|
+
def test_plus_operator
|
402
|
+
names = @xml.search("name")
|
403
|
+
positions = @xml.search("position")
|
404
|
+
|
405
|
+
names_len = names.length
|
406
|
+
positions_len = positions.length
|
407
|
+
|
408
|
+
assert_raises(ArgumentError) { result = names + positions.first }
|
409
|
+
|
410
|
+
result = names + positions
|
411
|
+
assert_equal names_len, names.length
|
412
|
+
assert_equal positions_len, positions.length
|
413
|
+
assert_equal names.length + positions.length, result.length
|
414
|
+
|
415
|
+
names += positions
|
416
|
+
assert_equal result.length, names.length
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_union
|
420
|
+
names = @xml.search("name")
|
421
|
+
|
422
|
+
assert_equal(names.length, (names | @xml.search("name")).length)
|
423
|
+
end
|
424
|
+
|
425
|
+
def test_minus_operator
|
426
|
+
employees = @xml.search("//employee")
|
427
|
+
females = @xml.search("//employee[gender[text()='Female']]")
|
428
|
+
|
429
|
+
employees_len = employees.length
|
430
|
+
females_len = females.length
|
431
|
+
|
432
|
+
assert_raises(ArgumentError) { result = employees - females.first }
|
433
|
+
|
434
|
+
result = employees - females
|
435
|
+
assert_equal employees_len, employees.length
|
436
|
+
assert_equal females_len, females.length
|
437
|
+
assert_equal employees.length - females.length, result.length
|
438
|
+
|
439
|
+
employees -= females
|
440
|
+
assert_equal result.length, employees.length
|
441
|
+
end
|
442
|
+
|
443
|
+
def test_array_index
|
444
|
+
employees = @xml.search("//employee")
|
445
|
+
other = @xml.search("//position").first
|
446
|
+
|
447
|
+
assert_equal 3, employees.index(employees[3])
|
448
|
+
assert_nil employees.index(other)
|
449
|
+
end
|
450
|
+
|
451
|
+
def test_slice_too_far
|
452
|
+
employees = @xml.search("//employee")
|
453
|
+
assert_equal employees.length, employees[0, employees.length + 1].length
|
454
|
+
assert_equal employees.length, employees[0, employees.length].length
|
455
|
+
end
|
456
|
+
|
457
|
+
def test_array_slice_with_start_and_end
|
458
|
+
employees = @xml.search("//employee")
|
459
|
+
assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
|
460
|
+
end
|
461
|
+
|
462
|
+
def test_array_index_bracket_equivalence
|
463
|
+
employees = @xml.search("//employee")
|
464
|
+
assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
|
465
|
+
assert_equal [employees[1], employees[2], employees[3]], employees.slice(1,3).to_a
|
466
|
+
end
|
467
|
+
|
468
|
+
def test_array_slice_with_negative_start
|
469
|
+
employees = @xml.search("//employee")
|
470
|
+
assert_equal [employees[2]], employees[-3,1].to_a
|
471
|
+
assert_equal [employees[2], employees[3]], employees[-3,2].to_a
|
472
|
+
end
|
473
|
+
|
474
|
+
def test_array_slice_with_invalid_args
|
475
|
+
employees = @xml.search("//employee")
|
476
|
+
assert_nil employees[99, 1] # large start
|
477
|
+
assert_nil employees[1, -1] # negative len
|
478
|
+
assert_equal [], employees[1, 0].to_a # zero len
|
479
|
+
end
|
480
|
+
|
481
|
+
def test_array_slice_with_range
|
482
|
+
employees = @xml.search("//employee")
|
483
|
+
assert_equal [employees[1], employees[2], employees[3]], employees[1..3].to_a
|
484
|
+
assert_equal [employees[0], employees[1], employees[2], employees[3]], employees[0..3].to_a
|
485
|
+
end
|
486
|
+
|
487
|
+
def test_intersection_with_no_overlap
|
488
|
+
employees = @xml.search("//employee")
|
489
|
+
positions = @xml.search("//position")
|
490
|
+
|
491
|
+
assert_equal [], (employees & positions).to_a
|
492
|
+
end
|
493
|
+
|
494
|
+
def test_intersection
|
495
|
+
employees = @xml.search("//employee")
|
496
|
+
first_set = employees[0..2]
|
497
|
+
second_set = employees[2..4]
|
498
|
+
|
499
|
+
assert_equal [employees[2]], (first_set & second_set).to_a
|
500
|
+
end
|
501
|
+
|
502
|
+
def test_include?
|
503
|
+
employees = @xml.search("//employee")
|
504
|
+
yes = employees.first
|
505
|
+
no = @xml.search("//position").first
|
506
|
+
|
507
|
+
assert employees.include?(yes)
|
508
|
+
assert ! employees.include?(no)
|
509
|
+
end
|
510
|
+
|
511
|
+
def test_children
|
512
|
+
employees = @xml.search("//employee")
|
513
|
+
count = 0
|
514
|
+
employees.each do |employee|
|
515
|
+
count += employee.children.length
|
516
|
+
end
|
517
|
+
set = employees.children
|
518
|
+
assert_equal count, set.length
|
519
|
+
end
|
520
|
+
|
521
|
+
def test_inspect
|
522
|
+
employees = @xml.search("//employee")
|
523
|
+
inspected = employees.inspect
|
524
|
+
|
525
|
+
assert_equal "[#{employees.map { |x| x.inspect }.join(', ')}]",
|
526
|
+
inspected
|
527
|
+
end
|
528
|
+
|
529
|
+
end
|
530
|
+
end
|
531
|
+
end
|