nokogiri 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (200) hide show
  1. data/.autotest +14 -2
  2. data/CHANGELOG.ja.rdoc +38 -0
  3. data/CHANGELOG.rdoc +43 -0
  4. data/Manifest.txt +80 -5
  5. data/README.ja.rdoc +12 -11
  6. data/README.rdoc +4 -2
  7. data/Rakefile +103 -173
  8. data/bin/nokogiri +47 -0
  9. data/ext/nokogiri/extconf.rb +19 -13
  10. data/ext/nokogiri/html_document.c +39 -3
  11. data/ext/nokogiri/html_document.h +1 -1
  12. data/ext/nokogiri/html_element_description.c +272 -0
  13. data/ext/nokogiri/html_element_description.h +10 -0
  14. data/ext/nokogiri/html_entity_lookup.h +1 -1
  15. data/ext/nokogiri/html_sax_parser.h +1 -1
  16. data/ext/nokogiri/{native.c → nokogiri.c} +11 -3
  17. data/ext/nokogiri/{native.h → nokogiri.h} +18 -4
  18. data/ext/nokogiri/xml_attr.c +14 -5
  19. data/ext/nokogiri/xml_attr.h +1 -1
  20. data/ext/nokogiri/xml_cdata.c +15 -6
  21. data/ext/nokogiri/xml_cdata.h +1 -1
  22. data/ext/nokogiri/xml_comment.c +13 -4
  23. data/ext/nokogiri/xml_comment.h +1 -1
  24. data/ext/nokogiri/xml_document.c +50 -41
  25. data/ext/nokogiri/xml_document.h +1 -1
  26. data/ext/nokogiri/xml_document_fragment.c +12 -4
  27. data/ext/nokogiri/xml_document_fragment.h +1 -1
  28. data/ext/nokogiri/xml_dtd.c +1 -1
  29. data/ext/nokogiri/xml_dtd.h +1 -1
  30. data/ext/nokogiri/xml_entity_reference.c +13 -4
  31. data/ext/nokogiri/xml_entity_reference.h +1 -1
  32. data/ext/nokogiri/xml_io.h +1 -1
  33. data/ext/nokogiri/xml_namespace.c +69 -0
  34. data/ext/nokogiri/xml_namespace.h +12 -0
  35. data/ext/nokogiri/xml_node.c +232 -124
  36. data/ext/nokogiri/xml_node.h +3 -4
  37. data/ext/nokogiri/xml_node_set.c +206 -19
  38. data/ext/nokogiri/xml_node_set.h +1 -1
  39. data/ext/nokogiri/xml_processing_instruction.c +14 -4
  40. data/ext/nokogiri/xml_processing_instruction.h +1 -1
  41. data/ext/nokogiri/xml_reader.c +87 -7
  42. data/ext/nokogiri/xml_reader.h +1 -1
  43. data/ext/nokogiri/xml_relax_ng.c +106 -0
  44. data/ext/nokogiri/xml_relax_ng.h +9 -0
  45. data/ext/nokogiri/xml_sax_parser.c +122 -2
  46. data/ext/nokogiri/xml_sax_parser.h +1 -1
  47. data/ext/nokogiri/xml_sax_push_parser.c +1 -0
  48. data/ext/nokogiri/xml_sax_push_parser.h +1 -1
  49. data/ext/nokogiri/xml_schema.c +107 -0
  50. data/ext/nokogiri/xml_schema.h +9 -0
  51. data/ext/nokogiri/xml_syntax_error.h +1 -1
  52. data/ext/nokogiri/xml_text.c +10 -3
  53. data/ext/nokogiri/xml_text.h +1 -1
  54. data/ext/nokogiri/xml_xpath.h +1 -1
  55. data/ext/nokogiri/xml_xpath_context.h +1 -1
  56. data/ext/nokogiri/xslt_stylesheet.c +29 -16
  57. data/ext/nokogiri/xslt_stylesheet.h +1 -1
  58. data/lib/action-nokogiri.rb +7 -1
  59. data/lib/nokogiri.rb +21 -5
  60. data/lib/nokogiri/css/generated_parser.rb +49 -14
  61. data/lib/nokogiri/css/generated_tokenizer.rb +2 -2
  62. data/lib/nokogiri/css/node.rb +13 -3
  63. data/lib/nokogiri/css/parser.rb +8 -0
  64. data/lib/nokogiri/css/parser.y +7 -7
  65. data/lib/nokogiri/css/tokenizer.rb +2 -0
  66. data/lib/nokogiri/css/xpath_visitor.rb +10 -6
  67. data/lib/nokogiri/decorators/hpricot/node.rb +1 -1
  68. data/lib/nokogiri/decorators/hpricot/node_set.rb +2 -2
  69. data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +2 -0
  70. data/lib/nokogiri/decorators/slop.rb +3 -1
  71. data/lib/nokogiri/ffi/html/document.rb +37 -0
  72. data/lib/nokogiri/ffi/html/element_description.rb +85 -0
  73. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  74. data/lib/nokogiri/ffi/html/sax/parser.rb +21 -0
  75. data/lib/nokogiri/ffi/io_callbacks.rb +32 -0
  76. data/lib/nokogiri/ffi/libxml.rb +314 -0
  77. data/lib/nokogiri/ffi/structs/common_node.rb +26 -0
  78. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  79. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  80. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  81. data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
  82. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  83. data/lib/nokogiri/ffi/structs/xml_document.rb +108 -0
  84. data/lib/nokogiri/ffi/structs/xml_dtd.rb +26 -0
  85. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  86. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  87. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  88. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  89. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  90. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  91. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +14 -0
  92. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  93. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  94. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  95. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +37 -0
  96. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  97. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  98. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  99. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  100. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  101. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  102. data/lib/nokogiri/ffi/xml/document.rb +107 -0
  103. data/lib/nokogiri/ffi/xml/document_fragment.rb +26 -0
  104. data/lib/nokogiri/ffi/xml/dtd.rb +42 -0
  105. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  106. data/lib/nokogiri/ffi/xml/namespace.rb +38 -0
  107. data/lib/nokogiri/ffi/xml/node.rb +380 -0
  108. data/lib/nokogiri/ffi/xml/node_set.rb +130 -0
  109. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  110. data/lib/nokogiri/ffi/xml/reader.rb +217 -0
  111. data/lib/nokogiri/ffi/xml/relax_ng.rb +51 -0
  112. data/lib/nokogiri/ffi/xml/sax/parser.rb +148 -0
  113. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +38 -0
  114. data/lib/nokogiri/ffi/xml/schema.rb +55 -0
  115. data/lib/nokogiri/ffi/xml/syntax_error.rb +76 -0
  116. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  117. data/lib/nokogiri/ffi/xml/xpath.rb +19 -0
  118. data/lib/nokogiri/ffi/xml/xpath_context.rb +135 -0
  119. data/lib/nokogiri/ffi/xslt/stylesheet.rb +47 -0
  120. data/lib/nokogiri/hpricot.rb +14 -3
  121. data/lib/nokogiri/html.rb +11 -46
  122. data/lib/nokogiri/html/builder.rb +27 -1
  123. data/lib/nokogiri/html/document.rb +62 -6
  124. data/lib/nokogiri/html/document_fragment.rb +15 -0
  125. data/lib/nokogiri/html/element_description.rb +23 -0
  126. data/lib/nokogiri/html/entity_lookup.rb +2 -0
  127. data/lib/nokogiri/html/sax/parser.rb +27 -1
  128. data/lib/nokogiri/version.rb +26 -1
  129. data/lib/nokogiri/version_warning.rb +11 -0
  130. data/lib/nokogiri/xml.rb +25 -51
  131. data/lib/nokogiri/xml/builder.rb +166 -10
  132. data/lib/nokogiri/xml/cdata.rb +3 -1
  133. data/lib/nokogiri/xml/document.rb +39 -6
  134. data/lib/nokogiri/xml/document_fragment.rb +41 -1
  135. data/lib/nokogiri/xml/dtd.rb +3 -1
  136. data/lib/nokogiri/xml/entity_declaration.rb +3 -1
  137. data/lib/nokogiri/xml/fragment_handler.rb +24 -3
  138. data/lib/nokogiri/xml/namespace.rb +7 -0
  139. data/lib/nokogiri/xml/node.rb +314 -65
  140. data/lib/nokogiri/xml/node/save_options.rb +12 -2
  141. data/lib/nokogiri/xml/node_set.rb +58 -8
  142. data/lib/nokogiri/xml/parse_options.rb +80 -0
  143. data/lib/nokogiri/xml/processing_instruction.rb +2 -0
  144. data/lib/nokogiri/xml/reader.rb +42 -3
  145. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  146. data/lib/nokogiri/xml/sax.rb +0 -7
  147. data/lib/nokogiri/xml/sax/document.rb +84 -0
  148. data/lib/nokogiri/xml/sax/parser.rb +38 -2
  149. data/lib/nokogiri/xml/sax/push_parser.rb +12 -0
  150. data/lib/nokogiri/xml/schema.rb +65 -0
  151. data/lib/nokogiri/xml/syntax_error.rb +11 -0
  152. data/lib/nokogiri/xml/xpath.rb +1 -1
  153. data/lib/nokogiri/xml/xpath_context.rb +2 -0
  154. data/lib/nokogiri/xslt.rb +21 -1
  155. data/lib/nokogiri/xslt/stylesheet.rb +19 -0
  156. data/lib/xsd/xmlparser/nokogiri.rb +12 -2
  157. data/tasks/test.rb +42 -19
  158. data/test/css/test_parser.rb +29 -0
  159. data/test/ffi/test_document.rb +35 -0
  160. data/test/files/address_book.rlx +12 -0
  161. data/test/files/address_book.xml +10 -0
  162. data/test/files/po.xml +32 -0
  163. data/test/files/po.xsd +66 -0
  164. data/test/helper.rb +38 -8
  165. data/test/html/sax/test_parser.rb +12 -0
  166. data/test/html/test_builder.rb +25 -2
  167. data/test/html/test_document.rb +91 -20
  168. data/test/html/test_document_fragment.rb +97 -0
  169. data/test/html/test_element_description.rb +95 -0
  170. data/test/html/test_node.rb +66 -3
  171. data/test/test_convert_xpath.rb +1 -1
  172. data/test/test_memory_leak.rb +57 -18
  173. data/test/test_nokogiri.rb +24 -2
  174. data/test/test_reader.rb +77 -0
  175. data/test/test_xslt_transforms.rb +120 -82
  176. data/test/xml/node/test_subclass.rb +44 -0
  177. data/test/xml/sax/test_parser.rb +9 -0
  178. data/test/xml/sax/test_push_parser.rb +24 -0
  179. data/test/xml/test_attr.rb +7 -0
  180. data/test/xml/test_builder.rb +48 -0
  181. data/test/xml/test_cdata.rb +19 -0
  182. data/test/xml/test_comment.rb +6 -0
  183. data/test/xml/test_document.rb +101 -2
  184. data/test/xml/test_document_fragment.rb +55 -3
  185. data/test/xml/test_entity_reference.rb +4 -0
  186. data/test/xml/test_namespace.rb +43 -0
  187. data/test/xml/test_node.rb +255 -8
  188. data/test/xml/test_node_attributes.rb +34 -0
  189. data/test/xml/test_node_encoding.rb +9 -2
  190. data/test/xml/test_node_set.rb +197 -1
  191. data/test/xml/test_parse_options.rb +52 -0
  192. data/test/xml/test_processing_instruction.rb +5 -0
  193. data/test/xml/test_relax_ng.rb +60 -0
  194. data/test/xml/test_schema.rb +65 -0
  195. data/test/xml/test_text.rb +5 -0
  196. data/test/xml/test_unparented_node.rb +3 -3
  197. metadata +128 -12
  198. data/lib/nokogiri/xml/comment.rb +0 -6
  199. data/lib/nokogiri/xml/element.rb +0 -6
  200. data/lib/nokogiri/xml/text.rb +0 -6
