nokogiri 1.11.0.rc2-x86-linux → 1.11.2-x86-linux

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (175) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/LICENSE-DEPENDENCIES.md +1015 -947
  4. data/LICENSE.md +1 -1
  5. data/README.md +171 -94
  6. data/ext/nokogiri/depend +37 -358
  7. data/ext/nokogiri/extconf.rb +581 -374
  8. data/ext/nokogiri/html_document.c +78 -82
  9. data/ext/nokogiri/html_element_description.c +84 -71
  10. data/ext/nokogiri/html_entity_lookup.c +21 -16
  11. data/ext/nokogiri/html_sax_parser_context.c +69 -66
  12. data/ext/nokogiri/html_sax_push_parser.c +42 -34
  13. data/ext/nokogiri/include/libexslt/exslt.h +102 -0
  14. data/ext/nokogiri/include/libexslt/exsltconfig.h +70 -0
  15. data/ext/nokogiri/include/libexslt/exsltexports.h +140 -0
  16. data/ext/nokogiri/include/libxml2/libxml/DOCBparser.h +96 -0
  17. data/ext/nokogiri/include/libxml2/libxml/HTMLparser.h +306 -0
  18. data/ext/nokogiri/include/libxml2/libxml/HTMLtree.h +147 -0
  19. data/ext/nokogiri/include/libxml2/libxml/SAX.h +173 -0
  20. data/ext/nokogiri/include/libxml2/libxml/SAX2.h +178 -0
  21. data/ext/nokogiri/include/libxml2/libxml/c14n.h +126 -0
  22. data/ext/nokogiri/include/libxml2/libxml/catalog.h +182 -0
  23. data/ext/nokogiri/include/libxml2/libxml/chvalid.h +230 -0
  24. data/ext/nokogiri/include/libxml2/libxml/debugXML.h +217 -0
  25. data/ext/nokogiri/include/libxml2/libxml/dict.h +79 -0
  26. data/ext/nokogiri/include/libxml2/libxml/encoding.h +245 -0
  27. data/ext/nokogiri/include/libxml2/libxml/entities.h +151 -0
  28. data/ext/nokogiri/include/libxml2/libxml/globals.h +508 -0
  29. data/ext/nokogiri/include/libxml2/libxml/hash.h +236 -0
  30. data/ext/nokogiri/include/libxml2/libxml/list.h +137 -0
  31. data/ext/nokogiri/include/libxml2/libxml/nanoftp.h +163 -0
  32. data/ext/nokogiri/include/libxml2/libxml/nanohttp.h +81 -0
  33. data/ext/nokogiri/include/libxml2/libxml/parser.h +1241 -0
  34. data/ext/nokogiri/include/libxml2/libxml/parserInternals.h +644 -0
  35. data/ext/nokogiri/include/libxml2/libxml/pattern.h +100 -0
  36. data/ext/nokogiri/include/libxml2/libxml/relaxng.h +217 -0
  37. data/ext/nokogiri/include/libxml2/libxml/schemasInternals.h +958 -0
  38. data/ext/nokogiri/include/libxml2/libxml/schematron.h +142 -0
  39. data/ext/nokogiri/include/libxml2/libxml/threads.h +89 -0
  40. data/ext/nokogiri/include/libxml2/libxml/tree.h +1311 -0
  41. data/ext/nokogiri/include/libxml2/libxml/uri.h +94 -0
  42. data/ext/nokogiri/include/libxml2/libxml/valid.h +458 -0
  43. data/ext/nokogiri/include/libxml2/libxml/xinclude.h +129 -0
  44. data/ext/nokogiri/include/libxml2/libxml/xlink.h +189 -0
  45. data/ext/nokogiri/include/libxml2/libxml/xmlIO.h +366 -0
  46. data/ext/nokogiri/include/libxml2/libxml/xmlautomata.h +146 -0
  47. data/ext/nokogiri/include/libxml2/libxml/xmlerror.h +945 -0
  48. data/ext/nokogiri/include/libxml2/libxml/xmlexports.h +153 -0
  49. data/ext/nokogiri/include/libxml2/libxml/xmlmemory.h +224 -0
  50. data/ext/nokogiri/include/libxml2/libxml/xmlmodule.h +57 -0
  51. data/ext/nokogiri/include/libxml2/libxml/xmlreader.h +428 -0
  52. data/ext/nokogiri/include/libxml2/libxml/xmlregexp.h +222 -0
  53. data/ext/nokogiri/include/libxml2/libxml/xmlsave.h +88 -0
  54. data/ext/nokogiri/include/libxml2/libxml/xmlschemas.h +246 -0
  55. data/ext/nokogiri/include/libxml2/libxml/xmlschemastypes.h +151 -0
  56. data/ext/nokogiri/include/libxml2/libxml/xmlstring.h +140 -0
  57. data/ext/nokogiri/include/libxml2/libxml/xmlunicode.h +202 -0
  58. data/ext/nokogiri/include/libxml2/libxml/xmlversion.h +485 -0
  59. data/ext/nokogiri/include/libxml2/libxml/xmlwriter.h +488 -0
  60. data/ext/nokogiri/include/libxml2/libxml/xpath.h +566 -0
  61. data/ext/nokogiri/include/libxml2/libxml/xpathInternals.h +632 -0
  62. data/ext/nokogiri/include/libxml2/libxml/xpointer.h +114 -0
  63. data/ext/nokogiri/include/libxslt/attributes.h +38 -0
  64. data/ext/nokogiri/include/libxslt/documents.h +93 -0
  65. data/ext/nokogiri/include/libxslt/extensions.h +262 -0
  66. data/ext/nokogiri/include/libxslt/extra.h +72 -0
  67. data/ext/nokogiri/include/libxslt/functions.h +78 -0
  68. data/ext/nokogiri/include/libxslt/imports.h +75 -0
  69. data/ext/nokogiri/include/libxslt/keys.h +53 -0
  70. data/ext/nokogiri/include/libxslt/namespaces.h +68 -0
  71. data/ext/nokogiri/include/libxslt/numbersInternals.h +73 -0
  72. data/ext/nokogiri/include/libxslt/pattern.h +84 -0
  73. data/ext/nokogiri/include/libxslt/preproc.h +43 -0
  74. data/ext/nokogiri/include/libxslt/security.h +104 -0
  75. data/ext/nokogiri/include/libxslt/templates.h +77 -0
  76. data/ext/nokogiri/include/libxslt/transform.h +207 -0
  77. data/ext/nokogiri/include/libxslt/variables.h +118 -0
  78. data/ext/nokogiri/include/libxslt/xslt.h +110 -0
  79. data/ext/nokogiri/include/libxslt/xsltInternals.h +1978 -0
  80. data/ext/nokogiri/include/libxslt/xsltconfig.h +180 -0
  81. data/ext/nokogiri/include/libxslt/xsltexports.h +142 -0
  82. data/ext/nokogiri/include/libxslt/xsltlocale.h +76 -0
  83. data/ext/nokogiri/include/libxslt/xsltutils.h +313 -0
  84. data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
  85. data/ext/nokogiri/nokogiri.c +192 -93
  86. data/ext/nokogiri/nokogiri.h +177 -98
  87. data/ext/nokogiri/test_global_handlers.c +40 -0
  88. data/ext/nokogiri/xml_attr.c +15 -15
  89. data/ext/nokogiri/xml_attribute_decl.c +18 -18
  90. data/ext/nokogiri/xml_cdata.c +13 -18
  91. data/ext/nokogiri/xml_comment.c +19 -26
  92. data/ext/nokogiri/xml_document.c +225 -163
  93. data/ext/nokogiri/xml_document_fragment.c +13 -15
  94. data/ext/nokogiri/xml_dtd.c +54 -48
  95. data/ext/nokogiri/xml_element_content.c +30 -27
  96. data/ext/nokogiri/xml_element_decl.c +22 -22
  97. data/ext/nokogiri/xml_encoding_handler.c +17 -11
  98. data/ext/nokogiri/xml_entity_decl.c +32 -30
  99. data/ext/nokogiri/xml_entity_reference.c +16 -18
  100. data/ext/nokogiri/xml_namespace.c +56 -49
  101. data/ext/nokogiri/xml_node.c +338 -286
  102. data/ext/nokogiri/xml_node_set.c +168 -156
  103. data/ext/nokogiri/xml_processing_instruction.c +17 -19
  104. data/ext/nokogiri/xml_reader.c +191 -157
  105. data/ext/nokogiri/xml_relax_ng.c +52 -28
  106. data/ext/nokogiri/xml_sax_parser.c +118 -118
  107. data/ext/nokogiri/xml_sax_parser_context.c +103 -86
  108. data/ext/nokogiri/xml_sax_push_parser.c +36 -27
  109. data/ext/nokogiri/xml_schema.c +95 -47
  110. data/ext/nokogiri/xml_syntax_error.c +42 -21
  111. data/ext/nokogiri/xml_text.c +13 -17
  112. data/ext/nokogiri/xml_xpath_context.c +206 -123
  113. data/ext/nokogiri/xslt_stylesheet.c +158 -161
  114. data/lib/nokogiri.rb +4 -8
  115. data/lib/nokogiri/2.5/nokogiri.so +0 -0
  116. data/lib/nokogiri/2.6/nokogiri.so +0 -0
  117. data/lib/nokogiri/2.7/nokogiri.so +0 -0
  118. data/lib/nokogiri/3.0/nokogiri.so +0 -0
  119. data/lib/nokogiri/css/parser.rb +62 -62
  120. data/lib/nokogiri/css/parser.y +2 -2
  121. data/lib/nokogiri/css/xpath_visitor.rb +70 -42
  122. data/lib/nokogiri/extension.rb +26 -0
  123. data/lib/nokogiri/html/document.rb +12 -26
  124. data/lib/nokogiri/html/document_fragment.rb +15 -15
  125. data/lib/nokogiri/version.rb +2 -148
  126. data/lib/nokogiri/version/constant.rb +5 -0
  127. data/lib/nokogiri/version/info.rb +205 -0
  128. data/lib/nokogiri/xml/builder.rb +2 -2
  129. data/lib/nokogiri/xml/document.rb +48 -18
  130. data/lib/nokogiri/xml/document_fragment.rb +4 -6
  131. data/lib/nokogiri/xml/node.rb +88 -69
  132. data/lib/nokogiri/xml/parse_options.rb +6 -0
  133. data/lib/nokogiri/xml/reader.rb +2 -9
  134. data/lib/nokogiri/xml/relax_ng.rb +6 -2
  135. data/lib/nokogiri/xml/schema.rb +12 -4
  136. data/lib/nokogiri/xml/searchable.rb +3 -1
  137. data/lib/nokogiri/xml/xpath.rb +1 -3
  138. data/lib/nokogiri/xml/xpath/syntax_error.rb +1 -1
  139. metadata +151 -158
  140. data/ext/nokogiri/html_document.h +0 -10
  141. data/ext/nokogiri/html_element_description.h +0 -10
  142. data/ext/nokogiri/html_entity_lookup.h +0 -8
  143. data/ext/nokogiri/html_sax_parser_context.h +0 -11
  144. data/ext/nokogiri/html_sax_push_parser.h +0 -9
  145. data/ext/nokogiri/xml_attr.h +0 -9
  146. data/ext/nokogiri/xml_attribute_decl.h +0 -9
  147. data/ext/nokogiri/xml_cdata.h +0 -9
  148. data/ext/nokogiri/xml_comment.h +0 -9
  149. data/ext/nokogiri/xml_document.h +0 -23
  150. data/ext/nokogiri/xml_document_fragment.h +0 -10
  151. data/ext/nokogiri/xml_dtd.h +0 -10
  152. data/ext/nokogiri/xml_element_content.h +0 -10
  153. data/ext/nokogiri/xml_element_decl.h +0 -9
  154. data/ext/nokogiri/xml_encoding_handler.h +0 -8
  155. data/ext/nokogiri/xml_entity_decl.h +0 -10
  156. data/ext/nokogiri/xml_entity_reference.h +0 -9
  157. data/ext/nokogiri/xml_io.c +0 -61
  158. data/ext/nokogiri/xml_io.h +0 -11
  159. data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
  160. data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
  161. data/ext/nokogiri/xml_namespace.h +0 -14
  162. data/ext/nokogiri/xml_node.h +0 -13
  163. data/ext/nokogiri/xml_node_set.h +0 -12
  164. data/ext/nokogiri/xml_processing_instruction.h +0 -9
  165. data/ext/nokogiri/xml_reader.h +0 -10
  166. data/ext/nokogiri/xml_relax_ng.h +0 -9
  167. data/ext/nokogiri/xml_sax_parser.h +0 -39
  168. data/ext/nokogiri/xml_sax_parser_context.h +0 -10
  169. data/ext/nokogiri/xml_sax_push_parser.h +0 -9
  170. data/ext/nokogiri/xml_schema.h +0 -9
  171. data/ext/nokogiri/xml_syntax_error.h +0 -13
  172. data/ext/nokogiri/xml_text.h +0 -9
  173. data/ext/nokogiri/xml_xpath_context.h +0 -10
  174. data/ext/nokogiri/xslt_stylesheet.h +0 -14
  175. data/lib/nokogiri/2.4/nokogiri.so +0 -0
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ module Nokogiri
3
+ # The version of Nokogiri you are using
4
+ VERSION = "1.11.2"
5
+ end
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+ require "singleton"
3
+ require "shellwords"
4
+
5
+ module Nokogiri
6
+ class VersionInfo # :nodoc:
7
+ include Singleton
8
+
9
+ def jruby?
10
+ ::JRUBY_VERSION if ::RUBY_PLATFORM == "java"
11
+ end
12
+
13
+ def windows?
14
+ ::RUBY_PLATFORM =~ /mingw|mswin/
15
+ end
16
+
17
+ def ruby_minor
18
+ Gem::Version.new(::RUBY_VERSION).segments[0..1].join(".")
19
+ end
20
+
21
+ def engine
22
+ defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : "mri"
23
+ end
24
+
25
+ def loaded_libxml_version
26
+ Gem::Version.new(Nokogiri::LIBXML_LOADED_VERSION
27
+ .scan(/^(\d+)(\d\d)(\d\d)(?!\d)/).first
28
+ .collect(&:to_i)
29
+ .join("."))
30
+ end
31
+
32
+ def compiled_libxml_version
33
+ Gem::Version.new(Nokogiri::LIBXML_COMPILED_VERSION)
34
+ end
35
+
36
+ def loaded_libxslt_version
37
+ Gem::Version.new(Nokogiri::LIBXSLT_LOADED_VERSION
38
+ .scan(/^(\d+)(\d\d)(\d\d)(?!\d)/).first
39
+ .collect(&:to_i)
40
+ .join("."))
41
+ end
42
+
43
+ def compiled_libxslt_version
44
+ Gem::Version.new(Nokogiri::LIBXSLT_COMPILED_VERSION)
45
+ end
46
+
47
+ def libxml2?
48
+ defined?(Nokogiri::LIBXML_COMPILED_VERSION)
49
+ end
50
+
51
+ def libxml2_has_iconv?
52
+ defined?(Nokogiri::LIBXML_ICONV_ENABLED) && Nokogiri::LIBXML_ICONV_ENABLED
53
+ end
54
+
55
+ def libxml2_using_packaged?
56
+ libxml2? && Nokogiri::PACKAGED_LIBRARIES
57
+ end
58
+
59
+ def libxml2_using_system?
60
+ libxml2? && !libxml2_using_packaged?
61
+ end
62
+
63
+ def libxml2_precompiled?
64
+ libxml2_using_packaged? && Nokogiri::PRECOMPILED_LIBRARIES
65
+ end
66
+
67
+ def warnings
68
+ warnings = []
69
+
70
+ if libxml2?
71
+ if compiled_libxml_version != loaded_libxml_version
72
+ warnings << "Nokogiri was built against libxml version #{compiled_libxml_version}, but has dynamically loaded #{loaded_libxml_version}"
73
+ end
74
+
75
+ if compiled_libxslt_version != loaded_libxslt_version
76
+ warnings << "Nokogiri was built against libxslt version #{compiled_libxslt_version}, but has dynamically loaded #{loaded_libxslt_version}"
77
+ end
78
+ end
79
+
80
+ warnings
81
+ end
82
+
83
+ def to_hash
84
+ header_directory = File.expand_path(File.join(File.dirname(__FILE__), "../../../ext/nokogiri"))
85
+
86
+ {}.tap do |vi|
87
+ vi["warnings"] = []
88
+ vi["nokogiri"] = {}.tap do |nokogiri|
89
+ nokogiri["version"] = Nokogiri::VERSION
90
+
91
+ unless jruby?
92
+ # enable gems like nokogumbo to build with the following in their extconf.rb:
93
+ #
94
+ # append_cflags(Nokogiri::VERSION_INFO["nokogiri"]["cppflags"])
95
+ # append_ldflags(Nokogiri::VERSION_INFO["nokogiri"]["ldflags"])
96
+ #
97
+ cppflags = ["-I#{header_directory.shellescape}"]
98
+ ldflags = []
99
+
100
+ if libxml2_using_packaged?
101
+ cppflags << "-I#{File.join(header_directory, 'include').shellescape}"
102
+ cppflags << "-I#{File.join(header_directory, 'include/libxml2').shellescape}"
103
+
104
+ if windows?
105
+ # on windows, nokogumbo needs to link against nokogiri.so to resolve symbols. see #2167
106
+ lib_directory = File.expand_path(File.join(File.dirname(__FILE__), "../#{ruby_minor}"))
107
+ unless File.exist?(lib_directory)
108
+ lib_directory = File.expand_path(File.join(File.dirname(__FILE__), ".."))
109
+ end
110
+ ldflags << "-L#{lib_directory.shellescape}"
111
+ ldflags << "-l:nokogiri.so"
112
+ end
113
+ end
114
+
115
+ nokogiri["cppflags"] = cppflags
116
+ nokogiri["ldflags"] = ldflags
117
+ end
118
+ end
119
+ vi["ruby"] = {}.tap do |ruby|
120
+ ruby["version"] = ::RUBY_VERSION
121
+ ruby["platform"] = ::RUBY_PLATFORM
122
+ ruby["gem_platform"] = ::Gem::Platform.local.to_s
123
+ ruby["description"] = ::RUBY_DESCRIPTION
124
+ ruby["engine"] = engine
125
+ ruby["jruby"] = jruby? if jruby?
126
+ end
127
+
128
+ if libxml2?
129
+ vi["libxml"] = {}.tap do |libxml|
130
+ if libxml2_using_packaged?
131
+ libxml["source"] = "packaged"
132
+ libxml["precompiled"] = libxml2_precompiled?
133
+ libxml["patches"] = Nokogiri::LIBXML2_PATCHES
134
+
135
+ # this is for nokogumbo and shouldn't be forever
136
+ libxml["libxml2_path"] = header_directory
137
+ else
138
+ libxml["source"] = "system"
139
+ end
140
+ libxml["iconv_enabled"] = libxml2_has_iconv?
141
+ libxml["compiled"] = compiled_libxml_version.to_s
142
+ libxml["loaded"] = loaded_libxml_version.to_s
143
+ end
144
+
145
+ vi["libxslt"] = {}.tap do |libxslt|
146
+ if libxml2_using_packaged?
147
+ libxslt["source"] = "packaged"
148
+ libxslt["precompiled"] = libxml2_precompiled?
149
+ libxslt["patches"] = Nokogiri::LIBXSLT_PATCHES
150
+ else
151
+ libxslt["source"] = "system"
152
+ end
153
+ libxslt["compiled"] = compiled_libxslt_version.to_s
154
+ libxslt["loaded"] = loaded_libxslt_version.to_s
155
+ end
156
+
157
+ vi["warnings"] = warnings
158
+ end
159
+
160
+ if defined?(Nokogiri::OTHER_LIBRARY_VERSIONS)
161
+ # see extconf for how this string is assembled: "lib1name:lib1version,lib2name:lib2version"
162
+ vi["other_libraries"] = Hash[*Nokogiri::OTHER_LIBRARY_VERSIONS.split(/[,:]/)]
163
+ elsif jruby?
164
+ vi["other_libraries"] = {}.tap do |ol|
165
+ ol["xerces"] = Nokogiri::XERCES_VERSION
166
+ ol["nekohtml"] = Nokogiri::NEKO_VERSION
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ def to_markdown
173
+ begin
174
+ require "psych"
175
+ rescue LoadError
176
+ end
177
+ require "yaml"
178
+ "# Nokogiri (#{Nokogiri::VERSION})\n" +
179
+ YAML.dump(to_hash).each_line.map { |line| " #{line}" }.join
180
+ end
181
+
182
+ instance.warnings.each do |warning|
183
+ warn "WARNING: #{warning}"
184
+ end
185
+ end
186
+
187
+ def self.uses_libxml?(requirement = nil) # :nodoc:
188
+ return false unless VersionInfo.instance.libxml2?
189
+ return true unless requirement
190
+ Gem::Requirement.new(requirement).satisfied_by?(VersionInfo.instance.loaded_libxml_version)
191
+ end
192
+
193
+ def self.jruby? # :nodoc:
194
+ VersionInfo.instance.jruby?
195
+ end
196
+
197
+ # Ensure constants used in this file are loaded - see #1896
198
+ if Nokogiri.jruby?
199
+ require "nokogiri/jruby/dependencies"
200
+ end
201
+ require "nokogiri/extension"
202
+
203
+ # More complete version information about libxml
204
+ VERSION_INFO = VersionInfo.instance.to_hash
205
+ end
@@ -245,8 +245,8 @@ module Nokogiri
245
245
  #
