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,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
@@ -9,6 +9,16 @@ module Nokogiri
9
9
  @parser = XML::SAX::PushParser.new(Doc.new)
10
10
  end
11
11
 
12
+ def test_exception
13
+ assert_raises(SyntaxError) do
14
+ @parser << "<foo /><foo />"
15
+ end
16
+
17
+ assert_raises(SyntaxError) do
18
+ @parser << nil
19
+ end
20
+ end
21
+
12
22
  def test_end_document_called
13
23
  @parser.<<(<<-eoxml)
14
24
  <p id="asdfasdf">
@@ -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
@@ -0,0 +1,82 @@
1
+ require "helper"
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestAttributeDecl < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @xml = Nokogiri::XML(<<-eoxml)
9
+ <?xml version="1.0"?><?TEST-STYLE PIDATA?>
10
+ <!DOCTYPE staff SYSTEM "staff.dtd" [
11
+ <!ATTLIST br width CDATA "0">
12
+ <!ATTLIST a width CDATA >
13
+ <!ATTLIST payment type (check|cash) "cash">
14
+ ]>
15
+ <root />
16
+ eoxml
17
+ @attrs = @xml.internal_subset.children
18
+ @attr_decl = @attrs.first
19
+ end
20
+
21
+ def test_inspect
22
+ assert_equal(
23
+ "#<#{@attr_decl.class.name}:#{sprintf("0x%x", @attr_decl.object_id)} #{@attr_decl.to_s.inspect}>",
24
+ @attr_decl.inspect
25
+ )
26
+ end
27
+
28
+ def test_type
29
+ assert_equal 16, @attr_decl.type
30
+ end
31
+
32
+ def test_class
33
+ assert_instance_of Nokogiri::XML::AttributeDecl, @attr_decl
34
+ end
35
+
36
+ def test_content
37
+ assert_raise NoMethodError do
38
+ @attr_decl.content
39
+ end
40
+ end
41
+
42
+ def test_attributes
43
+ assert_raise NoMethodError do
44
+ @attr_decl.attributes
45
+ end
46
+ end
47
+
48
+ def test_namespace
49
+ assert_raise NoMethodError do
50
+ @attr_decl.namespace
51
+ end
52
+ end
53
+
54
+ def test_namespace_definitions
55
+ assert_raise NoMethodError do
56
+ @attr_decl.namespace_definitions
57
+ end
58
+ end
59
+
60
+ def test_line
61
+ assert_raise NoMethodError do
62
+ @attr_decl.line
63
+ end
64
+ end
65
+
66
+ def test_attribute_type
67
+ assert_equal 1, @attr_decl.attribute_type
68
+ end
69
+
70
+ def test_default
71
+ assert_equal '0', @attr_decl.default
72
+ assert_nil @attrs[1].default
73
+ end
74
+
75
+ def test_enumeration
76
+ assert_equal [], @attr_decl.enumeration
77
+ assert_equal ['check', 'cash'], @attrs[2].enumeration
78
+ end
79
+ end
80
+ end
81
+ end
82
+
@@ -1,8 +1,82 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
1
+ require "helper"
2
2
 
3
3
  module Nokogiri
4
4
  module XML
5
5
  class TestBuilder < Nokogiri::TestCase
