nokogiri 1.3.3 → 1.4.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 (201) hide show
  1. data/CHANGELOG.ja.rdoc +48 -3
  2. data/CHANGELOG.rdoc +42 -0
  3. data/Manifest.txt +44 -29
  4. data/README.ja.rdoc +0 -2
  5. data/README.rdoc +4 -7
  6. data/Rakefile +42 -6
  7. data/bin/nokogiri +7 -5
  8. data/ext/nokogiri/extconf.rb +5 -21
  9. data/ext/nokogiri/html_document.c +14 -50
  10. data/ext/nokogiri/html_element_description.c +7 -7
  11. data/ext/nokogiri/html_entity_lookup.c +6 -4
  12. data/ext/nokogiri/html_sax_parser_context.c +92 -0
  13. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  14. data/ext/nokogiri/nokogiri.c +9 -3
  15. data/ext/nokogiri/nokogiri.h +16 -20
  16. data/ext/nokogiri/xml_attr.c +1 -1
  17. data/ext/nokogiri/xml_attribute_decl.c +67 -0
  18. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  19. data/ext/nokogiri/xml_cdata.c +6 -5
  20. data/ext/nokogiri/xml_comment.c +3 -2
  21. data/ext/nokogiri/xml_document.c +93 -23
  22. data/ext/nokogiri/xml_document_fragment.c +1 -3
  23. data/ext/nokogiri/xml_dtd.c +63 -6
  24. data/ext/nokogiri/xml_element_content.c +123 -0
  25. data/ext/nokogiri/xml_element_content.h +10 -0
  26. data/ext/nokogiri/xml_element_decl.c +69 -0
  27. data/ext/nokogiri/xml_element_decl.h +9 -0
  28. data/ext/nokogiri/xml_entity_decl.c +97 -0
  29. data/ext/nokogiri/xml_entity_decl.h +10 -0
  30. data/ext/nokogiri/xml_entity_reference.c +1 -1
  31. data/ext/nokogiri/xml_io.c +10 -3
  32. data/ext/nokogiri/xml_io.h +1 -0
  33. data/ext/nokogiri/xml_namespace.c +2 -2
  34. data/ext/nokogiri/xml_node.c +139 -34
  35. data/ext/nokogiri/xml_node.h +0 -1
  36. data/ext/nokogiri/xml_node_set.c +23 -16
  37. data/ext/nokogiri/xml_processing_instruction.c +1 -1
  38. data/ext/nokogiri/xml_reader.c +78 -50
  39. data/ext/nokogiri/xml_sax_parser.c +109 -168
  40. data/ext/nokogiri/xml_sax_parser.h +33 -0
  41. data/ext/nokogiri/xml_sax_parser_context.c +155 -0
  42. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  43. data/ext/nokogiri/xml_sax_push_parser.c +11 -6
  44. data/ext/nokogiri/xml_syntax_error.c +63 -12
  45. data/ext/nokogiri/xml_text.c +4 -3
  46. data/ext/nokogiri/xml_xpath.c +1 -1
  47. data/ext/nokogiri/xml_xpath_context.c +12 -25
  48. data/ext/nokogiri/xslt_stylesheet.c +3 -3
  49. data/lib/nokogiri.rb +4 -4
  50. data/lib/nokogiri/css/generated_tokenizer.rb +1 -0
  51. data/lib/nokogiri/css/node.rb +1 -9
  52. data/lib/nokogiri/css/xpath_visitor.rb +11 -21
  53. data/lib/nokogiri/ffi/html/document.rb +0 -9
  54. data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
  55. data/lib/nokogiri/ffi/io_callbacks.rb +4 -2
  56. data/lib/nokogiri/ffi/libxml.rb +44 -10
  57. data/lib/nokogiri/ffi/structs/common_node.rb +1 -1
  58. data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
  59. data/lib/nokogiri/ffi/structs/xml_dtd.rb +3 -1
  60. data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
  61. data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
  62. data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
  63. data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
  64. data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
  65. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +4 -3
  66. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +1 -1
  67. data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
  68. data/lib/nokogiri/ffi/xml/comment.rb +2 -2
  69. data/lib/nokogiri/ffi/xml/document.rb +29 -12
  70. data/lib/nokogiri/ffi/xml/document_fragment.rb +0 -5
  71. data/lib/nokogiri/ffi/xml/dtd.rb +14 -3
  72. data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
  73. data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
  74. data/lib/nokogiri/ffi/xml/entity_decl.rb +27 -0
  75. data/lib/nokogiri/ffi/xml/node.rb +45 -5
  76. data/lib/nokogiri/ffi/xml/node_set.rb +1 -1
  77. data/lib/nokogiri/ffi/xml/reader.rb +45 -24
  78. data/lib/nokogiri/ffi/xml/sax/parser.rb +27 -34
  79. data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
  80. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +5 -4
  81. data/lib/nokogiri/ffi/xml/syntax_error.rb +31 -16
  82. data/lib/nokogiri/ffi/xml/text.rb +2 -2
  83. data/lib/nokogiri/html.rb +1 -0
  84. data/lib/nokogiri/html/document.rb +39 -24
  85. data/lib/nokogiri/html/sax/parser.rb +2 -2
  86. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  87. data/lib/nokogiri/version.rb +1 -1
  88. data/lib/nokogiri/xml.rb +6 -1
  89. data/lib/nokogiri/xml/attr.rb +5 -0
  90. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  91. data/lib/nokogiri/xml/builder.rb +121 -13
  92. data/lib/nokogiri/xml/character_data.rb +7 -0
  93. data/lib/nokogiri/xml/document.rb +43 -29
  94. data/lib/nokogiri/xml/document_fragment.rb +26 -6
  95. data/lib/nokogiri/xml/dtd.rb +5 -5
  96. data/lib/nokogiri/xml/element_content.rb +36 -0
  97. data/lib/nokogiri/xml/element_decl.rb +13 -0
  98. data/lib/nokogiri/xml/entity_decl.rb +15 -0
  99. data/lib/nokogiri/xml/fragment_handler.rb +22 -11
  100. data/lib/nokogiri/xml/namespace.rb +6 -0
  101. data/lib/nokogiri/xml/node.rb +33 -15
  102. data/lib/nokogiri/xml/node_set.rb +66 -44
  103. data/lib/nokogiri/xml/pp.rb +2 -0
  104. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  105. data/lib/nokogiri/xml/pp/node.rb +56 -0
  106. data/lib/nokogiri/xml/reader.rb +8 -0
  107. data/lib/nokogiri/xml/sax.rb +1 -1
  108. data/lib/nokogiri/xml/sax/document.rb +18 -1
  109. data/lib/nokogiri/xml/sax/parser.rb +15 -8
  110. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  111. data/lib/nokogiri/xml/sax/push_parser.rb +0 -3
  112. data/lib/nokogiri/xml/syntax_error.rb +4 -0
  113. data/lib/nokogiri/xslt/stylesheet.rb +1 -1
  114. data/test/css/test_nthiness.rb +1 -1
  115. data/test/css/test_parser.rb +1 -1
  116. data/test/css/test_tokenizer.rb +1 -1
  117. data/test/css/test_xpath_visitor.rb +1 -1
  118. data/test/ffi/test_document.rb +1 -1
  119. data/test/files/shift_jis.html +10 -0
  120. data/test/files/staff.dtd +10 -0
  121. data/test/helper.rb +12 -3
  122. data/test/html/sax/test_parser.rb +1 -1
  123. data/test/html/sax/test_parser_context.rb +48 -0
  124. data/test/html/test_builder.rb +8 -2
  125. data/test/html/test_document.rb +23 -1
  126. data/test/html/test_document_encoding.rb +15 -1
  127. data/test/html/test_document_fragment.rb +10 -1
  128. data/test/html/test_element_description.rb +1 -2
  129. data/test/html/test_named_characters.rb +1 -1
  130. data/test/html/test_node.rb +61 -1
  131. data/test/html/test_node_encoding.rb +27 -0
  132. data/test/test_convert_xpath.rb +1 -3
  133. data/test/test_css_cache.rb +1 -1
  134. data/test/test_gc.rb +1 -1
  135. data/test/test_memory_leak.rb +1 -1
  136. data/test/test_nokogiri.rb +3 -3
  137. data/test/test_reader.rb +29 -1
  138. data/test/test_xslt_transforms.rb +1 -1
  139. data/test/xml/node/test_save_options.rb +1 -1
  140. data/test/xml/node/test_subclass.rb +1 -1
  141. data/test/xml/sax/test_parser.rb +64 -3
  142. data/test/xml/sax/test_parser_context.rb +56 -0
  143. data/test/xml/sax/test_push_parser.rb +11 -1
  144. data/test/xml/test_attr.rb +1 -1
  145. data/test/xml/test_attribute_decl.rb +82 -0
  146. data/test/xml/test_builder.rb +95 -1
  147. data/test/xml/test_cdata.rb +1 -1
  148. data/test/xml/test_comment.rb +7 -1
  149. data/test/xml/test_document.rb +147 -6
  150. data/test/xml/test_document_encoding.rb +1 -1
  151. data/test/xml/test_document_fragment.rb +55 -5
  152. data/test/xml/test_dtd.rb +40 -5
  153. data/test/xml/test_dtd_encoding.rb +3 -1
  154. data/test/xml/test_element_content.rb +56 -0
  155. data/test/xml/test_element_decl.rb +73 -0
  156. data/test/xml/test_entity_decl.rb +83 -0
  157. data/test/xml/test_entity_reference.rb +1 -1
  158. data/test/xml/test_namespace.rb +21 -1
  159. data/test/xml/test_node.rb +70 -4
  160. data/test/xml/test_node_attributes.rb +1 -1
  161. data/test/xml/test_node_encoding.rb +1 -1
  162. data/test/xml/test_node_set.rb +136 -2
  163. data/test/xml/test_parse_options.rb +1 -1
  164. data/test/xml/test_processing_instruction.rb +1 -1
  165. data/test/xml/test_reader_encoding.rb +1 -1
  166. data/test/xml/test_relax_ng.rb +1 -1
  167. data/test/xml/test_schema.rb +1 -1
  168. data/test/xml/test_syntax_error.rb +27 -0
  169. data/test/xml/test_text.rb +13 -1
  170. data/test/xml/test_unparented_node.rb +1 -1
  171. data/test/xml/test_xpath.rb +1 -1
  172. metadata +57 -40
  173. data/ext/nokogiri/html_sax_parser.c +0 -57
  174. data/ext/nokogiri/html_sax_parser.h +0 -11
  175. data/lib/action-nokogiri.rb +0 -38
  176. data/lib/nokogiri/decorators.rb +0 -2
  177. data/lib/nokogiri/decorators/hpricot.rb +0 -3
  178. data/lib/nokogiri/decorators/hpricot/node.rb +0 -56
  179. data/lib/nokogiri/decorators/hpricot/node_set.rb +0 -54
  180. data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +0 -30
  181. data/lib/nokogiri/ffi/html/sax/parser.rb +0 -21
  182. data/lib/nokogiri/hpricot.rb +0 -92
  183. data/lib/nokogiri/xml/entity_declaration.rb +0 -11
  184. data/lib/nokogiri/xml/sax/legacy_handlers.rb +0 -65
  185. data/test/hpricot/files/basic.xhtml +0 -17
  186. data/test/hpricot/files/boingboing.html +0 -2266
  187. data/test/hpricot/files/cy0.html +0 -3653
  188. data/test/hpricot/files/immob.html +0 -400
  189. data/test/hpricot/files/pace_application.html +0 -1320
  190. data/test/hpricot/files/tenderlove.html +0 -16
  191. data/test/hpricot/files/uswebgen.html +0 -220
  192. data/test/hpricot/files/utf8.html +0 -1054
  193. data/test/hpricot/files/week9.html +0 -1723
  194. data/test/hpricot/files/why.xml +0 -19
  195. data/test/hpricot/load_files.rb +0 -11
  196. data/test/hpricot/test_alter.rb +0 -68
  197. data/test/hpricot/test_builder.rb +0 -20
  198. data/test/hpricot/test_parser.rb +0 -350
  199. data/test/hpricot/test_paths.rb +0 -15
  200. data/test/hpricot/test_preserved.rb +0 -77
  201. data/test/hpricot/test_xml.rb +0 -30