@@ -0,0 +1,97 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module HTML
5
+ class TestDocumentFragment < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @html = Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE)
9
+ end
10
+
11
+ def test_new
12
+ fragment = Nokogiri::HTML::DocumentFragment.new(@html)
13
+ end
14
+
15
+ def test_fragment_should_have_document
16
+ fragment = Nokogiri::HTML::DocumentFragment.new(@html)
17
+ assert_equal @html, fragment.document
18
+ end
19
+
20
+ def test_name
21
+ fragment = Nokogiri::HTML::DocumentFragment.new(@html)
22
+ assert_equal '#document-fragment', fragment.name
23
+ end
24
+
25
+ def test_static_method
26
+ fragment = Nokogiri::HTML::DocumentFragment.parse("<div>a</div>")
27
+ assert_instance_of Nokogiri::HTML::DocumentFragment, fragment
28
+ end
29
+
30
+ def test_many_fragments
31
+ 100.times { Nokogiri::HTML::DocumentFragment.new(@html) }
32
+ end
33
+
34
+ def test_subclass
35
+ klass = Class.new(Nokogiri::HTML::DocumentFragment)
36
+ fragment = klass.new(@html, "<div>a</div>")
37
+ assert_instance_of klass, fragment
38
+ end
39
+
40
+ def test_html_fragment
41
+ fragment = Nokogiri::HTML.fragment("<div>a</div>")
42
+ assert_equal "<div>a</div>", fragment.to_s
43
+ end
44
+
45
+ def test_html_fragment_has_outer_text
46
+ doc = "a<div>b</div>c"
47
+ fragment = Nokogiri::HTML::Document.new.fragment(doc)
48
+ if Nokogiri::VERSION_INFO['libxml']['loaded'] <= "2.6.16"
49
+ assert_equal "a<div>b</div><p>c</p>", fragment.to_s
50
+ else
51
+ assert_equal "a<div>b</div>c", fragment.to_s
52
+ end
53
+ end
54
+
55
+ def test_html_fragment_case_insensitivity
56
+ doc = "<crazyDiv>b</crazyDiv>"
57
+ fragment = Nokogiri::HTML::Document.new.fragment(doc)
58
+ assert_equal "<crazydiv>b</crazydiv>", fragment.to_s
59
+ end
60
+
61
+ def test_html_fragment_with_leading_whitespace
62
+ doc = " <div>b</div> "
63
+ fragment = Nokogiri::HTML::Document.new.fragment(doc)
64
+ assert_equal "<div>b</div>", fragment.to_s
65
+ end
66
+
67
+ def test_to_s
68
+ doc = "<span>foo<br></span><span>bar</span>"
69
+ fragment = Nokogiri::HTML::Document.new.fragment(doc)
70
+ assert_equal "<span>foo<br></span><span>bar</span>", fragment.to_s
71
+ end
72
+
73
+ def test_to_html
74
+ doc = "<span>foo<br></span><span>bar</span>"
75
+ fragment = Nokogiri::HTML::Document.new.fragment(doc)
76
+ assert_equal "<span>foo<br></span><span>bar</span>", fragment.to_html
77
+ end
78
+
79
+ def test_to_xhtml
80
+ doc = "<span>foo<br></span><span>bar</span>"
81
+ fragment = Nokogiri::HTML::Document.new.fragment(doc)
82
+ if Nokogiri::VERSION_INFO['libxml']['loaded'] >= "2.7.0"
83
+ assert_equal "<span>foo<br /></span><span>bar</span>", fragment.to_xhtml
84
+ else
85
+ assert_equal "<span>foo<br></span><span>bar</span>", fragment.to_xhtml
86
+ end
87
+ end
88
+
89
+ def test_to_xml
90
+ doc = "<span>foo<br></span><span>bar</span>"
91
+ fragment = Nokogiri::HTML::Document.new.fragment(doc)
92
+ assert_equal "<span>foo<br/></span><span>bar</span>", fragment.to_xml
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,95 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module HTML
5
+ class TestElementDescription < Nokogiri::TestCase
6
+ def test_fetch_nonexistent
7
+ assert_nil ElementDescription['foo']
8
+ end
9
+
10
+ def test_fetch_element_description
11
+ assert desc = ElementDescription['a']
12
+ assert_instance_of ElementDescription, desc
13
+ end
14
+
15
+ def test_name
16
+ assert_equal 'a', ElementDescription['a'].name
17
+ end
18
+
19
+ def test_implied_start_tag?
20
+ assert !ElementDescription['a'].implied_start_tag?
21
+ end
22
+
23
+ def test_implied_end_tag?
24
+ assert !ElementDescription['a'].implied_end_tag?
25
+ assert ElementDescription['p'].implied_end_tag?
26
+ end
27
+
28
+ def test_save_end_tag?
29
+ assert !ElementDescription['a'].save_end_tag?
30
+ assert ElementDescription['br'].save_end_tag?
31
+ end
32
+
33
+ def test_empty?
34
+ assert ElementDescription['br'].empty?
35
+ assert !ElementDescription['a'].empty?
36
+ end
37
+
38
+ def test_deprecated?
39
+ assert ElementDescription['applet'].deprecated?
40
+ assert !ElementDescription['br'].deprecated?
41
+ end
42
+
43
+ def test_inline?
44
+ assert ElementDescription['a'].inline?
45
+ assert !ElementDescription['div'].inline?
46
+ end
47
+
48
+ def test_block?
49
+ element = ElementDescription['a']
50
+ assert_equal(!element.inline?, element.block?)
51
+ end
52
+
53
+ def test_description
54
+ assert ElementDescription['a'].description
55
+ end
56
+
57
+ def test_subelements
58
+ sub_elements = ElementDescription['body'].sub_elements
59
+ assert_equal 61, sub_elements.length
60
+ end
61
+
62
+ def test_default_sub_element
63
+ assert_equal 'div', ElementDescription['body'].default_sub_element
64
+ end
65
+
66
+ def test_optional_attributes
67
+ attrs = ElementDescription['table'].optional_attributes
68
+ assert attrs
69
+ assert_equal 22, attrs.length
70
+ end
71
+
72
+ def test_deprecated_attributes
73
+ attrs = ElementDescription['table'].deprecated_attributes
74
+ assert attrs
75
+ assert_equal 2, attrs.length
76
+ end
77
+
78
+ def test_required_attributes
79
+ attrs = ElementDescription['table'].required_attributes
80
+ assert attrs
81
+ assert_equal 0, attrs.length
82
+ end
83
+
84
+ def test_inspect
85
+ desc = ElementDescription['input']
86
+ assert_match desc.name, desc.inspect
87
+ end
88
+
89
+ def test_to_s
90
+ desc = ElementDescription['input']
91
+ assert_match desc.name, desc.to_s
92
+ end
93
+ end
94
+ end
95
+ end
@@ -11,12 +11,46 @@ module Nokogiri
11
11
  <html>
