nokogiri 1.3.0-x86-mswin32

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 (256) hide show
  1. data/.autotest +27 -0
  2. data/CHANGELOG.ja.rdoc +233 -0
  3. data/CHANGELOG.rdoc +222 -0
  4. data/Manifest.txt +247 -0
  5. data/README.ja.rdoc +103 -0
  6. data/README.rdoc +117 -0
  7. data/Rakefile +205 -0
  8. data/bin/nokogiri +47 -0
  9. data/ext/nokogiri/extconf.rb +89 -0
  10. data/ext/nokogiri/html_document.c +183 -0
  11. data/ext/nokogiri/html_document.h +10 -0
  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.c +30 -0
  15. data/ext/nokogiri/html_entity_lookup.h +8 -0
  16. data/ext/nokogiri/html_sax_parser.c +57 -0
  17. data/ext/nokogiri/html_sax_parser.h +11 -0
  18. data/ext/nokogiri/iconv.dll +0 -0
  19. data/ext/nokogiri/libexslt.dll +0 -0
  20. data/ext/nokogiri/libxml2.dll +0 -0
  21. data/ext/nokogiri/libxslt.dll +0 -0
  22. data/ext/nokogiri/nokogiri.c +81 -0
  23. data/ext/nokogiri/nokogiri.h +149 -0
  24. data/ext/nokogiri/xml_attr.c +92 -0
  25. data/ext/nokogiri/xml_attr.h +9 -0
  26. data/ext/nokogiri/xml_cdata.c +53 -0
  27. data/ext/nokogiri/xml_cdata.h +9 -0
  28. data/ext/nokogiri/xml_comment.c +51 -0
  29. data/ext/nokogiri/xml_comment.h +9 -0
  30. data/ext/nokogiri/xml_document.c +308 -0
  31. data/ext/nokogiri/xml_document.h +21 -0
  32. data/ext/nokogiri/xml_document_fragment.c +48 -0
  33. data/ext/nokogiri/xml_document_fragment.h +10 -0
  34. data/ext/nokogiri/xml_dtd.c +102 -0
  35. data/ext/nokogiri/xml_dtd.h +8 -0
  36. data/ext/nokogiri/xml_entity_reference.c +50 -0
  37. data/ext/nokogiri/xml_entity_reference.h +9 -0
  38. data/ext/nokogiri/xml_io.c +24 -0
  39. data/ext/nokogiri/xml_io.h +10 -0
  40. data/ext/nokogiri/xml_namespace.c +69 -0
  41. data/ext/nokogiri/xml_namespace.h +12 -0
  42. data/ext/nokogiri/xml_node.c +928 -0
  43. data/ext/nokogiri/xml_node.h +14 -0
  44. data/ext/nokogiri/xml_node_set.c +386 -0
  45. data/ext/nokogiri/xml_node_set.h +9 -0
  46. data/ext/nokogiri/xml_processing_instruction.c +54 -0
  47. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  48. data/ext/nokogiri/xml_reader.c +572 -0
  49. data/ext/nokogiri/xml_reader.h +10 -0
  50. data/ext/nokogiri/xml_relax_ng.c +106 -0
  51. data/ext/nokogiri/xml_relax_ng.h +9 -0
  52. data/ext/nokogiri/xml_sax_parser.c +336 -0
  53. data/ext/nokogiri/xml_sax_parser.h +10 -0
  54. data/ext/nokogiri/xml_sax_push_parser.c +86 -0
  55. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  56. data/ext/nokogiri/xml_schema.c +107 -0
  57. data/ext/nokogiri/xml_schema.h +9 -0
  58. data/ext/nokogiri/xml_syntax_error.c +203 -0
  59. data/ext/nokogiri/xml_syntax_error.h +12 -0
  60. data/ext/nokogiri/xml_text.c +47 -0
  61. data/ext/nokogiri/xml_text.h +9 -0
  62. data/ext/nokogiri/xml_xpath.c +53 -0
  63. data/ext/nokogiri/xml_xpath.h +11 -0
  64. data/ext/nokogiri/xml_xpath_context.c +252 -0
  65. data/ext/nokogiri/xml_xpath_context.h +9 -0
  66. data/ext/nokogiri/xslt_stylesheet.c +131 -0
  67. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  68. data/ext/nokogiri/zlib1.dll +0 -0
  69. data/lib/action-nokogiri.rb +36 -0
  70. data/lib/nokogiri.rb +110 -0
  71. data/lib/nokogiri/1.8/nokogiri.so +0 -0
  72. data/lib/nokogiri/1.9/nokogiri.so +0 -0
  73. data/lib/nokogiri/css.rb +25 -0
  74. data/lib/nokogiri/css/generated_parser.rb +748 -0
  75. data/lib/nokogiri/css/generated_tokenizer.rb +144 -0
  76. data/lib/nokogiri/css/node.rb +107 -0
  77. data/lib/nokogiri/css/parser.rb +82 -0
  78. data/lib/nokogiri/css/parser.y +227 -0
  79. data/lib/nokogiri/css/syntax_error.rb +7 -0
  80. data/lib/nokogiri/css/tokenizer.rb +11 -0
  81. data/lib/nokogiri/css/tokenizer.rex +54 -0
  82. data/lib/nokogiri/css/xpath_visitor.rb +172 -0
  83. data/lib/nokogiri/decorators.rb +2 -0
  84. data/lib/nokogiri/decorators/hpricot.rb +3 -0
  85. data/lib/nokogiri/decorators/hpricot/node.rb +56 -0
  86. data/lib/nokogiri/decorators/hpricot/node_set.rb +54 -0
  87. data/lib/nokogiri/decorators/hpricot/xpath_visitor.rb +30 -0
  88. data/lib/nokogiri/decorators/slop.rb +33 -0
  89. data/lib/nokogiri/ffi/html/document.rb +37 -0
  90. data/lib/nokogiri/ffi/html/element_description.rb +85 -0
  91. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  92. data/lib/nokogiri/ffi/html/sax/parser.rb +21 -0
  93. data/lib/nokogiri/ffi/io_callbacks.rb +32 -0
  94. data/lib/nokogiri/ffi/libxml.rb +314 -0
  95. data/lib/nokogiri/ffi/structs/common_node.rb +26 -0
  96. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  97. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  98. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  99. data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
  100. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  101. data/lib/nokogiri/ffi/structs/xml_document.rb +108 -0
  102. data/lib/nokogiri/ffi/structs/xml_dtd.rb +26 -0
  103. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  104. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  105. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  106. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  107. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  108. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  109. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +14 -0
  110. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  111. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  112. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  113. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +37 -0
  114. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  115. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  116. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  117. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  118. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  119. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  120. data/lib/nokogiri/ffi/xml/document.rb +107 -0
  121. data/lib/nokogiri/ffi/xml/document_fragment.rb +26 -0
  122. data/lib/nokogiri/ffi/xml/dtd.rb +42 -0
  123. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  124. data/lib/nokogiri/ffi/xml/namespace.rb +38 -0
  125. data/lib/nokogiri/ffi/xml/node.rb +380 -0
  126. data/lib/nokogiri/ffi/xml/node_set.rb +130 -0
  127. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  128. data/lib/nokogiri/ffi/xml/reader.rb +217 -0
  129. data/lib/nokogiri/ffi/xml/relax_ng.rb +51 -0
  130. data/lib/nokogiri/ffi/xml/sax/parser.rb +148 -0
  131. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +38 -0
  132. data/lib/nokogiri/ffi/xml/schema.rb +55 -0
  133. data/lib/nokogiri/ffi/xml/syntax_error.rb +76 -0
  134. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  135. data/lib/nokogiri/ffi/xml/xpath.rb +19 -0
  136. data/lib/nokogiri/ffi/xml/xpath_context.rb +135 -0
  137. data/lib/nokogiri/ffi/xslt/stylesheet.rb +47 -0
  138. data/lib/nokogiri/hpricot.rb +62 -0
  139. data/lib/nokogiri/html.rb +34 -0
  140. data/lib/nokogiri/html/builder.rb +35 -0
  141. data/lib/nokogiri/html/document.rb +71 -0
  142. data/lib/nokogiri/html/document_fragment.rb +15 -0
  143. data/lib/nokogiri/html/element_description.rb +23 -0
  144. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  145. data/lib/nokogiri/html/sax/parser.rb +47 -0
  146. data/lib/nokogiri/nokogiri.rb +1 -0
  147. data/lib/nokogiri/syntax_error.rb +4 -0
  148. data/lib/nokogiri/version.rb +29 -0
  149. data/lib/nokogiri/version_warning.rb +11 -0
  150. data/lib/nokogiri/xml.rb +62 -0
  151. data/lib/nokogiri/xml/attr.rb +9 -0
  152. data/lib/nokogiri/xml/builder.rb +254 -0
  153. data/lib/nokogiri/xml/cdata.rb +11 -0
  154. data/lib/nokogiri/xml/document.rb +100 -0
  155. data/lib/nokogiri/xml/document_fragment.rb +49 -0
  156. data/lib/nokogiri/xml/dtd.rb +11 -0
  157. data/lib/nokogiri/xml/entity_declaration.rb +11 -0
  158. data/lib/nokogiri/xml/fragment_handler.rb +55 -0
  159. data/lib/nokogiri/xml/namespace.rb +7 -0
  160. data/lib/nokogiri/xml/node.rb +745 -0
  161. data/lib/nokogiri/xml/node/save_options.rb +42 -0
  162. data/lib/nokogiri/xml/node_set.rb +238 -0
  163. data/lib/nokogiri/xml/notation.rb +6 -0
  164. data/lib/nokogiri/xml/parse_options.rb +80 -0
  165. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  166. data/lib/nokogiri/xml/reader.rb +66 -0
  167. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  168. data/lib/nokogiri/xml/sax.rb +3 -0
  169. data/lib/nokogiri/xml/sax/document.rb +143 -0
  170. data/lib/nokogiri/xml/sax/parser.rb +101 -0
  171. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  172. data/lib/nokogiri/xml/schema.rb +65 -0
  173. data/lib/nokogiri/xml/syntax_error.rb +34 -0
  174. data/lib/nokogiri/xml/xpath.rb +10 -0
  175. data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
  176. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  177. data/lib/nokogiri/xslt.rb +48 -0
  178. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  179. data/lib/xsd/xmlparser/nokogiri.rb +64 -0
  180. data/tasks/test.rb +161 -0
  181. data/test/css/test_nthiness.rb +160 -0
  182. data/test/css/test_parser.rb +277 -0
  183. data/test/css/test_tokenizer.rb +176 -0
  184. data/test/css/test_xpath_visitor.rb +76 -0
  185. data/test/ffi/test_document.rb +35 -0
  186. data/test/files/address_book.rlx +12 -0
  187. data/test/files/address_book.xml +10 -0
  188. data/test/files/dont_hurt_em_why.xml +422 -0
  189. data/test/files/exslt.xml +8 -0
  190. data/test/files/exslt.xslt +35 -0
  191. data/test/files/po.xml +32 -0
  192. data/test/files/po.xsd +66 -0
  193. data/test/files/staff.xml +59 -0
  194. data/test/files/staff.xslt +32 -0
  195. data/test/files/tlm.html +850 -0
  196. data/test/helper.rb +123 -0
  197. data/test/hpricot/files/basic.xhtml +17 -0
  198. data/test/hpricot/files/boingboing.html +2266 -0
  199. data/test/hpricot/files/cy0.html +3653 -0
  200. data/test/hpricot/files/immob.html +400 -0
  201. data/test/hpricot/files/pace_application.html +1320 -0
  202. data/test/hpricot/files/tenderlove.html +16 -0
  203. data/test/hpricot/files/uswebgen.html +220 -0
  204. data/test/hpricot/files/utf8.html +1054 -0
  205. data/test/hpricot/files/week9.html +1723 -0
  206. data/test/hpricot/files/why.xml +19 -0
  207. data/test/hpricot/load_files.rb +11 -0
  208. data/test/hpricot/test_alter.rb +68 -0
  209. data/test/hpricot/test_builder.rb +20 -0
  210. data/test/hpricot/test_parser.rb +426 -0
  211. data/test/hpricot/test_paths.rb +15 -0
  212. data/test/hpricot/test_preserved.rb +77 -0
  213. data/test/hpricot/test_xml.rb +30 -0
  214. data/test/html/sax/test_parser.rb +52 -0
  215. data/test/html/test_builder.rb +156 -0
  216. data/test/html/test_document.rb +361 -0
  217. data/test/html/test_document_encoding.rb +46 -0
  218. data/test/html/test_document_fragment.rb +97 -0
  219. data/test/html/test_element_description.rb +95 -0
  220. data/test/html/test_named_characters.rb +14 -0
  221. data/test/html/test_node.rb +165 -0
  222. data/test/test_convert_xpath.rb +186 -0
  223. data/test/test_css_cache.rb +56 -0
  224. data/test/test_gc.rb +15 -0
  225. data/test/test_memory_leak.rb +77 -0
  226. data/test/test_nokogiri.rb +127 -0
  227. data/test/test_reader.rb +316 -0
  228. data/test/test_xslt_transforms.rb +131 -0
  229. data/test/xml/node/test_save_options.rb +20 -0
  230. data/test/xml/node/test_subclass.rb +44 -0
  231. data/test/xml/sax/test_parser.rb +169 -0
  232. data/test/xml/sax/test_push_parser.rb +92 -0
  233. data/test/xml/test_attr.rb +38 -0
  234. data/test/xml/test_builder.rb +73 -0
  235. data/test/xml/test_cdata.rb +38 -0
  236. data/test/xml/test_comment.rb +23 -0
  237. data/test/xml/test_document.rb +397 -0
  238. data/test/xml/test_document_encoding.rb +26 -0
  239. data/test/xml/test_document_fragment.rb +76 -0
  240. data/test/xml/test_dtd.rb +42 -0
  241. data/test/xml/test_dtd_encoding.rb +31 -0
  242. data/test/xml/test_entity_reference.rb +21 -0
  243. data/test/xml/test_namespace.rb +43 -0
  244. data/test/xml/test_node.rb +808 -0
  245. data/test/xml/test_node_attributes.rb +34 -0
  246. data/test/xml/test_node_encoding.rb +84 -0
  247. data/test/xml/test_node_set.rb +368 -0
  248. data/test/xml/test_parse_options.rb +52 -0
  249. data/test/xml/test_processing_instruction.rb +30 -0
  250. data/test/xml/test_reader_encoding.rb +126 -0
  251. data/test/xml/test_relax_ng.rb +60 -0
  252. data/test/xml/test_schema.rb +65 -0
  253. data/test/xml/test_text.rb +18 -0
  254. data/test/xml/test_unparented_node.rb +381 -0
  255. data/test/xml/test_xpath.rb +106 -0
  256. metadata +409 -0
