glebm-nokogiri 1.4.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +26 -0
- data/CHANGELOG.ja.rdoc +411 -0
- data/CHANGELOG.rdoc +397 -0
- data/Manifest.txt +276 -0
- data/README.ja.rdoc +106 -0
- data/README.rdoc +132 -0
- data/Rakefile +183 -0
- data/bin/nokogiri +49 -0
- data/deps.rip +5 -0
- data/ext/nokogiri/extconf.rb +97 -0
- data/ext/nokogiri/html_document.c +154 -0
- data/ext/nokogiri/html_document.h +10 -0
- data/ext/nokogiri/html_element_description.c +276 -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 +94 -0
- data/ext/nokogiri/html_sax_parser_context.h +11 -0
- data/ext/nokogiri/nokogiri.c +95 -0
- data/ext/nokogiri/nokogiri.h +153 -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 +464 -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 +31 -0
- data/ext/nokogiri/xml_io.h +11 -0
- data/ext/nokogiri/xml_namespace.c +84 -0
- data/ext/nokogiri/xml_namespace.h +13 -0
- data/ext/nokogiri/xml_node.c +1347 -0
- data/ext/nokogiri/xml_node.h +13 -0
- data/ext/nokogiri/xml_node_set.c +418 -0
- data/ext/nokogiri/xml_node_set.h +9 -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 +665 -0
- data/ext/nokogiri/xml_reader.h +10 -0
- data/ext/nokogiri/xml_relax_ng.c +168 -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 +39 -0
- data/ext/nokogiri/xml_sax_parser_context.c +159 -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 +58 -0
- data/ext/nokogiri/xml_syntax_error.h +13 -0
- data/ext/nokogiri/xml_text.c +50 -0
- data/ext/nokogiri/xml_text.h +9 -0
- data/ext/nokogiri/xml_xpath_context.c +276 -0
- data/ext/nokogiri/xml_xpath_context.h +9 -0
- data/ext/nokogiri/xslt_stylesheet.c +142 -0
- data/ext/nokogiri/xslt_stylesheet.h +9 -0
- data/lib/nokogiri.rb +133 -0
- data/lib/nokogiri/css.rb +25 -0
- data/lib/nokogiri/css/generated_parser.rb +669 -0
- data/lib/nokogiri/css/generated_tokenizer.rb +145 -0
- data/lib/nokogiri/css/node.rb +99 -0
- data/lib/nokogiri/css/parser.rb +82 -0
- data/lib/nokogiri/css/parser.y +232 -0
- data/lib/nokogiri/css/syntax_error.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rb +7 -0
- data/lib/nokogiri/css/tokenizer.rex +55 -0
- data/lib/nokogiri/css/xpath_visitor.rb +169 -0
- data/lib/nokogiri/decorators/slop.rb +33 -0
- data/lib/nokogiri/ffi/encoding_handler.rb +42 -0
- data/lib/nokogiri/ffi/html/document.rb +28 -0
- data/lib/nokogiri/ffi/html/element_description.rb +81 -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 +386 -0
- data/lib/nokogiri/ffi/structs/common_node.rb +38 -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_char_encoding_handler.rb +11 -0
- data/lib/nokogiri/ffi/structs/xml_document.rb +117 -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 +124 -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 +38 -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/weak_bucket.rb +40 -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 +162 -0
- data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
- data/lib/nokogiri/ffi/xml/dtd.rb +67 -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 +36 -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 +556 -0
- data/lib/nokogiri/ffi/xml/node_set.rb +149 -0
- data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
- data/lib/nokogiri/ffi/xml/reader.rb +232 -0
- data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
- data/lib/nokogiri/ffi/xml/sax/parser.rb +135 -0
- data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
- data/lib/nokogiri/ffi/xml/sax/push_parser.rb +51 -0
- data/lib/nokogiri/ffi/xml/schema.rb +109 -0
- data/lib/nokogiri/ffi/xml/syntax_error.rb +98 -0
- data/lib/nokogiri/ffi/xml/text.rb +18 -0
- data/lib/nokogiri/ffi/xml/xpath.rb +9 -0
- data/lib/nokogiri/ffi/xml/xpath_context.rb +148 -0
- data/lib/nokogiri/ffi/xslt/stylesheet.rb +53 -0
- data/lib/nokogiri/html.rb +35 -0
- data/lib/nokogiri/html/builder.rb +35 -0
- data/lib/nokogiri/html/document.rb +90 -0
- data/lib/nokogiri/html/document_fragment.rb +36 -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 +37 -0
- data/lib/nokogiri/version_warning.rb +14 -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 +418 -0
- data/lib/nokogiri/xml/cdata.rb +11 -0
- data/lib/nokogiri/xml/character_data.rb +7 -0
- data/lib/nokogiri/xml/document.rb +194 -0
- data/lib/nokogiri/xml/document_fragment.rb +77 -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 +19 -0
- data/lib/nokogiri/xml/namespace.rb +13 -0
- data/lib/nokogiri/xml/node.rb +793 -0
- data/lib/nokogiri/xml/node/save_options.rb +42 -0
- data/lib/nokogiri/xml/node_set.rb +325 -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 +57 -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 +48 -0
- data/lib/nokogiri/xslt/stylesheet.rb +25 -0
- data/lib/xsd/xmlparser/nokogiri.rb +90 -0
- data/tasks/cross_compile.rb +158 -0
- data/tasks/test.rb +94 -0
- data/test/css/test_nthiness.rb +159 -0
- data/test/css/test_parser.rb +282 -0
- data/test/css/test_tokenizer.rb +190 -0
- data/test/css/test_xpath_visitor.rb +85 -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 +169 -0
- data/test/html/sax/test_parser.rb +74 -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 +398 -0
- data/test/html/test_document_encoding.rb +77 -0
- data/test/html/test_document_fragment.rb +182 -0
- data/test/html/test_element_description.rb +98 -0
- data/test/html/test_named_characters.rb +14 -0
- data/test/html/test_node.rb +181 -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_encoding_handler.rb +46 -0
- data/test/test_memory_leak.rb +87 -0
- data/test/test_nokogiri.rb +138 -0
- data/test/test_reader.rb +386 -0
- data/test/test_soap4r_sax.rb +52 -0
- data/test/test_xslt_transforms.rb +188 -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 +63 -0
- data/test/xml/sax/test_push_parser.rb +139 -0
- data/test/xml/test_attr.rb +38 -0
- data/test/xml/test_attribute_decl.rb +82 -0
- data/test/xml/test_builder.rb +210 -0
- data/test/xml/test_cdata.rb +50 -0
- data/test/xml/test_comment.rb +29 -0
- data/test/xml/test_document.rb +668 -0
- data/test/xml/test_document_encoding.rb +26 -0
- data/test/xml/test_document_fragment.rb +180 -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 +120 -0
- data/test/xml/test_entity_reference.rb +21 -0
- data/test/xml/test_namespace.rb +68 -0
- data/test/xml/test_node.rb +865 -0
- data/test/xml/test_node_attributes.rb +34 -0
- data/test/xml/test_node_encoding.rb +107 -0
- data/test/xml/test_node_reparenting.rb +293 -0
- data/test/xml/test_node_set.rb +649 -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 +12 -0
- data/test/xml/test_text.rb +38 -0
- data/test/xml/test_unparented_node.rb +381 -0
- data/test/xml/test_xpath.rb +138 -0
- metadata +533 -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,293 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class TestNodeReparenting < Nokogiri::TestCase
|
6
|
+
|
7
|
+
describe "standard node reparenting behavior" do
|
8
|
+
# describe "namespace handling during reparenting" do
|
9
|
+
# describe "given a Node" do
|
10
|
+
# describe "with a Namespace" do
|
11
|
+
# it "keeps the Namespace"
|
12
|
+
# end
|
13
|
+
# describe "given a parent Node with a default and a non-default Namespace" do
|
14
|
+
# describe "passed an Node without a namespace" do
|
15
|
+
# it "inserts an Node that inherits the default Namespace"
|
16
|
+
# end
|
17
|
+
# describe "passed a Node with a Namespace that matches the parent's non-default Namespace" do
|
18
|
+
# it "inserts a Node that inherits the matching parent Namespace"
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
# describe "given a markup string" do
|
23
|
+
# describe "parsed relative to the document" do
|
24
|
+
# describe "with a Namespace" do
|
25
|
+
# it "keeps the Namespace"
|
26
|
+
# end
|
27
|
+
# describe "given a parent Node with a default and a non-default Namespace" do
|
28
|
+
# describe "passed an Node without a namespace" do
|
29
|
+
# it "inserts an Node that inherits the default Namespace"
|
30
|
+
# end
|
31
|
+
# describe "passed a Node with a Namespace that matches the parent's non-default Namespace" do
|
32
|
+
# it "inserts a Node that inherits the matching parent Namespace"
|
33
|
+
# end
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
# describe "parsed relative to a specific node" do
|
37
|
+
# describe "with a Namespace" do
|
38
|
+
# it "keeps the Namespace"
|
39
|
+
# end
|
40
|
+
# describe "given a parent Node with a default and a non-default Namespace" do
|
41
|
+
# describe "passed an Node without a namespace" do
|
42
|
+
# it "inserts an Node that inherits the default Namespace"
|
43
|
+
# end
|
44
|
+
# describe "passed a Node with a Namespace that matches the parent's non-default Namespace" do
|
45
|
+
# it "inserts a Node that inherits the matching parent Namespace"
|
46
|
+
# end
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
|
52
|
+
{
|
53
|
+
:add_child => {:target => "/root/a1", :returns => :reparented, :children_tags => %w[text b1 b2]},
|
54
|
+
:<< => {:target => "/root/a1", :returns => :reparented, :children_tags => %w[text b1 b2]},
|
55
|
+
|
56
|
+
:replace => {:target => "/root/a1/node()", :returns => :reparented, :children_tags => %w[b1 b2]},
|
57
|
+
:swap => {:target => "/root/a1/node()", :returns => :self, :children_tags => %w[b1 b2]},
|
58
|
+
|
59
|
+
:inner_html= => {:target => "/root/a1", :returns => :self, :children_tags => %w[b1 b2]},
|
60
|
+
|
61
|
+
:add_previous_sibling => {:target => "/root/a1/text()", :returns => :reparented, :children_tags => %w[b1 b2 text]},
|
62
|
+
:previous= => {:target => "/root/a1/text()", :returns => :reparented, :children_tags => %w[b1 b2 text]},
|
63
|
+
:before => {:target => "/root/a1/text()", :returns => :self, :children_tags => %w[b1 b2 text]},
|
64
|
+
|
65
|
+
:add_next_sibling => {:target => "/root/a1/text()", :returns => :reparented, :children_tags => %w[text b1 b2]},
|
66
|
+
:next= => {:target => "/root/a1/text()", :returns => :reparented, :children_tags => %w[text b1 b2]},
|
67
|
+
:after => {:target => "/root/a1/text()", :returns => :self, :children_tags => %w[text b1 b2]}
|
68
|
+
}.each do |method, params|
|
69
|
+
|
70
|
+
before do
|
71
|
+
@doc = Nokogiri::XML "<root><a1>First node</a1><a2>Second node</a2><a3>Third <bx />node</a3></root>"
|
72
|
+
@doc2 = @doc.dup
|
73
|
+
@fragment_string = "<b1>foo</b1><b2>bar</b2>"
|
74
|
+
@fragment = Nokogiri::XML::DocumentFragment.parse @fragment_string
|
75
|
+
@node_set = Nokogiri::XML("<root><b1>foo</b1><b2>bar</b2></root>").xpath("/root/node()")
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "##{method}" do
|
79
|
+
describe "passed a Node" do
|
80
|
+
[:current, :another].each do |which|
|
81
|
+
describe "passed a Node in the #{which} document" do
|
82
|
+
before do
|
83
|
+
@other_doc = which == :current ? @doc : @doc2
|
84
|
+
@other_node = @other_doc.at_xpath("/root/a2")
|
85
|
+
end
|
86
|
+
|
87
|
+
it "unlinks the Node from its previous position" do
|
88
|
+
@doc.at_xpath(params[:target]).send(method, @other_node)
|
89
|
+
@other_doc.at_xpath("/root/a2").must_be_nil
|
90
|
+
end
|
91
|
+
|
92
|
+
it "inserts the Node in the proper position" do
|
93
|
+
@doc.at_xpath(params[:target]).send(method, @other_node)
|
94
|
+
@doc.at_xpath("/root/a1/a2").wont_be_nil
|
95
|
+
end
|
96
|
+
|
97
|
+
it "returns the expected value" do
|
98
|
+
if params[:returns] == :self
|
99
|
+
sendee = @doc.at_xpath(params[:target])
|
100
|
+
sendee.send(method, @other_node).must_equal sendee
|
101
|
+
else
|
102
|
+
@doc.at_xpath(params[:target]).send(method, @other_node).must_equal @other_node
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
describe "passed a markup string" do
|
109
|
+
it "inserts the fragment roots in the proper position" do
|
110
|
+
@doc.at_xpath(params[:target]).send(method, @fragment_string)
|
111
|
+
@doc.xpath("/root/a1/node()").collect {|n| n.name}.must_equal params[:children_tags]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
describe "passed a fragment" do
|
115
|
+
it "inserts the fragment roots in the proper position" do
|
116
|
+
@doc.at_xpath(params[:target]).send(method, @fragment)
|
117
|
+
@doc.xpath("/root/a1/node()").collect {|n| n.name}.must_equal params[:children_tags]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
describe "passed a document" do
|
121
|
+
it "raises an exception" do
|
122
|
+
proc { @doc.at_xpath("/root/a1").send(method, @doc2) }.must_raise(ArgumentError)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
describe "passed a non-Node" do
|
126
|
+
it "raises an exception" do
|
127
|
+
proc { @doc.at_xpath("/root/a1").send(method, 42) }.must_raise(ArgumentError)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
describe "passed a NodeSet" do
|
131
|
+
it "inserts each member of the NodeSet in the proper order" do
|
132
|
+
@doc.at_xpath(params[:target]).send(method, @node_set)
|
133
|
+
@doc.xpath("/root/a1/node()").collect {|n| n.name}.must_equal params[:children_tags]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "text node merging" do
|
139
|
+
describe "#add_child" do
|
140
|
+
it "merges the Text node with adjacent Text nodes" do
|
141
|
+
@doc.at_xpath("/root/a1").add_child Nokogiri::XML::Text.new('hello', @doc)
|
142
|
+
@doc.at_xpath("/root/a1/text()").content.must_equal "First nodehello"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
describe "#replace" do
|
146
|
+
it "merges the Text node with adjacent Text nodes" do
|
147
|
+
@doc.at_xpath("/root/a3/bx").replace Nokogiri::XML::Text.new('hello', @doc)
|
148
|
+
@doc.at_xpath("/root/a3/text()").content.must_equal "Third hellonode"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe "ad hoc node reparenting behavior" do
|
156
|
+
before do
|
157
|
+
@xml = Nokogiri::XML "<root><a1>First node</a1><a2>Second node</a2><a3>Third node</a3></root>"
|
158
|
+
@html = Nokogiri::HTML(<<-eohtml)
|
159
|
+
<html>
|
160
|
+
<head></head>
|
161
|
+
<body>
|
162
|
+
<div class='baz'><a href="foo" class="bar">first</a></div>
|
163
|
+
</body>
|
164
|
+
</html>
|
165
|
+
eohtml
|
166
|
+
end
|
167
|
+
|
168
|
+
describe "#add_child" do
|
169
|
+
describe "given a new node with a namespace" do
|
170
|
+
it "keeps the namespace" do
|
171
|
+
doc = Nokogiri::XML::Document.new
|
172
|
+
item = Nokogiri::XML::Element.new('item', doc)
|
173
|
+
doc.root = item
|
174
|
+
|
175
|
+
entry = Nokogiri::XML::Element.new('entry', doc)
|
176
|
+
entry.add_namespace('tlm', 'http://tenderlovemaking.com')
|
177
|
+
assert_equal 'http://tenderlovemaking.com', entry.namespaces['xmlns:tlm']
|
178
|
+
item.add_child(entry)
|
179
|
+
assert_equal 'http://tenderlovemaking.com', entry.namespaces['xmlns:tlm']
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe "given a parent node with a default namespace" do
|
184
|
+
before do
|
185
|
+
@doc = Nokogiri::XML(<<-eoxml)
|
186
|
+
<root xmlns="http://tenderlovemaking.com/">
|
187
|
+
<first>
|
188
|
+
</first>
|
189
|
+
</root>
|
190
|
+
eoxml
|
191
|
+
end
|
192
|
+
|
193
|
+
it "inserts a node that inherits the default namespace" do
|
194
|
+
assert node = @doc.at('//xmlns:first')
|
195
|
+
child = Nokogiri::XML::Node.new('second', @doc)
|
196
|
+
node.add_child(child)
|
197
|
+
assert @doc.at('//xmlns:second')
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe "given a parent node with a non-default namespace" do
|
202
|
+
before do
|
203
|
+
@doc = Nokogiri::XML(<<-eoxml)
|
204
|
+
<root xmlns="http://tenderlovemaking.com/" xmlns:foo="http://flavorjon.es/">
|
205
|
+
<first>
|
206
|
+
</first>
|
207
|
+
</root>
|
208
|
+
eoxml
|
209
|
+
end
|
210
|
+
|
211
|
+
describe "and a child node with a namespace matching the parent's non-default namespace" do
|
212
|
+
it "inserts a node that inherits the matching parent namespace" do
|
213
|
+
assert node = @doc.at('//xmlns:first')
|
214
|
+
child = Nokogiri::XML::Node.new('second', @doc)
|
215
|
+
|
216
|
+
ns = @doc.root.namespace_definitions.detect { |x| x.prefix == "foo" }
|
217
|
+
child.namespace = ns
|
218
|
+
|
219
|
+
node.add_child(child)
|
220
|
+
assert @doc.at('//foo:second', "foo" => "http://flavorjon.es/")
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
describe "#replace" do
|
227
|
+
describe "when a document has a default namespace" do
|
228
|
+
before do
|
229
|
+
@fruits = Nokogiri::XML(<<-eoxml)
|
230
|
+
<fruit xmlns="http://fruits.org">
|
231
|
+
<apple />
|
232
|
+
</fruit>
|
233
|
+
eoxml
|
234
|
+
end
|
235
|
+
|
236
|
+
it "inserts a node with default namespaces" do
|
237
|
+
apple = @fruits.css('apple').first
|
238
|
+
|
239
|
+
orange = Nokogiri::XML::Node.new('orange', @fruits)
|
240
|
+
apple.replace(orange)
|
241
|
+
|
242
|
+
assert_equal orange, @fruits.css('orange').first
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
describe "unlinking a node and then reparenting it" do
|
248
|
+
it "not blow up" do
|
249
|
+
# see http://github.com/tenderlove/nokogiri/issues#issue/22
|
250
|
+
10.times do
|
251
|
+
STDOUT.putc "."
|
252
|
+
STDOUT.flush
|
253
|
+
begin
|
254
|
+
doc = Nokogiri::XML <<-EOHTML
|
255
|
+
<root>
|
256
|
+
<a>
|
257
|
+
<b/>
|
258
|
+
<c/>
|
259
|
+
</a>
|
260
|
+
</root>
|
261
|
+
EOHTML
|
262
|
+
|
263
|
+
root = doc.at("root")
|
264
|
+
a = root.at("a")
|
265
|
+
b = a.at("b")
|
266
|
+
c = a.at("c")
|
267
|
+
a.add_next_sibling(b.unlink)
|
268
|
+
c.unlink
|
269
|
+
end
|
270
|
+
GC.start
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
describe "replace-merging text nodes" do
|
276
|
+
[
|
277
|
+
['<root>a<br/></root>', 'afoo'],
|
278
|
+
['<root>a<br/>b</root>', 'afoob'],
|
279
|
+
['<root><br/>b</root>', 'foob']
|
280
|
+
].each do |xml, result|
|
281
|
+
it "doesn't blow up on #{xml}" do
|
282
|
+
doc = Nokogiri::XML.parse(xml)
|
283
|
+
saved_nodes = doc.root.children
|
284
|
+
doc.at_xpath("/root/br").replace(Nokogiri::XML::Text.new('foo', doc))
|
285
|
+
saved_nodes.each { |child| child.inspect } # try to cause a crash
|
286
|
+
assert_equal result, doc.at_xpath("/root/text()").inner_text
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
@@ -0,0 +1,649 @@
|
|
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_filter
|
13
|
+
list = @xml.css('address').filter('*[domestic="Yes"]')
|
14
|
+
assert_equal(%w{ Yes } * 4, list.map { |n| n['domestic'] })
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_remove_attr
|
18
|
+
@list.each { |x| x['class'] = 'blah' }
|
19
|
+
assert_equal @list, @list.remove_attr('class')
|
20
|
+
@list.each { |x| assert_nil x['class'] }
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_add_class
|
24
|
+
assert_equal @list, @list.add_class('bar')
|
25
|
+
@list.each { |x| assert_equal 'bar', x['class'] }
|
26
|
+
|
27
|
+
@list.add_class('bar')
|
28
|
+
@list.each { |x| assert_equal 'bar', x['class'] }
|
29
|
+
|
30
|
+
@list.add_class('baz')
|
31
|
+
@list.each { |x| assert_equal 'bar baz', x['class'] }
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_remove_class_with_no_class
|
35
|
+
assert_equal @list, @list.remove_class('bar')
|
36
|
+
@list.each { |e| assert_nil e['class'] }
|
37
|
+
|
38
|
+
@list.each { |e| e['class'] = '' }
|
39
|
+
assert_equal @list, @list.remove_class('bar')
|
40
|
+
@list.each { |e| assert_nil e['class'] }
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_remove_class_single
|
44
|
+
@list.each { |e| e['class'] = 'foo bar' }
|
45
|
+
|
46
|
+
assert_equal @list, @list.remove_class('bar')
|
47
|
+
@list.each { |e| assert_equal 'foo', e['class'] }
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_remove_class_completely
|
51
|
+
@list.each { |e| e['class'] = 'foo' }
|
52
|
+
|
53
|
+
assert_equal @list, @list.remove_class
|
54
|
+
@list.each { |e| assert_nil e['class'] }
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_attribute_set
|
58
|
+
@list.each { |e| assert_nil e['foo'] }
|
59
|
+
|
60
|
+
[ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
|
61
|
+
@list.send(t.first.to_sym, 'foo', t.last)
|
62
|
+
@list.each { |e| assert_equal t.last, e['foo'] }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_attribute_set_with_block
|
67
|
+
@list.each { |e| assert_nil e['foo'] }
|
68
|
+
|
69
|
+
[ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
|
70
|
+
@list.send(t.first.to_sym, 'foo') { |x| t.last }
|
71
|
+
@list.each { |e| assert_equal t.last, e['foo'] }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_attribute_set_with_hash
|
76
|
+
@list.each { |e| assert_nil e['foo'] }
|
77
|
+
|
78
|
+
[ ['attribute', 'bar'], ['attr', 'biz'], ['set', 'baz'] ].each do |t|
|
79
|
+
@list.send(t.first.to_sym, 'foo' => t.last)
|
80
|
+
@list.each { |e| assert_equal t.last, e['foo'] }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_attribute_no_args
|
85
|
+
@list.first['foo'] = 'bar'
|
86
|
+
assert_equal @list.first.attribute('foo'), @list.attribute('foo')
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_search_empty_node_set
|
90
|
+
set = Nokogiri::XML::NodeSet.new(Nokogiri::XML::Document.new)
|
91
|
+
assert_equal 0, set.css('foo').length
|
92
|
+
assert_equal 0, set.xpath('.//foo').length
|
93
|
+
assert_equal 0, set.search('foo').length
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_xpath_with_custom_object
|
97
|
+
set = @xml.xpath('//staff')
|
98
|
+
custom_employees = set.xpath('//*[awesome(.)]', Class.new {
|
99
|
+
def awesome ns
|
100
|
+
ns.select { |n| n.name == 'employee' }
|
101
|
+
end
|
102
|
+
}.new)
|
103
|
+
|
104
|
+
assert_equal @xml.xpath('//employee'), custom_employees
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_css_with_custom_object
|
108
|
+
set = @xml.xpath('//staff')
|
109
|
+
custom_employees = set.css('*:awesome', Class.new {
|
110
|
+
def awesome ns
|
111
|
+
ns.select { |n| n.name == 'employee' }
|
112
|
+
end
|
113
|
+
}.new)
|
114
|
+
|
115
|
+
assert_equal @xml.xpath('//employee'), custom_employees
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_search_with_custom_object
|
119
|
+
set = @xml.xpath('//staff')
|
120
|
+
custom_employees = set.search('//*[awesome(.)]', Class.new {
|
121
|
+
def awesome ns
|
122
|
+
ns.select { |n| n.name == 'employee' }
|
123
|
+
end
|
124
|
+
}.new)
|
125
|
+
|
126
|
+
assert_equal @xml.xpath('//employee'), custom_employees
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_css_searches_match_self
|
130
|
+
html = Nokogiri::HTML("<html><body><div class='a'></div></body></html>")
|
131
|
+
set = html.xpath("/html/body/div")
|
132
|
+
assert_equal set.first, set.css(".a").first
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_search_with_css_matches_self
|
136
|
+
html = Nokogiri::HTML("<html><body><div class='a'></div></body></html>")
|
137
|
+
set = html.xpath("/html/body/div")
|
138
|
+
assert_equal set.first, set.search(".a").first
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_double_equal
|
142
|
+
assert node_set_one = @xml.xpath('//employee')
|
143
|
+
assert node_set_two = @xml.xpath('//employee')
|
144
|
+
|
145
|
+
assert_not_equal node_set_one.object_id, node_set_two.object_id
|
146
|
+
|
147
|
+
assert_equal node_set_one, node_set_two
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_node_set_not_equal_to_string
|
151
|
+
node_set_one = @xml.xpath('//employee')
|
152
|
+
assert_not_equal node_set_one, "asdfadsf"
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_out_of_order_not_equal
|
156
|
+
one = @xml.xpath('//employee')
|
157
|
+
two = @xml.xpath('//employee')
|
158
|
+
two.push two.shift
|
159
|
+
assert_not_equal one, two
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_shorter_is_not_equal
|
163
|
+
node_set_one = @xml.xpath('//employee')
|
164
|
+
node_set_two = @xml.xpath('//employee')
|
165
|
+
node_set_two.delete(node_set_two.first)
|
166
|
+
|
167
|
+
assert_not_equal node_set_one, node_set_two
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_pop
|
171
|
+
set = @xml.xpath('//employee')
|
172
|
+
last = set.last
|
173
|
+
assert_equal last, set.pop
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_shift
|
177
|
+
set = @xml.xpath('//employee')
|
178
|
+
first = set.first
|
179
|
+
assert_equal first, set.shift
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_shift_empty
|
183
|
+
set = Nokogiri::XML::NodeSet.new(@xml)
|
184
|
+
assert_nil set.shift
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_pop_empty
|
188
|
+
set = Nokogiri::XML::NodeSet.new(@xml)
|
189
|
+
assert_nil set.pop
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_first_takes_arguments
|
193
|
+
assert node_set = @xml.xpath('//employee')
|
194
|
+
assert_equal 2, node_set.first(2).length
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_dup
|
198
|
+
assert node_set = @xml.xpath('//employee')
|
199
|
+
dup = node_set.dup
|
200
|
+
assert_equal node_set.length, dup.length
|
201
|
+
node_set.zip(dup).each do |a,b|
|
202
|
+
assert_equal a, b
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_xmlns_is_automatically_registered
|
207
|
+
doc = Nokogiri::XML(<<-eoxml)
|
208
|
+
<root xmlns="http://tenderlovemaking.com/">
|
209
|
+
<foo>
|
210
|
+
<bar/>
|
211
|
+
</foo>
|
212
|
+
</root>
|
213
|
+
eoxml
|
214
|
+
set = doc.css('foo')
|
215
|
+
assert_equal 1, set.css('xmlns|bar').length
|
216
|
+
assert_equal 0, set.css('|bar').length
|
217
|
+
assert_equal 1, set.xpath('//xmlns:bar').length
|
218
|
+
assert_equal 1, set.search('xmlns|bar').length
|
219
|
+
assert_equal 1, set.search('//xmlns:bar').length
|
220
|
+
assert set.at('//xmlns:bar')
|
221
|
+
assert set.at('xmlns|bar')
|
222
|
+
assert set.at('bar')
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_children_has_document
|
226
|
+
set = @xml.root.children
|
227
|
+
assert_instance_of(NodeSet, set)
|
228
|
+
assert_equal @xml, set.document
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_length_size
|
232
|
+
assert node_set = @xml.search('//employee')
|
233
|
+
assert_equal node_set.length, node_set.size
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_to_xml
|
237
|
+
assert node_set = @xml.search('//employee')
|
238
|
+
assert node_set.to_xml
|
239
|
+
end
|
240
|
+
|
241
|
+
def test_inner_html
|
242
|
+
doc = Nokogiri::HTML(<<-eohtml)
|
243
|
+
<html>
|
244
|
+
<body>
|
245
|
+
<div>
|
246
|
+
<a>one</a>
|
247
|
+
</div>
|
248
|
+
<div>
|
249
|
+
<a>two</a>
|
250
|
+
</div>
|
251
|
+
</body>
|
252
|
+
</html>
|
253
|
+
eohtml
|
254
|
+
assert html = doc.css('div').inner_html
|
255
|
+
assert_match '<a>', html
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_at
|
259
|
+
assert node_set = @xml.search('//employee')
|
260
|
+
assert_equal node_set.first, node_set.at(0)
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_percent
|
264
|
+
assert node_set = @xml.search('//employee')
|
265
|
+
assert_equal node_set.first, node_set % 0
|
266
|
+
end
|
267
|
+
|
268
|
+
def test_to_ary
|
269
|
+
assert node_set = @xml.search('//employee')
|
270
|
+
foo = []
|
271
|
+
foo += node_set
|
272
|
+
assert_equal node_set.length, foo.length
|
273
|
+
end
|
274
|
+
|
275
|
+
def test_push
|
276
|
+
node = Nokogiri::XML::Node.new('foo', @xml)
|
277
|
+
node.content = 'bar'
|
278
|
+
|
279
|
+
assert node_set = @xml.search('//employee')
|
280
|
+
node_set.push(node)
|
281
|
+
|
282
|
+
assert node_set.include?(node)
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_delete_with_invalid_argument
|
286
|
+
employees = @xml.search("//employee")
|
287
|
+
positions = @xml.search("//position")
|
288
|
+
|
289
|
+
assert_raises(ArgumentError) { employees.delete(positions) }
|
290
|
+
end
|
291
|
+
|
292
|
+
def test_delete_when_present
|
293
|
+
employees = @xml.search("//employee")
|
294
|
+
wally = employees.first
|
295
|
+
assert employees.include?(wally) # testing setup
|
296
|
+
length = employees.length
|
297
|
+
|
298
|
+
result = employees.delete(wally)
|
299
|
+
assert_equal result, wally
|
300
|
+
assert ! employees.include?(wally)
|
301
|
+
assert length-1, employees.length
|
302
|
+
end
|
303
|
+
|
304
|
+
def test_delete_when_not_present
|
305
|
+
employees = @xml.search("//employee")
|
306
|
+
phb = @xml.search("//position").first
|
307
|
+
assert ! employees.include?(phb) # testing setup
|
308
|
+
length = employees.length
|
309
|
+
|
310
|
+
result = employees.delete(phb)
|
311
|
+
assert_nil result
|
312
|
+
assert length, employees.length
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_unlink
|
316
|
+
xml = Nokogiri::XML.parse(<<-eoxml)
|
317
|
+
<root>
|
318
|
+
<a class='foo bar'>Bar</a>
|
319
|
+
<a class='bar foo'>Bar</a>
|
320
|
+
<a class='bar'>Bar</a>
|
321
|
+
<a>Hello world</a>
|
322
|
+
<a class='baz bar foo'>Bar</a>
|
323
|
+
<a class='bazbarfoo'>Awesome</a>
|
324
|
+
<a class='bazbar'>Awesome</a>
|
325
|
+
</root>
|
326
|
+
eoxml
|
327
|
+
set = xml.xpath('//a')
|
328
|
+
set.unlink
|
329
|
+
set.each do |node|
|
330
|
+
assert !node.parent
|
331
|
+
#assert !node.document
|
332
|
+
assert !node.previous_sibling
|
333
|
+
assert !node.next_sibling
|
334
|
+
end
|
335
|
+
assert_no_match(/Hello world/, xml.to_s)
|
336
|
+
end
|
337
|
+
|
338
|
+
def test_nodeset_search_takes_namespace
|
339
|
+
@xml = Nokogiri::XML.parse(<<-eoxml)
|
340
|
+
<root>
|
341
|
+
<car xmlns:part="http://general-motors.com/">
|
342
|
+
<part:tire>Michelin Model XGV</part:tire>
|
343
|
+
</car>
|
344
|
+
<bicycle xmlns:part="http://schwinn.com/">
|
345
|
+
<part:tire>I'm a bicycle tire!</part:tire>
|
346
|
+
</bicycle>
|
347
|
+
</root>
|
348
|
+
eoxml
|
349
|
+
set = @xml/'root'
|
350
|
+
assert_equal 1, set.length
|
351
|
+
bike_tire = set.search('//bike:tire', 'bike' => "http://schwinn.com/")
|
352
|
+
assert_equal 1, bike_tire.length
|
353
|
+
end
|
354
|
+
|
355
|
+
def test_new_nodeset
|
356
|
+
node_set = Nokogiri::XML::NodeSet.new(@xml)
|
357
|
+
assert_equal(0, node_set.length)
|
358
|
+
node = Nokogiri::XML::Node.new('form', @xml)
|
359
|
+
node_set << node
|
360
|
+
assert_equal(1, node_set.length)
|
361
|
+
assert_equal(node, node_set.last)
|
362
|
+
end
|
363
|
+
|
364
|
+
def test_search_on_nodeset
|
365
|
+
assert node_set = @xml.search('//employee')
|
366
|
+
assert sub_set = node_set.search('.//name')
|
367
|
+
assert_equal(node_set.length, sub_set.length)
|
368
|
+
end
|
369
|
+
|
370
|
+
def test_negative_index_works
|
371
|
+
assert node_set = @xml.search('//employee')
|
372
|
+
assert_equal node_set.last, node_set[-1]
|
373
|
+
end
|
374
|
+
|
375
|
+
def test_large_negative_index_returns_nil
|
376
|
+
assert node_set = @xml.search('//employee')
|
377
|
+
assert_nil(node_set[-1 * (node_set.length + 1)])
|
378
|
+
end
|
379
|
+
|
380
|
+
def test_node_set_fetches_private_data
|
381
|
+
assert node_set = @xml.search('//employee')
|
382
|
+
|
383
|
+
set = node_set
|
384
|
+
assert_equal(set[0], set[0])
|
385
|
+
end
|
386
|
+
|
387
|
+
def test_node_set_returns_0
|
388
|
+
assert node_set = @xml.search('//asdkfjhasdlkfjhaldskfh')
|
389
|
+
assert_equal(0, node_set.length)
|
390
|
+
end
|
391
|
+
|
392
|
+
def test_wrap
|
393
|
+
employees = (@xml/"//employee").wrap("<wrapper/>")
|
394
|
+
assert_equal 'wrapper', employees[0].parent.name
|
395
|
+
assert_equal 'employee', @xml.search("//wrapper").first.children[0].name
|
396
|
+
end
|
397
|
+
|
398
|
+
def test_wrap_preserves_document_structure
|
399
|
+
assert_equal "employeeId",
|
400
|
+
@xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
|
401
|
+
@xml.xpath("//employeeId[text()='EMP0001']").wrap("<wrapper/>")
|
402
|
+
assert_equal "wrapper",
|
403
|
+
@xml.at_xpath("//employee").children.detect{|j| ! j.text? }.name
|
404
|
+
end
|
405
|
+
|
406
|
+
def test_plus_operator
|
407
|
+
names = @xml.search("name")
|
408
|
+
positions = @xml.search("position")
|
409
|
+
|
410
|
+
names_len = names.length
|
411
|
+
positions_len = positions.length
|
412
|
+
|
413
|
+
assert_raises(ArgumentError) { result = names + positions.first }
|
414
|
+
|
415
|
+
result = names + positions
|
416
|
+
assert_equal names_len, names.length
|
417
|
+
assert_equal positions_len, positions.length
|
418
|
+
assert_equal names.length + positions.length, result.length
|
419
|
+
|
420
|
+
names += positions
|
421
|
+
assert_equal result.length, names.length
|
422
|
+
end
|
423
|
+
|
424
|
+
def test_union
|
425
|
+
names = @xml.search("name")
|
426
|
+
|
427
|
+
assert_equal(names.length, (names | @xml.search("name")).length)
|
428
|
+
end
|
429
|
+
|
430
|
+
def test_minus_operator
|
431
|
+
employees = @xml.search("//employee")
|
432
|
+
females = @xml.search("//employee[gender[text()='Female']]")
|
433
|
+
|
434
|
+
employees_len = employees.length
|
435
|
+
females_len = females.length
|
436
|
+
|
437
|
+
assert_raises(ArgumentError) { result = employees - females.first }
|
438
|
+
|
439
|
+
result = employees - females
|
440
|
+
assert_equal employees_len, employees.length
|
441
|
+
assert_equal females_len, females.length
|
442
|
+
assert_equal employees.length - females.length, result.length
|
443
|
+
|
444
|
+
employees -= females
|
445
|
+
assert_equal result.length, employees.length
|
446
|
+
end
|
447
|
+
|
448
|
+
def test_array_index
|
449
|
+
employees = @xml.search("//employee")
|
450
|
+
other = @xml.search("//position").first
|
451
|
+
|
452
|
+
assert_equal 3, employees.index(employees[3])
|
453
|
+
assert_nil employees.index(other)
|
454
|
+
end
|
455
|
+
|
456
|
+
def test_slice_too_far
|
457
|
+
employees = @xml.search("//employee")
|
458
|
+
assert_equal employees.length, employees[0, employees.length + 1].length
|
459
|
+
assert_equal employees.length, employees[0, employees.length].length
|
460
|
+
end
|
461
|
+
|
462
|
+
def test_slice_waaaaaay_off_the_end
|
463
|
+
xml = Nokogiri::XML::Builder.new {
|
464
|
+
root { 100.times { div } }
|
465
|
+
}.doc
|
466
|
+
nodes = xml.css "div"
|
467
|
+
assert_equal 1, nodes.slice(99, 100_000).length
|
468
|
+
assert_equal 0, nodes.slice(100, 100_000).length
|
469
|
+
end
|
470
|
+
|
471
|
+
def test_array_slice_with_start_and_end
|
472
|
+
employees = @xml.search("//employee")
|
473
|
+
assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
|
474
|
+
end
|
475
|
+
|
476
|
+
def test_array_index_bracket_equivalence
|
477
|
+
employees = @xml.search("//employee")
|
478
|
+
assert_equal [employees[1], employees[2], employees[3]], employees[1,3].to_a
|
479
|
+
assert_equal [employees[1], employees[2], employees[3]], employees.slice(1,3).to_a
|
480
|
+
end
|
481
|
+
|
482
|
+
def test_array_slice_with_negative_start
|
483
|
+
employees = @xml.search("//employee")
|
484
|
+
assert_equal [employees[2]], employees[-3,1].to_a
|
485
|
+
assert_equal [employees[2], employees[3]], employees[-3,2].to_a
|
486
|
+
end
|
487
|
+
|
488
|
+
def test_array_slice_with_invalid_args
|
489
|
+
employees = @xml.search("//employee")
|
490
|
+
assert_nil employees[99, 1] # large start
|
491
|
+
assert_nil employees[1, -1] # negative len
|
492
|
+
assert_equal [], employees[1, 0].to_a # zero len
|
493
|
+
end
|
494
|
+
|
495
|
+
def test_array_slice_with_range
|
496
|
+
employees = @xml.search("//employee")
|
497
|
+
assert_equal [employees[1], employees[2], employees[3]], employees[1..3].to_a
|
498
|
+
assert_equal [employees[0], employees[1], employees[2], employees[3]], employees[0..3].to_a
|
499
|
+
end
|
500
|
+
|
501
|
+
def test_intersection_with_no_overlap
|
502
|
+
employees = @xml.search("//employee")
|
503
|
+
positions = @xml.search("//position")
|
504
|
+
|
505
|
+
assert_equal [], (employees & positions).to_a
|
506
|
+
end
|
507
|
+
|
508
|
+
def test_intersection
|
509
|
+
employees = @xml.search("//employee")
|
510
|
+
first_set = employees[0..2]
|
511
|
+
second_set = employees[2..4]
|
512
|
+
|
513
|
+
assert_equal [employees[2]], (first_set & second_set).to_a
|
514
|
+
end
|
515
|
+
|
516
|
+
def test_include?
|
517
|
+
employees = @xml.search("//employee")
|
518
|
+
yes = employees.first
|
519
|
+
no = @xml.search("//position").first
|
520
|
+
|
521
|
+
assert employees.include?(yes)
|
522
|
+
assert ! employees.include?(no)
|
523
|
+
end
|
524
|
+
|
525
|
+
def test_children
|
526
|
+
employees = @xml.search("//employee")
|
527
|
+
count = 0
|
528
|
+
employees.each do |employee|
|
529
|
+
count += employee.children.length
|
530
|
+
end
|
531
|
+
set = employees.children
|
532
|
+
assert_equal count, set.length
|
533
|
+
end
|
534
|
+
|
535
|
+
def test_inspect
|
536
|
+
employees = @xml.search("//employee")
|
537
|
+
inspected = employees.inspect
|
538
|
+
|
539
|
+
assert_equal "[#{employees.map { |x| x.inspect }.join(', ')}]",
|
540
|
+
inspected
|
541
|
+
end
|
542
|
+
|
543
|
+
def test_should_not_splode_when_accessing_namespace_declarations_in_a_node_set
|
544
|
+
xml = Nokogiri::XML "<foo></foo>"
|
545
|
+
node_set = xml.xpath("//namespace::*")
|
546
|
+
assert_equal 1, node_set.size
|
547
|
+
node = node_set.first
|
548
|
+
node.to_s # segfaults in 1.4.0 and earlier
|
549
|
+
|
550
|
+
# if we haven't segfaulted, let's make sure we handled it correctly
|
551
|
+
assert_instance_of Nokogiri::XML::Namespace, node
|
552
|
+
end
|
553
|
+
|
554
|
+
def test_should_not_splode_when_arrayifying_node_set_containing_namespace_declarations
|
555
|
+
xml = Nokogiri::XML "<foo></foo>"
|
556
|
+
node_set = xml.xpath("//namespace::*")
|
557
|
+
assert_equal 1, node_set.size
|
558
|
+
|
559
|
+
node_array = node_set.to_a
|
560
|
+
node = node_array.first
|
561
|
+
node.to_s # segfaults in 1.4.0 and earlier
|
562
|
+
|
563
|
+
# if we haven't segfaulted, let's make sure we handled it correctly
|
564
|
+
assert_instance_of Nokogiri::XML::Namespace, node
|
565
|
+
end
|
566
|
+
|
567
|
+
def test_should_not_splode_when_unlinking_node_set_containing_namespace_declarations
|
568
|
+
xml = Nokogiri::XML "<foo></foo>"
|
569
|
+
node_set = xml.xpath("//namespace::*")
|
570
|
+
assert_equal 1, node_set.size
|
571
|
+
|
572
|
+
node_set.unlink
|
573
|
+
end
|
574
|
+
|
575
|
+
def test_reverse
|
576
|
+
xml = Nokogiri::XML "<root><a />b<c />d<e /></root>"
|
577
|
+
children = xml.root.children
|
578
|
+
assert_instance_of Nokogiri::XML::NodeSet, children
|
579
|
+
|
580
|
+
reversed = children.reverse
|
581
|
+
assert_equal reversed[0], children[4]
|
582
|
+
assert_equal reversed[1], children[3]
|
583
|
+
assert_equal reversed[2], children[2]
|
584
|
+
assert_equal reversed[3], children[1]
|
585
|
+
assert_equal reversed[4], children[0]
|
586
|
+
|
587
|
+
assert_equal children, children.reverse.reverse
|
588
|
+
end
|
589
|
+
|
590
|
+
def test_node_set_dup_result_has_document_and_is_decorated
|
591
|
+
x = Module.new do
|
592
|
+
def awesome! ; end
|
593
|
+
end
|
594
|
+
util_decorate(@xml, x)
|
595
|
+
node_set = @xml.css("address")
|
596
|
+
new_set = node_set.dup
|
597
|
+
assert_equal node_set.document, new_set.document
|
598
|
+
assert new_set.respond_to?(:awesome!)
|
599
|
+
end
|
600
|
+
|
601
|
+
def test_node_set_union_result_has_document_and_is_decorated
|
602
|
+
x = Module.new do
|
603
|
+
def awesome! ; end
|
604
|
+
end
|
605
|
+
util_decorate(@xml, x)
|
606
|
+
node_set1 = @xml.css("address")
|
607
|
+
node_set2 = @xml.css("address")
|
608
|
+
new_set = node_set1 | node_set2
|
609
|
+
assert_equal node_set1.document, new_set.document
|
610
|
+
assert new_set.respond_to?(:awesome!)
|
611
|
+
end
|
612
|
+
|
613
|
+
def test_node_set_intersection_result_has_document_and_is_decorated
|
614
|
+
x = Module.new do
|
615
|
+
def awesome! ; end
|
616
|
+
end
|
617
|
+
util_decorate(@xml, x)
|
618
|
+
node_set1 = @xml.css("address")
|
619
|
+
node_set2 = @xml.css("address")
|
620
|
+
new_set = node_set1 & node_set2
|
621
|
+
assert_equal node_set1.document, new_set.document
|
622
|
+
assert new_set.respond_to?(:awesome!)
|
623
|
+
end
|
624
|
+
|
625
|
+
def test_node_set_difference_result_has_document_and_is_decorated
|
626
|
+
x = Module.new do
|
627
|
+
def awesome! ; end
|
628
|
+
end
|
629
|
+
util_decorate(@xml, x)
|
630
|
+
node_set1 = @xml.css("address")
|
631
|
+
node_set2 = @xml.css("address")
|
632
|
+
new_set = node_set1 - node_set2
|
633
|
+
assert_equal node_set1.document, new_set.document
|
634
|
+
assert new_set.respond_to?(:awesome!)
|
635
|
+
end
|
636
|
+
|
637
|
+
def test_node_set_slice_result_has_document_and_is_decorated
|
638
|
+
x = Module.new do
|
639
|
+
def awesome! ; end
|
640
|
+
end
|
641
|
+
util_decorate(@xml, x)
|
642
|
+
node_set = @xml.css("address")
|
643
|
+
new_set = node_set[0..-1]
|
644
|
+
assert_equal node_set.document, new_set.document
|
645
|
+
assert new_set.respond_to?(:awesome!)
|
646
|
+
end
|
647
|
+
end
|
648
|
+
end
|
649
|
+
end
|