12
12
  <head></head>
13
13
  <body>
14
- <div>first</div>
14
+ <div class='baz'><a href="foo" class="bar">first</a></div>
15
15
  </body>
16
16
  </html>
17
17
  eohtml
18
18
  end
19
19
 
20
+ def test_description
21
+ assert desc = @html.at('a.bar').description
22
+ assert_equal 'a', desc.name
23
+ end
24
+
25
+ def test_add_next_sibling_with_empty_nodeset
26
+ assert_raises(ArgumentError) {
27
+ @html.at('a').add_next_sibling(@html.at('head').children)
28
+ }
29
+ end
30
+
31
+ def test_add_next_sibling_with_non_empty_nodeset
32
+ assert_raises(ArgumentError) {
33
+ @html.at('head').add_next_sibling(@html.at('div').children)
34
+ }
35
+ end
36
+
37
+ def test_ancestors_with_selector
38
+ assert node = @html.at('a.bar').child
39
+ assert list = node.ancestors('.baz')
40
+ assert_equal 1, list.length
41
+ assert_equal 'div', list.first.name
42
+ end
43
+
44
+ def test_css_matches?
45
+ assert node = @html.at('a.bar')
46
+ assert node.matches?('a.bar')
47
+ end
48
+
49
+ def test_xpath_matches?
50
+ assert node = @html.at('//a')
51
+ assert node.matches?('//a')
52
+ end
53
+
20
54
  def test_swap