246
246
  # For example:
247
247
  #
248
- # doc = Nokogiri::XML(open('somedoc.xml'))
249
- # Nokogiri::XML::Builder.with(doc.at('some_tag')) do |xml|
248
+ # doc = Nokogiri::XML(File.read('somedoc.xml'))
249
+ # Nokogiri::XML::Builder.with(doc.at_css('some_tag')) do |xml|
250
250
  # # ... Use normal builder methods here ...
251
251
  # xml.awesome # add the "awesome" tag below "some_tag"
252
252
  # end
@@ -1,4 +1,8 @@
1
+ # coding: utf-8
1
2
  # frozen_string_literal: true
3
+
4
+ require 'pathname'
5
+
2
6
  module Nokogiri
3
7
  module XML
4
8
  ##
@@ -44,9 +48,11 @@ module Nokogiri
44
48
  #
45
49
  def self.parse string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML
46
50
  options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
47
- # Give the options to the user
51
+
48
52
  yield options if block_given?
49
53
 
54
+ url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
55
+
50
56
  if empty_doc?(string_or_io)
51
57
  if options.strict?
52
58
  raise Nokogiri::XML::SyntaxError.new("Empty document")
@@ -56,12 +62,17 @@ module Nokogiri
56
62
  end
57
63
 