@@ -1,10 +1,24 @@
1
1
  # -*- coding: utf-8 -*-
2
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+ require "helper"
3
3
 
4
4
  module Nokogiri
5
5
  module HTML
6
6
  if RUBY_VERSION =~ /^1\.9/
7
7
  class TestDocumentEncoding < Nokogiri::TestCase
8
+ def test_encoding
9
+ doc = Nokogiri::HTML File.open(SHIFT_JIS_HTML, 'rb')
10
+
11
+ hello = "こんにちは"
12
+
13
+ assert_match doc.encoding, doc.to_html
14
+ assert_match hello.encode('Shift_JIS'), doc.to_html
15
+ assert_equal 'Shift_JIS', doc.to_html.encoding.name
16
+
17
+ assert_match hello, doc.to_html(:encoding => 'UTF-8')
18
+ assert_match 'UTF-8', doc.to_html(:encoding => 'UTF-8')
19
+ assert_match 'UTF-8', doc.to_html(:encoding => 'UTF-8').encoding.name
20
+ end
21
+
8
22
  def test_default_to_encoding_from_string
9
23
  bad_charset = <<-eohtml
10
24
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@@ -1,4 +1,5 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
1
+ # -*- coding: utf-8 -*-
2
+ require "helper"
2
3
 
3
4
  module Nokogiri
