superfeedr-nokogiri 1.4.0.20091116183308

Sign up to get free protection for your applications and to get access to all the features.
Files changed (270) hide show
  1. data/.autotest +27 -0
  2. data/CHANGELOG.ja.rdoc +330 -0
  3. data/CHANGELOG.rdoc +314 -0
  4. data/Manifest.txt +269 -0
  5. data/README.ja.rdoc +105 -0
  6. data/README.rdoc +118 -0
  7. data/Rakefile +244 -0
  8. data/bin/nokogiri +49 -0
  9. data/ext/nokogiri/extconf.rb +145 -0
  10. data/ext/nokogiri/html_document.c +145 -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 +32 -0
  15. data/ext/nokogiri/html_entity_lookup.h +8 -0
  16. data/ext/nokogiri/html_sax_parser_context.c +92 -0
  17. data/ext/nokogiri/html_sax_parser_context.h +11 -0
  18. data/ext/nokogiri/nokogiri.c +89 -0
  19. data/ext/nokogiri/nokogiri.h +145 -0
  20. data/ext/nokogiri/xml_attr.c +92 -0
  21. data/ext/nokogiri/xml_attr.h +9 -0
  22. data/ext/nokogiri/xml_attribute_decl.c +67 -0
  23. data/ext/nokogiri/xml_attribute_decl.h +9 -0
  24. data/ext/nokogiri/xml_cdata.c +54 -0
  25. data/ext/nokogiri/xml_cdata.h +9 -0
  26. data/ext/nokogiri/xml_comment.c +52 -0
  27. data/ext/nokogiri/xml_comment.h +9 -0
  28. data/ext/nokogiri/xml_document.c +388 -0
  29. data/ext/nokogiri/xml_document.h +24 -0
  30. data/ext/nokogiri/xml_document_fragment.c +46 -0
  31. data/ext/nokogiri/xml_document_fragment.h +10 -0
  32. data/ext/nokogiri/xml_dtd.c +192 -0
  33. data/ext/nokogiri/xml_dtd.h +10 -0
  34. data/ext/nokogiri/xml_element_content.c +123 -0
  35. data/ext/nokogiri/xml_element_content.h +10 -0
  36. data/ext/nokogiri/xml_element_decl.c +69 -0
  37. data/ext/nokogiri/xml_element_decl.h +9 -0
  38. data/ext/nokogiri/xml_entity_decl.c +97 -0
  39. data/ext/nokogiri/xml_entity_decl.h +10 -0
  40. data/ext/nokogiri/xml_entity_reference.c +50 -0
  41. data/ext/nokogiri/xml_entity_reference.h +9 -0
  42. data/ext/nokogiri/xml_io.c +31 -0
  43. data/ext/nokogiri/xml_io.h +11 -0
  44. data/ext/nokogiri/xml_namespace.c +74 -0
  45. data/ext/nokogiri/xml_namespace.h +12 -0
  46. data/ext/nokogiri/xml_node.c +1060 -0
  47. data/ext/nokogiri/xml_node.h +13 -0
  48. data/ext/nokogiri/xml_node_set.c +397 -0
  49. data/ext/nokogiri/xml_node_set.h +9 -0
  50. data/ext/nokogiri/xml_processing_instruction.c +54 -0
  51. data/ext/nokogiri/xml_processing_instruction.h +9 -0
  52. data/ext/nokogiri/xml_reader.c +593 -0
  53. data/ext/nokogiri/xml_reader.h +10 -0
  54. data/ext/nokogiri/xml_relax_ng.c +159 -0
  55. data/ext/nokogiri/xml_relax_ng.h +9 -0
  56. data/ext/nokogiri/xml_sax_parser.c +286 -0
  57. data/ext/nokogiri/xml_sax_parser.h +43 -0
  58. data/ext/nokogiri/xml_sax_parser_context.c +155 -0
  59. data/ext/nokogiri/xml_sax_parser_context.h +10 -0
  60. data/ext/nokogiri/xml_sax_push_parser.c +114 -0
  61. data/ext/nokogiri/xml_sax_push_parser.h +9 -0
  62. data/ext/nokogiri/xml_schema.c +156 -0
  63. data/ext/nokogiri/xml_schema.h +9 -0
  64. data/ext/nokogiri/xml_syntax_error.c +261 -0
  65. data/ext/nokogiri/xml_syntax_error.h +13 -0
  66. data/ext/nokogiri/xml_text.c +48 -0
  67. data/ext/nokogiri/xml_text.h +9 -0
  68. data/ext/nokogiri/xml_xpath.c +53 -0
  69. data/ext/nokogiri/xml_xpath.h +11 -0
  70. data/ext/nokogiri/xml_xpath_context.c +239 -0
  71. data/ext/nokogiri/xml_xpath_context.h +9 -0
  72. data/ext/nokogiri/xslt_stylesheet.c +131 -0
  73. data/ext/nokogiri/xslt_stylesheet.h +9 -0
  74. data/lib/nokogiri.rb +116 -0
  75. data/lib/nokogiri/css.rb +25 -0
  76. data/lib/nokogiri/css/generated_parser.rb +646 -0
  77. data/lib/nokogiri/css/generated_tokenizer.rb +142 -0
  78. data/lib/nokogiri/css/node.rb +99 -0
  79. data/lib/nokogiri/css/parser.rb +82 -0
  80. data/lib/nokogiri/css/parser.y +227 -0
  81. data/lib/nokogiri/css/syntax_error.rb +7 -0
  82. data/lib/nokogiri/css/tokenizer.rb +7 -0
  83. data/lib/nokogiri/css/tokenizer.rex +54 -0
  84. data/lib/nokogiri/css/xpath_visitor.rb +162 -0
  85. data/lib/nokogiri/decorators/slop.rb +33 -0
  86. data/lib/nokogiri/ffi/html/document.rb +28 -0
  87. data/lib/nokogiri/ffi/html/element_description.rb +85 -0
  88. data/lib/nokogiri/ffi/html/entity_lookup.rb +16 -0
  89. data/lib/nokogiri/ffi/html/sax/parser_context.rb +38 -0
  90. data/lib/nokogiri/ffi/io_callbacks.rb +42 -0
  91. data/lib/nokogiri/ffi/libxml.rb +356 -0
  92. data/lib/nokogiri/ffi/structs/common_node.rb +26 -0
  93. data/lib/nokogiri/ffi/structs/html_elem_desc.rb +24 -0
  94. data/lib/nokogiri/ffi/structs/html_entity_desc.rb +13 -0
  95. data/lib/nokogiri/ffi/structs/xml_alloc.rb +16 -0
  96. data/lib/nokogiri/ffi/structs/xml_attr.rb +19 -0
  97. data/lib/nokogiri/ffi/structs/xml_attribute.rb +27 -0
  98. data/lib/nokogiri/ffi/structs/xml_buffer.rb +16 -0
  99. data/lib/nokogiri/ffi/structs/xml_document.rb +108 -0
  100. data/lib/nokogiri/ffi/structs/xml_dtd.rb +28 -0
  101. data/lib/nokogiri/ffi/structs/xml_element.rb +26 -0
  102. data/lib/nokogiri/ffi/structs/xml_element_content.rb +17 -0
  103. data/lib/nokogiri/ffi/structs/xml_entity.rb +32 -0
  104. data/lib/nokogiri/ffi/structs/xml_enumeration.rb +12 -0
  105. data/lib/nokogiri/ffi/structs/xml_node.rb +28 -0
  106. data/lib/nokogiri/ffi/structs/xml_node_set.rb +53 -0
  107. data/lib/nokogiri/ffi/structs/xml_notation.rb +11 -0
  108. data/lib/nokogiri/ffi/structs/xml_ns.rb +15 -0
  109. data/lib/nokogiri/ffi/structs/xml_parser_context.rb +19 -0
  110. data/lib/nokogiri/ffi/structs/xml_relax_ng.rb +14 -0
  111. data/lib/nokogiri/ffi/structs/xml_sax_handler.rb +51 -0
  112. data/lib/nokogiri/ffi/structs/xml_sax_push_parser_context.rb +15 -0
  113. data/lib/nokogiri/ffi/structs/xml_schema.rb +13 -0
  114. data/lib/nokogiri/ffi/structs/xml_syntax_error.rb +31 -0
  115. data/lib/nokogiri/ffi/structs/xml_text_reader.rb +12 -0
  116. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +37 -0
  117. data/lib/nokogiri/ffi/structs/xml_xpath_object.rb +35 -0
  118. data/lib/nokogiri/ffi/structs/xml_xpath_parser_context.rb +20 -0
  119. data/lib/nokogiri/ffi/structs/xslt_stylesheet.rb +13 -0
  120. data/lib/nokogiri/ffi/xml/attr.rb +41 -0
  121. data/lib/nokogiri/ffi/xml/attribute_decl.rb +27 -0
  122. data/lib/nokogiri/ffi/xml/cdata.rb +19 -0
  123. data/lib/nokogiri/ffi/xml/comment.rb +18 -0
  124. data/lib/nokogiri/ffi/xml/document.rb +135 -0
  125. data/lib/nokogiri/ffi/xml/document_fragment.rb +21 -0
  126. data/lib/nokogiri/ffi/xml/dtd.rb +69 -0
  127. data/lib/nokogiri/ffi/xml/element_content.rb +43 -0
  128. data/lib/nokogiri/ffi/xml/element_decl.rb +19 -0
  129. data/lib/nokogiri/ffi/xml/entity_decl.rb +27 -0
  130. data/lib/nokogiri/ffi/xml/entity_reference.rb +19 -0
  131. data/lib/nokogiri/ffi/xml/namespace.rb +44 -0
  132. data/lib/nokogiri/ffi/xml/node.rb +444 -0
  133. data/lib/nokogiri/ffi/xml/node_set.rb +133 -0
  134. data/lib/nokogiri/ffi/xml/processing_instruction.rb +20 -0
  135. data/lib/nokogiri/ffi/xml/reader.rb +227 -0
  136. data/lib/nokogiri/ffi/xml/relax_ng.rb +85 -0
  137. data/lib/nokogiri/ffi/xml/sax/parser.rb +142 -0
  138. data/lib/nokogiri/ffi/xml/sax/parser_context.rb +67 -0
  139. data/lib/nokogiri/ffi/xml/sax/push_parser.rb +39 -0
  140. data/lib/nokogiri/ffi/xml/schema.rb +92 -0
  141. data/lib/nokogiri/ffi/xml/syntax_error.rb +91 -0
  142. data/lib/nokogiri/ffi/xml/text.rb +18 -0
  143. data/lib/nokogiri/ffi/xml/xpath.rb +19 -0
  144. data/lib/nokogiri/ffi/xml/xpath_context.rb +135 -0
  145. data/lib/nokogiri/ffi/xslt/stylesheet.rb +47 -0
  146. data/lib/nokogiri/html.rb +35 -0
  147. data/lib/nokogiri/html/builder.rb +35 -0
  148. data/lib/nokogiri/html/document.rb +88 -0
  149. data/lib/nokogiri/html/document_fragment.rb +15 -0
  150. data/lib/nokogiri/html/element_description.rb +23 -0
  151. data/lib/nokogiri/html/entity_lookup.rb +13 -0
  152. data/lib/nokogiri/html/sax/parser.rb +48 -0
  153. data/lib/nokogiri/html/sax/parser_context.rb +16 -0
  154. data/lib/nokogiri/syntax_error.rb +4 -0
  155. data/lib/nokogiri/version.rb +33 -0
  156. data/lib/nokogiri/version_warning.rb +11 -0
  157. data/lib/nokogiri/xml.rb +67 -0
  158. data/lib/nokogiri/xml/attr.rb +14 -0
  159. data/lib/nokogiri/xml/attribute_decl.rb +18 -0
  160. data/lib/nokogiri/xml/builder.rb +405 -0
  161. data/lib/nokogiri/xml/cdata.rb +11 -0
  162. data/lib/nokogiri/xml/character_data.rb +7 -0
  163. data/lib/nokogiri/xml/document.rb +131 -0
  164. data/lib/nokogiri/xml/document_fragment.rb +69 -0
  165. data/lib/nokogiri/xml/dtd.rb +11 -0
  166. data/lib/nokogiri/xml/element_content.rb +36 -0
  167. data/lib/nokogiri/xml/element_decl.rb +13 -0
  168. data/lib/nokogiri/xml/entity_decl.rb +15 -0
  169. data/lib/nokogiri/xml/fragment_handler.rb +71 -0
  170. data/lib/nokogiri/xml/namespace.rb +13 -0
  171. data/lib/nokogiri/xml/node.rb +665 -0
  172. data/lib/nokogiri/xml/node/save_options.rb +42 -0
  173. data/lib/nokogiri/xml/node_set.rb +307 -0
  174. data/lib/nokogiri/xml/notation.rb +6 -0
  175. data/lib/nokogiri/xml/parse_options.rb +85 -0
  176. data/lib/nokogiri/xml/pp.rb +2 -0
  177. data/lib/nokogiri/xml/pp/character_data.rb +18 -0
  178. data/lib/nokogiri/xml/pp/node.rb +56 -0
  179. data/lib/nokogiri/xml/processing_instruction.rb +8 -0
  180. data/lib/nokogiri/xml/reader.rb +74 -0
  181. data/lib/nokogiri/xml/relax_ng.rb +32 -0
  182. data/lib/nokogiri/xml/sax.rb +4 -0
  183. data/lib/nokogiri/xml/sax/document.rb +160 -0
  184. data/lib/nokogiri/xml/sax/parser.rb +115 -0
  185. data/lib/nokogiri/xml/sax/parser_context.rb +16 -0
  186. data/lib/nokogiri/xml/sax/push_parser.rb +60 -0
  187. data/lib/nokogiri/xml/schema.rb +61 -0
  188. data/lib/nokogiri/xml/syntax_error.rb +38 -0
  189. data/lib/nokogiri/xml/xpath.rb +10 -0
  190. data/lib/nokogiri/xml/xpath/syntax_error.rb +8 -0
  191. data/lib/nokogiri/xml/xpath_context.rb +16 -0
  192. data/lib/nokogiri/xslt.rb +48 -0
  193. data/lib/nokogiri/xslt/stylesheet.rb +25 -0
  194. data/lib/xsd/xmlparser/nokogiri.rb +71 -0
  195. data/tasks/test.rb +100 -0
  196. data/test/css/test_nthiness.rb +159 -0
  197. data/test/css/test_parser.rb +277 -0
  198. data/test/css/test_tokenizer.rb +183 -0
  199. data/test/css/test_xpath_visitor.rb +76 -0
  200. data/test/ffi/test_document.rb +35 -0
  201. data/test/files/2ch.html +108 -0
  202. data/test/files/address_book.rlx +12 -0
  203. data/test/files/address_book.xml +10 -0
  204. data/test/files/bar/bar.xsd +4 -0
  205. data/test/files/dont_hurt_em_why.xml +422 -0
  206. data/test/files/exslt.xml +8 -0
  207. data/test/files/exslt.xslt +35 -0
  208. data/test/files/foo/foo.xsd +4 -0
  209. data/test/files/po.xml +32 -0
  210. data/test/files/po.xsd +66 -0
  211. data/test/files/shift_jis.html +10 -0
  212. data/test/files/shift_jis.xml +5 -0
  213. data/test/files/snuggles.xml +3 -0
  214. data/test/files/staff.dtd +10 -0
  215. data/test/files/staff.xml +59 -0
  216. data/test/files/staff.xslt +32 -0
  217. data/test/files/tlm.html +850 -0
  218. data/test/files/valid_bar.xml +2 -0
  219. data/test/helper.rb +136 -0
  220. data/test/html/sax/test_parser.rb +64 -0
  221. data/test/html/sax/test_parser_context.rb +48 -0
  222. data/test/html/test_builder.rb +164 -0
  223. data/test/html/test_document.rb +390 -0
  224. data/test/html/test_document_encoding.rb +77 -0
  225. data/test/html/test_document_fragment.rb +132 -0
  226. data/test/html/test_element_description.rb +94 -0
  227. data/test/html/test_named_characters.rb +14 -0
  228. data/test/html/test_node.rb +228 -0
  229. data/test/html/test_node_encoding.rb +27 -0
  230. data/test/test_convert_xpath.rb +135 -0
  231. data/test/test_css_cache.rb +45 -0
  232. data/test/test_gc.rb +15 -0
  233. data/test/test_memory_leak.rb +77 -0
  234. data/test/test_nokogiri.rb +134 -0
  235. data/test/test_reader.rb +358 -0
  236. data/test/test_xslt_transforms.rb +131 -0
  237. data/test/xml/node/test_save_options.rb +20 -0
  238. data/test/xml/node/test_subclass.rb +44 -0
  239. data/test/xml/sax/test_parser.rb +307 -0
  240. data/test/xml/sax/test_parser_context.rb +56 -0
  241. data/test/xml/sax/test_push_parser.rb +131 -0
  242. data/test/xml/test_attr.rb +38 -0
  243. data/test/xml/test_attribute_decl.rb +82 -0
  244. data/test/xml/test_builder.rb +167 -0
  245. data/test/xml/test_cdata.rb +38 -0
  246. data/test/xml/test_comment.rb +29 -0
  247. data/test/xml/test_document.rb +607 -0
  248. data/test/xml/test_document_encoding.rb +26 -0
  249. data/test/xml/test_document_fragment.rb +138 -0
  250. data/test/xml/test_dtd.rb +82 -0
  251. data/test/xml/test_dtd_encoding.rb +33 -0
  252. data/test/xml/test_element_content.rb +56 -0
  253. data/test/xml/test_element_decl.rb +73 -0
  254. data/test/xml/test_entity_decl.rb +83 -0
  255. data/test/xml/test_entity_reference.rb +21 -0
  256. data/test/xml/test_namespace.rb +68 -0
  257. data/test/xml/test_node.rb +889 -0
  258. data/test/xml/test_node_attributes.rb +34 -0
  259. data/test/xml/test_node_encoding.rb +107 -0
  260. data/test/xml/test_node_set.rb +531 -0
  261. data/test/xml/test_parse_options.rb +52 -0
  262. data/test/xml/test_processing_instruction.rb +30 -0
  263. data/test/xml/test_reader_encoding.rb +126 -0
  264. data/test/xml/test_relax_ng.rb +60 -0
  265. data/test/xml/test_schema.rb +89 -0
  266. data/test/xml/test_syntax_error.rb +27 -0
  267. data/test/xml/test_text.rb +30 -0
  268. data/test/xml/test_unparented_node.rb +381 -0
  269. data/test/xml/test_xpath.rb +106 -0
  270. metadata +430 -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,131 @@
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
+ # Get the list of decorators given +key+
64
+ def decorators key
65
+ @decorators ||= Hash.new
66
+ @decorators[key] ||= []
67
+ end
68
+
69
+ ###
70
+ # Validate this Document against it's DTD. Returns a list of errors on
71
+ # the document or +nil+ when there is no DTD.
72
+ def validate
73
+ return nil unless internal_subset
74
+ internal_subset.validate self
75
+ end
76
+
77
+ ###
78
+ # Explore a document with shortcut methods.
79
+ def slop!
80
+ unless decorators(XML::Node).include? Nokogiri::Decorators::Slop
81
+ decorators(XML::Node) << Nokogiri::Decorators::Slop
82
+ decorate!
83
+ end
84
+
85
+ self
86
+ end
87
+
88
+ ###
89
+ # Apply any decorators to +node+
90
+ def decorate node
91
+ return unless @decorators
92
+ @decorators.each { |klass,list|
93
+ next unless node.is_a?(klass)
94
+ list.each { |moodule| node.extend(moodule) }
95
+ }
96
+ end
97
+
98
+ alias :to_xml :serialize
99
+ alias :clone :dup
100
+
101
+ # Get the hash of namespaces on the root Nokogiri::XML::Node
102
+ def namespaces
103
+ root ? root.namespaces : {}
104
+ end
105
+
106
+ ####
107
+ # Create a Nokogiri::XML::DocumentFragment from +tags+
108
+ # Returns an empty fragment if +tags+ is nil.
109
+ def fragment tags = nil
110
+ DocumentFragment.new(self, tags)
111
+ end
112
+
113
+ undef_method :swap, :parent, :namespace, :default_namespace=
114
+ undef_method :add_namespace_definition, :attributes
115
+ undef_method :namespace_definitions, :line
116
+
117
+ def add_child child
118
+ if [Node::ELEMENT_NODE, Node::DOCUMENT_FRAG_NODE].include? child.type
119
+ raise "Document already has a root node" if root
120
+ end
121
+ super
122
+ end
123
+ alias :<< :add_child
124
+
125
+ private
126
+ def inspect_attributes
127
+ [:name, :children]
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,69 @@
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
+ children.css(*args)
55
+ end
56
+
57
+ alias :serialize :to_s
58
+
59
+ class << self
60
+ ####
61
+ # Create a Nokogiri::XML::DocumentFragment from +tags+
62
+ def parse tags
63
+ self.new(XML::Document.new, tags)
64
+ end
65
+ end
66
+
67
+ end
68
+ end
69
+ 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,71 @@
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
+
12
+ # the regexes used in start_element() and characters() anchor at
13
+ # start-of-line, but we really only want them to anchor at
14
+ # start-of-doc. so let's only save up to the first newline.
15
+ #
16
+ # this implementation choice was the result of some benchmarks, if
17
+ # you're curious: http://gist.github.com/115936
18
+ #
19
+ @original_html = original_html.lstrip
20
+ newline_index = @original_html.index("\n")
21
+ @original_html = @original_html[0,newline_index] if newline_index
22
+ end
23
+
24
+ def start_element name, attrs = []
25
+ regex = @html_eh ? %r{^\s*<#{Regexp.escape(name)}}i :
26
+ %r{^\s*<#{Regexp.escape(name)}}
27
+
28
+ @doc_started = true if @original_html =~ regex
29
+ return unless @doc_started
30
+
31
+ if match = name.match(QNAME_REGEX)
32
+ prefix, name = match[1], match[2]
33
+ ns = @document.root.namespace_definitions.detect { |x|
34
+ x.prefix == prefix
35
+ }
36
+ else
37
+ ns = nil
38
+ end
39
+
40
+ node = Element.new(name, @document)
41
+ attrs << "" unless (attrs.length % 2) == 0
42
+ Hash[*attrs].each do |k,v|
43
+ node[k] = v
44
+ end
45
+
46
+ node.namespace = ns if ns
47
+
48
+ @stack.last << node
49
+ @stack << node
50
+ end
51
+
52
+ def characters string
53
+ @doc_started = true if @original_html.strip =~ %r{^\s*#{Regexp.escape(string.strip)}}
54
+ @stack.last << Text.new(string, @document)
55
+ end
56
+
57
+ def comment string
58
+ @stack.last << Comment.new(@document, string)
59
+ end
60
+
61
+ def cdata_block string
62
+ @stack.last << CDATA.new(@document, string)
63
+ end
64
+
65
+ def end_element name
66
+ return unless @stack.last.name == name
67
+ @stack.pop
68
+ end
69
+ end
70
+ end
71
+ 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,665 @@
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 for the first occurrence of +path+.
197
+ # Returns nil if nothing is found, otherwise a Node.
198
+ def at path, ns = document.root ? document.root.namespaces : {}
199
+ search(path, ns).first
200
+ end
201
+ alias :% :at
202
+
203
+ ##
204
+ # Search this node for the first occurrence of XPath +paths+.
205
+ # Equivalent to <tt>xpath(paths).first</tt>
206
+ # See Node#xpath for more information.
207
+ #
208
+ def at_xpath *paths
209
+ xpath(*paths).first
210
+ end
211
+
212
+ ##
213
+ # Search this node for the first occurrence of CSS +rules+.
214
+ # Equivalent to <tt>css(rules).first</tt>
215
+ # See Node#css for more information.
216
+ #
217
+ def at_css *rules
218
+ css(*rules).first
219
+ end
220
+
221
+ ###
222
+ # Get the attribute value for the attribute +name+
223
+ def [] name
224
+ return nil unless key?(name.to_s)
225
+ get(name.to_s)
226
+ end
227
+
228
+ alias :next :next_sibling
229
+ alias :previous :previous_sibling
230
+ alias :remove :unlink
231
+ alias :get_attribute :[]
232
+ alias :set_attribute :[]=
233
+ alias :text :content
234
+ alias :inner_text :content
235
+ alias :has_attribute? :key?
236
+ alias :<< :add_child
237
+ alias :name :node_name
238
+ alias :name= :node_name=
239
+ alias :type :node_type
240
+ alias :to_str :text
241
+ alias :clone :dup
242
+
243
+ ####
244
+ # Returns a hash containing the node's attributes. The key is the
245
+ # attribute name, the value is the string value of the attribute.
246
+ def attributes
247
+ Hash[*(attribute_nodes.map { |node|
248
+ [node.node_name, node]
249
+ }.flatten)]
250
+ end
251
+
252
+ ###
253
+ # Get the attribute values for this Node.
254
+ def values
255
+ attribute_nodes.map { |node| node.value }
256
+ end
257
+
258
+ ###
259
+ # Get the attribute names for this Node.
260
+ def keys
261
+ attribute_nodes.map { |node| node.node_name }
262
+ end
263
+
264
+ ###
265
+ # Iterate over each attribute name and value pair for this Node.
266
+ def each &block
267
+ attribute_nodes.each { |node|
268
+ block.call(node.node_name, node.value)
269
+ }
270
+ end
271
+
272
+ ###
273
+ # Remove the attribute named +name+
274
+ def remove_attribute name
275
+ attributes[name].remove if key? name
276
+ end
277
+ alias :delete :remove_attribute
278
+
279
+ ###
280
+ # Returns true if this Node matches +selector+
281
+ def matches? selector
282
+ document.search(selector).include?(self)
283
+ end
284
+
285
+ ####
286
+ # Create nodes from +data+ and insert them before this node
287
+ # (as a sibling).
288
+ def before data
289
+ fragment(data).children.each do |node|
290
+ add_previous_sibling node
291
+ end
292
+ self
293
+ end
294
+
295
+ ####
296
+ # Create nodes from +data+ and insert them after this node
297
+ # (as a sibling).
298
+ def after data
299
+ fragment(data).children.to_a.reverse.each do |node|
300
+ add_next_sibling node
301
+ end
302
+ self
303
+ end
304
+
305
+ ####
306
+ # Swap this Node for new nodes made from +data+
307
+ def swap data
308
+ before(data)
309
+ remove
310
+ self
311
+ end
312
+
313
+ ####
314
+ # Set the inner_html for this Node to +tags+
315
+ def inner_html= tags
316
+ children.each { |x| x.remove}
317
+
318
+ fragment(tags).children.to_a.each do |node|
319
+ add_child node
320
+ end
321
+ self
322
+ end
323
+
324
+ def fragment tags # :nodoc:
325
+ # TODO: deprecate?
326
+ document.fragment(tags)
327
+ end
328
+
329
+ ####
330
+ # Set the Node content to +string+. The content gets XML escaped.
331
+ def content= string
332
+ self.native_content = encode_special_chars(string.to_s)
333
+ end
334
+
335
+ ###
336
+ # Set the parent Node for this Node
337
+ def parent= parent_node
338
+ parent_node.add_child(self)
339
+ parent_node
340
+ end
341
+
342
+ ###
343
+ # Get a hash containing the Namespace definitions for this Node
344
+ def namespaces
345
+ Hash[*namespace_definitions.map { |nd|
346
+ key = ['xmlns', nd.prefix].compact.join(':')
347
+ if RUBY_VERSION >= '1.9' && document.encoding
348
+ begin
349
+ key.force_encoding document.encoding
350
+ rescue ArgumentError
351
+ end
352
+ end
353
+ [key, nd.href]
354
+ }.flatten]
355
+ end
356
+
357
+ # Returns true if this is a Comment
358
+ def comment?
359
+ type == COMMENT_NODE
360
+ end
361
+
362
+ # Returns true if this is a CDATA
363
+ def cdata?
364
+ type == CDATA_SECTION_NODE
365
+ end
366
+
367
+ # Returns true if this is an XML::Document node
368
+ def xml?
369
+ type == DOCUMENT_NODE
370
+ end
371
+
372
+ # Returns true if this is an HTML::Document node
373
+ def html?
374
+ type == HTML_DOCUMENT_NODE
375
+ end
376
+
377
+ # Returns true if this is a Text node
378
+ def text?
379
+ type == TEXT_NODE
380
+ end
381
+
382
+ ###
383
+ # Fetch the Nokogiri::HTML::ElementDescription for this node. Returns
384
+ # nil on XML documents and on unknown tags.
385
+ def description
386
+ return nil if document.xml?
387
+ Nokogiri::HTML::ElementDescription[name]
388
+ end
389
+
390
+ ###
391
+ # Is this a read only node?
392
+ def read_only?
393
+ # According to gdome2, these are read-only node types
394
+ [NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
395
+ end
396
+
397
+ # Returns true if this is an Element node
398
+ def element?
399
+ type == ELEMENT_NODE
400
+ end
401
+ alias :elem? :element?
402
+
403
+ ###
404
+ # Turn this node in to a string. If the document is HTML, this method
405
+ # returns html. If the document is XML, this method returns XML.
406
+ def to_s
407
+ document.xml? ? to_xml : to_html
408
+ end
409
+
410
+ # Get the inner_html for this node's Node#children
411
+ def inner_html *args
412
+ children.map { |x| x.to_html(*args) }.join
413
+ end
414
+
415
+ # Get the path to this node as a CSS expression
416
+ def css_path
417
+ path.split(/\//).map { |part|
418
+ part.length == 0 ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
419
+ }.compact.join(' > ')
420
+ end
421
+
422
+ ###
423
+ # Get a list of ancestor Node for this Node. If +selector+ is given,
424
+ # the ancestors must match +selector+
425
+ def ancestors selector = nil
426
+ return NodeSet.new(document) unless respond_to?(:parent)
427
+ return NodeSet.new(document) unless parent
428
+
429
+ parents = [parent]
430
+
431
+ while parents.last.respond_to?(:parent)
432
+ break unless ctx_parent = parents.last.parent
433
+ parents << ctx_parent
434
+ end
435
+
436
+ return NodeSet.new(document, parents) unless selector
437
+
438
+ NodeSet.new(document, parents.find_all { |parent|
439
+ parent.matches?(selector)
440
+ })
441
+ end
442
+
443
+ ###
444
+ # Set the default namespace for this node to +url+
445
+ def default_namespace= url
446
+ add_namespace_definition(nil, url)
447
+ end
448
+ alias :add_namespace :add_namespace_definition
449
+
450
+ ###
451
+ # Set the namespace for this node to +ns+
452
+ def namespace= ns
453
+ if ns.document != document
454
+ raise ArgumentError, 'namespace must be declared on the same document'
455
+ end
456
+ unless ns.is_a? Nokogiri::XML::Namespace
457
+ raise TypeError, "#{ns.class} can't be coerced into Nokogiri::XML::Namespace"
458
+ end
459
+ set_namespace ns
460
+ end
461
+
462
+ ####
463
+ # Yields self and all children to +block+ recursively.
464
+ def traverse &block
465
+ children.each{|j| j.traverse(&block) }
466
+ block.call(self)
467
+ end
468
+
469
+ ###
470
+ # Accept a visitor. This method calls "visit" on +visitor+ with self.
471
+ def accept visitor
472
+ visitor.visit(self)
473
+ end
474
+
475
+ ####
476
+ # +replace+ this Node with the +new_node+ in the Document. The new node
477
+ # must be a Nokogiri::XML::Node or a non-empty String
478
+ def replace new_node
479
+ if new_node.is_a?(Document) || !new_node.is_a?(XML::Node)
480
+ raise ArgumentError, <<-EOERR
481
+ Node.replace requires a Node argument, and cannot accept a Document.
482
+ (You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
483
+ EOERR
484
+ end
485
+ replace_with_node new_node
486
+ end
487
+
488
+ ###
489
+ # Test to see if this Node is equal to +other+
490
+ def == other
491
+ return false unless other
492
+ return false unless other.respond_to?(:pointer_id)
493
+ pointer_id == other.pointer_id
494
+ end
495
+
496
+ ###
497
+ # Serialize Node using +options+. Save options can also be set using a
498
+ # block. See SaveOptions.
499
+ #
500
+ # These two statements are equivalent:
501
+ #
502
+ # node.serialize(:encoding => 'UTF-8', :save_with => FORMAT | AS_XML)
503
+ #
504
+ # or
505
+ #
506
+ # node.serialize(:encoding => 'UTF-8') do |config|
507
+ # config.format.as_xml
508
+ # end
509
+ #
510
+ def serialize *args, &block
511
+ options = args.first.is_a?(Hash) ? args.shift : {
512
+ :encoding => args[0],
513
+ :save_with => args[1] || SaveOptions::FORMAT
514
+ }
515
+
516
+ encoding = options[:encoding] || document.encoding
517
+
518
+ outstring = ""
519
+ if encoding && outstring.respond_to?(:force_encoding)
520
+ outstring.force_encoding(Encoding.find(encoding))
521
+ end
522
+ io = StringIO.new(outstring)
523
+ write_to io, options, &block
524
+ io.string
525
+ end
526
+
527
+ ###
528
+ # Serialize this Node to HTML
529
+ #
530
+ # doc.to_html
531
+ #
532
+ # See Node#write_to for a list of +options+. For formatted output,
533
+ # use Node#to_xhtml instead.
534
+ def to_html options = {}
535
+ # FIXME: this is a hack around broken libxml versions
536
+ return dump_html if %w[2 6] === LIBXML_VERSION.split('.')[0..1]
537
+
538
+ options[:save_with] ||= SaveOptions::FORMAT |
539
+ SaveOptions::NO_DECLARATION |
540
+ SaveOptions::NO_EMPTY_TAGS |
541
+ SaveOptions::AS_HTML
542
+
543
+ serialize(options)
544
+ end
545
+
546
+ ###
547
+ # Serialize this Node to XML using +options+
548
+ #
549
+ # doc.to_xml(:indent => 5, :encoding => 'UTF-8')
550
+ #
551
+ # See Node#write_to for a list of +options+
552
+ def to_xml options = {}
553
+ encoding = nil
554
+
555
+ options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::AS_XML
556
+
557
+ serialize(options)
558
+ end
559
+
560
+ ###
561
+ # Serialize this Node to XHTML using +options+
562
+ #
563
+ # doc.to_xhtml(:indent => 5, :encoding => 'UTF-8')
564
+ #
565
+ # See Node#write_to for a list of +options+
566
+ def to_xhtml options = {}
567
+ # FIXME: this is a hack around broken libxml versions
568
+ return dump_html if %w[2 6] === LIBXML_VERSION.split('.')[0..1]
569
+
570
+ options[:save_with] ||= SaveOptions::FORMAT |
571
+ SaveOptions::NO_DECLARATION |
572
+ SaveOptions::NO_EMPTY_TAGS |
573
+ SaveOptions::AS_XHTML
574
+
575
+ serialize(options)
576
+ end
577
+
578
+ ###
579
+ # Write Node to +io+ with +options+. +options+ modify the output of
580
+ # this method. Valid options are:
581
+ #
582
+ # * +:encoding+ for changing the encoding
583
+ # * +:indent_text+ the indentation text, defaults to one space
584
+ # * +:indent+ the number of +:indent_text+ to use, defaults to 2
585
+ # * +:save_with+ a combination of SaveOptions constants.
586
+ #
587
+ # To save with UTF-8 indented twice:
588
+ #
589
+ # node.write_to(io, :encoding => 'UTF-8', :indent => 2)
590
+ #
591
+ # To save indented with two dashes:
592
+ #
593
+ # node.write_to(io, :indent_text => '-', :indent => 2
594
+ #
595
+ def write_to io, *options
596
+ options = options.first.is_a?(Hash) ? options.shift : {}
597
+ encoding = options[:encoding] || options[0]
598
+ save_options = options[:save_with] || options[1] || SaveOptions::FORMAT
599
+ indent_text = options[:indent_text] || ' '
600
+ indent_times = options[:indent] || 2
601
+
602
+
603
+ config = SaveOptions.new(save_options)
604
+ yield config if block_given?
605
+
606
+ native_write_to(io, encoding, indent_text * indent_times, config.options)
607
+ end
608
+
609
+ ###
610
+ # Write Node as HTML to +io+ with +options+
611
+ #
612
+ # See Node#write_to for a list of +options+
613
+ def write_html_to io, options = {}
614
+ # FIXME: this is a hack around broken libxml versions
615
+ return (io << dump_html) if %w[2 6] === LIBXML_VERSION.split('.')[0..1]
616
+
617
+ options[:save_with] ||= SaveOptions::FORMAT |
618
+ SaveOptions::NO_DECLARATION |
619
+ SaveOptions::NO_EMPTY_TAGS |
620
+ SaveOptions::AS_HTML
621
+ write_to io, options
622
+ end
623
+
624
+ ###
625
+ # Write Node as XHTML to +io+ with +options+
626
+ #
627
+ # See Node#write_to for a list of +options+
628
+ def write_xhtml_to io, options = {}
629
+ # FIXME: this is a hack around broken libxml versions
630
+ return (io << 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
+ write_to io, options
637
+ end
638
+
639
+ ###
640
+ # Write Node as XML to +io+ with +options+
641
+ #
642
+ # doc.write_xml_to io, :encoding => 'UTF-8'
643
+ #
644
+ # See Node#write_to for a list of options
645
+ def write_xml_to io, options = {}
646
+ options[:save_with] ||= SaveOptions::FORMAT | SaveOptions::AS_XML
647
+ write_to io, options
648
+ end
649
+
650
+ ###
651
+ # Compare two Node objects with respect to their Document. Nodes from
652
+ # different documents cannot be compared.
653
+ def <=> other
654
+ return nil unless other.is_a?(Nokogiri::XML::Node)
655
+ return nil unless document == other.document
656
+ compare other
657
+ end
658
+
659
+ private
660
+ def inspect_attributes
661
+ [:name, :namespace, :attribute_nodes, :children]
662
+ end
663
+ end
664
+ end
665
+ end