58
64
  doc = if string_or_io.respond_to?(:read)
59
- url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
60
- read_io(string_or_io, url, encoding, options.to_i)
61
- else
62
- # read_memory pukes on empty docs
63
- read_memory(string_or_io, url, encoding, options.to_i)
64
- end
65
+ if string_or_io.is_a?(Pathname)
66
+ # resolve the Pathname to the file and open it as an IO object, see #2110
67
+ string_or_io = string_or_io.expand_path.open
68
+ url ||= string_or_io.path
69
+ end
70
+
71
+ read_io(string_or_io, url, encoding, options.to_i)
72
+ else
73
+ # read_memory pukes on empty docs
74
+ read_memory(string_or_io, url, encoding, options.to_i)
75
+ end
65
76
 
66
77
  # do xinclude processing
67
78
  doc.do_xinclude(options) if options.xinclude?
@@ -69,6 +80,35 @@ module Nokogiri
69
80
  return doc
70
81
  end
71
82
 
83
+ ##
84
+ # @!method wrap(java_document)
85
+ # @!scope class
86
+ #
87
+ # Create a {Document} using an existing Java DOM document object.
88
+ #
89
+ # The returned {Document} shares the same underlying data structure as the Java object, so
90
+ # changes in one are reflected in the other.
91
+ #
92
+ # @param java_document [Java::OrgW3cDom::Document]
93
+ # @return [Nokogiri::XML::Document]
94
+ # @note This method is only available when running JRuby.
95
+ # @note The class +Java::OrgW3cDom::Document+ is also accessible as +org.w3c.dom.Document+.
96
+ # @see #to_java
97
+
98
+ ##
99
+ # @!method to_java()
100
+ #
101
+ # Returns the underlying Java DOM document object for the {Document}.
102
+ #
103
+ # The returned Java object shares the same underlying data structure as the {Document}, so
104
+ # changes in one are reflected in the other.
105
+ #
106
+ # @return [Java::OrgW3cDom::Document]
107
+ # @note This method is only available when running JRuby.
108
+ # @note The class +Java::OrgW3cDom::Document+ is also accessible as +org.w3c.dom.Document+.
109
+ # @see .wrap
110
+
111
+
72
112
  # A list of Nokogiri::XML::SyntaxError found when parsing a document