4
5
  module HTML
@@ -8,6 +9,14 @@ module Nokogiri
8
9
  @html = Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE)
9
10
  end
10
11
 
12
+ def test_fun_encoding
13
+ string = %Q(<body>こんにちは</body>)
14
+ html = Nokogiri::HTML::DocumentFragment.parse(
15
+ string
16
+ ).to_html(:encoding => 'UTF-8')
17
+ assert_equal string, html
18
+ end
19
+
11
20
  def test_new
12
21
  fragment = Nokogiri::HTML::DocumentFragment.new(@html)
13
22
  end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
1
+ require "helper"
2
2
 
3
3
  module Nokogiri
4
4
  module HTML
@@ -66,7 +66,6 @@ module Nokogiri
66
66
  def test_optional_attributes
67
67
  attrs = ElementDescription['table'].optional_attributes
68
68
  assert attrs
69
- assert_equal 22, attrs.length
70
69
  end
71
70
 
72
71
  def test_deprecated_attributes
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
1
+ require "helper"
2
2
 
3
3
  module Nokogiri
4
4
  module HTML
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
1
+ require "helper"
2
2
 
3
3
  require 'nkf'
4
4
 
@@ -17,6 +17,43 @@ module Nokogiri
17
17
  eohtml
18
18
  end
19
19
 