@@ -0,0 +1,26 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ if RUBY_VERSION =~ /^1\.9/
6
+ class TestDocumentEncoding < Nokogiri::TestCase
7
+ def setup
8
+ super
9
+ @xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE, 'UTF-8')
10
+ end
11
+
12
+ def test_url
13
+ assert_equal @xml.encoding, @xml.url.encoding.name
14
+ end
15
+
16
+ def test_encoding
17
+ assert_equal @xml.encoding, @xml.encoding.encoding.name
18
+ end
19
+
20
+ def test_dotted_version
21
+ assert_equal 'UTF-8', Nokogiri::LIBXML_VERSION.encoding.name
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,76 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestDocumentFragment < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
9
+ end
10
+
11
+ def test_new
12
+ fragment = Nokogiri::XML::DocumentFragment.new(@xml)
13
+ end
14
+
15
+ def test_fragment_should_have_document
16
+ fragment = Nokogiri::XML::DocumentFragment.new(@xml)
17
+ assert_equal @xml, fragment.document
18
+ end
19
+
20
+ def test_name
21
+ fragment = Nokogiri::XML::DocumentFragment.new(@xml)
22
+ assert_equal '#document-fragment', fragment.name
23
+ end
24
+
25
+ def test_static_method
26
+ fragment = Nokogiri::XML::DocumentFragment.parse("<div>a</div>")
27
+ assert_instance_of Nokogiri::XML::DocumentFragment, fragment
28
+ end
29
+
30
+ def test_many_fragments
31
+ 100.times { Nokogiri::XML::DocumentFragment.new(@xml) }
32
+ end
33
+
34
+ def test_subclass
35
+ klass = Class.new(Nokogiri::XML::DocumentFragment)
36
+ fragment = klass.new(@xml, "<div>a</div>")
37
+ assert_instance_of klass, fragment
38
+ end
39
+
40
+ def test_xml_fragment
41
+ fragment = Nokogiri::XML.fragment("<div>a</div>")
42
+ assert_equal "<div>a</div>", fragment.to_s
43
+ end
44
+
45
+ def test_xml_fragment_has_multiple_toplevel_children
46
+ # TODO: this is lame. xml fragment() should support multiple top-level children
47
+ doc = "<div>b</div><div>e</div>"
48
+ fragment = Nokogiri::XML::Document.new.fragment(doc)
49
+ assert_equal "<div>b</div>", fragment.to_s
50
+ end
51
+
52
+ def test_xml_fragment_has_outer_text
53
+ # this test is descriptive, not prescriptive.
54
+ doc = "a<div>b</div>"
55
+ fragment = Nokogiri::XML::Document.new.fragment(doc)
56
+ assert_equal "", fragment.to_s
57
+
58
+ doc = "<div>b</div>c"
59
+ fragment = Nokogiri::XML::Document.new.fragment(doc)
60
+ assert_equal "<div>b</div>", fragment.to_s
61
+ end
62
+
63
+ def test_xml_fragment_case_sensitivity
64
+ doc = "<crazyDiv>b</crazyDiv>"
65
+ fragment = Nokogiri::XML::Document.new.fragment(doc)
66
+ assert_equal "<crazyDiv>b</crazyDiv>", fragment.to_s
67
+ end
68
+
69
+ def test_xml_fragment_with_leading_whitespace
70
+ doc = " <div>b</div> "
71
+ fragment = Nokogiri::XML::Document.new.fragment(doc)
72
+ assert_equal "<div>b</div>", fragment.to_s
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,42 @@
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
+ super
8
+ @xml = Nokogiri::XML(File.open(XML_FILE))
9
+ assert @dtd = @xml.internal_subset
10
+ end
11
+
12
+ def test_external_subsets
13
+ assert subset = @xml.internal_subset
14
+ assert_equal 'staff', subset.name
15
+ end
16
+
17
+ def test_entities
18
+ assert entities = @dtd.entities
19
+ assert_equal %w[ ent1 ent2 ent3 ent4 ent5 ].sort, entities.keys.sort
20
+ end
21
+
22
+ def test_attributes
23
+ assert_nil @dtd.attributes
24
+ end
25
+
26
+ def test_elements
27
+ assert elements = @dtd.elements
28
+ assert_equal %w[ br ], elements.keys
29
+ assert_equal 'br', elements['br'].name
30
+ end
31
+
32
+ def test_notations
33
+ assert notations = @dtd.notations
34
+ assert_equal %w[ notation1 notation2 ].sort, notations.keys.sort
35
+ assert notation1 = notations['notation1']
36
+ assert_equal 'notation1', notation1.name
37
+ assert_equal 'notation1File', notation1.public_id
38
+ assert_nil notation1.system_id
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ if RUBY_VERSION =~ /^1\.9/
6
+ class TestDTDEncoding < Nokogiri::TestCase
7
+ def setup
8
+ super
9
+ @xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE, 'UTF-8')
10
+ assert @dtd = @xml.internal_subset
11
+ end
12
+
13
+ def test_entities
14
+ @dtd.entities.each do |k,v|
15
+ assert_equal @xml.encoding, k.encoding.name
16
+ end
17
+ end
18
+
19
+ def test_notations
20
+ @dtd.notations.each do |k,notation|
21
+ assert_equal 'UTF-8', k.encoding.name
22
+ %w{ name public_id system_id }.each do |attribute|
23
+ v = notation.send(:"#{attribute}") || next
24
+ assert_equal 'UTF-8', v.encoding.name
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestEntityReference < Nokogiri::TestCase
6
+ def setup
7
+ super
8
+ @xml = Nokogiri::XML(File.open(XML_FILE), XML_FILE)
9
+ end
10
+
11
+ def test_new
12
+ assert ref = EntityReference.new(@xml, 'ent4')
13
+ assert_instance_of EntityReference, ref
14
+ end
15
+
16
+ def test_many_references
17
+ 100.times { EntityReference.new(@xml, 'foo') }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ module Nokogiri
4
+ module XML
5
+ class TestNamespace < Nokogiri::TestCase
6
+ def setup
7
+ @xml = Nokogiri::XML <<-eoxml
8
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
9
+ <awesome/>
10
+ </root>
11
+ eoxml
12
+ end
13
+
14
+ def test_namespace_node_prefix
15
+ namespaces = @xml.root.namespace_definitions
16
+ assert_equal [nil, 'foo'], namespaces.map { |x| x.prefix }
17
+ end
18
+
19
+ def test_namespace_node_href
20
+ namespaces = @xml.root.namespace_definitions
21
+ assert_equal [
22
+ 'http://tenderlovemaking.com/',
23
+ 'bar'
24
+ ], namespaces.map { |x| x.href }
25
+ end
26
+
27
+ def test_equality
28
+ namespaces = @xml.root.namespace_definitions
29
+ assert_equal namespaces, @xml.root.namespace_definitions
30
+ end
31
+
32
+ def test_add_definition
33
+ @xml.root.add_namespace_definition('baz', 'bar')
34
+ assert_equal 3, @xml.root.namespace_definitions.length
35
+ end
36
+
37
+ def test_add_definition_return
38
+ ns = @xml.root.add_namespace_definition('baz', 'bar')
39
+ assert_equal 'baz', ns.prefix
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,808 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "helper"))
2
+
3
+ require 'stringio'
4
+
5
+ module Nokogiri
6
+ module XML
7
+ class TestNode < Nokogiri::TestCase
8
+ def setup
9
+ super
10
+ @xml = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
11
+ end
12
+
13
+ def test_namespace_nodes
14
+ xml = Nokogiri::XML <<-eoxml
15
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
16
+ <awesome/>
17
+ </root>
18
+ eoxml
19
+ awesome = xml.root
20
+ namespaces = awesome.namespace_definitions
21
+ assert_equal 2, namespaces.length
22
+ end
23
+
24
+ def test_no_definitions
25
+ xml = Nokogiri::XML <<-eoxml
26
+ <root xmlns="http://tenderlovemaking.com/" xmlns:foo="bar">
27
+ <awesome/>
28
+ </root>
29
+ eoxml
30
+ awesome = xml.at('//xmlns:awesome')
31
+ namespaces = awesome.namespace_definitions
32
+ assert_equal 0, namespaces.length
33
+ end
34
+
35
+ def test_subclass_dup
36
+ subclass = Class.new(Nokogiri::XML::Node)
37
+ node = subclass.new('foo', @xml).dup
38
+ assert_instance_of subclass, node
39
+ end
40
+
41
+ def test_namespace_goes_to_children
42
+ fruits = Nokogiri::XML(<<-eoxml)
43
+ <Fruit xmlns='www.fruits.org'>
44
+ </Fruit>
45
+ eoxml
46
+ apple = Nokogiri::XML::Node.new('Apple', fruits)
47
+ orange = Nokogiri::XML::Node.new('Orange', fruits)
48
+ apple << orange
49
+ fruits.root << apple
50
+ assert fruits.at('//fruit:Orange',{'fruit'=>'www.fruits.org'})
51
+ assert fruits.at('//fruit:Apple',{'fruit'=>'www.fruits.org'})
52
+ end
53
+
54
+ def test_description
55
+ assert_nil @xml.at('employee').description
56
+ end
57
+
58
+ def test_add_namespace_add_child
59
+ doc = Nokogiri::XML::Document.new
60
+ item = Nokogiri::XML::Element.new('item', doc)
61
+ doc.root = item
62
+
63
+ entry = Nokogiri::XML::Element.new('entry', doc)
64
+ entry.add_namespace('tlm', 'http://tenderlovemaking.com')
65
+ assert_equal 'http://tenderlovemaking.com', entry.namespaces['xmlns:tlm']
66
+ item.add_child(entry)
67
+ assert_equal 'http://tenderlovemaking.com', entry.namespaces['xmlns:tlm']
68
+ end
69
+
70
+ def test_spaceship
71
+ nodes = @xml.xpath('//employee')
72
+ assert_equal(-1, (nodes.first <=> nodes.last))
73
+ list = [nodes.first, nodes.last].sort
74
+ assert_equal nodes.first, list.first
75
+ assert_equal nodes.last, list.last
76
+ end
77
+
78
+ def test_incorrect_spaceship
79
+ nodes = @xml.xpath('//employee')
80
+ assert_nil(nodes.first <=> 'asdf')
81
+ end
82
+
83
+ def test_document_compare
84
+ nodes = @xml.xpath('//employee')
85
+ assert_equal(-1, (nodes.first <=> @xml))
86
+ end
87
+
88
+ def test_different_document_compare
89
+ nodes = @xml.xpath('//employee')
90
+ doc = Nokogiri::XML('<a><b/></a>')
91
+ b = doc.at('b')
92
+ assert_nil(nodes.first <=> b)
93
+ end
94
+
95
+ def test_duplicate_node_removes_namespace
96
+ fruits = Nokogiri::XML(<<-eoxml)
97
+ <Fruit xmlns='www.fruits.org'>
98
+ <Apple></Apple>
99
+ </Fruit>
100
+ eoxml
101
+ apple = fruits.root.xpath('fruit:Apple', {'fruit'=>'www.fruits.org'})[0]
102
+ new_apple = apple.dup
103
+ fruits.root << new_apple
104
+ assert_equal 2, fruits.xpath('//xmlns:Apple').length
105
+ assert_equal 1, fruits.to_xml.scan('www.fruits.org').length
106
+ end
107
+
108
+ [:clone, :dup].each do |symbol|
109
+ define_method "test_#{symbol}" do
110
+ node = @xml.at('//employee')
111
+ other = node.send(symbol)
112
+ assert_equal "employee", other.name
113
+ assert_nil other.parent
114
+ end
115
+ end
116
+
117
+ def test_node_added_to_root_should_get_namespace
118
+ fruits = Nokogiri::XML(<<-eoxml)
119
+ <Fruit xmlns='http://www.fruits.org'>
120
+ </Fruit>
121
+ eoxml
122
+ apple = fruits.fragment('<Apple/>')
123
+ fruits << apple
124
+ assert_equal 1, fruits.xpath('//xmlns:Apple').length
125
+ end
126
+
127
+ def test_add_child_path_following_sequential_text_nodes
128
+ xml = Nokogiri::XML('<root>text</root>')
129
+ xml.root.add_child(Nokogiri::XML::Text.new('text', xml))
130
+ item = xml.root.add_child(Nokogiri::XML::Element.new('item', xml))
131
+ assert_equal '/root/item', item.path
132
+ end
133
+
134
+ def test_new_node_can_have_ancestors
135
+ xml = Nokogiri::XML('<root>text</root>')
136
+ item = Nokogiri::XML::Element.new('item', xml)
137
+ assert_equal 0, item.ancestors.length
138
+ end
139
+
140
+ def test_children
141
+ doc = Nokogiri::XML(<<-eoxml)
142
+ <root>#{'<a/>' * 9 }</root>
143
+ eoxml
144
+ assert_equal 9, doc.root.children.length
145
+ assert_equal 9, doc.root.children.to_a.length
146
+
147
+ doc = Nokogiri::XML(<<-eoxml)
148
+ <root>#{'<a/>' * 15 }</root>
149
+ eoxml
150
+ assert_equal 15, doc.root.children.length
151
+ assert_equal 15, doc.root.children.to_a.length
152
+ end
153
+
154
+ def test_add_namespace
155
+ node = @xml.at('address')
156
+ node.add_namespace('foo', 'http://tenderlovemaking.com')
157
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
158
+ end
159
+
160
+ def test_add_default_ns
161
+ node = @xml.at('address')
162
+ node.add_namespace(nil, 'http://tenderlovemaking.com')
163
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
164
+ end
165
+
166
+ def test_add_multiple_namespaces
167
+ node = @xml.at('address')
168
+
169
+ node.add_namespace(nil, 'http://tenderlovemaking.com')
170
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
171
+
172
+ node.add_namespace('foo', 'http://tenderlovemaking.com')
173
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns:foo']
174
+ end
175
+
176
+ def test_default_namespace=
177
+ node = @xml.at('address')
178
+ node.default_namespace = 'http://tenderlovemaking.com'
179
+ assert_equal 'http://tenderlovemaking.com', node.namespaces['xmlns']
180
+ end
181
+
182
+ def test_namespace=
183
+ node = @xml.at('address')
184
+ assert_nil node.namespace
185
+ definition = node.add_namespace_definition 'bar', 'http://tlm.com/'
186
+
187
+ node.namespace = definition
188
+
189
+ assert_equal definition, node.namespace
190
+
191
+ assert_equal node, @xml.at('//foo:address', {
192
+ 'foo' => 'http://tlm.com/'
193
+ })
194
+ end
195
+
196
+ def test_add_namespace_with_nil_associates_node
197
+ node = @xml.at('address')
198
+ assert_nil node.namespace
199
+ definition = node.add_namespace_definition nil, 'http://tlm.com/'
200
+ assert_equal definition, node.namespace
201
+ end
202
+
203
+ def test_add_namespace_does_not_associate_node
204
+ node = @xml.at('address')
205
+ assert_nil node.namespace
206
+ definition = node.add_namespace_definition 'foo', 'http://tlm.com/'
207
+ assert_nil node.namespace
208
+ end
209
+
210
+ def test_set_namespace_from_different_doc
211
+ node = @xml.at('address')
212
+ doc = Nokogiri::XML(File.read(XML_FILE), XML_FILE)
213
+ decl = doc.root.add_namespace_definition 'foo', 'bar'
214
+
215
+ assert_raises(ArgumentError) do
216
+ node.namespace = decl
217
+ end
218
+ end
219
+
220
+ def test_set_namespace_must_only_take_a_namespace
221
+ node = @xml.at('address')
222
+ assert_raises(TypeError) do
223
+ node.namespace = node
224
+ end
225
+ end
226
+
227
+ def test_at
228
+ node = @xml.at('address')
229
+ assert_equal node, @xml.xpath('//address').first
230
+ end
231
+
232
+ def test_percent
233
+ node = @xml % ('address')
234
+ assert_equal node, @xml.xpath('//address').first
235
+ end
236
+
237
+ def test_accept
238
+ visitor = Class.new {
239
+ attr_accessor :visited
240
+ def accept target
241
+ target.accept(self)
242
+ end
243
+
244
+ def visit node
245
+ node.children.each { |c| c.accept(self) }
246
+ @visited = true
247
+ end
248
+ }.new
249
+ visitor.accept(@xml.root)
250
+ assert visitor.visited
251
+ end
252
+
253
+ def test_replace_with_default_namespaces
254
+ fruits = Nokogiri::XML(<<-eoxml)
255
+ <fruit xmlns="http://fruits.org">
256
+ <apple />
257
+ </fruit>
258
+ eoxml
259
+
260
+ apple = fruits.css('apple').first
261
+
262
+ orange = Nokogiri::XML::Node.new('orange', fruits)
263
+ apple.replace(orange)
264
+
265
+ assert_equal orange, fruits.css('orange').first
266
+ end
267
+
268
+ def test_add_child_should_inherit_namespace
269
+ doc = Nokogiri::XML(<<-eoxml)
270
+ <root xmlns="http://tenderlovemaking.com/">
271
+ <first>
272
+ </first>
273
+ </root>
274
+ eoxml
275
+ assert node = doc.at('//xmlns:first')
276
+ child = Nokogiri::XML::Node.new('second', doc)
277
+ node.add_child(child)
278
+ assert doc.at('//xmlns:second')
279
+ end
280
+
281
+ def test_write_to
282
+ io = StringIO.new
283
+ @xml.write_to io
284
+ io.rewind
285
+ assert_equal @xml.to_xml, io.read
286
+ end
287
+
288
+ def test_attribute_with_symbol
289
+ assert_equal 'Yes', @xml.css('address').first[:domestic]
290
+ end
291
+
292
+ def test_write_to_with_block
293
+ called = false
294
+ io = StringIO.new
295
+ conf = nil
296
+ @xml.write_to io do |config|
297
+ called = true
298
+ conf = config
299
+ config.format.as_html.no_empty_tags
300
+ end
301
+ io.rewind
302
+ assert called
303
+ assert_equal @xml.serialize(nil, conf.options), io.read
304
+ end
305
+
306
+ %w{ xml html xhtml }.each do |type|
307
+ define_method(:"test_write_#{type}_to") do
308
+ io = StringIO.new
309
+ assert @xml.send(:"write_#{type}_to", io)
310
+ io.rewind
311
+ assert_match @xml.send(:"to_#{type}"), io.read
312
+ end
313
+ end
314
+
315
+ def test_serialize_with_block
316
+ called = false
317
+ conf = nil
318
+ string = @xml.serialize do |config|
319
+ called = true
320
+ conf = config
321
+ config.format.as_html.no_empty_tags
322
+ end
323
+ assert called
324
+ assert_equal @xml.serialize(nil, conf.options), string
325
+ end
326
+
327
+ def test_hold_refence_to_subnode
328
+ doc = Nokogiri::XML(<<-eoxml)
329
+ <root>
330
+ <a>
331
+ <b />
332
+ </a>
333
+ </root>
334
+ eoxml
335
+ assert node_a = doc.css('a').first
336
+ assert node_b = node_a.css('b').first
337
+ node_a.unlink
338
+ assert_equal 'b', node_b.name
339
+ end
340
+
341
+ def test_values
342
+ assert_equal %w{ Yes Yes }, @xml.xpath('//address')[1].values
343
+ end
344
+
345
+ def test_keys
346
+ assert_equal %w{ domestic street }, @xml.xpath('//address')[1].keys
347
+ end
348
+
349
+ def test_each
350
+ attributes = []
351
+ @xml.xpath('//address')[1].each do |key, value|
352
+ attributes << [key, value]
353
+ end
354
+ assert_equal [['domestic', 'Yes'], ['street', 'Yes']], attributes
355
+ end
356
+
357
+ def test_new
358
+ assert node = Nokogiri::XML::Node.new('input', @xml)
359
+ assert_equal 1, node.node_type
360
+ assert_instance_of Nokogiri::XML::Node, node
361
+ end
362
+
363
+ def test_to_str
364
+ name = @xml.xpath('//name').first
365
+ assert_match(/Margaret/, '' + name)
366
+ assert_equal('Margaret Martin', '' + name.children.first)
367
+ end
368
+
369
+ def test_ancestors
370
+ address = @xml.xpath('//address').first
371
+ assert_equal 3, address.ancestors.length
372
+ assert_equal ['employee', 'staff', 'document'],
373
+ address.ancestors.map { |x| x.name }
374
+ end
375
+
376
+ def test_read_only?
377
+ assert entity_decl = @xml.internal_subset.children.find { |x|
378
+ x.type == Node::ENTITY_DECL
379
+ }
380
+ assert entity_decl.read_only?
381
+ end
382
+
383
+ def test_remove_attribute
384
+ address = @xml.xpath('/staff/employee/address').first
385
+ assert_equal 'Yes', address['domestic']
386
+ address.remove_attribute 'domestic'
387
+ assert_nil address['domestic']
388
+ end
389
+
390
+ def test_delete
391
+ address = @xml.xpath('/staff/employee/address').first
392
+ assert_equal 'Yes', address['domestic']
393
+ address.delete 'domestic'
394
+ assert_nil address['domestic']
395
+ end
396
+
397
+ def test_add_child_in_same_document
398
+ child = @xml.css('employee').first
399
+
400
+ assert previous_last_child = child.children.last
401
+ assert new_child = child.children.first
402
+
403
+ last = child.children.last
404
+
405
+ child.add_child(new_child)
406
+ assert_equal new_child, child.children.last
407
+ assert_equal last, child.children.last
408
+ end
409
+
410
+ def test_add_child_from_other_document
411
+ d1 = Nokogiri::XML("<root><item>1</item><item>2</item></root>")
412
+ d2 = Nokogiri::XML("<root><item>3</item><item>4</item></root>")
413
+
414
+ d2.at('root').search('item').each do |i|
415
+ d1.at('root').add_child i
416
+ end
417
+
418
+ assert_equal 0, d2.search('item').size
419
+ assert_equal 4, d1.search('item').size
420
+ end
421
+
422
+ def test_add_child
423
+ xml = Nokogiri::XML(<<-eoxml)
424
+ <root>
425
+ <a>Hello world</a>
426
+ </root>
427
+ eoxml
428
+ text_node = Nokogiri::XML::Text.new('hello', xml)
429
+ assert_equal Nokogiri::XML::Node::TEXT_NODE, text_node.type
430
+ xml.root.add_child text_node
431
+ assert_match 'hello', xml.to_s
432
+ end
433
+
434
+ def test_chevron_works_as_add_child
435
+ xml = Nokogiri::XML(<<-eoxml)
436
+ <root>
437
+ <a>Hello world</a>
438
+ </root>
439
+ eoxml
440
+ text_node = Nokogiri::XML::Text.new('hello', xml)
441
+ xml.root << text_node
442
+ assert_match 'hello', xml.to_s
443
+ end
444
+
445
+ def test_set_content_with_symbol
446
+ node = @xml.at('//name')
447
+ node.content = :foo
448
+ assert_equal 'foo', node.content
449
+ end
450
+
451
+ def test_add_previous_sibling
452
+ xml = Nokogiri::XML(<<-eoxml)
453
+ <root>
454
+ <a>Hello world</a>
455
+ </root>
456
+ eoxml
457
+ b_node = Nokogiri::XML::Node.new('a', xml)
458
+ assert_equal Nokogiri::XML::Node::ELEMENT_NODE, b_node.type
459
+ b_node.content = 'first'
460
+ a_node = xml.xpath('//a').first
461
+ a_node.add_previous_sibling(b_node)
462
+ assert_equal('first', xml.xpath('//a').first.text)
463
+ end
464
+
465
+ def test_add_previous_sibling_merge
466
+ xml = Nokogiri::XML(<<-eoxml)
467
+ <root>
468
+ <a>Hello world</a>
469
+ </root>
470
+ eoxml
471
+
472
+ assert a_tag = xml.css('a').first
473
+
474
+ left_space = a_tag.previous
475
+ right_space = a_tag.next
476
+ assert left_space.text?
477
+ assert right_space.text?
478
+
479
+ left_space.add_previous_sibling(right_space)
480
+ assert_equal left_space, right_space
481
+ end
482
+
483
+ def test_add_next_sibling_merge
484
+ xml = Nokogiri::XML(<<-eoxml)
485
+ <root>
486
+ <a>Hello world</a>
487
+ </root>
488
+ eoxml
489
+
490
+ assert a_tag = xml.css('a').first
491
+
492
+ left_space = a_tag.previous
493
+ right_space = a_tag.next
494
+ assert left_space.text?
495
+ assert right_space.text?
496
+
497
+ right_space.add_next_sibling(left_space)
498
+ assert_equal left_space, right_space
499
+ end
500
+
501
+ def test_find_by_css_with_tilde_eql
502
+ xml = Nokogiri::XML.parse(<<-eoxml)
503
+ <root>
504
+ <a>Hello world</a>
505
+ <a class='foo bar'>Bar</a>
506
+ <a class='bar foo'>Bar</a>
507
+ <a class='bar'>Bar</a>
508
+ <a class='baz bar foo'>Bar</a>
509
+ <a class='bazbarfoo'>Awesome</a>
510
+ <a class='bazbar'>Awesome</a>
511
+ </root>
512
+ eoxml
513
+ set = xml.css('a[@class~="bar"]')
514
+ assert_equal 4, set.length
515
+ assert_equal ['Bar'], set.map { |node| node.content }.uniq
516
+ end
517
+
518
+ def test_unlink
519
+ xml = Nokogiri::XML.parse(<<-eoxml)
520
+ <root>
521
+ <a class='foo bar'>Bar</a>
522
+ <a class='bar foo'>Bar</a>
523
+ <a class='bar'>Bar</a>
524
+ <a>Hello world</a>
525
+ <a class='baz bar foo'>Bar</a>
526
+ <a class='bazbarfoo'>Awesome</a>
527
+ <a class='bazbar'>Awesome</a>
528
+ </root>
529
+ eoxml
530
+ node = xml.xpath('//a')[3]
531
+ assert_equal('Hello world', node.text)
532
+ assert_match(/Hello world/, xml.to_s)
533
+ assert node.parent
534
+ assert node.document
535
+ assert node.previous_sibling
536
+ assert node.next_sibling
537
+ node.unlink
538
+ assert !node.parent
539
+ #assert !node.document
540
+ assert !node.previous_sibling
541
+ assert !node.next_sibling
542
+ assert_no_match(/Hello world/, xml.to_s)
543
+ end
544
+
545
+ def test_next_sibling
546
+ assert node = @xml.root
547
+ assert sibling = node.child.next_sibling
548
+ assert_equal('employee', sibling.name)
549
+ end
550
+
551
+ def test_previous_sibling
552
+ assert node = @xml.root
553
+ assert sibling = node.child.next_sibling
554
+ assert_equal('employee', sibling.name)
555
+ assert_equal(sibling.previous_sibling, node.child)
556
+ end
557
+
558
+ def test_name=
559
+ assert node = @xml.root
560
+ node.name = 'awesome'
561
+ assert_equal('awesome', node.name)
562
+ end
563
+
564
+ def test_child
565
+ assert node = @xml.root
566
+ assert child = node.child
567
+ assert_equal('text', child.name)
568
+ end
569
+
570
+ def test_key?
571
+ assert node = @xml.search('//address').first
572
+ assert(!node.key?('asdfasdf'))
573
+ end
574
+
575
+ def test_set_property
576
+ assert node = @xml.search('//address').first
577
+ node['foo'] = 'bar'
578
+ assert_equal('bar', node['foo'])
579
+ end
580
+
581
+ def test_attributes
582
+ assert node = @xml.search('//address').first
583
+ assert_nil(node['asdfasdfasdf'])
584
+ assert_equal('Yes', node['domestic'])
585
+
586
+ assert node = @xml.search('//address')[2]
587
+ attr = node.attributes
588
+ assert_equal 2, attr.size
589
+ assert_equal 'Yes', attr['domestic'].value
590
+ assert_equal 'Yes', attr['domestic'].to_s
591
+ assert_equal 'No', attr['street'].value
592
+ end
593
+
594
+ def test_path
595
+ assert set = @xml.search('//employee')
596
+ assert node = set.first
597
+ assert_equal('/staff/employee[1]', node.path)
598
+ end
599
+
600
+ def test_search_by_symbol
601
+ assert set = @xml.search(:employee)
602
+ assert 5, set.length
603
+
604
+ assert node = @xml.at(:employee)
605
+ assert node.text =~ /EMP0001/
606
+ end
607
+
608
+ def test_new_node
609
+ node = Nokogiri::XML::Node.new('form', @xml)
610
+ assert_equal('form', node.name)
611
+ assert(node.document)
612
+ end
613
+
614
+ def test_encode_special_chars
615
+ foo = @xml.css('employee').first.encode_special_chars('&')
616
+ assert_equal '&amp;', foo
617
+ end
618
+
619
+ def test_content
620
+ node = Nokogiri::XML::Node.new('form', @xml)
621
+ assert_equal('', node.content)
622
+
623
+ node.content = 'hello world!'
624
+ assert_equal('hello world!', node.content)
625
+ end
626
+
627
+ def test_whitespace_nodes
628
+ doc = Nokogiri::XML.parse("<root><b>Foo</b>\n<i>Bar</i> <p>Bazz</p></root>")
629
+ children = doc.at('//root').children.collect{|j| j.to_s}
630
+ assert_equal "\n", children[1]
631
+ assert_equal " ", children[3]
632
+ end
633
+
634
+ def test_replace
635
+ set = @xml.search('//employee')
636
+ assert 5, set.length
637
+ assert 0, @xml.search('//form').length
638
+
639
+ first = set[0]
640
+ second = set[1]
641
+
642
+ node = Nokogiri::XML::Node.new('form', @xml)
643
+ first.replace(node)
644
+
645
+ assert set = @xml.search('//employee')
646
+ assert_equal 4, set.length
647
+ assert 1, @xml.search('//form').length
648
+
649
+ assert_equal set[0].to_xml, second.to_xml
650
+ end
651
+
652
+ def test_illegal_replace_of_node_with_doc
653
+ new_node = Nokogiri::XML.parse('<foo>bar</foo>')
654
+ old_node = @xml.at('//employee')
655
+ assert_raises(ArgumentError){ old_node.replace new_node }
656
+ end
657
+
658
+ def test_node_equality
659
+ doc1 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
660
+ doc2 = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
661
+
662
+ address1_1 = doc1.xpath('//address').first
663
+ address1_2 = doc1.xpath('//address').first
664
+
665
+ address2 = doc2.xpath('//address').first
666
+
667
+ assert_not_equal address1_1, address2 # two references to very, very similar nodes
668
+ assert_equal address1_1, address1_2 # two references to the exact same node
669
+ end
670
+
671
+ def test_namespace_as_hash
672
+ xml = Nokogiri::XML.parse(<<-eoxml)
673
+ <root>
674
+ <car xmlns:part="http://general-motors.com/">
675
+ <part:tire>Michelin Model XGV</part:tire>
676
+ </car>
677
+ <bicycle xmlns:part="http://schwinn.com/">
678
+ <part:tire>I'm a bicycle tire!</part:tire>
679
+ </bicycle>
680
+ </root>
681
+ eoxml
682
+
683
+ tires = xml.xpath('//bike:tire', {'bike' => 'http://schwinn.com/'})
684
+ assert_equal 1, tires.length
685
+ end
686
+
687
+ def test_namespace_search_with_css
688
+ xml = Nokogiri::XML.parse(<<-eoxml)
689
+ <root>
690
+ <car xmlns:part="http://general-motors.com/">
691
+ <part:tire>Michelin Model XGV</part:tire>
692
+ </car>
693
+ <bicycle xmlns:part="http://schwinn.com/">
694
+ <part:tire>I'm a bicycle tire!</part:tire>
695
+ </bicycle>
696
+ </root>
697
+ eoxml
698
+
699
+ tires = xml.css('bike|tire', 'bike' => 'http://schwinn.com/')
700
+ assert_equal 1, tires.length
701
+ end
702
+
703
+ def test_namespaces
704
+ xml = Nokogiri::XML.parse(<<-EOF)
705
+ <x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
706
+ <y xmlns:c='http://bazz.com/'>
707
+ <a:div>hello a</a:div>
708
+ <b:div>hello b</b:div>
709
+ <c:div>hello c</c:div>
710
+ </y>
711
+ </x>
712
+ EOF
713
+ assert namespaces = xml.root.namespaces
714
+ assert namespaces.key?('xmlns:a')
715
+ assert_equal 'http://foo.com/', namespaces['xmlns:a']
716
+ assert namespaces.key?('xmlns:b')
717
+ assert_equal 'http://bar.com/', namespaces['xmlns:b']
718
+ assert ! namespaces.key?('xmlns:c')
719
+
720
+ assert namespaces = xml.namespaces
721
+ assert namespaces.key?('xmlns:a')
722
+ assert_equal 'http://foo.com/', namespaces['xmlns:a']
723
+ assert namespaces.key?('xmlns:b')
724
+ assert_equal 'http://bar.com/', namespaces['xmlns:b']
725
+ assert namespaces.key?('xmlns:c')
726
+ assert_equal 'http://bazz.com/', namespaces['xmlns:c']
727
+
728
+ assert_equal "hello a", xml.search("//a:div", xml.namespaces).first.inner_text
729
+ assert_equal "hello b", xml.search("//b:div", xml.namespaces).first.inner_text
730
+ assert_equal "hello c", xml.search("//c:div", xml.namespaces).first.inner_text
731
+ end
732
+
733
+ def test_namespace
734
+ xml = Nokogiri::XML.parse(<<-EOF)
735
+ <x xmlns:a='http://foo.com/' xmlns:b='http://bar.com/'>
736
+ <y xmlns:c='http://bazz.com/'>
737
+ <a:div>hello a</a:div>
738
+ <b:div>hello b</b:div>
739
+ <c:div>hello c</c:div>
740
+ <div>hello moon</div>
741
+ </y>
742
+ </x>
743
+ EOF
744
+ set = xml.search("//y/*")
745
+ assert_equal "a", set[0].namespace.prefix
746
+ assert_equal "b", set[1].namespace.prefix
747
+ assert_equal "c", set[2].namespace.prefix
748
+ assert_equal nil, set[3].namespace
749
+ end
750
+
751
+ def test_namespace_without_an_href_on_html_node
752
+ # because microsoft word's HTML formatting does this. ick.
753
+ xml = Nokogiri::HTML.parse <<-EOF
754
+ <div><o:p>foo</o:p></div>
755
+ EOF
756
+
757
+ assert_not_nil(node = xml.at('p'))
758
+
759
+ assert_equal 1, node.namespaces.keys.size
760
+ assert node.namespaces.has_key?('xmlns:o')
761
+ assert_equal nil, node.namespaces['xmlns:o']
762
+ end
763
+
764
+ def test_line
765
+ xml = Nokogiri::XML(<<-eoxml)
766
+ <root>
767
+ <a>
768
+ Hello world
769
+ </a>
770
+ </root>
771
+ eoxml
772
+
773
+ set = xml.search("//a")
774
+ node = set.first
775
+ assert_equal 2, node.line
776
+ end
777
+
778
+ def test_unlink_then_reparent
779
+ # see http://github.com/tenderlove/nokogiri/issues#issue/22
780
+ 10.times do
781
+ STDOUT.putc "."
782
+ STDOUT.flush
783
+ begin
784
+ doc = Nokogiri::XML <<-EOHTML
785
+ <root>
786
+ <a>
787
+ <b/>
788
+ <c/>
789
+ </a>
790
+ </root>
791
+ EOHTML
792
+
793
+ root = doc.at("root")
794
+ a = root.at("a")
795
+ b = a.at("b")
796
+ c = a.at("c")
797
+ a.add_next_sibling(b.unlink)
798
+ c.unlink
799
+ end
800
+
801
+ GC.start
802
+ end
803
+
804
+ end
805
+
806
+ end
807
+ end
808
+ end