21
55
  @html.at('div').swap('<a href="foo">bar</a>')
22
56
  a_tag = @html.css('a').first
@@ -24,6 +58,12 @@ module Nokogiri
24
58
  assert_equal 0, @html.css('div').length
25
59
  end
26
60
 
61
+ def test_swap_with_regex_characters
62
+ @html.at('div').swap('<a href="foo">ba)r</a>')
63
+ a_tag = @html.css('a').first
64
+ assert_equal 'ba)r', a_tag.text
65
+ end
66
+
27
67
  def test_attribute_decodes_entities
28
68
  node = @html.at('div')
29
69
  node['href'] = 'foo&bar'
@@ -39,6 +79,13 @@ module Nokogiri
39
79
  assert_equal 'some text', @html.at('//body').children[0].content.strip
40
80
  end
41
81
 
82
+ def test_fragment_handler_does_not_regurge_on_invalid_attributes
83
+ iframe = %Q{<iframe style="width: 0%; height: 0px" src="http://someurl" allowtransparency></iframe>}
84
+ assert_nothing_raised { @html.at('div').before(iframe) }
85
+ assert_nothing_raised { @html.at('div').after(iframe) }
86
+ assert_nothing_raised { @html.at('div').inner_html=(iframe) }
87
+ end
88
+
42
89
  def test_inner_html=
