tenderlove-nokogiri 0.0.0-x86-mswin32-60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. data/History.txt +6 -0
  2. data/Manifest.txt +120 -0
  3. data/README.ja.txt +86 -0
  4. data/README.txt +87 -0
  5. data/Rakefile +264 -0
  6. data/ext/nokogiri/extconf.rb +59 -0
  7. data/ext/nokogiri/html_document.c +83 -0
  8. data/ext/nokogiri/html_document.h +10 -0
  9. data/ext/nokogiri/html_sax_parser.c +32 -0
  10. data/ext/nokogiri/html_sax_parser.h +11 -0
  11. data/ext/nokogiri/native.c +40 -0
  12. data/ext/nokogiri/native.h +51 -0
  13. data/ext/nokogiri/xml_cdata.c +52 -0
  14. data/ext/nokogiri/xml_cdata.h +9 -0
  15. data/ext/nokogiri/xml_document.c +159 -0
  16. data/ext/nokogiri/xml_document.h +10 -0
  17. data/ext/nokogiri/xml_dtd.c +117 -0
  18. data/ext/nokogiri/xml_dtd.h +8 -0
  19. data/ext/nokogiri/xml_node.c +709 -0
  20. data/ext/nokogiri/xml_node.h +15 -0
  21. data/ext/nokogiri/xml_node_set.c +124 -0
  22. data/ext/nokogiri/xml_node_set.h +9 -0
  23. data/ext/nokogiri/xml_reader.c +429 -0
  24. data/ext/nokogiri/xml_reader.h +10 -0
  25. data/ext/nokogiri/xml_sax_parser.c +174 -0
  26. data/ext/nokogiri/xml_sax_parser.h +10 -0
  27. data/ext/nokogiri/xml_syntax_error.c +194 -0
  28. data/ext/nokogiri/xml_syntax_error.h +11 -0
  29. data/ext/nokogiri/xml_text.c +29 -0
  30. data/ext/nokogiri/xml_text.h +9 -0
  31. data/ext/nokogiri/xml_xpath.c +46 -0
  32. data/ext/nokogiri/xml_xpath.h +11 -0
  33. data/ext/nokogiri/xml_xpath_context.c +81 -0
  34. data/ext/nokogiri/xml_xpath_context.h +9 -0
  35. data/ext/nokogiri/xslt_stylesheet.c +108 -0
  36. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  37. data/lib/nokogiri/css/node.rb +95 -0
  38. data/lib/nokogiri/css/parser.rb +24 -0
  39. data/lib/nokogiri/css/parser.y +198 -0
  40. data/lib/nokogiri/css/tokenizer.rb +9 -0
  41. data/lib/nokogiri/css/tokenizer.rex +63 -0
  42. data/lib/nokogiri/css/xpath_visitor.rb +165 -0
  43. data/lib/nokogiri/css.rb +6 -0
  44. data/lib/nokogiri/decorators/hpricot/node.rb +58 -0
  45. data/lib/nokogiri/decorators/hpricot/node_set.rb +14 -0
  46. data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +17 -0
  47. data/lib/nokogiri/decorators/hpricot.rb +3 -0
  48. data/lib/nokogiri/decorators.rb +1 -0
  49. data/lib/nokogiri/hpricot.rb +47 -0
  50. data/lib/nokogiri/html/builder.rb +9 -0
  51. data/lib/nokogiri/html/document.rb +9 -0
  52. data/lib/nokogiri/html/sax/parser.rb +21 -0
  53. data/lib/nokogiri/html.rb +95 -0
  54. data/lib/nokogiri/version.rb +3 -0
  55. data/lib/nokogiri/xml/after_handler.rb +18 -0
  56. data/lib/nokogiri/xml/before_handler.rb +32 -0
  57. data/lib/nokogiri/xml/builder.rb +79 -0
  58. data/lib/nokogiri/xml/cdata.rb +9 -0
  59. data/lib/nokogiri/xml/document.rb +30 -0
  60. data/lib/nokogiri/xml/dtd.rb +6 -0
  61. data/lib/nokogiri/xml/node.rb +195 -0
  62. data/lib/nokogiri/xml/node_set.rb +183 -0
  63. data/lib/nokogiri/xml/notation.rb +6 -0
  64. data/lib/nokogiri/xml/reader.rb +14 -0
  65. data/lib/nokogiri/xml/sax/document.rb +59 -0
  66. data/lib/nokogiri/xml/sax/parser.rb +33 -0
  67. data/lib/nokogiri/xml/sax.rb +9 -0
  68. data/lib/nokogiri/xml/syntax_error.rb +21 -0
  69. data/lib/nokogiri/xml/text.rb +6 -0
  70. data/lib/nokogiri/xml/xpath.rb +6 -0
  71. data/lib/nokogiri/xml/xpath_context.rb +14 -0
  72. data/lib/nokogiri/xml.rb +67 -0
  73. data/lib/nokogiri/xslt/stylesheet.rb +6 -0
  74. data/lib/nokogiri/xslt.rb +11 -0
  75. data/lib/nokogiri.rb +51 -0
  76. data/nokogiri.gemspec +34 -0
  77. data/test/css/test_nthiness.rb +159 -0
  78. data/test/css/test_parser.rb +224 -0
  79. data/test/css/test_tokenizer.rb +162 -0
  80. data/test/css/test_xpath_visitor.rb +54 -0
  81. data/test/files/staff.xml +59 -0
  82. data/test/files/staff.xslt +32 -0
  83. data/test/files/tlm.html +850 -0
  84. data/test/helper.rb +70 -0
  85. data/test/hpricot/files/basic.xhtml +17 -0
  86. data/test/hpricot/files/boingboing.html +2266 -0
  87. data/test/hpricot/files/cy0.html +3653 -0
  88. data/test/hpricot/files/immob.html +400 -0
  89. data/test/hpricot/files/pace_application.html +1320 -0
  90. data/test/hpricot/files/tenderlove.html +16 -0
  91. data/test/hpricot/files/uswebgen.html +220 -0
  92. data/test/hpricot/files/utf8.html +1054 -0
  93. data/test/hpricot/files/week9.html +1723 -0
  94. data/test/hpricot/files/why.xml +19 -0
  95. data/test/hpricot/load_files.rb +7 -0
  96. data/test/hpricot/test_alter.rb +67 -0
  97. data/test/hpricot/test_builder.rb +27 -0
  98. data/test/hpricot/test_parser.rb +423 -0
  99. data/test/hpricot/test_paths.rb +15 -0
  100. data/test/hpricot/test_preserved.rb +78 -0
  101. data/test/hpricot/test_xml.rb +30 -0
  102. data/test/html/sax/test_parser.rb +27 -0
  103. data/test/html/test_builder.rb +78 -0
  104. data/test/html/test_document.rb +86 -0
  105. data/test/test_convert_xpath.rb +180 -0
  106. data/test/test_nokogiri.rb +36 -0
  107. data/test/test_reader.rb +222 -0
  108. data/test/test_xslt_transforms.rb +29 -0
  109. data/test/xml/sax/test_parser.rb +93 -0
  110. data/test/xml/test_builder.rb +16 -0
  111. data/test/xml/test_cdata.rb +18 -0
  112. data/test/xml/test_document.rb +171 -0
  113. data/test/xml/test_dtd.rb +43 -0
  114. data/test/xml/test_node.rb +223 -0
  115. data/test/xml/test_node_set.rb +116 -0
  116. data/test/xml/test_text.rb +13 -0
  117. metadata +214 -0
