jwagener-nokogiri 1.4.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 +366 -0
  3. data/CHANGELOG.rdoc +345 -0
  4. data/Manifest.txt +276 -0
  5. data/README.ja.rdoc +106 -0
  6. data/README.rdoc +126 -0
  7. data/Rakefile +248 -0
  8. data/bin/nokogiri +49 -0
  9. data/deps.rip +5 -0
  10. data/ext/nokogiri/extconf.rb +149 -0
  11. data/ext/nokogiri/html_document.c +145 -0
  12. data/ext/nokogiri/html_document.h +10 -0
  13. data/ext/nokogiri/html_element_description.c +272 -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 +92 -0
  18. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  19. data/ext/nokogiri/nokogiri.c +96 -0
  20. data/ext/nokogiri/nokogiri.h +148 -0
  21. data/ext/nokogiri/xml_attr.c +92 -0
  22. data/ext/nokogiri/xml_attr.h +9 -0
  23. data/ext/nokogiri/xml_attribute_decl.c +67 -0
  24. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  25. data/ext/nokogiri/xml_cdata.c +54 -0
  26. data/ext/nokogiri/xml_cdata.h +9 -0
  27. data/ext/nokogiri/xml_comment.c +52 -0
  28. data/ext/nokogiri/xml_comment.h +9 -0
  29. data/ext/nokogiri/xml_document.c +386 -0
  30. data/ext/nokogiri/xml_document.h +24 -0
  31. data/ext/nokogiri/xml_document_fragment.c +46 -0
  32. data/ext/nokogiri/xml_document_fragment.h +10 -0
  33. data/ext/nokogiri/xml_dtd.c +192 -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 +97 -0
  42. data/ext/nokogiri/xml_entity_decl.h +10 -0
  43. data/ext/nokogiri/xml_entity_reference.c +50 -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 +82 -0
  48. data/ext/nokogiri/xml_namespace.h +13 -0
  49. data/ext/nokogiri/xml_node.c +1080 -0
  50. data/ext/nokogiri/xml_node.h +13 -0
  51. data/ext/nokogiri/xml_node_set.c +405 -0
  52. data/ext/nokogiri/xml_node_set.h +9 -0
  53. data/ext/nokogiri/xml_processing_instruction.c +54 -0
  54. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  55. data/ext/nokogiri/xml_reader.c +593 -0
  56. data/ext/nokogiri/xml_reader.h +10 -0
  57. data/ext/nokogiri/xml_relax_ng.c +159 -0
  58. data/ext/nokogiri/xml_relax_ng.h +9 -0
  59. data/ext/nokogiri/xml_sax_parser.c +283 -0
  60. data/ext/nokogiri/xml_sax_parser.h +43 -0
  61. data/ext/nokogiri/xml_sax_parser_context.c +157 -0
  62. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  63. data/ext/nokogiri/xml_sax_push_parser.c +114 -0
  64. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  65. data/ext/nokogiri/xml_schema.c +156 -0
  66. data/ext/nokogiri/xml_schema.h +9 -0
  67. data/ext/nokogiri/xml_syntax_error.c +52 -0
  68. data/ext/nokogiri/xml_syntax_error.h +13 -0
  69. data/ext/nokogiri/xml_text.c +48 -0
  70. data/ext/nokogiri/xml_text.h +9 -0
  71. data/ext/nokogiri/xml_xpath.c +53 -0
  72. data/ext/nokogiri/xml_xpath.h +11 -0
  73. data/ext/nokogiri/xml_xpath_context.c +239 -0
  74. data/ext/nokogiri/xml_xpath_context.h +9 -0
  75. data/ext/nokogiri/xslt_stylesheet.c +131 -0
  76. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  77. data/lib/nokogiri.rb +116 -0
  78. data/lib/nokogiri/css.rb +25 -0
  79. data/lib/nokogiri/css/generated_parser.rb +661 -0
  80. data/lib/nokogiri/css/generated_tokenizer.rb +145 -0
  81. data/lib/nokogiri/css/node.rb +99 -0
  82. data/lib/nokogiri/css/parser.rb +82 -0
  83. data/lib/nokogiri/css/parser.y +230 -0
  84. data/lib/nokogiri/css/syntax_error.rb +7 -0
  85. data/lib/nokogiri/css/tokenizer.rb +7 -0
  86. data/lib/nokogiri/css/tokenizer.rex +55 -0
  87. data/lib/nokogiri/css/xpath_visitor.rb +164 -0
  88. data/lib/nokogiri/decorators/slop.rb +33 -0
  89. data/lib/nokogiri/ffi/encoding_handler.rb +42 -0
  90. data/lib/nokogiri/ffi/html/document.rb +28 -0
  91. data/lib/nokogiri/ffi/html/element_description.rb +81 -0
  92. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  93. data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
  94. data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
  95. data/lib/nokogiri/ffi/libxml.rb +372 -0
  96. data/lib/nokogiri/ffi/structs/common_node.rb +26 -0
  97. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  98. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  99. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  100. data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
  101. data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
  102. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  103. data/lib/nokogiri/ffi/structs/xml_char_encoding_handler.rb +11 -0
  104. data/lib/nokogiri/ffi/structs/xml_document.rb +108 -0
  105. data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
  106. data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
  107. data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
  108. data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
  109. data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
  110. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  111. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  112. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  113. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  114. data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
  115. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  116. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  117. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +124 -0
  118. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  119. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  120. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  121. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +37 -0
  122. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  123. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  124. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  125. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  126. data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
  127. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  128. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  129. data/lib/nokogiri/ffi/xml/document.rb +135 -0
  130. data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
  131. data/lib/nokogiri/ffi/xml/dtd.rb +67 -0
  132. data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
  133. data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
  134. data/lib/nokogiri/ffi/xml/entity_decl.rb +27 -0
  135. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  136. data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
  137. data/lib/nokogiri/ffi/xml/node.rb +465 -0
  138. data/lib/nokogiri/ffi/xml/node_set.rb +146 -0
  139. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  140. data/lib/nokogiri/ffi/xml/reader.rb +227 -0
  141. data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
  142. data/lib/nokogiri/ffi/xml/sax/parser.rb +135 -0
  143. data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
  144. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +51 -0
  145. data/lib/nokogiri/ffi/xml/schema.rb +92 -0
  146. data/lib/nokogiri/ffi/xml/syntax_error.rb +98 -0
  147. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  148. data/lib/nokogiri/ffi/xml/xpath.rb +19 -0
  149. data/lib/nokogiri/ffi/xml/xpath_context.rb +135 -0
  150. data/lib/nokogiri/ffi/xslt/stylesheet.rb +50 -0
  151. data/lib/nokogiri/html.rb +35 -0
  152. data/lib/nokogiri/html/builder.rb +35 -0
  153. data/lib/nokogiri/html/document.rb +88 -0
  154. data/lib/nokogiri/html/document_fragment.rb +15 -0
  155. data/lib/nokogiri/html/element_description.rb +23 -0
  156. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  157. data/lib/nokogiri/html/sax/parser.rb +48 -0
  158. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  159. data/lib/nokogiri/syntax_error.rb +4 -0
  160. data/lib/nokogiri/version.rb +33 -0
  161. data/lib/nokogiri/version_warning.rb +11 -0
  162. data/lib/nokogiri/xml.rb +67 -0
  163. data/lib/nokogiri/xml/attr.rb +14 -0
  164. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  165. data/lib/nokogiri/xml/builder.rb +405 -0
  166. data/lib/nokogiri/xml/cdata.rb +11 -0
  167. data/lib/nokogiri/xml/character_data.rb +7 -0
  168. data/lib/nokogiri/xml/document.rb +162 -0
  169. data/lib/nokogiri/xml/document_fragment.rb +73 -0
  170. data/lib/nokogiri/xml/dtd.rb +11 -0
  171. data/lib/nokogiri/xml/element_content.rb +36 -0
  172. data/lib/nokogiri/xml/element_decl.rb +13 -0
  173. data/lib/nokogiri/xml/entity_decl.rb +15 -0
  174. data/lib/nokogiri/xml/fragment_handler.rb +79 -0
  175. data/lib/nokogiri/xml/namespace.rb +13 -0
  176. data/lib/nokogiri/xml/node.rb +736 -0
  177. data/lib/nokogiri/xml/node/save_options.rb +42 -0
  178. data/lib/nokogiri/xml/node_set.rb +324 -0
  179. data/lib/nokogiri/xml/notation.rb +6 -0
  180. data/lib/nokogiri/xml/parse_options.rb +85 -0
  181. data/lib/nokogiri/xml/pp.rb +2 -0
  182. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  183. data/lib/nokogiri/xml/pp/node.rb +56 -0
  184. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  185. data/lib/nokogiri/xml/reader.rb +74 -0
  186. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  187. data/lib/nokogiri/xml/sax.rb +4 -0
  188. data/lib/nokogiri/xml/sax/document.rb +160 -0
  189. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  190. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  191. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  192. data/lib/nokogiri/xml/schema.rb +61 -0
  193. data/lib/nokogiri/xml/syntax_error.rb +43 -0
  194. data/lib/nokogiri/xml/xpath.rb +10 -0
  195. data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
  196. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  197. data/lib/nokogiri/xslt.rb +48 -0
  198. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  199. data/lib/xsd/xmlparser/nokogiri.rb +90 -0
  200. data/tasks/test.rb +100 -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 +76 -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 +131 -0
  225. data/test/html/sax/test_parser.rb +64 -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 +390 -0
  229. data/test/html/test_document_encoding.rb +77 -0
  230. data/test/html/test_document_fragment.rb +170 -0
  231. data/test/html/test_element_description.rb +94 -0
  232. data/test/html/test_named_characters.rb +14 -0
  233. data/test/html/test_node.rb +242 -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 +358 -0
  241. data/test/test_soap4r_sax.rb +52 -0
  242. data/test/test_xslt_transforms.rb +174 -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 +167 -0
  251. data/test/xml/test_cdata.rb +38 -0
  252. data/test/xml/test_comment.rb +29 -0
  253. data/test/xml/test_document.rb +633 -0
  254. data/test/xml/test_document_encoding.rb +26 -0
  255. data/test/xml/test_document_fragment.rb +144 -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 +83 -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 +738 -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 +261 -0
  267. data/test/xml/test_node_set.rb +582 -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 +30 -0
  275. data/test/xml/test_unparented_node.rb +381 -0
  276. data/test/xml/test_xpath.rb +106 -0
  277. metadata +482 -0