43
90
  assert div = @html.at('//div')
44
91
  div.inner_html = '<span>testing</span>'
@@ -63,6 +110,15 @@ module Nokogiri
63
110
  assert_equal 'foo', p_tag.parent['class']
64
111
  end
65
112
 
113
+ def test_fragment_serialization
114
+ fragment = Nokogiri::HTML.fragment("<div>foo</div>")
115
+ assert_equal "<div>foo</div>", fragment.serialize.chomp
116
+ assert_equal "<div>foo</div>", fragment.to_xml.chomp
117
+ assert_equal "<div>foo</div>", fragment.inner_html
118
+ assert_equal "<div>foo</div>", fragment.to_html
119
+ assert_equal "<div>foo</div>", fragment.to_s
120
+ end
121
+
66
122
  def test_after_will_append_text_nodes
67
123
  assert node = @html.at('//body/div')
68
124
  node.after "some text"
@@ -94,8 +150,15 @@ module Nokogiri
94
150
  EOH
95
151
  nokogiri = Nokogiri::HTML.parse(html)
96
152
 
97
- assert_equal "<p>testparagraph\r\nfoobar</p>",
98
- nokogiri.at("p").to_html.gsub(/ /, '')
153
+ if RUBY_PLATFORM =~ /java/
154
+ # NKF linebreak modes are not supported as of jruby 1.2
155
+ # see http://jira.codehaus.org/browse/JRUBY-3602 for status
156
+ assert_equal "<p>testparagraph\nfoobar</p>",
157
+ nokogiri.at("p").to_html.gsub(/ /, '')
158
+ else
159
+ assert_equal "<p>testparagraph\r\nfoobar</p>",
160
+ nokogiri.at("p").to_html.gsub(/ /, '')
161
+ end
99
162
  end