6
+ def test_with_root
7
+ doc = Nokogiri::XML(File.read(XML_FILE))
8
+ Nokogiri::XML::Builder.with(doc.at('employee')) do |xml|
9
+ xml.foo
10
+ end
11
+ assert_equal 1, doc.xpath('//employee/foo').length
12
+ end
13
+
14
+ def test_root_namespace_default_decl
15
+ b = Nokogiri::XML::Builder.new { |xml| xml.root(:xmlns => 'one:two') }
16
+ doc = b.doc
17
+ assert_equal 'one:two', doc.root.namespace.href
18
+ assert_equal({ 'xmlns' => 'one:two' }, doc.root.namespaces)
19
+ end
20
+
21
+ def test_root_namespace_multi_decl
22
+ b = Nokogiri::XML::Builder.new { |xml|
23
+ xml.root(:xmlns => 'one:two', 'xmlns:foo' => 'bar') do
24
+ xml.hello
25
+ end
26
+ }
27
+ doc = b.doc
28
+ assert_equal 'one:two', doc.root.namespace.href
29
+ assert_equal({ 'xmlns' => 'one:two', 'xmlns:foo' => 'bar' }, doc.root.namespaces)
30
+
31
+ assert_equal 'one:two', doc.at('hello').namespace.href
32
+ end
33
+
34
+ def test_non_root_namespace
35
+ b = Nokogiri::XML::Builder.new { |xml|
36
+ xml.root { xml.hello(:xmlns => 'one') }
37
+ }
38
+ assert_equal 'one', b.doc.at('hello', 'xmlns' => 'one').namespace.href
39
+ end
40
+
41
+ def test_specify_namespace
42
+ b = Nokogiri::XML::Builder.new { |xml|
43
+ xml.root('xmlns:foo' => 'bar') do
44
+ xml[:foo].bar
45
+ xml['foo'].baz
46
+ end
47
+ }
48
+ doc = b.doc
49
+ assert_equal 'bar', b.doc.at('foo|bar', 'foo' => 'bar').namespace.href
50
+ assert_equal 'bar', b.doc.at('foo|baz', 'foo' => 'bar').namespace.href
51
+ end
52
+
53
+ def test_specify_namespace_nested
54
+ b = Nokogiri::XML::Builder.new { |xml|
55
+ xml.root('xmlns:foo' => 'bar') do
56
+ xml.yay do
57
+ xml[:foo].bar
58
+
59
+ xml.yikes do
60
+ xml['foo'].baz
61
+ end
62
+ end
63
+ end
64
+ }
65
+ doc = b.doc
66
+ assert_equal 'bar', b.doc.at('foo|bar', 'foo' => 'bar').namespace.href
67
+ assert_equal 'bar', b.doc.at('foo|baz', 'foo' => 'bar').namespace.href
68
+ end
69
+
70
+ def test_specified_namespace_undeclared
71
+ b = Nokogiri::XML::Builder.new { |xml|
72
+ xml.root do
73
+ assert_raises(ArgumentError) do
74
+ xml[:foo]
75
+ end
76
+ end
77
+ }
78
+ end
79
+
6
80
  def test_set_encoding
7
81
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
8
82
  xml.root do
@@ -51,6 +125,26 @@ module Nokogiri
51
125
  assert_equal 'hello', builder.doc.at('baz').content
52
126
  end
53
127
 
128
+ def test_raw_append
129
+ builder = Nokogiri::XML::Builder.new do |xml|
130
+ xml.root do
131
+ xml << 'hello'
132
+ end
133
+ end
134
+
135
+ assert_equal 'hello', builder.doc.at('/root').content
136
+ end
137
+
138
+ def test_raw_append_with_instance_eval
139
+ builder = Nokogiri::XML::Builder.new do
140
+ root do
141
+ self << 'hello'
142
+ end
143
+ end
144
+
145
+ assert_equal 'hello', builder.doc.at('/root').content
146
+ end
147
+
54
148
  def test_cdata
55
149
  builder = Nokogiri::XML::Builder.new do