73
113
  attr_accessor :errors
74
114
 
@@ -252,24 +292,14 @@ module Nokogiri
252
292
  end
253
293
  alias :<< :add_child
254
294
 
255
- ##
256
- # +JRuby+
257
- # Wraps Java's org.w3c.dom.document and returns Nokogiri::XML::Document
258
- def self.wrap(document) end if false # native-ext provides Document.wrap
259
-
260
- ##
261
- # +JRuby+
262
- # Returns Java's org.w3c.dom.document of this Document.
263
- def to_java; end if false # JRuby provides #to_java
264
-
265
295
  private
296
+
266
297
  def self.empty_doc? string_or_io
267
298
  string_or_io.nil? ||
268
299
  (string_or_io.respond_to?(:empty?) && string_or_io.empty?) ||
269
300
  (string_or_io.respond_to?(:eof?) && string_or_io.eof?)
270
301
  end
271
302
 
272
- # @private
273
303
  IMPLIED_XPATH_CONTEXTS = [ '//'.freeze ].freeze # :nodoc:
274
304
 
275
305
  def inspect_attributes
@@ -141,6 +141,10 @@ module Nokogiri
141
141
  document.errors = things
142
142
  end
143
143
 
144
+ def fragment(data)
145
+ document.fragment(data)
146
+ end
147
+
144
148
  private