100
163
  end
101
164
  end
@@ -21,7 +21,7 @@ class TestConvertXPath < Nokogiri::TestCase
21
21
  blk ||= lambda {|j| j.first}
22
22
  assert_equal match, blk.call(@N.search(xpath)), "xpath result did not match"
23
23
  if HAS_HPRICOT
24
- assert_equal match, blk.call(@H.search(hpath)), "hpath result did not match"
24
+ assert_equal match, blk.call(@H.search(hpath)).chomp, "hpath result did not match"
25
25
  end
26
26
  assert_equal [xpath], @NH.convert_to_xpath(hpath), "converted hpath did not match xpath"
27
27
  end
@@ -1,15 +1,18 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
2
 
3
3
  class TestMemoryLeak < Nokogiri::TestCase
4
- def test_for_memory_leak
5
- begin
6
- # we don't use Dike in any tests, but requiring it has side effects
7
- # that can create memory leaks, and that's what we're testing for.
8
- require 'rubygems'
9
- require 'dike' # do not remove!
10
-
11
- count_start = count_object_space_documents
12
- xml_data = <<-EOS
4
+
5
+ if ENV['NOKOGIRI_GC'] # turning these off by default for now
6
+
7
+ def test_for_memory_leak
8
+ begin
9
+ # we don't use Dike in any tests, but requiring it has side effects
10
+ # that can create memory leaks, and that's what we're testing for.
11
+ require 'rubygems'
12
+ require 'dike' # do not remove!
13
+
14
+ count_start = count_object_space_documents
15
+ xml_data = <<-EOS
13
16
  <test>