@@ -0,0 +1,93 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ module SAX
6
+ class TestParser < Nokogiri::SAX::TestCase
7
+ def setup
8
+ @parser = XML::SAX::Parser.new(Doc.new)
9
+ end
10
+
11
+ def test_parse
12
+ File.open(XML_FILE, 'rb') { |f|
13
+ @parser.parse(f)
14
+ }
15
+ @parser.parse(File.read(XML_FILE))
16
+ end
17
+
18
+ def test_parse_io
19
+ File.open(XML_FILE, 'rb') { |f|
20
+ @parser.parse_io(f)
21
+ }
22
+ end
23
+
24
+ def test_parse_file
25
+ @parser.parse_file(XML_FILE)
26
+ assert_raises(Errno::ENOENT) {
27
+ @parser.parse_file('')
28
+ }
29
+ assert_raises(Errno::EISDIR) {
30
+ @parser.parse_file(File.expand_path(File.dirname(__FILE__)))
31
+ }
32
+ end
33
+
34
+ def test_ctag
35
+ @parser.parse_memory(<<-eoxml)
36
+ <p id="asdfasdf">
37
+ <![CDATA[ This is a comment ]]>
38
+ Paragraph 1
39
+ </p>
40
+ eoxml
41
+ assert_equal [' This is a comment '], @parser.document.cdata_blocks
42
+ end
43
+
44
+ def test_comment
45
+ @parser.parse_memory(<<-eoxml)
46
+ <p id="asdfasdf">
47
+ <!-- This is a comment -->
48
+ Paragraph 1
49
+ </p>
50
+ eoxml
51
+ assert_equal [' This is a comment '], @parser.document.comments
52
+ end
53
+
54
+ def test_characters
55
+ @parser.parse_memory(<<-eoxml)
56
+ <p id="asdfasdf">Paragraph 1</p>
57
+ eoxml
58
+ assert_equal ['Paragraph 1'], @parser.document.data
59
+ end
60
+
61
+ def test_end_document
62
+ @parser.parse_memory(<<-eoxml)
63
+ <p id="asdfasdf">Paragraph 1</p>
64
+ eoxml
65
+ assert @parser.document.end_document_called
66
+ end
67
+
68
+ def test_end_element
69
+ @parser.parse_memory(<<-eoxml)
70
+ <p id="asdfasdf">Paragraph 1</p>
71
+ eoxml
72
+ assert_equal [["p"]],
73
+ @parser.document.end_elements
74
+ end
75
+
76
+ def test_start_element_attrs
77
+ @parser.parse_memory(<<-eoxml)
78
+ <p id="asdfasdf">Paragraph 1</p>
79
+ eoxml
80
+ assert_equal [["p", ["id", "asdfasdf"]]],
81
+ @parser.document.start_elements
82
+ end
83
+
84
+ def test_parse_document
85
+ @parser.parse_memory(<<-eoxml)
86
+ <p>Paragraph 1</p>
87
+ <p>Paragraph 2</p>
88
+ eoxml
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestBuilder < Nokogiri::TestCase
6
+ def test_cdata
7
+ builder = Nokogiri::XML::Builder.new do
8
+ root {
9
+ cdata "hello world"
10
+ }
11
+ end
12
+ assert_equal("<?xml version=\"1.0\"?><root><![CDATA[hello world]]></root>", builder.to_xml.gsub(/\n/, ''))
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestCDATA < Nokogiri::TestCase
6
+ def setup
7
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
8
+ end
9
+
10
+ def test_cdata_node
11
+ name = @xml.xpath('//employee[2]/name').first
12
+ assert cdata = name.children[1]
13
+ assert cdata.cdata?
14
+ assert_equal 'cdata-section', cdata.name
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,171 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestDocument < Nokogiri::TestCase
6
+ def setup
7
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
8
+ end
9
+
10
+ def test_XML_function
11
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
12
+ assert xml.xml?
13
+ end
14
+
15
+ def test_document_name
16
+ xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
17
+ assert_equal 'document', xml.name
18
+ end
19
+
20
+ def test_parse_can_take_io
21
+ xml = nil
22
+ File.open(XML_FILE, 'rb') { |f|
23
+ xml = Nokogiri::XML(f)
24
+ }
25
+ assert xml.xml?
26
+ end
27
+
28
+ def test_search_on_empty_documents
29
+ doc = Nokogiri::XML::Document.new
30
+ ns = doc.search('//foo')
31
+ assert_equal 0, ns.length
32
+ end
33
+
34
+ def test_new_document_collect_namespaces
35
+ doc = Nokogiri::XML::Document.new
36
+ assert_equal({}, doc.collect_namespaces)
37
+ end
38
+
39
+ def test_find_with_namespace
40
+ doc = Nokogiri::XML.parse(<<-eoxml)
41
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
42
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
43
+ </x>
44
+ eoxml
45
+
46
+ ctx = Nokogiri::XML::XPathContext.new(doc)
47
+ ctx.register_ns 'tenderlove', 'http://tenderlovemaking.com/'
48
+ set = ctx.evaluate('//tenderlove:foo').node_set
49
+ assert_equal 1, set.length
50
+ assert_equal 'foo', set.first.name
51
+
52
+ # It looks like only the URI is important:
53
+ ctx = Nokogiri::XML::XPathContext.new(doc)
54
+ ctx.register_ns 'america', 'http://tenderlovemaking.com/'
55
+ set = ctx.evaluate('//america:foo').node_set
56
+ assert_equal 1, set.length
57
+ assert_equal 'foo', set.first.name
58
+
59
+ # Its so important that a missing slash will cause it to return nothing
60
+ ctx = Nokogiri::XML::XPathContext.new(doc)
61
+ ctx.register_ns 'america', 'http://tenderlovemaking.com'
62
+ set = ctx.evaluate('//america:foo').node_set
63
+ assert_equal 0, set.length
64
+ end
65
+
66
+ def test_xml?
67
+ assert @xml.xml?
68
+ end
69
+
70
+ def test_document
71
+ assert @xml.document
72
+ end
73
+
74
+ def test_multiple_search
75
+ assert node_set = @xml.search('//employee', '//name')
76
+ employees = @xml.search('//employee')
77
+ names = @xml.search('//name')
78
+ assert_equal(employees.length + names.length, node_set.length)
79
+ end
80
+
81
+ def test_node_set_index
82
+ assert node_set = @xml.search('//employee')
83
+
84
+ assert_equal(5, node_set.length)
85
+ assert node_set[4]
86
+ assert_nil node_set[5]
87
+ end
88
+
89
+ def test_search
90
+ assert node_set = @xml.search('//employee')
91
+
92
+ assert_equal(5, node_set.length)
93
+
94
+ node_set.each do |node|
95
+ assert_equal('employee', node.name)
96
+ end
97
+ end
98
+
99
+ def test_dump
100
+ assert @xml.serialize
101
+ assert @xml.to_xml
102
+ end
103
+
104
+ def test_subset_is_decorated
105
+ x = Module.new do
106
+ def awesome!
107
+ end
108
+ end
109
+ util_decorate(@xml, x)
110
+
111
+ assert @xml.respond_to?(:awesome!)
112
+ assert node_set = @xml.search('//staff')
113
+ assert node_set.respond_to?(:awesome!)
114
+ assert subset = node_set.search('.//employee')
115
+ assert subset.respond_to?(:awesome!)
116
+ assert sub_subset = node_set.search('.//name')
117
+ assert sub_subset.respond_to?(:awesome!)
118
+ end
119
+
120
+ def test_decorator_is_applied
121
+ x = Module.new do
122
+ def awesome!
123
+ end
124
+ end
125
+ util_decorate(@xml, x)
126
+
127
+ assert @xml.respond_to?(:awesome!)
128
+ assert node_set = @xml.search('//employee')
129
+ assert node_set.respond_to?(:awesome!)
130
+ node_set.each do |node|
131
+ assert node.respond_to?(:awesome!), node.class
132
+ end
133
+ assert @xml.root.respond_to?(:awesome!)
134
+ end
135
+
136
+ def test_new
137
+ doc = nil
138
+ assert_nothing_raised {
139
+ doc = Nokogiri::XML::Document.new
140
+ }
141
+ assert doc
142
+ assert doc.xml?
143
+ assert_nil doc.root
144
+ end
145
+
146
+ def test_set_root
147
+ doc = nil
148
+ assert_nothing_raised {
149
+ doc = Nokogiri::XML::Document.new
150
+ }
151
+ assert doc
152
+ assert doc.xml?
153
+ assert_nil doc.root
154
+ node = Nokogiri::XML::Node.new("b") { |n|
155
+ n.content = 'hello world'
156
+ }
157
+ assert_equal('hello world', node.content)
158
+ doc.root = node
159
+ assert_equal(node, doc.root)
160
+ end
161
+
162
+ def util_decorate(document, x)
163
+ document.decorators['document'] << x
164
+ document.decorators['node'] << x
165
+ document.decorators['element'] << x
166
+ document.decorators['nodeset'] << x
167
+ document.decorate!
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module HTML
5
+ class TestDTD < Nokogiri::TestCase
6
+ def setup
7
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE))
8
+ assert @dtd = @xml.internal_subset
9
+ end
10
+
11
+ def test_external_subsets
12
+ assert subset = @xml.internal_subset
13
+ assert_equal 'staff', subset.name
14
+ end
15
+
16
+ def test_entities
17
+ assert entities = @dtd.entities
18
+ assert_equal %w[ ent1 ent2 ent3 ent4 ent5 ].sort, entities.keys.sort
19
+ end
20
+
21
+ def test_attributes
22
+ assert attributes = @dtd.attributes
23
+ assert_equal %w[ width ], attributes.keys
24
+ assert_equal 'width', attributes['width'].name
25
+ end
26
+
27
+ def test_elements
28
+ assert elements = @dtd.elements
29
+ assert_equal %w[ br ], elements.keys
30
+ assert_equal 'br', elements['br'].name
31
+ end
32
+
33
+ def test_notations
34
+ assert notations = @dtd.notations
35
+ assert_equal %w[ notation1 notation2 ].sort, notations.keys.sort
36
+ assert notation1 = notations['notation1']
37
+ assert_equal 'notation1', notation1.name
38
+ assert_equal 'notation1File', notation1.public_id
39
+ assert_nil notation1.system_id
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,223 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestNode < Nokogiri::TestCase
6
+ def test_find_by_css_with_tilde_eql
7
+ xml = Nokogiri::XML.parse(<<-eoxml)
8
+ <root>
9
+ <a>Hello world</a>
10
+ <a class='foo bar'>Bar</a>
11
+ <a class='bar foo'>Bar</a>
12
+ <a class='bar'>Bar</a>
13
+ <a class='baz bar foo'>Bar</a>
14
+ <a class='bazbarfoo'>Awesome</a>
15
+ <a class='bazbar'>Awesome</a>
16
+ </root>
17
+ eoxml
18
+ set = xml.css('a[@class~="bar"]')
19
+ assert_equal 4, set.length
20
+ assert_equal ['Bar'], set.map { |node| node.content }.uniq
21
+ end
22
+
23
+ def test_unlink
24
+ xml = Nokogiri::XML.parse(<<-eoxml)
25
+ <root>
26
+ <a class='foo bar'>Bar</a>
27
+ <a class='bar foo'>Bar</a>
28
+ <a class='bar'>Bar</a>
29
+ <a>Hello world</a>
30
+ <a class='baz bar foo'>Bar</a>
31
+ <a class='bazbarfoo'>Awesome</a>
32
+ <a class='bazbar'>Awesome</a>
33
+ </root>
34
+ eoxml
35
+ node = xml.xpath('//a')[3]
36
+ assert_equal('Hello world', node.text)
37
+ assert_match(/Hello world/, xml.to_s)
38
+ assert node.parent
39
+ assert node.document
40
+ assert node.previous_sibling
41
+ assert node.next_sibling
42
+ assert node.instance_eval{ owned? }
43
+ node.unlink
44
+ assert !node.parent
45
+ # assert !node.document # ugh. libxml doesn't clear node->doc pointer, due to xmlDict implementation.
46
+ assert !node.previous_sibling
47
+ assert !node.next_sibling
48
+ assert !node.instance_eval{ owned? }
49
+ assert_no_match(/Hello world/, xml.to_s)
50
+ end
51
+
52
+ def test_dup
53
+ html = Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE)
54
+ found = html.search('//div/a').first
55
+ dup = found.dup
56
+ assert dup
57
+ assert_equal found.content, dup.content
58
+ end
59
+
60
+ def test_search_can_handle_xpath_and_css
61
+ html = Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE)
62
+ found = html.search('//div/a', 'div > p')
63
+ length = html.xpath('//div/a').length +
64
+ html.css('div > p').length
65
+ assert_equal length, found.length
66
+ end
67
+
68
+ def test_find_by_xpath
69
+ html = Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE)
70
+ found = html.xpath('//div/a')
71
+ assert_equal 3, found.length
72
+ end
73
+
74
+ def test_find_by_css
75
+ html = Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE)
76
+ found = html.css('div > a')
77
+ assert_equal 3, found.length
78
+ end
79
+
80
+ def test_next_sibling
81
+ xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
82
+ assert node = xml.root
83
+ assert sibling = node.child.next_sibling
84
+ assert_equal('employee', sibling.name)
85
+ end
86
+
87
+ def test_previous_sibling
88
+ xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
89
+ assert node = xml.root
90
+ assert sibling = node.child.next_sibling
91
+ assert_equal('employee', sibling.name)
92
+ assert_equal(sibling.previous_sibling, node.child)
93
+ end
94
+
95
+ def test_name=
96
+ xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
97
+ assert node = xml.root
98
+ node.name = 'awesome'
99
+ assert_equal('awesome', node.name)
100
+ end
101
+
102
+ def test_child
103
+ xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
104
+ assert node = xml.root
105
+ assert child = node.child
106
+ assert_equal('text', child.name)
107
+ end
108
+
109
+ def test_key?
110
+ xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
111
+ assert node = xml.search('//address').first
112
+ assert(!node.key?('asdfasdf'))
113
+ end
114
+
115
+ def test_set_property
116
+ xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
117
+ assert node = xml.search('//address').first
118
+ node['foo'] = 'bar'
119
+ assert_equal('bar', node['foo'])
120
+ end
121
+
122
+ def test_attributes
123
+ xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
124
+ assert node = xml.search('//address').first
125
+ assert_nil(node['asdfasdfasdf'])
126
+ assert_equal('Yes', node['domestic'])
127
+
128
+ assert node = xml.search('//address')[2]
129
+ attr = node.attributes
130
+ assert_equal 2, attr.size
131
+ assert_equal 'Yes', attr['domestic']
132
+ assert_equal 'No', attr['street']
133
+ end
134
+
135
+ def test_path
136
+ xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
137
+ assert set = xml.search('//employee')
138
+ assert node = set.first
139
+ assert_equal('/staff/employee[1]', node.path)
140
+ end
141
+
142
+ def test_new_node
143
+ node = Nokogiri::XML::Node.new('form')
144
+ assert_equal('form', node.name)
145
+ assert_nil(node.document)
146
+ end
147
+
148
+ def test_content
149
+ node = Nokogiri::XML::Node.new('form')
150
+ assert_equal('', node.content)
151
+
152
+ node.content = 'hello world!'
153
+ assert_equal('hello world!', node.content)
154
+ end
155
+
156
+ def test_replace
157
+ xml = Nokogiri::XML.parse(File.read(XML_FILE))
158
+ set = xml.search('//employee')
159
+ assert 5, set.length
160
+ assert 0, xml.search('//form').length
161
+
162
+ first = set[0]
163
+ second = set[1]
164
+
165
+ node = Nokogiri::XML::Node.new('form')
166
+ first.replace(node)
167
+
168
+ assert set = xml.search('//employee')
169
+ assert_equal 4, set.length
170
+ assert 1, xml.search('//form').length
171
+
172
+ assert_equal set[0].to_xml, second.to_xml
173
+ end
174
+
175
+ def test_namespace_as_hash
176
+ xml = Nokogiri::XML.parse(<<-eoxml)
177
+ <root>
178
+ <car xmlns:part="http://general-motors.com/">
179
+ <part:tire>Michelin Model XGV</part:tire>
180
+ </car>
181
+ <bicycle xmlns:part="http://schwinn.com/">
182
+ <part:tire>I'm a bicycle tire!</part:tire>
183
+ </bicycle>
184
+ </root>
185
+ eoxml
186
+
187
+ tires = xml.xpath('//bike:tire', {'bike' => 'http://schwinn.com/'})
188
+ assert_equal 1, tires.length
189
+ end
190
+
191
+ def test_namespaces
192
+ xml = Nokogiri::XML.parse(<<-EOF)
193
+ <x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
194
+ <y xmlns:c='http://bazz.com/'>
195
+ <a:div>hello a</a:div>
196
+ <b:div>hello b</b:div>
197
+ <c:div>hello c</c:div>
198
+ </y>
199
+ </x>
200
+ EOF
201
+ assert namespaces = xml.root.namespaces
202
+ assert namespaces.key?('xmlns:a')
203
+ assert_equal 'http://foo.com/', namespaces['xmlns:a']
204
+ assert namespaces.key?('xmlns:b')
205
+ assert_equal 'http://bar.com/', namespaces['xmlns:b']
206
+ assert ! namespaces.key?('xmlns:c')
207
+
208
+ assert namespaces = xml.namespaces
209
+ assert namespaces.key?('xmlns:a')
210
+ assert_equal 'http://foo.com/', namespaces['xmlns:a']
211
+ assert namespaces.key?('xmlns:b')
212
+ assert_equal 'http://bar.com/', namespaces['xmlns:b']
213
+ assert namespaces.key?('xmlns:c')
214
+ assert_equal 'http://bazz.com/', namespaces['xmlns:c']
215
+
216
+ assert_equal "hello a", xml.search("//a:div", xml.namespaces).first.inner_text
217
+ assert_equal "hello b", xml.search("//b:div", xml.namespaces).first.inner_text
218
+ assert_equal "hello c", xml.search("//c:div", xml.namespaces).first.inner_text
219
+ end
220
+
221
+ end
222
+ end
223
+ end
@@ -0,0 +1,116 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestNodeSet < Nokogiri::TestCase
6
+ def setup
7
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
8
+ end
9
+
10
+ def test_length_size
11
+ assert node_set = @xml.search('//employee')
12
+ assert_equal node_set.length, node_set.size
13
+ end
14
+
15
+ def test_at
16
+ assert node_set = @xml.search('//employee')
17
+ assert_equal node_set.first, node_set.at(0)
18
+ end
19
+
20
+ def test_push
21
+ node = Nokogiri::XML::Node.new('foo')
22
+ node.content = 'bar'
23
+
24
+ assert node_set = @xml.search('//employee')
25
+ node_set.push(node)
26
+
27
+ assert node_set.include?(node)
28
+ end
29
+
30
+ def test_unlink
31
+ xml = Nokogiri::XML.parse(<<-eoxml)
32
+ <root>
33
+ <a class='foo bar'>Bar</a>
34
+ <a class='bar foo'>Bar</a>
35
+ <a class='bar'>Bar</a>
36
+ <a>Hello world</a>
37
+ <a class='baz bar foo'>Bar</a>
38
+ <a class='bazbarfoo'>Awesome</a>
39
+ <a class='bazbar'>Awesome</a>
40
+ </root>
41
+ eoxml
42
+ set = xml.xpath('//a')
43
+ set.unlink
44
+ set.each do |node|
45
+ assert !node.parent
46
+ # assert !node.document # ugh. libxml doesn't clear node->doc pointer, due to xmlDict implementation.
47
+ assert !node.previous_sibling
48
+ assert !node.next_sibling
49
+ assert !node.instance_eval{ owned? }
50
+ end
51
+ assert !set.document
52
+ assert_no_match(/Hello world/, xml.to_s)
53
+ end
54
+
55
+ def test_nodeset_search_takes_namespace
56
+ @xml = Nokogiri::XML.parse(<<-eoxml)
57
+ <root>
58
+ <car xmlns:part="http://general-motors.com/">
59
+ <part:tire>Michelin Model XGV</part:tire>
60
+ </car>
61
+ <bicycle xmlns:part="http://schwinn.com/">
62
+ <part:tire>I'm a bicycle tire!</part:tire>
63
+ </bicycle>
64
+ </root>
65
+ eoxml
66
+ set = @xml/'root'
67
+ assert_equal 1, set.length
68
+ bike_tire = set.search('//bike:tire', 'bike' => "http://schwinn.com/")
69
+ assert_equal 1, bike_tire.length
70
+ end
71
+
72
+ def test_new_nodeset
73
+ node_set = Nokogiri::XML::NodeSet.new
74
+ assert_equal(0, node_set.length)
75
+ node = Nokogiri::XML::Node.new('form')
76
+ node_set << node
77
+ assert_equal(1, node_set.length)
78
+ assert_equal(node, node_set.last)
79
+ end
80
+
81
+ def test_search_on_nodeset
82
+ assert node_set = @xml.search('//employee')
83
+ assert sub_set = node_set.search('.//name')
84
+ assert_equal(node_set.length, sub_set.length)
85
+ end
86
+
87
+ def test_negative_index_works
88
+ assert node_set = @xml.search('//employee')
89
+ assert_equal node_set.last, node_set[-1]
90
+ end
91
+
92
+ def test_large_negative_index_returns_nil
93
+ assert node_set = @xml.search('//employee')
94
+ assert_nil(node_set[-1 * (node_set.length + 1)])
95
+ end
96
+
97
+ def test_node_set_fetches_private_data
98
+ assert node_set = @xml.search('//employee')
99
+
100
+ set = node_set
101
+ assert_equal(set[0], set[0])
102
+ end
103
+
104
+ def test_node_set_returns_0
105
+ assert node_set = @xml.search('//asdkfjhasdlkfjhaldskfh')
106
+ assert_equal(0, node_set.length)
107
+ end
108
+
109
+ def test_wrap
110
+ employees = (@xml/"//employee").wrap("<wrapper/>")
111
+ assert_equal 'wrapper', employees[0].parent.name
112
+ assert_equal 'employee', @xml.search("//wrapper").first.children[0].name
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestText < Nokogiri::TestCase
6
+ def test_new
7
+ node = Text.new('hello world')
8
+ assert node
9
+ assert_equal('hello world', node.content)
10
+ end
11
+ end
12
+ end
13
+ end