glebm-nokogiri 1.4.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (277) hide show
  1. data/.autotest +26 -0
  2. data/CHANGELOG.ja.rdoc +411 -0
  3. data/CHANGELOG.rdoc +397 -0
  4. data/Manifest.txt +276 -0
  5. data/README.ja.rdoc +106 -0
  6. data/README.rdoc +132 -0
  7. data/Rakefile +183 -0
  8. data/bin/nokogiri +49 -0
  9. data/deps.rip +5 -0
  10. data/ext/nokogiri/extconf.rb +97 -0
  11. data/ext/nokogiri/html_document.c +154 -0
  12. data/ext/nokogiri/html_document.h +10 -0
  13. data/ext/nokogiri/html_element_description.c +276 -0
  14. data/ext/nokogiri/html_element_description.h +10 -0
  15. data/ext/nokogiri/html_entity_lookup.c +32 -0
  16. data/ext/nokogiri/html_entity_lookup.h +8 -0
  17. data/ext/nokogiri/html_sax_parser_context.c +94 -0
  18. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  19. data/ext/nokogiri/nokogiri.c +95 -0
  20. data/ext/nokogiri/nokogiri.h +153 -0
  21. data/ext/nokogiri/xml_attr.c +94 -0
  22. data/ext/nokogiri/xml_attr.h +9 -0
  23. data/ext/nokogiri/xml_attribute_decl.c +70 -0
  24. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  25. data/ext/nokogiri/xml_cdata.c +56 -0
  26. data/ext/nokogiri/xml_cdata.h +9 -0
  27. data/ext/nokogiri/xml_comment.c +54 -0
  28. data/ext/nokogiri/xml_comment.h +9 -0
  29. data/ext/nokogiri/xml_document.c +464 -0
  30. data/ext/nokogiri/xml_document.h +23 -0
  31. data/ext/nokogiri/xml_document_fragment.c +48 -0
  32. data/ext/nokogiri/xml_document_fragment.h +10 -0
  33. data/ext/nokogiri/xml_dtd.c +202 -0
  34. data/ext/nokogiri/xml_dtd.h +10 -0
  35. data/ext/nokogiri/xml_element_content.c +123 -0
  36. data/ext/nokogiri/xml_element_content.h +10 -0
  37. data/ext/nokogiri/xml_element_decl.c +69 -0
  38. data/ext/nokogiri/xml_element_decl.h +9 -0
  39. data/ext/nokogiri/xml_encoding_handler.c +79 -0
  40. data/ext/nokogiri/xml_encoding_handler.h +8 -0
  41. data/ext/nokogiri/xml_entity_decl.c +110 -0
  42. data/ext/nokogiri/xml_entity_decl.h +10 -0
  43. data/ext/nokogiri/xml_entity_reference.c +52 -0
  44. data/ext/nokogiri/xml_entity_reference.h +9 -0
  45. data/ext/nokogiri/xml_io.c +31 -0
  46. data/ext/nokogiri/xml_io.h +11 -0
  47. data/ext/nokogiri/xml_namespace.c +84 -0
  48. data/ext/nokogiri/xml_namespace.h +13 -0
  49. data/ext/nokogiri/xml_node.c +1347 -0
  50. data/ext/nokogiri/xml_node.h +13 -0
  51. data/ext/nokogiri/xml_node_set.c +418 -0
  52. data/ext/nokogiri/xml_node_set.h +9 -0
  53. data/ext/nokogiri/xml_processing_instruction.c +56 -0
  54. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  55. data/ext/nokogiri/xml_reader.c +665 -0
  56. data/ext/nokogiri/xml_reader.h +10 -0
  57. data/ext/nokogiri/xml_relax_ng.c +168 -0
  58. data/ext/nokogiri/xml_relax_ng.h +9 -0
  59. data/ext/nokogiri/xml_sax_parser.c +286 -0
  60. data/ext/nokogiri/xml_sax_parser.h +39 -0
  61. data/ext/nokogiri/xml_sax_parser_context.c +159 -0
  62. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  63. data/ext/nokogiri/xml_sax_push_parser.c +115 -0
  64. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  65. data/ext/nokogiri/xml_schema.c +205 -0
  66. data/ext/nokogiri/xml_schema.h +9 -0
  67. data/ext/nokogiri/xml_syntax_error.c +58 -0
  68. data/ext/nokogiri/xml_syntax_error.h +13 -0
  69. data/ext/nokogiri/xml_text.c +50 -0
  70. data/ext/nokogiri/xml_text.h +9 -0
  71. data/ext/nokogiri/xml_xpath_context.c +276 -0
  72. data/ext/nokogiri/xml_xpath_context.h +9 -0
  73. data/ext/nokogiri/xslt_stylesheet.c +142 -0
  74. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  75. data/lib/nokogiri.rb +133 -0
  76. data/lib/nokogiri/css.rb +25 -0
  77. data/lib/nokogiri/css/generated_parser.rb +669 -0
  78. data/lib/nokogiri/css/generated_tokenizer.rb +145 -0
  79. data/lib/nokogiri/css/node.rb +99 -0
  80. data/lib/nokogiri/css/parser.rb +82 -0
  81. data/lib/nokogiri/css/parser.y +232 -0
  82. data/lib/nokogiri/css/syntax_error.rb +7 -0
  83. data/lib/nokogiri/css/tokenizer.rb +7 -0
  84. data/lib/nokogiri/css/tokenizer.rex +55 -0
  85. data/lib/nokogiri/css/xpath_visitor.rb +169 -0
  86. data/lib/nokogiri/decorators/slop.rb +33 -0
  87. data/lib/nokogiri/ffi/encoding_handler.rb +42 -0
  88. data/lib/nokogiri/ffi/html/document.rb +28 -0
  89. data/lib/nokogiri/ffi/html/element_description.rb +81 -0
  90. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  91. data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
  92. data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
  93. data/lib/nokogiri/ffi/libxml.rb +386 -0
  94. data/lib/nokogiri/ffi/structs/common_node.rb +38 -0
  95. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  96. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  97. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  98. data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
  99. data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
  100. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  101. data/lib/nokogiri/ffi/structs/xml_char_encoding_handler.rb +11 -0
  102. data/lib/nokogiri/ffi/structs/xml_document.rb +117 -0
  103. data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
  104. data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
  105. data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
  106. data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
  107. data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
  108. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  109. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  110. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  111. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  112. data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
  113. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  114. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  115. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +124 -0
  116. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  117. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  118. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  119. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +38 -0
  120. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  121. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  122. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  123. data/lib/nokogiri/ffi/weak_bucket.rb +40 -0
  124. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  125. data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
  126. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  127. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  128. data/lib/nokogiri/ffi/xml/document.rb +162 -0
  129. data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
  130. data/lib/nokogiri/ffi/xml/dtd.rb +67 -0
  131. data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
  132. data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
  133. data/lib/nokogiri/ffi/xml/entity_decl.rb +36 -0
  134. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  135. data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
  136. data/lib/nokogiri/ffi/xml/node.rb +556 -0
  137. data/lib/nokogiri/ffi/xml/node_set.rb +149 -0
  138. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  139. data/lib/nokogiri/ffi/xml/reader.rb +232 -0
  140. data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
  141. data/lib/nokogiri/ffi/xml/sax/parser.rb +135 -0
  142. data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
  143. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +51 -0
  144. data/lib/nokogiri/ffi/xml/schema.rb +109 -0
  145. data/lib/nokogiri/ffi/xml/syntax_error.rb +98 -0
  146. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  147. data/lib/nokogiri/ffi/xml/xpath.rb +9 -0
  148. data/lib/nokogiri/ffi/xml/xpath_context.rb +148 -0
  149. data/lib/nokogiri/ffi/xslt/stylesheet.rb +53 -0
  150. data/lib/nokogiri/html.rb +35 -0
  151. data/lib/nokogiri/html/builder.rb +35 -0
  152. data/lib/nokogiri/html/document.rb +90 -0
  153. data/lib/nokogiri/html/document_fragment.rb +36 -0
  154. data/lib/nokogiri/html/element_description.rb +23 -0
  155. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  156. data/lib/nokogiri/html/sax/parser.rb +48 -0
  157. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  158. data/lib/nokogiri/syntax_error.rb +4 -0
  159. data/lib/nokogiri/version.rb +37 -0
  160. data/lib/nokogiri/version_warning.rb +14 -0
  161. data/lib/nokogiri/xml.rb +67 -0
  162. data/lib/nokogiri/xml/attr.rb +14 -0
  163. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  164. data/lib/nokogiri/xml/builder.rb +418 -0
  165. data/lib/nokogiri/xml/cdata.rb +11 -0
  166. data/lib/nokogiri/xml/character_data.rb +7 -0
  167. data/lib/nokogiri/xml/document.rb +194 -0
  168. data/lib/nokogiri/xml/document_fragment.rb +77 -0
  169. data/lib/nokogiri/xml/dtd.rb +11 -0
  170. data/lib/nokogiri/xml/element_content.rb +36 -0
  171. data/lib/nokogiri/xml/element_decl.rb +13 -0
  172. data/lib/nokogiri/xml/entity_decl.rb +19 -0
  173. data/lib/nokogiri/xml/namespace.rb +13 -0
  174. data/lib/nokogiri/xml/node.rb +793 -0
  175. data/lib/nokogiri/xml/node/save_options.rb +42 -0
  176. data/lib/nokogiri/xml/node_set.rb +325 -0
  177. data/lib/nokogiri/xml/notation.rb +6 -0
  178. data/lib/nokogiri/xml/parse_options.rb +85 -0
  179. data/lib/nokogiri/xml/pp.rb +2 -0
  180. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  181. data/lib/nokogiri/xml/pp/node.rb +56 -0
  182. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  183. data/lib/nokogiri/xml/reader.rb +74 -0
  184. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  185. data/lib/nokogiri/xml/sax.rb +4 -0
  186. data/lib/nokogiri/xml/sax/document.rb +160 -0
  187. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  188. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  189. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  190. data/lib/nokogiri/xml/schema.rb +57 -0
  191. data/lib/nokogiri/xml/syntax_error.rb +47 -0
  192. data/lib/nokogiri/xml/text.rb +9 -0
  193. data/lib/nokogiri/xml/xpath.rb +10 -0
  194. data/lib/nokogiri/xml/xpath/syntax_error.rb +11 -0
  195. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  196. data/lib/nokogiri/xslt.rb +48 -0
  197. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  198. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  199. data/tasks/cross_compile.rb +158 -0
  200. data/tasks/test.rb +94 -0
  201. data/test/css/test_nthiness.rb +159 -0
  202. data/test/css/test_parser.rb +282 -0
  203. data/test/css/test_tokenizer.rb +190 -0
  204. data/test/css/test_xpath_visitor.rb +85 -0
  205. data/test/ffi/test_document.rb +35 -0
  206. data/test/files/2ch.html +108 -0
  207. data/test/files/address_book.rlx +12 -0
  208. data/test/files/address_book.xml +10 -0
  209. data/test/files/bar/bar.xsd +4 -0
  210. data/test/files/dont_hurt_em_why.xml +422 -0
  211. data/test/files/exslt.xml +8 -0
  212. data/test/files/exslt.xslt +35 -0
  213. data/test/files/foo/foo.xsd +4 -0
  214. data/test/files/po.xml +32 -0
  215. data/test/files/po.xsd +66 -0
  216. data/test/files/shift_jis.html +10 -0
  217. data/test/files/shift_jis.xml +5 -0
  218. data/test/files/snuggles.xml +3 -0
  219. data/test/files/staff.dtd +10 -0
  220. data/test/files/staff.xml +59 -0
  221. data/test/files/staff.xslt +32 -0
  222. data/test/files/tlm.html +850 -0
  223. data/test/files/valid_bar.xml +2 -0
  224. data/test/helper.rb +169 -0
  225. data/test/html/sax/test_parser.rb +74 -0
  226. data/test/html/sax/test_parser_context.rb +48 -0
  227. data/test/html/test_builder.rb +164 -0
  228. data/test/html/test_document.rb +398 -0
  229. data/test/html/test_document_encoding.rb +77 -0
  230. data/test/html/test_document_fragment.rb +182 -0
  231. data/test/html/test_element_description.rb +98 -0
  232. data/test/html/test_named_characters.rb +14 -0
  233. data/test/html/test_node.rb +181 -0
  234. data/test/html/test_node_encoding.rb +27 -0
  235. data/test/test_convert_xpath.rb +135 -0
  236. data/test/test_css_cache.rb +45 -0
  237. data/test/test_encoding_handler.rb +46 -0
  238. data/test/test_memory_leak.rb +87 -0
  239. data/test/test_nokogiri.rb +138 -0
  240. data/test/test_reader.rb +386 -0
  241. data/test/test_soap4r_sax.rb +52 -0
  242. data/test/test_xslt_transforms.rb +188 -0
  243. data/test/xml/node/test_save_options.rb +20 -0
  244. data/test/xml/node/test_subclass.rb +44 -0
  245. data/test/xml/sax/test_parser.rb +307 -0
  246. data/test/xml/sax/test_parser_context.rb +63 -0
  247. data/test/xml/sax/test_push_parser.rb +139 -0
  248. data/test/xml/test_attr.rb +38 -0
  249. data/test/xml/test_attribute_decl.rb +82 -0
  250. data/test/xml/test_builder.rb +210 -0
  251. data/test/xml/test_cdata.rb +50 -0
  252. data/test/xml/test_comment.rb +29 -0
  253. data/test/xml/test_document.rb +668 -0
  254. data/test/xml/test_document_encoding.rb +26 -0
  255. data/test/xml/test_document_fragment.rb +180 -0
  256. data/test/xml/test_dtd.rb +82 -0
  257. data/test/xml/test_dtd_encoding.rb +33 -0
  258. data/test/xml/test_element_content.rb +56 -0
  259. data/test/xml/test_element_decl.rb +73 -0
  260. data/test/xml/test_entity_decl.rb +120 -0
  261. data/test/xml/test_entity_reference.rb +21 -0
  262. data/test/xml/test_namespace.rb +68 -0
  263. data/test/xml/test_node.rb +865 -0
  264. data/test/xml/test_node_attributes.rb +34 -0
  265. data/test/xml/test_node_encoding.rb +107 -0
  266. data/test/xml/test_node_reparenting.rb +293 -0
  267. data/test/xml/test_node_set.rb +649 -0
  268. data/test/xml/test_parse_options.rb +52 -0
  269. data/test/xml/test_processing_instruction.rb +30 -0
  270. data/test/xml/test_reader_encoding.rb +126 -0
  271. data/test/xml/test_relax_ng.rb +60 -0
  272. data/test/xml/test_schema.rb +89 -0
  273. data/test/xml/test_syntax_error.rb +12 -0
  274. data/test/xml/test_text.rb +38 -0
  275. data/test/xml/test_unparented_node.rb +381 -0
  276. data/test/xml/test_xpath.rb +138 -0
  277. metadata +533 -0