20
+ def test_get_attribute
21
+ element = @html.at('div')
22
+ assert_equal 'baz', element.get_attribute('class')
23
+ assert_equal 'baz', element['class']
24
+ element['href'] = "javascript:alert(\"AGGA-KA-BOO!\")"
25
+ assert_match(/%22AGGA-KA-BOO!%22/, element.to_html)
26
+ end
27
+
28
+ def test_css_path_round_trip
29
+ doc = Nokogiri::HTML(File.read(HTML_FILE))
30
+ %w{ #header small div[2] div.post body }.each do |css_sel|
31
+ ele = doc.at css_sel
32
+ assert_equal ele, doc.at(ele.css_path), ele.css_path
33
+ end
34
+ end
35
+
36
+ def test_path_round_trip
37
+ doc = Nokogiri::HTML(File.read(HTML_FILE))
38
+ %w{ #header small div[2] div.post body }.each do |css_sel|
39
+ ele = doc.at css_sel
40
+ assert_equal ele, doc.at(ele.path), ele.path
41
+ end
42
+ end
43
+
44
+ def test_append_with_document
45
+ assert_raises(ArgumentError) do
46
+ @html.root << Nokogiri::HTML::Document.new
47
+ end
48
+ end
49
+
50
+ ###
51
+ # Make sure a document that doesn't declare a meta encoding returns
52
+ # nil.
53
+ def test_meta_encoding
54
+ assert_nil @html.meta_encoding
55
+ end
56
+
20
57
  def test_description
21
58
  assert desc = @html.at('a.bar').description
22
59
  assert_equal 'a', desc.name
@@ -51,6 +88,19 @@ module Nokogiri
51
88
  assert node.matches?('//a')
52
89
  end
53
90
 
91
+ def test_unlink_then_swap
92
+ node = @html.at('a')
93
+ node.unlink
94
+
95
+ another_node = @html.at('div')
96
+ assert another_node, 'should have a node'
97
+
98
+ # This used to segv
99
+ assert_nothing_raised do
100
+ node.add_previous_sibling another_node
101
+ end
102
+ end
103
+
54
104
  def test_swap
55
105
  @html.at('div').swap('<a href="foo">bar</a>')
56
106
  a_tag = @html.css('a').first
@@ -79,6 +129,11 @@ module Nokogiri
79
129
  assert_equal 'some text', @html.at('//body').children[0].content.strip
80
130
  end
81
131
 
132
+ def test_before
133
+ @html.at('//div').before('<a href="awesome">town</a>')
134
+ assert_equal 'awesome', @html.at('//div').previous['href']
135
+ end
136
+
82
137
  def test_fragment_handler_does_not_regurge_on_invalid_attributes
83
138
  iframe = %Q{<iframe style="width: 0%; height: 0px" src="http://someurl" allowtransparency></iframe>}
84
139
  assert_nothing_raised { @html.at('div').before(iframe) }
@@ -128,6 +183,11 @@ module Nokogiri
128
183
  assert_equal 'some text', node.next.text.strip
129
184
  end
130
185
 
186
+ def test_after
187
+ @html.at('//div').after('<a href="awesome">town</a>')
188
+ assert_equal 'awesome', @html.at('//div').next['href']
189
+ end
190
+
131
191
  def test_replace
132
192
  doc = Nokogiri::HTML(<<-eohtml)
133
193
  <html>
@@ -0,0 +1,27 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "helper"
3
+
4
+ module Nokogiri
5
+ module HTML
6
+ if RUBY_VERSION =~ /^1\.9/
7
+ class TestNodeEncoding < Nokogiri::TestCase
8
+ def test_inner_html
9
+ doc = Nokogiri::HTML File.open(SHIFT_JIS_HTML, 'rb')
10
+
11
+ hello = "こんにちは"
12
+
13
+ contents = doc.at('h2').inner_html
14
+ assert_equal doc.encoding, contents.encoding.name
15
+ assert_match hello.encode('Shift_JIS'), contents
16
+
17
+ contents = doc.at('h2').inner_html(:encoding => 'UTF-8')
18
+ assert_match hello, contents
19
+
20
+ doc.encoding = 'UTF-8'
21
+ contents = doc.at('h2').inner_html
22
+ assert_match hello, contents
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,6 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
-
3
- require 'rubygems'
1
+ require "helper"
4
2
 
5
3
  class TestConvertXPath < Nokogiri::TestCase
6
4
 
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
1
+ require "helper"
2
2
 
3
3
  class TestCssCache < Nokogiri::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
1
+ require "helper"
2
2
 
3
3
  class TestGc < Nokogiri::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
1
+ require "helper"
2
2
 
3
3
  class TestMemoryLeak < Nokogiri::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
1
+ require "helper"
2
2
 
3
3
  class TestNokogiri < Nokogiri::TestCase
4
4
  def test_versions
@@ -69,7 +69,7 @@ class TestNokogiri < Nokogiri::TestCase
69
69
  doc = Nokogiri.make { b "bold tag" }
70
70
  assert_equal('<b>bold tag</b>', doc.to_html.chomp)
71
71
  end
72
-
72
+
73
73
  SLOP_HTML = <<-END
74
74
  <html>
75
75
  <body>
@@ -119,7 +119,7 @@ class TestNokogiri < Nokogiri::TestCase
119
119
 
120
120
  assert_raise(NoMethodError) { doc.nonexistent }
121
121
  end
122
-
122
+
123
123
  def test_slop_decorator
124
124
  doc = Nokogiri(SLOP_HTML)
125
125
  assert !doc.decorators(Nokogiri::XML::Node).include?(Nokogiri::Decorators::Slop)
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
- require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
2
+ require "helper"
3
3
 
4
4
  class TestReader < Nokogiri::TestCase
5
5
  def test_from_io_sets_io_as_source
@@ -260,6 +260,34 @@ class TestReader < Nokogiri::TestCase
260
260
  reader.map { |n| n.prefix }
261
261
  end
262
262
 
263
+ def test_node_type
264
+ reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
265
+ <x>
266
+ <y>hello</y>
267
+ </x>
268
+ eoxml
269
+ assert_equal 0, reader.node_type
270
+ assert_equal [1, 14, 1, 3, 15, 14, 15], reader.map { |n| n.node_type }
271
+ end
272
+
273
+ def test_inner_xml
274
+ str = "<x><y>hello</y></x>"
275
+ reader = Nokogiri::XML::Reader.from_memory(str)
276
+
277
+ reader.read
278
+
279
+ assert_equal "<y>hello</y>", reader.inner_xml
280
+ end
281
+
282
+ def test_outer_xml
283
+ str = "<x><y>hello</y></x>"
284
+ reader = Nokogiri::XML::Reader.from_memory(str)
285
+
286
+ reader.read
287
+
288
+ assert_equal str, reader.outer_xml
289
+ end
290
+
263
291
  def test_state
264
292
  reader = Nokogiri::XML::Reader.from_memory('<foo>bar</bar>')
265
293
  assert reader.state
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "helper"))
1
+ require "helper"
2
2
 