56
150
  root {
@@ -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
@@ -13,6 +13,12 @@ module Nokogiri
13
13
  assert_equal('<!--hello world-->', comment.to_s)
14
14
  end
15
15
 
16
+ def test_comment?
17
+ comment = Nokogiri::XML::Comment.new(@xml, 'hello world')
18
+ assert(comment.comment?)
19
+ assert(!@xml.root.comment?)
20
+ end
21
+
16
22
  def test_many_comments
17
23
  100.times {
18
24
  Nokogiri::XML::Comment.new(@xml, 'hello world')
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
1
+ require "helper"
2
2
 
3
3
  require 'uri'
4
4
 
@@ -10,11 +10,126 @@ module Nokogiri
10
10
  @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
11
11
  end
12
12
 
13
+ def test_subclass_initialize_modify # testing a segv
14
+ Class.new(Nokogiri::XML::Document) {
15
+ def initialize
16
+ super
17
+ body_node = Nokogiri::XML::Node.new "body", self
18
+ body_node.content = "stuff"
19
+ self.root = body_node
20
+ end
21
+ }.new
22
+ end
23
+
24
+ def test_create_text_node
25
+ txt = @xml.create_text_node 'foo'
26
+ assert_instance_of Nokogiri::XML::Text, txt
27
+ assert_equal 'foo', txt.text
28
+ assert_equal @xml, txt.document
29
+ end
30
+
31
+ def test_create_element
32
+ elm = @xml.create_element('foo')
33
+ assert_instance_of Nokogiri::XML::Element, elm
34
+ assert_equal 'foo', elm.name
35
+ assert_equal @xml, elm.document
36
+ end
37
+
38
+ def test_pp
39
+ out = StringIO.new('')
40
+ assert_nothing_raised do
41
+ ::PP.pp @xml, out
42
+ end
43
+ end
44
+
45
+ def test_create_internal_subset_on_existing_subset
46
+ assert_not_nil @xml.internal_subset
47
+ assert_raises(RuntimeError) do
48
+ @xml.create_internal_subset('staff', nil, 'staff.dtd')
49
+ end
50
+ end
51
+
52
+ def test_create_internal_subset
53
+ xml = Nokogiri::XML('<root />')
54
+ assert_nil xml.internal_subset
55
+
56
+ xml.create_internal_subset('name', nil, 'staff.dtd')
57
+ ss = xml.internal_subset
58
+ assert_equal 'name', ss.name
59
+ assert_nil ss.external_id
60
+ assert_equal 'staff.dtd', ss.system_id
61
+ end
62
+
63
+ def test_external_subset
64
+ assert_nil @xml.external_subset
65
+ Dir.chdir(ASSETS_DIR) do
66
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) { |cfg|
67
+ cfg.dtdload
68
+ }
69
+ end
70
+ assert @xml.external_subset
71
+ end
72
+
73
+ def test_create_external_subset_fails_with_existing_subset
74
+ assert_nil @xml.external_subset
75
+ Dir.chdir(ASSETS_DIR) do
76
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE) { |cfg|
77
+ cfg.dtdload
78
+ }
79
+ end
80
+ assert @xml.external_subset
81
+
82
+ assert_raises(RuntimeError) do
83
+ @xml.create_external_subset('staff', nil, 'staff.dtd')
84
+ end
85
+ end
86
+
87
+ def test_create_external_subset
88
+ dtd = @xml.create_external_subset('staff', nil, 'staff.dtd')
89
+ assert_nil dtd.external_id
90
+ assert_equal 'staff.dtd', dtd.system_id
91
+ assert_equal 'staff', dtd.name
92
+ assert_equal dtd, @xml.external_subset
93
+ end
94
+
95
+ def test_version
96
+ assert_equal '1.0', @xml.version
97
+ end
98
+
99
+ def test_attributes
100
+ assert_raise NoMethodError do
101
+ @xml.attributes
102
+ end
103
+ end
104
+
105
+ def test_namespace
106
+ assert_raise NoMethodError do
107
+ @xml.namespace
108
+ end
109
+ end
110
+
111
+ def test_namespace_definitions
112
+ assert_raise NoMethodError do
113
+ @xml.namespace_definitions
114
+ end
115
+ end
116
+
117
+ def test_line
118
+ assert_raise NoMethodError do
119
+ @xml.line
120
+ end
121
+ end
122
+
13
123
  def test_empty_node_converted_to_html_is_not_self_closing
14
124
  doc = Nokogiri::XML('<a></a>')
15
125
  assert_equal "<a></a>", doc.inner_html
16
126
  end
17
127
 
128
+ def test_fragment
129
+ fragment = @xml.fragment
130
+ assert_equal 0, fragment.children.length
131
+ end
132
+
18
133
  def test_add_child_with_fragment
19
134
  doc = Nokogiri::XML::Document.new
20
135
  fragment = doc.fragment('<hello />')
@@ -259,6 +374,12 @@ module Nokogiri
259
374
  cfg.strict
260
375
  }
261
376
  }
377
+
378
+ assert_raises(Nokogiri::XML::SyntaxError) {
379
+ Nokogiri::XML(StringIO.new('<foo><bar></foo>')) { |cfg|
380
+ cfg.strict
381
+ }
382
+ }
262
383
  end
263
384
 
264
385
  def test_XML_function
@@ -311,11 +432,6 @@ module Nokogiri
311
432
  }
312
433
  end
313
434
 
314
- def test_new_document_collect_namespaces
315
- doc = Nokogiri::XML::Document.new
316
- assert_equal({}, doc.collect_namespaces)
317
- end
318
-
319
435
  def test_find_with_namespace
320
436
  doc = Nokogiri::XML.parse(<<-eoxml)
321
437
  <x xmlns:tenderlove='http://tenderlovemaking.com/'>
@@ -456,6 +572,31 @@ module Nokogiri
456
572
  assert_equal(node, doc.root)
457
573
  end
458
574
 