@@ -0,0 +1,19 @@
1
+ # :stopdoc:
2
+ module Nokogiri
3
+ module XML
4
+ class ElementDecl < Nokogiri::XML::Node
5
+ def element_type
6
+ cstruct[:etype]
7
+ end
8
+
9
+ def prefix
10
+ cstruct[:prefix]
11
+ end
12
+
13
+ def content
14
+ ElementContent.wrap cstruct[:content], document
15
+ end
16
+ end
17
+ end
18
+ end
19
+ # :startdoc:
@@ -0,0 +1,36 @@
1
+ # :stopdoc:
2
+ module Nokogiri
3
+ module XML
4
+ class EntityDecl < Nokogiri::XML::Node
5
+
6
+ # from libxml/entities.h
7
+ INTERNAL_GENERAL = 1
8
+ EXTERNAL_GENERAL_PARSED = 2
9
+ EXTERNAL_GENERAL_UNPARSED = 3
10
+ INTERNAL_PARAMETER = 4
11
+ EXTERNAL_PARAMETER = 5
12
+ INTERNAL_PREDEFINED = 6
13
+
14
+ def content
15
+ cstruct[:content]
16
+ end
17
+
18
+ def entity_type
19
+ cstruct[:etype]
20
+ end
21
+
22
+ def external_id
23
+ cstruct[:external_id]
24
+ end
25
+
26
+ def system_id
27
+ cstruct[:system_id]
28
+ end
29
+
30
+ def original_content
31
+ cstruct[:orig]
32
+ end
33
+ end
34
+ end
35
+ end
36
+ # :startdoc:
@@ -0,0 +1,19 @@
1
+ module Nokogiri
2
+ module XML
3
+ class EntityReference < Node
4
+
5
+ def self.new(document, name, *rest) # :nodoc:
6
+ node_ptr = LibXML.xmlNewReference(document.cstruct, name)
7
+ node_cstruct = LibXML::XmlNode.new(node_ptr)
8
+ node_cstruct.keep_reference_from_document!
9
+
10
+ node = Node.wrap(node_cstruct, self)
11
+ node.send :initialize, document, name, *rest
12
+ yield node if block_given?
13
+ node
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,44 @@
1
+ module Nokogiri
2
+ module XML
3
+ class Namespace
4
+ # :stopdoc:
5
+
6
+ attr_accessor :cstruct
7
+ attr_accessor :document
8
+
9
+ def prefix
10
+ cstruct[:prefix].nil? ? nil : cstruct[:prefix] # TODO: encoding?
11
+ end
12
+
13
+ def href
14
+ cstruct[:href].nil? ? nil : cstruct[:href] # TODO: encoding?
15
+ end
16
+
17
+ class << self
18
+ def wrap(document, node_struct)
19
+ if node_struct.is_a?(FFI::Pointer)
20
+ # cast native pointers up into a node cstruct
21
+ return nil if node_struct.null?
22
+ node_struct = LibXML::XmlNs.new(node_struct)
23
+ end
24
+
25
+ ruby_node = node_struct.ruby_node
26
+ return ruby_node unless ruby_node.nil?
27
+
28
+ ns = Nokogiri::XML::Namespace.allocate
29
+ ns.document = document.ruby_doc
30
+ ns.cstruct = node_struct
31
+
32
+ ns.cstruct.ruby_node = ns
33
+
34
+ cache = ns.document.instance_variable_get(:@node_cache)
35
+ cache << ns
36
+
37
+ ns
38
+ end
39
+ end
40
+
41
+ # :startdoc:
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,556 @@
1
+ module Nokogiri
2
+ module XML
3
+ class Node
4
+ # :stopdoc:
5
+
6
+ attr_accessor :cstruct
7
+
8
+ def pointer_id
9
+ cstruct.pointer
10
+ end
11
+
12
+ def encode_special_chars(string)
13
+ char_ptr = LibXML.xmlEncodeSpecialChars(self[:doc], string)
14
+ encoded = char_ptr.read_string
15
+ # TODO: encoding?
16
+ LibXML.xmlFree(char_ptr)
17
+ encoded
18
+ end
19
+
20
+ def internal_subset
21
+ doc = cstruct.document
22
+ dtd = LibXML.xmlGetIntSubset(doc)
23
+ return nil if dtd.null?
24
+ Node.wrap(dtd)
25
+ end
26
+
27
+ def external_subset
28
+ doc = cstruct.document
29
+ return nil if doc[:extSubset].null?
30
+
31
+ Node.wrap(doc[:extSubset])
32
+ end
33
+
34
+ def create_internal_subset name, external_id, system_id
35
+ raise("Document already has an internal subset") if internal_subset
36
+
37
+ doc = cstruct.document
38
+ dtd_ptr = LibXML.xmlCreateIntSubset doc, name, external_id, system_id
39
+
40
+ return nil if dtd_ptr.null?
41
+
42
+ Node.wrap dtd_ptr
43
+ end
44
+
45
+ def create_external_subset name, external_id, system_id
46
+ raise("Document already has an external subset") if external_subset
47
+
48
+ doc = cstruct.document
49
+ dtd_ptr = LibXML.xmlNewDtd doc, name, external_id, system_id
50
+
51
+ return nil if dtd_ptr.null?
52
+
53
+ Node.wrap dtd_ptr
54
+ end
55
+
56
+ def dup(deep = 1)
57
+ dup_ptr = LibXML.xmlDocCopyNode(cstruct, cstruct.document, deep)
58
+ return nil if dup_ptr.null?
59
+ Node.wrap(dup_ptr, self.class)
60
+ end
61
+
62
+ def unlink
63
+ LibXML.xmlUnlinkNode(cstruct)
64
+ cstruct.keep_reference_from_document!
65
+ self
66
+ end
67
+
68
+ def blank?
69
+ LibXML.xmlIsBlankNode(cstruct) == 1
70
+ end
71
+
72
+ def next_sibling
73
+ cstruct_node_from :next
74
+ end
75
+
76
+ def previous_sibling
77
+ cstruct_node_from :prev
78
+ end
79
+
80
+ def next_element
81
+ sibling_ptr = LibXML.xmlNextElementSibling cstruct
82
+ sibling_ptr.null? ? nil : Node.wrap(sibling_ptr)
83
+ end
84
+
85
+ def previous_element
86
+ #
87
+ # note that we don't use xmlPreviousElementSibling here because it's buggy pre-2.7.7.
88
+ #
89
+ sibling_ptr = cstruct[:prev]
90
+
91
+ while ! sibling_ptr.null?
92
+ sibling_cstruct = LibXML::XmlNode.new(sibling_ptr)
93
+ break if sibling_cstruct[:type] == ELEMENT_NODE
94
+ sibling_ptr = sibling_cstruct[:prev]
95
+ end
96
+
97
+ return sibling_ptr.null? ? nil : Node.wrap(sibling_ptr)
98
+ end
99
+
100
+ def replace_node new_node
101
+ Node.reparent_node_with(self, new_node) do |pivot_struct, reparentee_struct|
102
+ retval = LibXML.xmlReplaceNode(pivot_struct, reparentee_struct)
103
+ retval = reparentee_struct if retval == pivot_struct.pointer # for reparent_node_with semantics
104
+ retval = LibXML::XmlNode.new(retval) if retval.is_a?(FFI::Pointer)
105
+ if retval[:type] == TEXT_NODE
106
+ if !retval[:prev].null? && LibXML::XmlNode.new(retval[:prev])[:type] == TEXT_NODE
107
+ retval = LibXML::XmlNode.new(LibXML.xmlTextMerge(retval[:prev], retval))
108
+ end
109
+ if !retval[:next].null? && LibXML::XmlNode.new(retval[:next])[:type] == TEXT_NODE
110
+ retval = LibXML::XmlNode.new(LibXML.xmlTextMerge(retval, retval[:next]))
111
+ end
112
+ end
113
+ retval
114
+ end
115
+ end
116
+
117
+ def children
118
+ return NodeSet.new(nil) if cstruct[:children].null?
119
+ child = Node.wrap(cstruct[:children])
120
+
121
+ set = NodeSet.wrap(LibXML.xmlXPathNodeSetCreate(child.cstruct), self.document)
122
+ return set unless child
123
+
124
+ child_ptr = child.cstruct[:next]
125
+ while ! child_ptr.null?
126
+ child = Node.wrap(child_ptr)
127
+ LibXML.xmlXPathNodeSetAddUnique(set.cstruct, child.cstruct)
128
+ child_ptr = child.cstruct[:next]
129
+ end
130
+
131
+ return set
132
+ end
133
+
134
+ def element_children
135
+ child = LibXML.xmlFirstElementChild(cstruct)
136
+ return NodeSet.new(nil) if child.null?
137
+ child = Node.wrap(child)
138
+
139
+ set = NodeSet.wrap(LibXML.xmlXPathNodeSetCreate(child.cstruct), self.document)
140
+ return set unless child
141
+
142
+ next_sibling = LibXML.xmlNextElementSibling(child.cstruct)
143
+ while ! next_sibling.null?
144
+ child = Node.wrap(next_sibling)
145
+ LibXML.xmlXPathNodeSetAddUnique(set.cstruct, child.cstruct)
146
+ next_sibling = LibXML.xmlNextElementSibling(child.cstruct)
147
+ end
148
+
149
+ return set
150
+ end
151
+
152
+ def child
153
+ (val = cstruct[:children]).null? ? nil : Node.wrap(val)
154
+ end
155
+
156
+ def first_element_child
157
+ element_child = LibXML.xmlFirstElementChild(cstruct)
158
+ element_child.null? ? nil : Node.wrap(element_child)
159
+ end
160
+
161
+ def last_element_child
162
+ element_child = LibXML.xmlLastElementChild(cstruct)
163
+ element_child.null? ? nil : Node.wrap(element_child)
164
+ end
165
+
166
+ def key?(attribute)
167
+ ! (prop = LibXML.xmlHasProp(cstruct, attribute.to_s)).null?
168
+ end
169
+
170
+ def namespaced_key?(attribute, namespace)
171
+ prop = LibXML.xmlHasNsProp(cstruct, attribute.to_s,
172
+ namespace.nil? ? nil : namespace.to_s)
173
+ !prop.null?
174
+ end
175
+
176
+ def []=(property, value)
177
+ LibXML.xmlSetProp(cstruct, property, value)
178
+ value
179
+ end
180
+
181
+ def get(attribute)
182
+ return nil unless attribute
183
+ propstr = LibXML.xmlGetProp(cstruct, attribute.to_s)
184
+ return nil if propstr.null?
185
+ rval = propstr.read_string # TODO: encoding?
186
+ LibXML.xmlFree(propstr)
187
+ rval
188
+ end
189
+
190
+ def set_namespace(namespace)
191
+ LibXML.xmlSetNs(cstruct, namespace ? namespace.cstruct : nil)
192
+ self
193
+ end
194
+
195
+ def attribute(name)
196
+ attribute_nodes.find { |x| x.name == name }
197
+ end
198
+
199
+ def attribute_with_ns(name, namespace)
200
+ prop = LibXML.xmlHasNsProp(cstruct, name.to_s,
201
+ namespace.nil? ? NULL : namespace.to_s)
202
+ return prop if prop.null?
203
+ Node.wrap(prop)
204
+ end
205
+
206
+ def attribute_nodes
207
+ Node.node_properties cstruct
208
+ end
209
+
210
+ def namespace
211
+ cstruct[:ns].null? ? nil : Namespace.wrap(cstruct.document, cstruct[:ns])
212
+ end
213
+
214
+ def namespace_definitions
215
+ list = []
216
+ ns_ptr = cstruct[:nsDef]
217
+ return list if ns_ptr.null?
218
+ while ! ns_ptr.null?
219
+ ns = Namespace.wrap(cstruct.document, ns_ptr)
220
+ list << ns
221
+ ns_ptr = ns.cstruct[:next]
222
+ end
223
+ list
224
+ end
225
+
226
+ def namespace_scopes
227
+ ns_list = LibXML.xmlGetNsList(self.cstruct[:doc], self.cstruct)
228
+ return [] if ns_list.null?
229
+
230
+ list = []
231
+ until (ns_ptr = ns_list.get_pointer(LibXML.pointer_offset(list.length))).null?
232
+ list << Namespace.wrap(cstruct.document, ns_ptr)
233
+ end
234
+
235
+ LibXML.xmlFree(ns_list)
236
+ list
237
+ end
238
+
239
+ def node_type
240
+ cstruct[:type]
241
+ end
242
+
243
+ def native_content=(content)
244
+ child_ptr = cstruct[:children]
245
+ while ! child_ptr.null?
246
+ child = Node.wrap(child_ptr)
247
+ next_ptr = child.cstruct[:next]
248
+ LibXML.xmlUnlinkNode(child.cstruct)
249
+ cstruct.keep_reference_from_document!
250
+ child_ptr = next_ptr
251
+ end
252
+ LibXML.xmlNodeSetContent(cstruct, content)
253
+ content
254
+ end
255
+
256
+ def content
257
+ content_ptr = LibXML.xmlNodeGetContent(cstruct)
258
+ return nil if content_ptr.null?
259
+ content = content_ptr.read_string # TODO: encoding?
260
+ LibXML.xmlFree(content_ptr)
261
+ content
262
+ end
263
+
264
+ def add_child_node child
265
+ Node.reparent_node_with(self, child) do |pivot_struct, reparentee_struct|
266
+ LibXML.xmlAddChild(pivot_struct, reparentee_struct)
267
+ end
268
+ end
269
+
270
+ def parent
271
+ cstruct_node_from :parent
272
+ end
273
+
274
+ def node_name=(string)
275
+ LibXML.xmlNodeSetName(cstruct, string)
276
+ string
277
+ end
278
+
279
+ def node_name
280
+ cstruct[:name] # TODO: encoding?
281
+ end
282
+
283
+ def path
284
+ path_ptr = LibXML.xmlGetNodePath(cstruct)
285
+ val = path_ptr.null? ? nil : path_ptr.read_string # TODO: encoding?
286
+ LibXML.xmlFree(path_ptr)
287
+ val
288
+ end
289
+
290
+ def add_next_sibling_node next_sibling
291
+ Node.reparent_node_with(self, next_sibling) do |pivot_struct, reparentee_struct|
292
+ LibXML.xmlAddNextSibling(pivot_struct, reparentee_struct)
293
+ end
294
+ end
295
+
296
+ def add_previous_sibling_node prev_sibling
297
+ Node.reparent_node_with(self, prev_sibling) do |pivot_struct, reparentee_struct|
298
+ LibXML.xmlAddPrevSibling(pivot_struct, reparentee_struct)
299
+ end
300
+ end
301
+
302
+ def native_write_to(io, encoding, indent_string, options)
303
+ set_xml_indent_tree_output 1
304
+ set_xml_tree_indent_string indent_string
305
+ savectx = LibXML.xmlSaveToIO(IoCallbacks.writer(io), nil, nil, encoding, options)
306
+ LibXML.xmlSaveTree(savectx, cstruct)
307
+ LibXML.xmlSaveClose(savectx)
308
+ io
309
+ end
310
+
311
+ def line
312
+ cstruct[:line]
313
+ end
314
+
315
+ def add_namespace_definition(prefix, href)
316
+ ns = LibXML.xmlNewNs(cstruct, href, prefix)
317
+ if ns.null?
318
+ ns = LibXML.xmlSearchNs(cstruct.document, cstruct,
319
+ prefix.nil? ? nil : prefix.to_s)
320
+ end
321
+ LibXML.xmlSetNs(cstruct, ns) if prefix.nil?
322
+ Namespace.wrap(cstruct.document, ns)
323
+ end
324
+
325
+ def self.new(name, doc, *rest)
326
+ ptr = LibXML.xmlNewNode(nil, name.to_s)
327
+
328
+ node_cstruct = LibXML::XmlNode.new(ptr)
329
+ node_cstruct[:doc] = doc.cstruct[:doc]
330
+ node_cstruct.keep_reference_from_document!
331
+
332
+ node = Node.wrap(
333
+ node_cstruct,
334
+ Node == self ? nil : self
335
+ )
336
+ node.send :initialize, name, doc, *rest
337
+ yield node if block_given?
338
+ node
339
+ end
340
+
341
+ def dump_html
342
+ return to_xml if type == DOCUMENT_NODE
343
+ buffer = LibXML::XmlBuffer.new(LibXML.xmlBufferCreate())
344
+ LibXML.htmlNodeDump(buffer, cstruct[:doc], cstruct)
345
+ buffer[:content] # TODO: encoding?
346
+ end
347
+
348
+ def compare(other)
349
+ LibXML.xmlXPathCmpNodes(other.cstruct, self.cstruct)
350
+ end
351
+
352
+ def self.wrap(node_struct, klass=nil)
353
+ if node_struct.is_a?(FFI::Pointer)
354
+ # cast native pointers up into a node cstruct
355
+ return nil if node_struct.null?
356
+ node_struct = LibXML::XmlNode.new(node_struct)
357
+ end
358
+
359
+ raise "wrapping a node without a document" unless node_struct.document
360
+
361
+ document_struct = node_struct.document
362
+ document_obj = document_struct.nil? ? nil : document_struct.ruby_doc
363
+ if node_struct[:type] == DOCUMENT_NODE || node_struct[:type] == HTML_DOCUMENT_NODE
364
+ return document_obj
365
+ end
366
+
367
+ ruby_node = node_struct.ruby_node
368
+ return ruby_node unless ruby_node.nil?
369
+
370
+ klasses = case node_struct[:type]
371
+ when ELEMENT_NODE then [XML::Element]
372
+ when TEXT_NODE then [XML::Text]
373
+ when ENTITY_REF_NODE then [XML::EntityReference]
374
+ when ATTRIBUTE_DECL then [XML::AttributeDecl, LibXML::XmlAttribute]
375
+ when ELEMENT_DECL then [XML::ElementDecl, LibXML::XmlElement]
376
+ when COMMENT_NODE then [XML::Comment]
377
+ when DOCUMENT_FRAG_NODE then [XML::DocumentFragment]
378
+ when PI_NODE then [XML::ProcessingInstruction]
379
+ when ATTRIBUTE_NODE then [XML::Attr]
380
+ when ENTITY_DECL then [XML::EntityDecl, LibXML::XmlEntity]
381
+ when CDATA_SECTION_NODE then [XML::CDATA]
382
+ when DTD_NODE then [XML::DTD, LibXML::XmlDtd]
383
+ else [XML::Node]
384
+ end
385
+
386
+ if klass
387
+ node = klass.allocate
388
+ else
389
+ node = klasses.first.allocate
390
+ end
391
+ node.cstruct = klasses[1] ? klasses[1].new(node_struct.pointer) : node_struct
392
+
393
+ node.cstruct.ruby_node = node
394
+
395
+ if document_obj
396
+ node.instance_variable_set(:@document, document_obj)
397
+ cache = document_obj.instance_variable_get(:@node_cache)
398
+ cache << node
399
+ document_obj.decorate(node)
400
+ end
401
+
402
+ node
403
+ end
404
+
405
+ def document
406
+ cstruct.document.ruby_doc
407
+ end
408
+
409
+ def in_context(string, options)
410
+ raise RuntimeError, "no contextual parsing on unlinked nodes" if parent.nil?
411
+
412
+ @errors = []
413
+ LibXML.xmlSetStructuredErrorFunc(nil, SyntaxError.error_array_pusher(@errors))
414
+ LibXML.htmlHandleOmittedElem(0)
415
+
416
+ list_memory = FFI::MemoryPointer.new :pointer
417
+ LibXML.xmlParseInNodeContext(cstruct, string, string.length, options, list_memory)
418
+
419
+ LibXML.htmlHandleOmittedElem(1)
420
+ LibXML.xmlSetStructuredErrorFunc(nil, nil)
421
+
422
+ set = NodeSet.wrap(LibXML.xmlXPathNodeSetCreate(nil), document)
423
+ list_ptr = list_memory.get_pointer(0)
424
+ while ! list_ptr.null?
425
+ list = Node.wrap(list_ptr)
426
+ LibXML.xmlXPathNodeSetAddUnique(set.cstruct, list.cstruct)
427
+ list_ptr = list.cstruct[:next]
428
+ end
429
+ set
430
+ end
431
+
432
+ class << self
433
+ def node_properties(cstruct)
434
+ attr = []
435
+ prop_cstruct = cstruct[:properties]
436
+ while ! prop_cstruct.null?
437
+ prop = Node.wrap(prop_cstruct)
438
+ attr << prop
439
+ prop_cstruct = prop.cstruct[:next]
440
+ end
441
+ attr
442
+ end
443
+ end
444
+
445
+ private
446
+
447
+ def self.reparent_node_with(pivot, reparentee, &block)
448
+ raise(ArgumentError, "node must be a Nokogiri::XML::Node") unless reparentee.is_a?(Nokogiri::XML::Node)
449
+ raise(ArgumentError, "cannot reparent a document node") if reparentee.node_type == DOCUMENT_NODE || reparentee.node_type == HTML_DOCUMENT_NODE
450
+
451
+ pivot_struct = pivot.cstruct
452
+ reparentee_struct = reparentee.cstruct
453
+
454
+ LibXML.xmlUnlinkNode(reparentee_struct)
455
+
456
+ if reparentee_struct[:doc] != pivot_struct[:doc] || reparentee_struct[:type] == TEXT_NODE
457
+ reparentee_struct.keep_reference_from_document!
458
+ reparentee_struct = LibXML.xmlDocCopyNode(reparentee_struct, pivot_struct.document, 1)
459
+ raise(RuntimeError, "Could not reparent node (xmlDocCopyNode)") unless reparentee_struct
460
+ reparentee_struct = LibXML::XmlNode.new(reparentee_struct)
461
+ end
462
+
463
+ if reparentee_struct[:type] == TEXT_NODE && !pivot_struct[:next].null?
464
+ next_text = Node.wrap(pivot_struct[:next])
465
+ if next_text.cstruct[:type] == TEXT_NODE
466
+ new_next_text = LibXML.xmlDocCopyNode(next_text.cstruct, pivot_struct[:doc], 1)
467
+ LibXML.xmlUnlinkNode(next_text.cstruct)
468
+ next_text.cstruct.keep_reference_from_document!
469
+ LibXML.xmlAddNextSibling(pivot_struct, new_next_text);
470
+ end
471
+ end
472
+
473
+ if reparentee_struct[:type] == TEXT_NODE && pivot_struct[:type] == TEXT_NODE && Nokogiri.is_2_6_16?
474
+ pivot_struct.pointer.put_pointer(pivot_struct.offset_of(:content), LibXML.xmlStrdup(pivot_struct[:content]))
475
+ end
476
+
477
+ reparented_struct = block.call(pivot_struct, reparentee_struct)
478
+ raise(RuntimeError, "Could not reparent node") unless reparented_struct
479
+
480
+ reparented_struct = LibXML::XmlNode.new(reparented_struct) if reparented_struct.is_a?(FFI::Pointer)
481
+ reparentee.cstruct = reparented_struct
482
+
483
+ relink_namespace reparented_struct
484
+
485
+ reparented = Node.wrap(reparented_struct)
486
+ reparented.decorate!
487
+ reparented
488
+ end
489
+
490
+ def self.relink_namespace(reparented_struct)
491
+ return if reparented_struct[:parent].null?
492
+
493
+ # Make sure that our reparented node has the correct namespaces
494
+ if reparented_struct[:ns].null? && reparented_struct[:doc] != reparented_struct[:parent]
495
+ LibXML.xmlSetNs(reparented_struct, LibXML::XmlNode.new(reparented_struct[:parent])[:ns])
496
+ end
497
+
498
+ # Search our parents for an existing definition
499
+ if ! reparented_struct[:nsDef].null?
500
+ curr = reparented_struct[:nsDef]
501
+ prev = nil
502
+
503
+ while (! curr.null?)
504
+ curr_ns = LibXML::XmlNs.new(curr)
505
+ ns = LibXML.xmlSearchNsByHref(
506
+ reparented_struct[:doc],
507
+ reparented_struct[:parent],
508
+ curr_ns[:href]
509
+ )
510
+ # If we find the namespace is already declared, remove it from this
511
+ # definition list.
512
+ if (! ns.null? && ns != curr)
513
+ if prev
514
+ prev[:next] = curr_ns[:next]
515
+ else
516
+ reparented_struct[:nsDef] = curr_ns[:next]
517
+ end
518
+ curr_ns.keep_reference_from!(reparented_struct.document)
519
+ else
520
+ prev = curr_ns
521
+ end
522
+ curr = curr_ns[:next]
523
+ end
524
+ end
525
+
526
+ # Only walk all children if there actually is a namespace we need to reparent.
527
+ return if reparented_struct[:ns].null?
528
+
529
+ # When a node gets reparented, walk it's children to make sure that
530
+ # their namespaces are reparented as well.
531
+ child_ptr = reparented_struct[:children]
532
+ while ! child_ptr.null?
533
+ child_struct = LibXML::XmlNode.new(child_ptr)
534
+ relink_namespace child_struct
535
+ child_ptr = child_struct[:next]
536
+ end
537
+ end
538
+
539
+ def cstruct_node_from(sym)
540
+ (val = cstruct[sym]).null? ? nil : Node.wrap(val)
541
+ end
542
+
543
+ def set_xml_indent_tree_output(value)
544
+ LibXML.__xmlIndentTreeOutput.write_int(value)
545
+ end
546
+
547
+ def set_xml_tree_indent_string(value)
548
+ LibXML.__xmlTreeIndentString.write_pointer(LibXML.xmlStrdup(value.to_s))
549
+ end
550
+
551
+ # :startdoc:
552
+ end
553
+ end
554
+ end
555
+ class Nokogiri::XML::Element < Nokogiri::XML::Node; end
556
+ class Nokogiri::XML::CharacterData < Nokogiri::XML::Node; end