3
3
  class TestXsltTransforms < Nokogiri::TestCase
4
4
 
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..',"helper"))
1
+ require "helper"
2
2
 
3
3
  module Nokogiri
4
4
  module XML
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..',"helper"))
1
+ require "helper"
2
2
 
3
3
  module Nokogiri
4
4
  module XML
@@ -1,4 +1,6 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', "helper"))
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "helper"
2
4
 
3
5
  module Nokogiri
4
6
  module XML
@@ -9,6 +11,61 @@ module Nokogiri
9
11
  @parser = XML::SAX::Parser.new(Doc.new)
10
12
  end
11
13
 
14
+ def test_parser_context_yielded_io
15
+ doc = Doc.new
16
+ parser = XML::SAX::Parser.new doc
17
+ xml = "<foo a='&amp;b'/>"
18
+
19
+ block_called = false
20
+ parser.parse(StringIO.new(xml)) { |ctx|
21
+ block_called = true
22
+ ctx.replace_entities = true
23
+ }
24
+
25
+ assert block_called
26
+
27
+ assert_equal ['a', '&b'], doc.start_elements.first.last
28
+ end
29
+
30
+ def test_parser_context_yielded_in_memory
31
+ doc = Doc.new
32
+ parser = XML::SAX::Parser.new doc
33
+ xml = "<foo a='&amp;b'/>"
34
+
35
+ block_called = false
36
+ parser.parse(xml) { |ctx|
37
+ block_called = true
38
+ ctx.replace_entities = true
39
+ }
40
+
41
+ assert block_called
42
+
43
+ assert_equal ['a', '&b'], doc.start_elements.first.last
44
+ end
45
+
46
+ def test_xml_decl
47
+ {
48
+ '' => nil,
49
+ '<?xml version="1.0" ?>' => ['1.0'],
50
+ '<?xml version="1.0" encoding="UTF-8" ?>' => ['1.0', 'UTF-8'],
51
+ '<?xml version="1.0" standalone="yes"?>' => ['1.0', 'yes'],
52
+ '<?xml version="1.0" standalone="no"?>' => ['1.0', 'no'],
53
+ }.each do |decl,value|
54
+ parser = XML::SAX::Parser.new(Doc.new)
55
+
56
+ xml = "#{decl}\n<root />"
57
+ parser.parse xml
58
+ assert parser.document.start_document_called, xml
59
+ assert_equal value, parser.document.xmldecls, xml
60
+ end
61
+ end
62
+
63
+ def test_parse_empty
64
+ assert_raises RuntimeError do
65
+ @parser.parse('')
66
+ end
67
+ end
68
+
12
69
  def test_namespace_declaration_order_is_saved