575
+ def test_remove_namespaces
576
+ doc = Nokogiri::XML <<-EOX
577
+ <root xmlns:a="http://a.flavorjon.es/" xmlns:b="http://b.flavorjon.es/">
578
+ <a:foo>hello from a</a:foo>
579
+ <b:foo>hello from b</b:foo>
580
+ <container xmlns:c="http://c.flavorjon.es/">
581
+ <c:foo>hello from c</c:foo>
582
+ </container>
583
+ </root>
584
+ EOX
585
+
586
+ # assert on setup
587
+ assert_equal 0, doc.xpath("//foo").length
588
+ assert_equal 1, doc.xpath("//a:foo").length
589
+ assert_equal 1, doc.xpath("//a:foo").length
590
+ assert_equal 1, doc.xpath("//x:foo", "x" => "http://c.flavorjon.es/").length
591
+
592
+ doc.remove_namespaces!
593
+
594
+ assert_equal 3, doc.xpath("//foo").length
595
+ assert_equal 0, doc.xpath("//a:foo").length
596
+ assert_equal 0, doc.xpath("//a:foo").length
597
+ assert_equal 0, doc.xpath("//x:foo", "x" => "http://c.flavorjon.es/").length
598
+ end
599
+
459
600
  def util_decorate(document, x)
460
601
  document.decorators(XML::Node) << x
461
602
  document.decorators(XML::NodeSet) << x
@@ -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
@@ -49,21 +49,20 @@ module Nokogiri
49
49
  end
50
50
 
51
51
  def test_xml_fragment_has_multiple_toplevel_children
52
- # TODO: this is lame. xml fragment() should support multiple top-level children
53
52
  doc = "<div>b</div><div>e</div>"
54
53
  fragment = Nokogiri::XML::Document.new.fragment(doc)
55
- assert_equal "<div>b</div>", fragment.to_s
54
+ assert_equal "<div>b</div><div>e</div>", fragment.to_s
56
55
  end
57
56
 
58
57
  def test_xml_fragment_has_outer_text
59
58
  # this test is descriptive, not prescriptive.
60
59
  doc = "a<div>b</div>"
61
60
  fragment = Nokogiri::XML::Document.new.fragment(doc)
62
- assert_equal "", fragment.to_s
61
+ assert_equal "a<div>b</div>", fragment.to_s
63
62
 
64
63
  doc = "<div>b</div>c"
65
64
  fragment = Nokogiri::XML::Document.new.fragment(doc)
66
- assert_equal "<div>b</div>", fragment.to_s
65
+ assert_equal "<div>b</div>c", fragment.to_s
67
66
  end
68
67
 
69
68
  def test_xml_fragment_case_sensitivity
@@ -83,6 +82,57 @@ module Nokogiri
83
82
  fragment = Nokogiri::XML::Document.new.fragment(doc)
84
83
  assert_equal "<div>b</div>", fragment.to_s
85
84
  end
85
+
86
+ def test_fragment_children_search
87
+ fragment = Nokogiri::XML::Document.new.fragment(
88
+ '<div><p id="content">hi</p></div>'
89
+ )
90
+ css = fragment.children.css('p')
91
+ xpath = fragment.children.xpath('.//p')
92
+ assert_equal css, xpath
93
+ end
94
+
95
+ def test_fragment_search
96
+ frag = Nokogiri::XML::Document.new.fragment '<p id="content">hi</p>'
97
+
98
+ p_tag = frag.css('#content').first
99
+ assert_equal 'p', p_tag.name
100
+
101
+ assert_equal p_tag, frag.xpath('./*[@id = \'content\']').first
102
+ end
103
+
104
+ def test_fragment_without_a_namespace_does_not_get_a_namespace
105
+ doc = Nokogiri::XML <<-EOX
106
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="http://flavorjon.es/" xmlns:bar="http://google.com/">
107
+ <foo:existing></foo:existing>
108
+ </root>
109
+ EOX
110
+ frag = doc.fragment "<newnode></newnode>"
111
+ assert_nil frag.namespace
112
+ end
113
+
114
+ def test_fragment_namespace_resolves_against_document_root
115
+ doc = Nokogiri::XML <<-EOX
116
+ <root xmlns:foo="http://flavorjon.es/" xmlns:bar="http://google.com/">
117
+ <foo:existing></foo:existing>
118
+ </root>
119
+ EOX
120
+ ns = doc.root.namespace_definitions.detect { |x| x.prefix == "bar" }
121
+
122
+ frag = doc.fragment "<bar:newnode></bar:newnode>"
123
+ assert frag.children.first.namespace
124
+ assert_equal ns, frag.children.first.namespace
125
+ end
126
+
127
+ def test_fragment_invalid_namespace_is_silently_ignored
128
+ doc = Nokogiri::XML <<-EOX
129
+ <root xmlns:foo="http://flavorjon.es/" xmlns:bar="http://google.com/">
130
+ <foo:existing></foo:existing>
131
+ </root>
132
+ EOX
133
+ frag = doc.fragment "<baz:newnode></baz:newnode>"
134
+ assert_nil frag.children.first.namespace
135
+ end
86
136
  end
87
137
  end
88
138
  end