14
17
  <items>
15
18
  <item>abc</item>
@@ -18,17 +21,53 @@ class TestMemoryLeak < Nokogiri::TestCase
18
21
  <items>
19
22
  </test>
20
23
  EOS
21
- 20.times do
22
- doc = Nokogiri::XML(xml_data)
23
- doc.xpath("//item")
24
+ 20.times do
25
+ doc = Nokogiri::XML(xml_data)
26
+ doc.xpath("//item")
27
+ end
28
+ 2.times { GC.start }
29
+ count_end = count_object_space_documents
30
+ assert((count_end - count_start) <= 2, "memory leak detected")
31
+ rescue LoadError
32
+ puts "\ndike is not installed, skipping memory leak test"
24
33
  end
25
- 2.times { GC.start }
26
- count_end = count_object_space_documents
27
- assert((count_end - count_start) <= 2, "memory leak detected")
28
- rescue LoadError
29
- puts "\ndike is not installed, skipping memory leak test"
30
34
  end
31
- end
35
+
36
+ if Nokogiri.ffi?
37
+ [ ['Node', 'p', nil],
38
+ ['CDATA', nil, 'content'],
39
+ ['Comment', nil, 'content'],
40
+ ['DocumentFragment', nil],
41
+ ['EntityReference', nil, 'p'],
42
+ ['ProcessingInstruction', nil, 'p', 'content'] ].each do |klass, *args|
43
+
44
+ define_method "test_for_leaked_#{klass}_nodes" do
45
+ Nokogiri::LibXML.expects(:xmlAddChild).at_least(1) # more than once shows we're GCing properly
46
+ 10.times {
47
+ xml = Nokogiri::XML("<root></root>")
48
+ 2.times { Nokogiri::XML.const_get(klass).new(*(args.collect{|arg| arg || xml})) }
49
+ GC.start
50
+ }
51
+ GC.start
52
+ end
53
+
54
+ end
55
+
56
+ def test_for_leaked_attr_nodes
57
+ Nokogiri::LibXML.expects(:xmlFreePropList).at_least(1) # more than once shows we're GCing properly
58
+ 10.times {
59
+ xml = Nokogiri::XML("<root></root>")
60
+ 2.times { Nokogiri::XML::Attr.new(xml, "p") }
61
+ GC.start
62
+ }
63
+ GC.start
64
+ end
65
+
66
+ end # if ffi
67
+
68
+ end # if NOKOGIRI_GC
69
+
70
+ private
32
71
 
33
72
  def count_object_space_documents
34
73
  count = 0
@@ -1,12 +1,33 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
2
 
3
3
  class TestNokogiri < Nokogiri::TestCase
4
- def test_version
4
+ def test_versions
5
+ version_match = /\d+\.\d+\.\d+/
6
+ assert_match version_match, Nokogiri::VERSION
7
+ assert_match version_match, Nokogiri::LIBXML_VERSION
8
+
9
+ if defined?(FFI)
10
+ assert_equal 'ffi', Nokogiri::VERSION_INFO['libxml']['binding']
11
+ if RUBY_PLATFORM =~ /java/
12
+ assert_equal 'jruby', Nokogiri::VERSION_INFO['libxml']['platform']
13
+ else
14
+ assert_equal 'ruby', Nokogiri::VERSION_INFO['libxml']['platform']
15
+ end
16
+ assert Nokogiri.ffi?
17
+ else
18
+ assert_equal 'extension', Nokogiri::VERSION_INFO['libxml']['binding']
19
+
20
+ assert_match version_match, Nokogiri::VERSION_INFO['libxml']['compiled']
21
+ assert_equal Nokogiri::LIBXML_VERSION, Nokogiri::VERSION_INFO['libxml']['compiled']
22
+ assert ! Nokogiri.ffi?
23
+ end
24
+
25
+ assert_match version_match, Nokogiri::VERSION_INFO['libxml']['loaded']
5
26
  Nokogiri::LIBXML_PARSER_VERSION =~ /(\d)(\d{2})(\d{2})/