13
70
  @parser.parse <<-eoxml
14
71
  <root xmlns:foo='http://foo.example.com/' xmlns='http://example.com/'>
@@ -114,11 +171,15 @@ module Nokogiri
114
171
  assert_equal doc.errors.length, @parser.document.errors.length
115
172
  end
116
173
 
117
- def test_parse
174
+ def test_parse_with_memory_argument
175
+ @parser.parse(File.read(XML_FILE))
176
+ assert(@parser.document.cdata_blocks.length > 0)
177
+ end
178
+
179
+ def test_parse_with_io_argument
118
180
  File.open(XML_FILE, 'rb') { |f|
119
181
  @parser.parse(f)
120
182
  }
121
- @parser.parse(File.read(XML_FILE))
122
183
  assert(@parser.document.cdata_blocks.length > 0)
123
184
  end
124
185
 
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "helper"
4
+
5
+ module Nokogiri
6
+ module XML
7
+ module SAX
8
+ class TestParserContext < Nokogiri::SAX::TestCase
9
+ def test_replace_entities
10
+ pc = ParserContext.new StringIO.new('<root />'), 'UTF-8'
11
+ pc.replace_entities = false
12
+ assert_equal false, pc.replace_entities
13
+
14
+ pc.replace_entities = true
15
+ assert_equal true, pc.replace_entities
16
+ end
17
+
18
+ def test_from_io
19
+ assert_nothing_raised do
20
+ ParserContext.new StringIO.new('fo'), 'UTF-8'
21
+ end
22
+ end
23
+
24
+ def test_from_string
25
+ assert_nothing_raised do
26
+ ParserContext.new 'blah blah'
27
+ end
28
+ end
29
+
30
+ def test_parse_with
31
+ ctx = ParserContext.new 'blah'
32
+ assert_raises ArgumentError do
33
+ ctx.parse_with nil
34
+ end
35
+ end
36
+
37
+ def test_parse_with_sax_parser
38
+ assert_nothing_raised do
39
+ xml = "<root />"
40
+ ctx = ParserContext.new xml
41
+ parser = Parser.new Doc.new
42
+ ctx.parse_with parser
43
+ end
44
+ end
45
+
46
+ def test_from_file
47
+ assert_nothing_raised do
48
+ ctx = ParserContext.file XML_FILE
49
+ parser = Parser.new Doc.new
50
+ ctx.parse_with parser
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end