145
149
 
146
150
  # fix for issue 770
@@ -150,12 +154,6 @@ module Nokogiri
150
154
  %Q{xmlns#{prefix}="#{namespace.href}"}
151
155
  end.join ' '
152
156
  end
153
-
154
- def coerce data
155
- return super unless String === data
156
-
157
- document.fragment(data).children
158
- end
159
157
  end
160
158
  end
161
159
  end
@@ -5,64 +5,54 @@ require "nokogiri/xml/node/save_options"
5
5
 
6
6
  module Nokogiri
7
7
  module XML
8
- ####
9
- # Nokogiri::XML::Node is your window to the fun filled world of dealing
10
- # with XML and HTML tags. A Nokogiri::XML::Node may be treated similarly
11
- # to a hash with regard to attributes. For example (from irb):
8
+ ##
9
+ # {Nokogiri::XML::Node} is your window to the fun filled world of dealing with XML and HTML
10
+ # tags. A {Nokogiri::XML::Node} may be treated similarly to a hash with regard to attributes. For
11
+ # example:
12
12
  #
13
- # irb(main):004:0> node
14
- # => <a href="#foo" id="link">link</a>
15
- # irb(main):005:0> node['href']
16
- # => "#foo"
17
- # irb(main):006:0> node.keys
18
- # => ["href", "id"]
19
- # irb(main):007:0> node.values
20
- # => ["#foo", "link"]
21
- # irb(main):008:0> node['class'] = 'green'
22
- # => "green"
23
- # irb(main):009:0> node
24
- # => <a href="#foo" id="link" class="green">link</a>
25
- # irb(main):010:0>
13
+ # node = Nokogiri::XML::DocumentFragment.parse("<a href='#foo' id='link'>link</a>").at_css("a")
14
+ # node.to_html # => "<a href=\"#foo\" id=\"link\">link</a>"
15
+ # node['href'] # => "#foo"
16
+ # node.keys # => ["href", "id"]
17
+ # node.values # => ["#foo", "link"]
18
+ # node['class'] = 'green' # => "green"
19
+ # node.to_html # => "<a href=\"#foo\" id=\"link\" class=\"green\">link</a>"
26
20
  #
27
- # See Nokogiri::XML::Node#[] and Nokogiri::XML#[]= for more information.
21
+ # See the method group entitled "Working With Node Attributes" for the full set of methods.
28
22
  #
29
- # Nokogiri::XML::Node also has methods that let you move around your
23
+ # {Nokogiri::XML::Node} also has methods that let you move around your
30
24
  # tree. For navigating your tree, see:
31
25
  #
32
- # * Nokogiri::XML::Node#parent
33
- # * Nokogiri::XML::Node#children
34
- # * Nokogiri::XML::Node#next
35
- # * Nokogiri::XML::Node#previous
36
- #
26
+ # * {#parent}
27
+ # * {#children}
28
+ # * {#next}
29
+ # * {#previous}
37
30
  #
38
31
  # When printing or otherwise emitting a document or a node (and
39
32
  # its subtree), there are a few methods you might want to use:
40
33
  #
41
- # * content, text, inner_text, to_str: emit plaintext
42
- #
43
- # These methods will all emit the plaintext version of your
44
- # document, meaning that entities will be replaced (e.g., "&lt;"
45
- # will be replaced with "<"), meaning that any sanitizing will
46
- # likely be un-done in the output.
34
+ # * {#content}, {#text}, {#inner_text}, {#to_str}: These methods will all <b>emit plaintext</b>,
35
+ # meaning that entities will be replaced (e.g., "&lt;" will be replaced with "<"), meaning
36
+ # that any sanitizing will likely be un-done in the output.
47
37
  #
48
- # * to_s, to_xml, to_html, inner_html: emit well-formed markup
38
+ # * {#to_s}, {#to_xml}, {#to_html}, {#inner_html}: These methods will all <b>emit
39
+ # properly-escaped markup</b>, meaning that it's suitable for consumption by browsers,
40
+ # parsers, etc.
49
41
  #
50
- # These methods will all emit properly-escaped markup, meaning
51
- # that it's suitable for consumption by browsers, parsers, etc.
42
+ # You may search this node's subtree using {#xpath} and {#css}
52
43
  #
53
- # You may search this node's subtree using Searchable#xpath and Searchable#css
54
44
  class Node
55
45
  include Nokogiri::XML::PP::Node
56
46
  include Nokogiri::XML::Searchable
57
47
  include Enumerable
58
48
 
59
- # Element node type, see Nokogiri::XML::Node#element?
49
+ # Element node type, see {Nokogiri::XML::Node#element?}
60
50
  ELEMENT_NODE = 1
61
51
  # Attribute node type
62
52
  ATTRIBUTE_NODE = 2
63
- # Text node type, see Nokogiri::XML::Node#text?
53
+ # Text node type, see {Nokogiri::XML::Node#text?}
64
54
  TEXT_NODE = 3
65
- # CDATA node type, see Nokogiri::XML::Node#cdata?
55
+ # CDATA node type, see {Nokogiri::XML::Node#cdata?}
66
56
  CDATA_SECTION_NODE = 4
67
57
  # Entity reference node type
68
58
  ENTITY_REF_NODE = 5
@@ -70,9 +60,9 @@ module Nokogiri
70
60
  ENTITY_NODE = 6
71
61
  # PI node type
72
62
  PI_NODE = 7
73
- # Comment node type, see Nokogiri::XML::Node#comment?
63
+ # Comment node type, see {Nokogiri::XML::Node#comment?}
74
64
  COMMENT_NODE = 8
75
- # Document node type, see Nokogiri::XML::Node#xml?
65
+ # Document node type, see {Nokogiri::XML::Node#xml?}
76
66
  DOCUMENT_NODE = 9
77
67
  # Document type node type
78
68
  DOCUMENT_TYPE_NODE = 10
@@ -80,7 +70,7 @@ module Nokogiri
80
70
  DOCUMENT_FRAG_NODE = 11
81
71
  # Notation node type
82
72
  NOTATION_NODE = 12
83
- # HTML document node type, see Nokogiri::XML::Node#html?
73
+ # HTML document node type, see {Nokogiri::XML::Node#html?}
84
74
  HTML_DOCUMENT_NODE = 13
85
75
  # DTD node type
86
76
  DTD_NODE = 14
@@ -99,8 +89,14 @@ module Nokogiri
99
89
  # DOCB document node type
100
90
  DOCB_DOCUMENT_NODE = 21
101
91
 
102
- def initialize(name, document) # :nodoc:
103
- # ... Ya. This is empty on purpose.
92
+ ##
93
+ # Create a new node with +name+ sharing GC lifecycle with +document+.
94
+ # @param name [String]
95
+ # @param document [Nokogiri::XML::Document]
96
+ # @return [Nokogiri::XML::Node]
97
+ # @see Nokogiri::XML::Node.new
98
+ def initialize(name, document)
99
+ # This is intentionally empty.
104
100
  end
105
101
 
106
102
  ###
@@ -267,6 +263,8 @@ module Nokogiri
267
263
  #
268
264
  # Also see related method +swap+.
269
265
  def replace(node_or_tags)
266
+ raise("Cannot replace a node with no parent") unless parent
267
+
270
268
  # We cannot replace a text node directly, otherwise libxml will return
271
269
  # an internal error at parser.c:13031, I don't know exactly why
272
270
  # libxml is trying to find a parent node that is an element or document
@@ -278,7 +276,7 @@ module Nokogiri
278
276
  return replacee.replace node_or_tags
279
277
  end
280
278
 
281
- node_or_tags = coerce(node_or_tags)
279
+ node_or_tags = parent.coerce(node_or_tags)
282
280
 
283
281
  if node_or_tags.is_a?(XML::NodeSet)
284
282
  node_or_tags.each { |n| add_previous_sibling n }
@@ -819,13 +817,30 @@ module Nokogiri
819
817
 
820
818
  return Nokogiri::XML::NodeSet.new(document) if contents.empty?
821
819
 
822
- ##
823
- # This is a horrible hack, but I don't care. See #313 for background.
820
+ # libxml2 does not obey the `recover` option after encountering errors during `in_context`
821
+ # parsing, and so this horrible hack is here to try to emulate recovery behavior.
822
+ #
823
+ # Unfortunately, this means we're no longer parsing "in context" and so namespaces that
824
+ # would have been inherited from the context node won't be handled correctly. This hack was
825
+ # written in 2010, and I regret it, because it's silently degrading functionality in a way
826
+ # that's not easily prevented (or even detected).
827
+ #
828
+ # I think preferable behavior would be to either:
829
+ #
830
+ # a. add an error noting that we "fell back" and pointing the user to turning off the `recover` option
831
+ # b. don't recover, but raise a sensible exception
832
+ #
833
+ # For context and background: https://github.com/sparklemotion/nokogiri/issues/313
834
+ # FIXME bug report: https://github.com/sparklemotion/nokogiri/issues/2092
824
835
  error_count = document.errors.length
825
836
  node_set = in_context(contents, options.to_i)
826
- if node_set.empty? and document.errors.length > error_count and options.recover?
827
- fragment = Nokogiri::HTML::DocumentFragment.parse contents
828
- node_set = fragment.children
837
+ if (node_set.empty? && (document.errors.length > error_count))
838
+ if options.recover?
839
+ fragment = Nokogiri::HTML::DocumentFragment.parse contents
840
+ node_set = fragment.children
841
+ else
842
+ raise document.errors[error_count]
843
+ end
829
844
  end
830
845
  node_set
831
846
  end
@@ -1128,6 +1143,28 @@ module Nokogiri
1128
1143
 
1129
1144
  # @!endgroup
1130
1145
 
1146
+ protected
1147
+
1148
+ def coerce(data)
1149
+ case data
1150
+ when XML::NodeSet
1151
+ return data
1152
+ when XML::DocumentFragment
1153
+ return data.children
1154
+ when String
1155
+ return fragment(data).children
1156
+ when Document, XML::Attr
1157
+ # unacceptable
1158
+ when XML::Node
1159
+ return data
1160
+ end
1161
+
1162
+ raise ArgumentError, <<-EOERR
1163
+ Requires a Node, NodeSet or String argument, and cannot accept a #{data.class}.
1164
+ (You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
1165
+ EOERR
1166
+ end
1167
+
1131
1168
  private
1132
1169
 
1133
1170
  def keywordify(keywords)
@@ -1142,10 +1179,12 @@ module Nokogiri
1142
1179
  end
1143
1180
 
1144
1181
  def add_sibling(next_or_previous, node_or_tags)
1182
+ raise("Cannot add sibling to a node with no parent") unless parent
1183
+
1145
1184
  impl = (next_or_previous == :next) ? :add_next_sibling_node : :add_previous_sibling_node
1146
1185
  iter = (next_or_previous == :next) ? :reverse_each : :each
1147
1186
 
1148
- node_or_tags = coerce node_or_tags
1187
+ node_or_tags = parent.coerce(node_or_tags)
1149
1188
  if node_or_tags.is_a?(XML::NodeSet)
1150
1189
  if text?
1151
1190
  pivot = Nokogiri::XML::Node.new "dummy", document
@@ -1182,26 +1221,6 @@ module Nokogiri
1182
1221
  [:name, :namespace, :attribute_nodes, :children]
1183
1222
  end
1184
1223
 
1185
- def coerce(data)
1186
- case data
1187
- when XML::NodeSet
1188
- return data
1189
- when XML::DocumentFragment
1190
- return data.children
1191
- when String
1192
- return fragment(data).children
1193
- when Document, XML::Attr
1194
- # unacceptable
1195
- when XML::Node
1196
- return data
1197
- end
1198
-
1199
- raise ArgumentError, <<-EOERR
1200
- Requires a Node, NodeSet or String argument, and cannot accept a #{data.class}.
1201
- (You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
1202
- EOERR
1203
- end
1204
-
1205
1224
  # @private
1206
1225
  IMPLIED_XPATH_CONTEXTS = [".//".freeze].freeze
1207
1226