6
27
  major = $1.to_i
7
28
  minor = $2.to_i
8
29
  bug = $3.to_i
9
- assert_equal "#{major}.#{minor}.#{bug}", Nokogiri::LIBXML_VERSION
30
+ assert_equal "#{major}.#{minor}.#{bug}", Nokogiri::VERSION_INFO['libxml']['loaded']
10
31
  end
11
32
 
12
33
  def test_xml?
@@ -102,4 +123,5 @@ class TestNokogiri < Nokogiri::TestCase
102
123
  doc.slop!
103
124
  assert_equal 1, doc.decorators(Nokogiri::XML::Node).select { |d| d == Nokogiri::Decorators::Slop }.size
104
125
  end
126
+
105
127
  end
@@ -2,6 +2,83 @@
2
2
  require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
3
3
 
4
4
  class TestReader < Nokogiri::TestCase
5
+ def test_from_io_sets_io_as_source
6
+ io = StringIO.new(<<-eoxml)
7
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
8
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
9
+ </x>
10
+ eoxml
11
+ reader = Nokogiri::XML::Reader.from_io(io)
12
+ assert_equal io, reader.source
13
+ end
14
+
15
+ def test_reader_takes_block
16
+ options = nil
17
+ Nokogiri::XML::Reader(File.read(XML_FILE), XML_FILE) do |cfg|
18
+ options = cfg
19
+ options.nonet.nowarning.dtdattr
20
+ end
21
+ assert options.nonet?
22
+ assert options.nowarning?
23
+ assert options.dtdattr?
24
+ end
25
+
26
+ def test_nil_raises
27
+ assert_raises(ArgumentError) {
28
+ Nokogiri::XML::Reader.from_memory(nil)
29
+ }
30
+ assert_raises(ArgumentError) {
31
+ Nokogiri::XML::Reader.from_io(nil)
32
+ }
33
+ end
34
+
35
+ def test_from_io
36
+ io = StringIO.new(<<-eoxml)
37
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
38
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
39
+ </x>
40
+ eoxml
41
+ reader = Nokogiri::XML::Reader.from_io(io)
42
+ assert_equal false, reader.default?
43
+ assert_equal [false, false, false, false, false, false, false],
44
+ reader.map { |x| x.default? }
45
+ end
46
+
47
+ def test_io
48
+ io = StringIO.new(<<-eoxml)
49
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
50
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
51
+ </x>
52
+ eoxml
53
+ reader = Nokogiri::XML::Reader(io)
54
+ assert_equal false, reader.default?
55
+ assert_equal [false, false, false, false, false, false, false],
56
+ reader.map { |x| x.default? }
57
+ end
58
+
59
+ def test_in_memory
60
+ reader = Nokogiri::XML::Reader(<<-eoxml)
61
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
62
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
63
+ </x>
64
+ eoxml
65
+ end
66
+
67
+ def test_reader_holds_on_to_string
68
+ xml = <<-eoxml
69
+ <x xmlns:tenderlove='http://tenderlovemaking.com/'>
70
+ <tenderlove:foo awesome='true'>snuggles!</tenderlove:foo>
71
+ </x>
72
+ eoxml
73
+ reader = Nokogiri::XML::Reader(xml)
74
+ if Nokogiri::VERSION_INFO['libxml']['binding'] == 'ffi'
75
+ assert_not_nil reader.source
76
+ assert reader.source.is_a?(FFI::MemoryPointer)
77
+ else
78
+ assert_equal xml, reader.source
79
+ end
80
+ end
81
+
5
82
  def test_default?
6
83
  reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
7
84
  <x xmlns:tenderlove='http://tenderlovemaking.com/'>