@@ -0,0 +1,11 @@
1
+ module Nokogiri
2
+ module XML
3
+ class CDATA < Nokogiri::XML::Text
4
+ ###
5
+ # Get the name of this CDATA node
6
+ def name
7
+ '#cdata-section'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Nokogiri
2
+ module XML
3
+ class CharacterData < Nokogiri::XML::Node
4
+ include Nokogiri::XML::PP::CharacterData
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,162 @@
1
+ module Nokogiri
2
+ module XML
3
+ ####
4
+ # Nokogiri::XML::Document is the main entry point for dealing with
5
+ # XML documents. The Document is created by parsing an XML document.
6
+ # See Nokogiri.XML()
7
+ #
8
+ # For searching a Document, see Nokogiri::XML::Node#css and
9
+ # Nokogiri::XML::Node#xpath
10
+ class Document < Node
11
+ ###
12
+ # Parse an XML file. +thing+ may be a String, or any object that
13
+ # responds to _read_ and _close_ such as an IO, or StringIO.
14
+ # +url+ is resource where this document is located. +encoding+ is the
15
+ # encoding that should be used when processing the document. +options+
16
+ # is a number that sets options in the parser, such as
17
+ # Nokogiri::XML::ParseOptions::RECOVER. See the constants in
18
+ # Nokogiri::XML::ParseOptions.
19
+ def self.parse string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML, &block
20
+
21
+ options = Nokogiri::XML::ParseOptions.new(options) if Fixnum === options
22
+ # Give the options to the user
23
+ yield options if block_given?
24
+
25
+ if string_or_io.respond_to?(:read)
26
+ url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
27
+ return read_io(string_or_io, url, encoding, options.to_i)
28
+ end
29
+
30
+ # read_memory pukes on empty docs
31
+ return new if string_or_io.nil? or string_or_io.empty?
32
+
33
+ read_memory(string_or_io, url, encoding, options.to_i)
34
+ end
35
+
36
+ # A list of Nokogiri::XML::SyntaxError found when parsing a document
37
+ attr_accessor :errors
38
+
39
+ def initialize *args
40
+ @decorators = nil
41
+ end
42
+
43
+ # Create an element with +name+
44
+ def create_element name, &block
45
+ Nokogiri::XML::Element.new(name, self, &block)
46
+ end
47
+
48
+ # Create a text node with +text+
49
+ def create_text_node text, &block
50
+ Nokogiri::XML::Text.new(text.to_s, self, &block)
51
+ end
52
+
53
+ # The name of this document. Always returns "document"
54
+ def name
55
+ 'document'
56
+ end
57
+
58
+ # A reference to +self+
59
+ def document
60
+ self
61
+ end
62
+
63
+ ###
64
+ # Recursively get all namespaces from this node and its subtree and
65
+ # return them as a hash.
66
+ #
67
+ # For example, given this document:
68
+ #
69
+ # <root xmlns:foo="bar">
70
+ # <bar xmlns:hello="world" />
71
+ # </root>
72
+ #
73
+ # This method will return:
74
+ #
75
+ # { 'xmlns:foo' => 'bar', 'xmlns:hello' => 'world' }
76
+ #
77
+ # WARNING: this method will clobber duplicate names in the keys.
78
+ # For example, given this document:
79
+ #
80
+ # <root xmlns:foo="bar">
81
+ # <bar xmlns:foo="baz" />
82
+ # </root>
83
+ #
84
+ # The hash returned will look like this: { 'xmlns:foo' => 'bar' }
85
+ def collect_namespaces
86
+ ns = {}
87
+ traverse { |j| ns.merge!(j.namespaces) }
88
+ ns
89
+ end
90
+
91
+ # Get the list of decorators given +key+
92
+ def decorators key
93
+ @decorators ||= Hash.new
94
+ @decorators[key] ||= []
95
+ end
96
+
97
+ ###
98
+ # Validate this Document against it's DTD. Returns a list of errors on
99
+ # the document or +nil+ when there is no DTD.
100
+ def validate
101
+ return nil unless internal_subset
102
+ internal_subset.validate self
103
+ end
104
+
105
+ ###
106
+ # Explore a document with shortcut methods.
107
+ def slop!
108
+ unless decorators(XML::Node).include? Nokogiri::Decorators::Slop
109
+ decorators(XML::Node) << Nokogiri::Decorators::Slop
110
+ decorate!
111
+ end
112
+
113
+ self
114
+ end
115
+
116
+ ###
117
+ # Apply any decorators to +node+
118
+ def decorate node
119
+ return unless @decorators
120
+ @decorators.each { |klass,list|
121
+ next unless node.is_a?(klass)
122
+ list.each { |moodule| node.extend(moodule) }
123
+ }
124
+ end
125
+
126
+ alias :to_xml :serialize
127
+ alias :clone :dup
128
+
129
+ # Get the hash of namespaces on the root Nokogiri::XML::Node
130
+ def namespaces
131
+ root ? root.namespaces : {}
132
+ end
133
+
134
+ ####
135
+ # Create a Nokogiri::XML::DocumentFragment from +tags+
136
+ # Returns an empty fragment if +tags+ is nil.
137
+ def fragment tags = nil
138
+ DocumentFragment.new(self, tags)
139
+ end
140
+
141
+ undef_method :swap, :parent, :namespace, :default_namespace=
142
+ undef_method :add_namespace_definition, :attributes
143
+ undef_method :namespace_definitions, :line, :add_namespace
144
+
145
+ def add_child child
146
+ raise "Document already has a root node" if root
147
+ if child.type == Node::DOCUMENT_FRAG_NODE
148
+ raise "Document cannot have multiple root nodes" if child.children.size > 1
149
+ super(child.children.first)
150
+ else
151
+ super
152
+ end
153
+ end
154
+ alias :<< :add_child
155
+
156
+ private
157
+ def inspect_attributes
158
+ [:name, :children]
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,73 @@
1
+ module Nokogiri
2
+ module XML
3
+ class DocumentFragment < Nokogiri::XML::Node
4
+ def initialize document, tags=nil
5
+ if tags
6
+ if self.kind_of?(Nokogiri::HTML::DocumentFragment)
7
+ HTML::SAX::Parser.new(FragmentHandler.new(self, tags)).parse(tags)
8
+ else
9
+ wrapped = "<div>#{tags.strip}</div>"
10
+ XML::SAX::Parser.new(FragmentHandler.new(self, wrapped)).parse(wrapped)
11
+ div = self.child
12
+ div.children.each { |child| child.parent = self }
13
+ div.unlink
14
+ end
15
+ end
16
+ end
17
+
18
+ ###
19
+ # return the name for DocumentFragment
20
+ def name
21
+ '#document-fragment'
22
+ end
23
+
24
+ ###
25
+ # Convert this DocumentFragment to a string
26
+ def to_s
27
+ children.to_s
28
+ end
29
+
30
+ ###
31
+ # Convert this DocumentFragment to html
32
+ # See Nokogiri::XML::NodeSet#to_html
33
+ def to_html *args
34
+ children.to_html(*args)
35
+ end
36
+
37
+ ###
38
+ # Convert this DocumentFragment to xhtml
39
+ # See Nokogiri::XML::NodeSet#to_xhtml
40
+ def to_xhtml *args
41
+ children.to_xhtml(*args)
42
+ end
43
+
44
+ ###
45
+ # Convert this DocumentFragment to xml
46
+ # See Nokogiri::XML::NodeSet#to_xml
47
+ def to_xml *args
48
+ children.to_xml(*args)
49
+ end
50
+
51
+ ###
52
+ # Search this fragment. See Nokogiri::XML::Node#css
53
+ def css *args
54
+ if children.any?
55
+ children.css(*args)
56
+ else
57
+ NodeSet.new(document)
58
+ end
59
+ end
60
+
61
+ alias :serialize :to_s
62
+
63
+ class << self
64
+ ####
65
+ # Create a Nokogiri::XML::DocumentFragment from +tags+
66
+ def parse tags
67
+ self.new(XML::Document.new, tags)
68
+ end
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,11 @@
1
+ module Nokogiri
2
+ module XML
3
+ class DTD < Nokogiri::XML::Node
4
+ undef_method :attribute_nodes
5
+ undef_method :content
6
+ undef_method :namespace
7
+ undef_method :namespace_definitions
8
+ undef_method :line
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ module Nokogiri
2
+ module XML
3
+ ###
4
+ # Represents the allowed content in an Element Declaration inside a DTD:
5
+ #
6
+ # <?xml version="1.0"?><?TEST-STYLE PIDATA?>
7
+ # <!DOCTYPE staff SYSTEM "staff.dtd" [
8
+ # <!ELEMENT div1 (head, (p | list | note)*, div2*)>
9
+ # ]>
10
+ # </root>
11
+ #
12
+ # ElementContent represents the tree inside the <!ELEMENT> tag shown above
13
+ # that lists the possible content for the div1 tag.
14
+ class ElementContent
15
+ # Possible definitions of type
16
+ PCDATA = 1
17
+ ELEMENT = 2
18
+ SEQ = 3
19
+ OR = 4
20
+
21
+ # Possible content occurrences
22
+ ONCE = 1
23
+ OPT = 2
24
+ MULT = 3
25
+ PLUS = 4
26
+
27
+ attr_reader :document
28
+
29
+ ###
30
+ # Get the children of this ElementContent node
31
+ def children
32
+ [c1, c2].compact
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ module Nokogiri
2
+ module XML
3
+ class ElementDecl < Nokogiri::XML::Node
4
+ undef_method :namespace
5
+ undef_method :namespace_definitions
6
+ undef_method :line
7
+
8
+ def inspect
9
+ "#<#{self.class.name}:#{sprintf("0x%x", object_id)} #{to_s.inspect}>"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Nokogiri
2
+ module XML
3
+ class EntityDecl < Nokogiri::XML::Node
4
+ undef_method :attribute_nodes
5
+ undef_method :attributes
6
+ undef_method :namespace
7
+ undef_method :namespace_definitions
8
+ undef_method :line
9
+
10
+ def inspect
11
+ "#<#{self.class.name}:#{sprintf("0x%x", object_id)} #{to_s.inspect}>"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,79 @@
1
+ module Nokogiri
2
+ module XML
3
+ class FragmentHandler < Nokogiri::XML::SAX::Document # :nodoc:
4
+ QNAME_REGEX = /(.*):(.*)/
5
+
6
+ def initialize node, original_html
7
+ @doc_started = false
8
+ @document = node.document
9
+ @stack = [node]
10
+ @html_eh = node.kind_of? HTML::DocumentFragment
11
+ @original_html = prepare_for_regex(original_html.strip)
12
+ end
13
+
14
+ def start_element name, attrs = []
15
+ regex = @html_eh ? %r{^\s*<#{Regexp.escape(name)}}i :
16
+ %r{^\s*<#{Regexp.escape(name)}}
17
+
18
+ if ! @doc_started && @original_html =~ regex
19
+ @doc_started = true
20
+ end
21
+ return unless @doc_started
22
+
23
+ ns = nil
24
+ if @document.root
25
+ match = name.match(QNAME_REGEX)
26
+ if match
27
+ prefix, name = match[1], match[2]
28
+ ns = @document.root.namespace_definitions.detect { |x|
29
+ x.prefix == prefix
30
+ }
31
+ end
32
+ end
33
+
34
+ node = Element.new(name, @document)
35
+ attrs << "" unless (attrs.length % 2) == 0
36
+ Hash[*attrs].each do |k,v|
37
+ node[k] = v
38
+ end
39
+
40
+ node.namespace = ns if ns
41
+
42
+ @stack.last << node
43
+ @stack << node
44
+ end
45
+
46
+ def characters string
47
+ @doc_started = true
48
+ @stack.last << Text.new(string, @document)
49
+ end
50
+
51
+ def comment string
52
+ @stack.last << Comment.new(@document, string)
53
+ end
54
+
55
+ def cdata_block string
56
+ @stack.last << CDATA.new(@document, string)
57
+ end
58
+
59
+ def end_element name
60
+ return unless @stack.last.name == name
61
+ @stack.pop
62
+ end
63
+
64
+ private
65
+
66
+ #
67
+ # the regexes used in start_element() and characters() anchor at
68
+ # start-of-line, but we really only want them to anchor at
69
+ # start-of-doc. so let's only save up to the first newline.
70
+ #
71
+ # this implementation choice was the result of some benchmarks, if
72
+ # you're curious: http://gist.github.com/115936
73
+ #
74
+ def prepare_for_regex(string)
75
+ (newline_index = string.index("\n")) ? string.slice(0,newline_index) : string
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,13 @@
1
+ module Nokogiri
2
+ module XML
3
+ class Namespace
4
+ include Nokogiri::XML::PP::Node
5
+ attr_reader :document
6
+
7
+ private
8
+ def inspect_attributes
9
+ [:prefix, :href]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,736 @@
1
+ require 'stringio'
2
+ require 'nokogiri/xml/node/save_options'
3
+
4
+ module Nokogiri
5
+ module XML
6
+ ####
7
+ # Nokogiri::XML::Node is your window to the fun filled world of dealing
8
+ # with XML and HTML tags. A Nokogiri::XML::Node may be treated similarly
9
+ # to a hash with regard to attributes. For example (from irb):
10
+ #
11
+ # irb(main):004:0> node
12
+ # => <a href="#foo" id="link">link</a>
13
+ # irb(main):005:0> node['href']
14
+ # => "#foo"
15
+ # irb(main):006:0> node.keys
16
+ # => ["href", "id"]
17
+ # irb(main):007:0> node.values
18
+ # => ["#foo", "link"]
19
+ # irb(main):008:0> node['class'] = 'green'
20
+ # => "green"
21
+ # irb(main):009:0> node
22
+ # => <a href="#foo" id="link" class="green">link</a>
23
+ # irb(main):010:0>
24
+ #
25
+ # See Nokogiri::XML::Node#[] and Nokogiri::XML#[]= for more information.
26
+ #
27
+ # Nokogiri::XML::Node also has methods that let you move around your
28
+ # tree. For navigating your tree, see:
29
+ #
30
+ # * Nokogiri::XML::Node#parent
31
+ # * Nokogiri::XML::Node#children
32
+ # * Nokogiri::XML::Node#next
33
+ # * Nokogiri::XML::Node#previous
34
+ #
35
+ # You may search this node's subtree using Node#xpath and Node#css
36
+ class Node
37
+ include Nokogiri::XML::PP::Node
38
+
39
+ # Element node type, see Nokogiri::XML::Node#element?
40
+ ELEMENT_NODE = 1
41
+ # Attribute node type
42
+ ATTRIBUTE_NODE = 2
43
+ # Text node type, see Nokogiri::XML::Node#text?
44
+ TEXT_NODE = 3
45
+ # CDATA node type, see Nokogiri::XML::Node#cdata?
46
+ CDATA_SECTION_NODE = 4
47
+ # Entity reference node type
48
+ ENTITY_REF_NODE = 5
49
+ # Entity node type
50
+ ENTITY_NODE = 6
51
+ # PI node type
52
+ PI_NODE = 7
53
+ # Comment node type, see Nokogiri::XML::Node#comment?
54
+ COMMENT_NODE = 8
55
+ # Document node type, see Nokogiri::XML::Node#xml?
56
+ DOCUMENT_NODE = 9
57
+ # Document type node type
58
+ DOCUMENT_TYPE_NODE = 10
59
+ # Document fragment node type
60
+ DOCUMENT_FRAG_NODE = 11
61
+ # Notation node type
62
+ NOTATION_NODE = 12
63
+ # HTML document node type, see Nokogiri::XML::Node#html?
64
+ HTML_DOCUMENT_NODE = 13
65
+ # DTD node type
66
+ DTD_NODE = 14
67
+ # Element declaration type
68
+ ELEMENT_DECL = 15
69
+ # Attribute declaration type
70
+ ATTRIBUTE_DECL = 16
71
+ # Entity declaration type
72
+ ENTITY_DECL = 17
73
+ # Namespace declaration type
74
+ NAMESPACE_DECL = 18
75
+ # XInclude start type
76
+ XINCLUDE_START = 19
77
+ # XInclude end type
78
+ XINCLUDE_END = 20
79
+ # DOCB document node type
80
+ DOCB_DOCUMENT_NODE = 21
81
+
82
+ def initialize name, document
83
+ # ... Ya. This is empty on purpose.
84
+ end
85
+
86
+ ###
87
+ # Decorate this node with the decorators set up in this node's Document
88
+ def decorate!
89
+ document.decorate(self)
90
+ end
91
+
92
+ ###
93
+ # Search this node for +paths+. +paths+ can be XPath or CSS, and an
94
+ # optional hash of namespaces may be appended.
95
+ # See Node#xpath and Node#css.
96
+ def search *paths
97
+ ns = paths.last.is_a?(Hash) ? paths.pop :
98
+ (document.root ? document.root.namespaces : {})
99
+ xpath(*(paths.map { |path|
100
+ path = path.to_s
101
+ path =~ /^(\.\/|\/)/ ? path : CSS.xpath_for(
102
+ path,
103
+ :prefix => ".//",
104
+ :ns => ns
105
+ )
106
+ }.flatten.uniq) + [ns])
107
+ end
108
+ alias :/ :search
109
+
110
+ ###
111
+ # Search this node for XPath +paths+. +paths+ must be one or more XPath
112
+ # queries. A hash of namespaces may be appended. For example:
113
+ #
114
+ # node.xpath('.//title')
115
+ # node.xpath('.//foo:name', { 'foo' => 'http://example.org/' })
116
+ # node.xpath('.//xmlns:name', node.root.namespaces)
117
+ #
118
+ # Custom XPath functions may also be defined. To define custom functions
119
+ # create a class and implement the # function you want to define.
120
+ # For example:
121
+ #
122
+ # node.xpath('.//title[regex(., "\w+")]', Class.new {
123
+ # def regex node_set, regex
124
+ # node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
125
+ # end
126
+ # }.new)
127
+ #
128
+ def xpath *paths
129
+ # Pop off our custom function handler if it exists
130
+ handler = ![
131
+ Hash, String, Symbol
132
+ ].include?(paths.last.class) ? paths.pop : nil
133
+
134
+ ns = paths.last.is_a?(Hash) ? paths.pop :
135
+ (document.root ? document.root.namespaces : {})
136
+
137
+ return NodeSet.new(document) unless document
138
+
139
+ sets = paths.map { |path|
140
+ ctx = XPathContext.new(self)
141
+ ctx.register_namespaces(ns)
142
+ set = ctx.evaluate(path, handler).node_set
143
+ set.document = document
144
+ document.decorate(set)
145
+ set
146
+ }
147
+ return sets.first if sets.length == 1
148
+
149
+ NodeSet.new(document) do |combined|
150
+ document.decorate(combined)
151
+ sets.each do |set|
152
+ set.each do |node|
153
+ combined << node
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ ###
160
+ # Search this node for CSS +rules+. +rules+ must be one or more CSS
161
+ # selectors. For example:
162
+ #
163
+ # node.css('title')
164
+ # node.css('body h1.bold')
165
+ # node.css('div + p.green', 'div#one')
166
+ #
167
+ # Custom CSS pseudo classes may also be defined. To define custom pseudo
168
+ # classes, create a class and implement the custom pseudo class you
169
+ # want defined. The first argument to the method will be the current
170
+ # matching NodeSet. Any other arguments are ones that you pass in.
171
+ # For example:
172
+ #
173
+ # node.css('title:regex("\w+")', Class.new {
174
+ # def regex node_set, regex
175
+ # node_set.find_all { |node| node['some_attribute'] =~ /#{regex}/ }
176
+ # end
177
+ # }.new)
178
+ #
179
+ def css *rules
180
+ # Pop off our custom function handler if it exists
181
+ handler = ![
182
+ Hash, String, Symbol
183
+ ].include?(rules.last.class) ? rules.pop : nil
184
+
185
+ ns = rules.last.is_a?(Hash) ? rules.pop :
186
+ (document.root ? document.root.namespaces : {})
187
+
188
+ rules = rules.map { |rule|
189
+ CSS.xpath_for(rule, :prefix => ".//", :ns => ns)
190
+ }.flatten.uniq + [ns, handler].compact
191
+
192
+ xpath(*rules)
193
+ end
194
+
195
+ ###
196
+ # Search this node's immidiate children using CSS selector +selector+
197
+ def > selector
198
+ ns = document.root.namespaces
199
+ xpath CSS.xpath_for(selector, :prefix => "./", :ns => ns).first
200
+ end
201
+
202
+ ###
203
+ # Search for the first occurrence of +path+.
204
+ # Returns nil if nothing is found, otherwise a Node.
205
+ def at path, ns = document.root ? document.root.namespaces : {}
206
+ search(path, ns).first
207
+ end
208
+ alias :% :at
209
+
210
+ ##
211
+ # Search this node for the first occurrence of XPath +paths+.
212
+ # Equivalent to <tt>xpath(paths).first</tt>
213
+ # See Node#xpath for more information.
214
+ #
215
+ def at_xpath *paths
216
+ xpath(*paths).first
217
+ end
218
+
219
+ ##
220
+ # Search this node for the first occurrence of CSS +rules+.
221
+ # Equivalent to <tt>css(rules).first</tt>
222
+ # See Node#css for more information.
223
+ #
224
+ def at_css *rules
225
+ css(*rules).first
226
+ end
227
+
228
+ ###
229
+ # Get the attribute value for the attribute +name+
230
+ def [] name
231
+ return nil unless key?(name.to_s)
232
+ get(name.to_s)
233
+ end
234
+
235
+ ###
236
+ # Add +node+ as a child of this Node.
237
+ # The new node must be a Nokogiri::XML::Node or a non-empty String.
238
+ # Returns the new child node.
239
+ def add_child(node)
240
+ Node.verify_nodeishness(node)
241
+ if node.type == DOCUMENT_FRAG_NODE
242
+ node.children.each do |child|
243
+ add_child_node child
244
+ end
245
+ else
246
+ add_child_node node
247
+ end
248
+ end
249
+
250
+ ###
251
+ # Insert +node+ before this Node (as a sibling).
252
+ def add_previous_sibling(node)
253
+ Node.verify_nodeishness(node)
254
+ if node.type == DOCUMENT_FRAG_NODE
255
+ node.children.each do |child|
256
+ add_previous_sibling_node child
257
+ end
258
+ else
259
+ add_previous_sibling_node node
260
+ end
261
+ end
262
+
263
+ ###
264
+ # Insert +node+ after this Node (as a sibling).
265
+ def add_next_sibling(node)
266
+ Node.verify_nodeishness(node)
267
+ if node.type == DOCUMENT_FRAG_NODE
268
+ node.children.reverse.each do |child|
269
+ add_next_sibling_node child
270
+ end
271
+ else
272
+ add_next_sibling_node node
273
+ end
274
+ end
275
+
276
+ alias :next :next_sibling
277
+ alias :previous :previous_sibling
278
+
279
+ # :stopdoc:
280
+ # HACK: This is to work around an RDoc bug
281
+ alias :next= :add_next_sibling
282
+ # :startdoc:
283
+
284
+ alias :previous= :add_previous_sibling
285
+ alias :remove :unlink
286
+ alias :get_attribute :[]
287
+ alias :attr :[]
288
+ alias :set_attribute :[]=
289
+ alias :text :content
290
+ alias :inner_text :content
291
+ alias :has_attribute? :key?
292
+ alias :<< :add_child
293
+ alias :name :node_name
294
+ alias :name= :node_name=
295
+ alias :type :node_type
296
+ alias :to_str :text
297
+ alias :clone :dup
298
+
299
+ ####
300
+ # Returns a hash containing the node's attributes. The key is
301
+ # the attribute name, the value is a Nokogiri::XML::Attr
302
+ # representing the attribute.
303
+ def attributes
304
+ Hash[*(attribute_nodes.map { |node|
305
+ [node.node_name, node]
306
+ }.flatten)]
307
+ end
308
+
309
+ ###
310
+ # Get the attribute values for this Node.
311
+ def values
312
+ attribute_nodes.map { |node| node.value }
313
+ end
314
+
315
+ ###
316
+ # Get the attribute names for this Node.
317
+ def keys
318
+ attribute_nodes.map { |node| node.node_name }
319
+ end
320
+
321
+ ###
322
+ # Iterate over each attribute name and value pair for this Node.
323
+ def each &block
324
+ attribute_nodes.each { |node|
325
+ block.call(node.node_name, node.value)
326
+ }
327
+ end
328
+
329
+ ###
330
+ # Remove the attribute named +name+
331
+ def remove_attribute name
332
+ attributes[name].remove if key? name
333
+ end
334
+ alias :delete :remove_attribute
335
+
336
+ ###
337
+ # Returns true if this Node matches +selector+
338
+ def matches? selector
339
+ ancestors.last.search(selector).include?(self)
340
+ end
341
+
342
+ ####
343
+ # Create nodes from +data+ and insert them before this node
344
+ # (as a sibling).
345
+ def before data
346
+ fragment(data).children.each do |node|
347
+ add_previous_sibling node
348
+ end
349
+ self
350
+ end
351
+
352
+ ####
353
+ # Create nodes from +data+ and insert them after this node
354
+ # (as a sibling).
355
+ def after data
356
+ fragment(data).children.to_a.reverse.each do |node|
357
+ add_next_sibling node
358
+ end
359
+ self
360
+ end
361
+
362
+ ####
363
+ # Swap this Node for new nodes made from +data+
364
+ def swap data
365
+ before(data)
366
+ remove
367
+ self
368
+ end
369
+
370
+ ####
371
+ # Set the inner_html for this Node to +tags+
372
+ def inner_html= tags
373
+ children.each { |x| x.remove}
374
+
375
+ fragment(tags).children.to_a.each do |node|
376
+ add_child node
377
+ end
378
+ self
379
+ end
380
+
381
+ def fragment tags # :nodoc:
382
+ # TODO: deprecate?
383
+ document.fragment(tags)
384
+ end
385
+
386
+ ####
387
+ # Set the Node content to +string+. The content gets XML escaped.
388
+ def content= string
389
+ self.native_content = encode_special_chars(string.to_s)
390
+ end
391
+
392
+ ###
393
+ # Set the parent Node for this Node
394
+ def parent= parent_node
395
+ parent_node.add_child(self)
396
+ parent_node
397
+ end
398
+
399
+ ###
400
+ # Get a hash containing the Namespace definitions for this Node
401
+ def namespaces
402
+ Hash[*namespace_definitions.map { |nd|
403
+ key = ['xmlns', nd.prefix].compact.join(':')
404
+ if RUBY_VERSION >= '1.9' && document.encoding
405
+ begin
406
+ key.force_encoding document.encoding
407
+ rescue ArgumentError
408
+ end
409
+ end
410
+ [key, nd.href]
411
+ }.flatten]
412
+ end
413
+
414
+ # Returns true if this is a Comment
415
+ def comment?
416
+ type == COMMENT_NODE
417
+ end
418
+
419
+ # Returns true if this is a CDATA
420
+ def cdata?
421
+ type == CDATA_SECTION_NODE
422
+ end
423
+
424
+ # Returns true if this is an XML::Document node
425
+ def xml?
426
+ type == DOCUMENT_NODE
427
+ end
428
+
429
+ # Returns true if this is an HTML::Document node
430
+ def html?
431
+ type == HTML_DOCUMENT_NODE
432
+ end
433
+
434
+ # Returns true if this is a Text node
435
+ def text?
436
+ type == TEXT_NODE
437
+ end
438
+
439
+ ###
440
+ # Fetch the Nokogiri::HTML::ElementDescription for this node. Returns
441
+ # nil on XML documents and on unknown tags.
442
+ def description
443
+ return nil if document.xml?
444
+ Nokogiri::HTML::ElementDescription[name]
445
+ end
446
+
447
+ ###
448
+ # Is this a read only node?
449
+ def read_only?
450
+ # According to gdome2, these are read-only node types
451
+ [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
452
+ end
453
+
454
+ # Returns true if this is an Element node
455
+ def element?
456
+ type == ELEMENT_NODE
457
+ end
458
+ alias :elem? :element?
459
+
460
+ ###
461
+ # Turn this node in to a string. If the document is HTML, this method
462
+ # returns html. If the document is XML, this method returns XML.
463
+ def to_s
464
+ document.xml? ? to_xml : to_html
465
+ end
466
+
467
+ # Get the inner_html for this node's Node#children
468
+ def inner_html *args
469
+ children.map { |x| x.to_html(*args) }.join
470
+ end
471
+
472
+ # Get the path to this node as a CSS expression
473
+ def css_path
474
+ path.split(/\//).map { |part|
475
+ part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
476
+ }.compact.join(' > ')
477
+ end
478
+
479
+ ###
480
+ # Get a list of ancestor Node for this Node. If +selector+ is given,
481
+ # the ancestors must match +selector+
482
+ def ancestors selector = nil
483
+ return NodeSet.new(document) unless respond_to?(:parent)
484
+ return NodeSet.new(document) unless parent
485
+
486
+ parents = [parent]
487
+
488
+ while parents.last.respond_to?(:parent)
489
+ break unless ctx_parent = parents.last.parent
490
+ parents << ctx_parent
491
+ end
492
+
493
+ return NodeSet.new(document, parents) unless selector
494
+
495
+ root = parents.last
496
+
497
+ NodeSet.new(document, parents.find_all { |parent|
498
+ root.search(selector).include?(parent)
499
+ })
500
+ end
501
+
502
+ ###
503
+ # Set the default namespace for this node to +url+
504
+ def default_namespace= url
505
+ add_namespace_definition(nil, url)
506
+ end
507
+ alias :add_namespace :add_namespace_definition
508
+
509
+ ###
510
+ # Set the namespace for this node to +ns+
511
+ def namespace= ns
512
+ if ns.document != document
513
+ raise ArgumentError, 'namespace must be declared on the same document'
514
+ end
515
+ unless ns.is_a? Nokogiri::XML::Namespace
516
+ raise TypeError, "#{ns.class} can't be coerced into Nokogiri::XML::Namespace"
517
+ end
518
+ set_namespace ns
519
+ end
520
+
521
+ ####
522
+ # Yields self and all children to +block+ recursively.
523
+ def traverse &block
524
+ children.each{|j| j.traverse(&block) }
525
+ block.call(self)
526
+ end
527
+
528
+ ###
529
+ # Accept a visitor. This method calls "visit" on +visitor+ with self.
530
+ def accept visitor
531
+ visitor.visit(self)
532
+ end
533
+
534
+ ####
535
+ # +replace+ this Node with the +node+ in the Document.
536
+ # The new node must be a Nokogiri::XML::Node or a non-empty String.
537
+ # Returns the new child node.
538
+ def replace node
539
+ Node.verify_nodeishness(node)
540
+ if node.type == DOCUMENT_FRAG_NODE
541
+ node.children.each do |child|
542
+ add_previous_sibling child
543
+ end
544
+ unlink
545
+ else
546
+ replace_node node
547
+ end
548
+ end
549
+
550
+ ###
551
+ # Test to see if this Node is equal to +other+
552
+ def == other
553
+ return false unless other
554
+ return false unless other.respond_to?(:pointer_id)
555
+ pointer_id == other.pointer_id
556
+ end
557
+
558
+ ###
559
+ # Serialize Node using +options+. Save options can also be set using a
560
+ # block. See SaveOptions.
561
+ #
562
+ # These two statements are equivalent:
563
+ #
564
+ # node.serialize(:encoding => 'UTF-8', :save_with => FORMAT | AS_XML)
565
+ #
566
+ # or
567
+ #
568
+ # node.serialize(:encoding => 'UTF-8') do |config|
569
+ # config.format.as_xml
570
+ # end
571
+ #
572
+ def serialize *args, &block
573
+ options = args.first.is_a?(Hash) ? args.shift : {
574
+ :encoding => args[0],
575
+ :save_with => args[1] || SaveOptions::FORMAT
576
+ }
577
+
578
+ encoding = options[:encoding] || document.encoding
579
+
580
+ outstring = ""
581
+ if encoding && outstring.respond_to?(:force_encoding)
582
+ outstring.force_encoding(Encoding.find(encoding))
583
+ end
584
+ io = StringIO.new(outstring)
585
+ write_to io, options, &block
586
+ io.string
587
+ end
588
+
589
+ ###
590
+ # Serialize this Node to HTML
591
+ #
592
+ # doc.to_html
593
+ #
594
+ # See Node#write_to for a list of +options+. For formatted output,
595
+ # use Node#to_xhtml instead.
596
+ def to_html options = {}
597
+ # FIXME: this is a hack around broken libxml versions
598
+ return dump_html if %w[2 6] === LIBXML_VERSION.split('.')[0..1]
599
+
600
+ options[:save_with] ||= SaveOptions::FORMAT |
601
+ SaveOptions::NO_DECLARATION |
602
+ SaveOptions::NO_EMPTY_TAGS |
603
+ SaveOptions::AS_HTML
604
+
605
+ serialize(options)
606
+ end
607
+
608
+ ###
609
+ # Serialize this Node to XML using +options+
610
+ #
611
+ # doc.to_xml(:indent => 5, :encoding => 'UTF-8')
612
+ #
613
+ # See Node#write_to for a list of +options+
614
+ def to_xml options = {}
615
+ encoding = nil
616
+
617
+ options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::AS_XML
618
+
619
+ serialize(options)
620
+ end
621
+
622
+ ###
623
+ # Serialize this Node to XHTML using +options+
624
+ #
625
+ # doc.to_xhtml(:indent => 5, :encoding => 'UTF-8')
626
+ #
627
+ # See Node#write_to for a list of +options+
628
+ def to_xhtml options = {}
629
+ # FIXME: this is a hack around broken libxml versions
630
+ return dump_html if %w[2 6] === LIBXML_VERSION.split('.')[0..1]
631
+
632
+ options[:save_with] ||= SaveOptions::FORMAT |
633
+ SaveOptions::NO_DECLARATION |
634
+ SaveOptions::NO_EMPTY_TAGS |
635
+ SaveOptions::AS_XHTML
636
+
637
+ serialize(options)
638
+ end
639
+
640
+ ###
641
+ # Write Node to +io+ with +options+. +options+ modify the output of
642
+ # this method. Valid options are:
643
+ #
644
+ # * +:encoding+ for changing the encoding
645
+ # * +:indent_text+ the indentation text, defaults to one space
646
+ # * +:indent+ the number of +:indent_text+ to use, defaults to 2
647
+ # * +:save_with+ a combination of SaveOptions constants.
648
+ #
649
+ # To save with UTF-8 indented twice:
650
+ #
651
+ # node.write_to(io, :encoding => 'UTF-8', :indent => 2)
652
+ #
653
+ # To save indented with two dashes:
654
+ #
655
+ # node.write_to(io, :indent_text => '-', :indent => 2
656
+ #
657
+ def write_to io, *options
658
+ options = options.first.is_a?(Hash) ? options.shift : {}
659
+ encoding = options[:encoding] || options[0]
660
+ save_options = options[:save_with] || options[1] || SaveOptions::FORMAT
661
+ indent_text = options[:indent_text] || ' '
662
+ indent_times = options[:indent] || 2
663
+
664
+
665
+ config = SaveOptions.new(save_options)
666
+ yield config if block_given?
667
+
668
+ native_write_to(io, encoding, indent_text * indent_times, config.options)
669
+ end
670
+
671
+ ###
672
+ # Write Node as HTML to +io+ with +options+
673
+ #
674
+ # See Node#write_to for a list of +options+
675
+ def write_html_to io, options = {}
676
+ # FIXME: this is a hack around broken libxml versions
677
+ return (io << dump_html) if %w[2 6] === LIBXML_VERSION.split('.')[0..1]
678
+
679
+ options[:save_with] ||= SaveOptions::FORMAT |
680
+ SaveOptions::NO_DECLARATION |
681
+ SaveOptions::NO_EMPTY_TAGS |
682
+ SaveOptions::AS_HTML
683
+ write_to io, options
684
+ end
685
+
686
+ ###
687
+ # Write Node as XHTML to +io+ with +options+
688
+ #
689
+ # See Node#write_to for a list of +options+
690
+ def write_xhtml_to io, options = {}
691
+ # FIXME: this is a hack around broken libxml versions
692
+ return (io << dump_html) if %w[2 6] === LIBXML_VERSION.split('.')[0..1]
693
+
694
+ options[:save_with] ||= SaveOptions::FORMAT |
695
+ SaveOptions::NO_DECLARATION |
696
+ SaveOptions::NO_EMPTY_TAGS |
697
+ SaveOptions::AS_XHTML
698
+ write_to io, options
699
+ end
700
+
701
+ ###
702
+ # Write Node as XML to +io+ with +options+
703
+ #
704
+ # doc.write_xml_to io, :encoding => 'UTF-8'
705
+ #
706
+ # See Node#write_to for a list of options
707
+ def write_xml_to io, options = {}
708
+ options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::AS_XML
709
+ write_to io, options
710
+ end
711
+
712
+ ###
713
+ # Compare two Node objects with respect to their Document. Nodes from
714
+ # different documents cannot be compared.
715
+ def <=> other
716
+ return nil unless other.is_a?(Nokogiri::XML::Node)
717
+ return nil unless document == other.document
718
+ compare other
719
+ end
720
+
721
+ private
722
+ def self.verify_nodeishness(node)
723
+ if node.is_a?(Document) || !node.is_a?(XML::Node)
724
+ raise ArgumentError, <<-EOERR
725
+ Node.replace requires a Node argument, and cannot accept a Document.
726
+ (You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
727
+ EOERR
728
+ end
729
+ end
730
+
731
+ def inspect_attributes
732
+ [:name, :namespace, :attribute_nodes, :children]
733
+ end
734
+ end
735
+ end
736
+ end