nokogiri 1.11.0.rc3-x86-linux → 1.11.0.rc4-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 (110) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE-DEPENDENCIES.md +1015 -947
  3. data/README.md +1 -1
  4. data/ext/nokogiri/depend +476 -357
  5. data/ext/nokogiri/extconf.rb +441 -321
  6. data/ext/nokogiri/html_document.c +79 -78
  7. data/ext/nokogiri/html_sax_parser_context.c +2 -2
  8. data/ext/nokogiri/include/libexslt/exslt.h +102 -0
  9. data/ext/nokogiri/include/libexslt/exsltconfig.h +70 -0
  10. data/ext/nokogiri/include/libexslt/exsltexports.h +140 -0
  11. data/ext/nokogiri/include/libxml2/libxml/DOCBparser.h +96 -0
  12. data/ext/nokogiri/include/libxml2/libxml/HTMLparser.h +306 -0
  13. data/ext/nokogiri/include/libxml2/libxml/HTMLtree.h +147 -0
  14. data/ext/nokogiri/include/libxml2/libxml/SAX.h +173 -0
  15. data/ext/nokogiri/include/libxml2/libxml/SAX2.h +178 -0
  16. data/ext/nokogiri/include/libxml2/libxml/c14n.h +126 -0
  17. data/ext/nokogiri/include/libxml2/libxml/catalog.h +182 -0
  18. data/ext/nokogiri/include/libxml2/libxml/chvalid.h +230 -0
  19. data/ext/nokogiri/include/libxml2/libxml/debugXML.h +217 -0
  20. data/ext/nokogiri/include/libxml2/libxml/dict.h +79 -0
  21. data/ext/nokogiri/include/libxml2/libxml/encoding.h +245 -0
  22. data/ext/nokogiri/include/libxml2/libxml/entities.h +151 -0
  23. data/ext/nokogiri/include/libxml2/libxml/globals.h +508 -0
  24. data/ext/nokogiri/include/libxml2/libxml/hash.h +236 -0
  25. data/ext/nokogiri/include/libxml2/libxml/list.h +137 -0
  26. data/ext/nokogiri/include/libxml2/libxml/nanoftp.h +163 -0
  27. data/ext/nokogiri/include/libxml2/libxml/nanohttp.h +81 -0
  28. data/ext/nokogiri/include/libxml2/libxml/parser.h +1241 -0
  29. data/ext/nokogiri/include/libxml2/libxml/parserInternals.h +644 -0
  30. data/ext/nokogiri/include/libxml2/libxml/pattern.h +100 -0
  31. data/ext/nokogiri/include/libxml2/libxml/relaxng.h +217 -0
  32. data/ext/nokogiri/include/libxml2/libxml/schemasInternals.h +958 -0
  33. data/ext/nokogiri/include/libxml2/libxml/schematron.h +142 -0
  34. data/ext/nokogiri/include/libxml2/libxml/threads.h +89 -0
  35. data/ext/nokogiri/include/libxml2/libxml/tree.h +1311 -0
  36. data/ext/nokogiri/include/libxml2/libxml/uri.h +94 -0
  37. data/ext/nokogiri/include/libxml2/libxml/valid.h +458 -0
  38. data/ext/nokogiri/include/libxml2/libxml/xinclude.h +129 -0
  39. data/ext/nokogiri/include/libxml2/libxml/xlink.h +189 -0
  40. data/ext/nokogiri/include/libxml2/libxml/xmlIO.h +366 -0
  41. data/ext/nokogiri/include/libxml2/libxml/xmlautomata.h +146 -0
  42. data/ext/nokogiri/include/libxml2/libxml/xmlerror.h +945 -0
  43. data/ext/nokogiri/include/libxml2/libxml/xmlexports.h +153 -0
  44. data/ext/nokogiri/include/libxml2/libxml/xmlmemory.h +224 -0
  45. data/ext/nokogiri/include/libxml2/libxml/xmlmodule.h +57 -0
  46. data/ext/nokogiri/include/libxml2/libxml/xmlreader.h +428 -0
  47. data/ext/nokogiri/include/libxml2/libxml/xmlregexp.h +222 -0
  48. data/ext/nokogiri/include/libxml2/libxml/xmlsave.h +88 -0
  49. data/ext/nokogiri/include/libxml2/libxml/xmlschemas.h +246 -0
  50. data/ext/nokogiri/include/libxml2/libxml/xmlschemastypes.h +151 -0
  51. data/ext/nokogiri/include/libxml2/libxml/xmlstring.h +140 -0
  52. data/ext/nokogiri/include/libxml2/libxml/xmlunicode.h +202 -0
  53. data/ext/nokogiri/include/libxml2/libxml/xmlversion.h +485 -0
  54. data/ext/nokogiri/include/libxml2/libxml/xmlwriter.h +488 -0
  55. data/ext/nokogiri/include/libxml2/libxml/xpath.h +566 -0
  56. data/ext/nokogiri/include/libxml2/libxml/xpathInternals.h +632 -0
  57. data/ext/nokogiri/include/libxml2/libxml/xpointer.h +114 -0
  58. data/ext/nokogiri/include/libxslt/attributes.h +38 -0
  59. data/ext/nokogiri/include/libxslt/documents.h +93 -0
  60. data/ext/nokogiri/include/libxslt/extensions.h +262 -0
  61. data/ext/nokogiri/include/libxslt/extra.h +72 -0
  62. data/ext/nokogiri/include/libxslt/functions.h +78 -0
  63. data/ext/nokogiri/include/libxslt/imports.h +75 -0
  64. data/ext/nokogiri/include/libxslt/keys.h +53 -0
  65. data/ext/nokogiri/include/libxslt/namespaces.h +68 -0
  66. data/ext/nokogiri/include/libxslt/numbersInternals.h +73 -0
  67. data/ext/nokogiri/include/libxslt/pattern.h +84 -0
  68. data/ext/nokogiri/include/libxslt/preproc.h +43 -0
  69. data/ext/nokogiri/include/libxslt/security.h +104 -0
  70. data/ext/nokogiri/include/libxslt/templates.h +77 -0
  71. data/ext/nokogiri/include/libxslt/transform.h +207 -0
  72. data/ext/nokogiri/include/libxslt/variables.h +118 -0
  73. data/ext/nokogiri/include/libxslt/xslt.h +110 -0
  74. data/ext/nokogiri/include/libxslt/xsltInternals.h +1978 -0
  75. data/ext/nokogiri/include/libxslt/xsltconfig.h +180 -0
  76. data/ext/nokogiri/include/libxslt/xsltexports.h +142 -0
  77. data/ext/nokogiri/include/libxslt/xsltlocale.h +76 -0
  78. data/ext/nokogiri/include/libxslt/xsltutils.h +313 -0
  79. data/ext/nokogiri/nokogiri.c +34 -46
  80. data/ext/nokogiri/nokogiri.h +22 -26
  81. data/ext/nokogiri/xml_document.c +2 -2
  82. data/ext/nokogiri/xml_node.c +1 -1
  83. data/ext/nokogiri/xml_node_set.c +1 -1
  84. data/ext/nokogiri/xml_relax_ng.c +29 -11
  85. data/ext/nokogiri/xml_sax_parser.c +2 -7
  86. data/ext/nokogiri/xml_sax_parser_context.c +2 -2
  87. data/ext/nokogiri/xml_schema.c +55 -13
  88. data/ext/nokogiri/xml_xpath_context.c +80 -4
  89. data/ext/nokogiri/xslt_stylesheet.c +1 -4
  90. data/lib/nokogiri.rb +1 -1
  91. data/lib/nokogiri/2.5/nokogiri.so +0 -0
  92. data/lib/nokogiri/2.6/nokogiri.so +0 -0
  93. data/lib/nokogiri/2.7/nokogiri.so +0 -0
  94. data/lib/nokogiri/3.0/nokogiri.so +0 -0
  95. data/lib/nokogiri/css/parser.rb +3 -3
  96. data/lib/nokogiri/css/parser.y +2 -2
  97. data/lib/nokogiri/css/xpath_visitor.rb +70 -42
  98. data/lib/nokogiri/html/document.rb +12 -26
  99. data/lib/nokogiri/version.rb +2 -149
  100. data/lib/nokogiri/version/constant.rb +5 -0
  101. data/lib/nokogiri/version/info.rb +182 -0
  102. data/lib/nokogiri/xml/document.rb +17 -7
  103. data/lib/nokogiri/xml/document_fragment.rb +4 -6
  104. data/lib/nokogiri/xml/node.rb +50 -27
  105. data/lib/nokogiri/xml/parse_options.rb +6 -0
  106. data/lib/nokogiri/xml/relax_ng.rb +6 -2
  107. data/lib/nokogiri/xml/schema.rb +12 -4
  108. data/lib/nokogiri/xml/searchable.rb +3 -1
  109. metadata +105 -28
  110. data/lib/nokogiri/2.4/nokogiri.so +0 -0
@@ -1,150 +1,3 @@
1
1
  # frozen_string_literal: true
2
- module Nokogiri
3
- # The version of Nokogiri you are using
4
- VERSION = "1.11.0.rc3"
5
-
6
- class VersionInfo # :nodoc:
7
- def jruby?
8
- ::JRUBY_VERSION if RUBY_PLATFORM == "java"
9
- end
10
-
11
- def engine
12
- defined?(RUBY_ENGINE) ? RUBY_ENGINE : "mri"
13
- end
14
-
15
- def loaded_libxml_version
16
- Gem::Version.new(LIBXML_LOADED_VERSION.
17
- scan(/^(\d+)(\d\d)(\d\d)(?!\d)/).first.
18
- collect(&:to_i).
19
- join("."))
20
- end
21
-
22
- def compiled_libxml_version
23
- Gem::Version.new LIBXML_COMPILED_VERSION
24
- end
25
-
26
- def loaded_libxslt_version
27
- Gem::Version.new(LIBXSLT_LOADED_VERSION.
28
- scan(/^(\d+)(\d\d)(\d\d)(?!\d)/).first.
29
- collect(&:to_i).
30
- join("."))
31
- end
32
-
33
- def compiled_libxslt_version
34
- Gem::Version.new LIBXSLT_COMPILED_VERSION
35
- end
36
-
37
- def libxml2?
38
- defined?(LIBXML_COMPILED_VERSION)
39
- end
40
-
41
- def libxml2_using_system?
42
- !libxml2_using_packaged?
43
- end
44
-
45
- def libxml2_using_packaged?
46
- NOKOGIRI_USE_PACKAGED_LIBRARIES
47
- end
48
-
49
- def warnings
50
- warnings = []
51
-
52
- if libxml2?
53
- if compiled_libxml_version != loaded_libxml_version
54
- warnings << "Nokogiri was built against libxml version #{compiled_libxml_version}, but has dynamically loaded #{loaded_libxml_version}"
55
- end
56
-
57
- if compiled_libxslt_version != loaded_libxslt_version
58
- warnings << "Nokogiri was built against libxslt version #{compiled_libxslt_version}, but has dynamically loaded #{loaded_libxslt_version}"
59
- end
60
- end
61
-
62
- warnings
63
- end
64
-
65
- def to_hash
66
- {}.tap do |vi|
67
- vi["warnings"] = []
68
- vi["nokogiri"] = Nokogiri::VERSION
69
- vi["ruby"] = {}.tap do |ruby|
70
- ruby["version"] = ::RUBY_VERSION
71
- ruby["platform"] = ::RUBY_PLATFORM
72
- ruby["gem_platform"] = ::Gem::Platform.local.to_s
73
- ruby["description"] = ::RUBY_DESCRIPTION
74
- ruby["engine"] = engine
75
- ruby["jruby"] = jruby? if jruby?
76
- end
77
-
78
- if libxml2?
79
- vi["libxml"] = {}.tap do |libxml|
80
- if libxml2_using_packaged?
81
- libxml["source"] = "packaged"
82
- libxml["patches"] = NOKOGIRI_LIBXML2_PATCHES
83
- else
84
- libxml["source"] = "system"
85
- end
86
- libxml["compiled"] = compiled_libxml_version.to_s
87
- libxml["loaded"] = loaded_libxml_version.to_s
88
- end
89
-
90
- vi["libxslt"] = {}.tap do |libxslt|
91
- if libxml2_using_packaged?
92
- libxslt["source"] = "packaged"
93
- libxslt["patches"] = NOKOGIRI_LIBXSLT_PATCHES
94
- else
95
- libxslt["source"] = "system"
96
- end
97
- libxslt["compiled"] = compiled_libxslt_version.to_s
98
- libxslt["loaded"] = loaded_libxslt_version.to_s
99
- end
100
-
101
- vi["warnings"] = warnings
102
- elsif jruby?
103
- vi["xerces"] = Nokogiri::XERCES_VERSION
104
- vi["nekohtml"] = Nokogiri::NEKO_VERSION
105
- end
106
- end
107
- end
108
-
109
- def to_markdown
110
- begin
111
- require "psych"
112
- rescue LoadError
113
- end
114
- require "yaml"
115
- "# Nokogiri (#{Nokogiri::VERSION})\n" +
116
- YAML.dump(to_hash).each_line.map { |line| " #{line}" }.join
117
- end
118
-
119
- # FIXME: maybe switch to singleton?
120
- @@instance = new
121
- @@instance.warnings.each do |warning|
122
- warn "WARNING: #{warning}"
123
- end
124
- def self.instance; @@instance; end
125
- end
126
-
127
- def self.uses_libxml?(requirement = nil) # :nodoc:
128
- return false unless VersionInfo.instance.libxml2?
129
- return true unless requirement
130
- return Gem::Requirement.new(requirement).satisfied_by?(VersionInfo.instance.loaded_libxml_version)
131
- end
132
-
133
- def self.jruby? # :nodoc:
134
- VersionInfo.instance.jruby?
135
- end
136
-
137
- # Ensure constants used in this file are loaded - see #1896
138
- if Nokogiri.jruby?
139
- require "nokogiri/jruby/dependencies"
140
- end
141
- begin
142
- RUBY_VERSION =~ /(\d+\.\d+)/
143
- require "nokogiri/#{$1}/nokogiri"
144
- rescue LoadError
145
- require "nokogiri/nokogiri"
146
- end
147
-
148
- # More complete version information about libxml
149
- VERSION_INFO = VersionInfo.instance.to_hash
150
- end
2
+ require_relative "version/constant"
3
+ require_relative "version/info"
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ module Nokogiri
3
+ # The version of Nokogiri you are using
4
+ VERSION = "1.11.0.rc4"
5
+ end
@@ -0,0 +1,182 @@
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 engine
14
+ defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : "mri"
15
+ end
16
+
17
+ def loaded_libxml_version
18
+ Gem::Version.new(Nokogiri::LIBXML_LOADED_VERSION
19
+ .scan(/^(\d+)(\d\d)(\d\d)(?!\d)/).first
20
+ .collect(&:to_i)
21
+ .join("."))
22
+ end
23
+
24
+ def compiled_libxml_version
25
+ Gem::Version.new(Nokogiri::LIBXML_COMPILED_VERSION)
26
+ end
27
+
28
+ def loaded_libxslt_version
29
+ Gem::Version.new(Nokogiri::LIBXSLT_LOADED_VERSION
30
+ .scan(/^(\d+)(\d\d)(\d\d)(?!\d)/).first
31
+ .collect(&:to_i)
32
+ .join("."))
33
+ end
34
+
35
+ def compiled_libxslt_version
36
+ Gem::Version.new(Nokogiri::LIBXSLT_COMPILED_VERSION)
37
+ end
38
+
39
+ def libxml2?
40
+ defined?(Nokogiri::LIBXML_COMPILED_VERSION)
41
+ end
42
+
43
+ def libxml2_has_iconv?
44
+ defined?(Nokogiri::LIBXML_ICONV_ENABLED) && Nokogiri::LIBXML_ICONV_ENABLED
45
+ end
46
+
47
+ def libxml2_using_packaged?
48
+ libxml2? && Nokogiri::PACKAGED_LIBRARIES
49
+ end
50
+
51
+ def libxml2_using_system?
52
+ libxml2? && !libxml2_using_packaged?
53
+ end
54
+
55
+ def libxml2_precompiled?
56
+ libxml2_using_packaged? && Nokogiri::PRECOMPILED_LIBRARIES
57
+ end
58
+
59
+ def warnings
60
+ warnings = []
61
+
62
+ if libxml2?
63
+ if compiled_libxml_version != loaded_libxml_version
64
+ warnings << "Nokogiri was built against libxml version #{compiled_libxml_version}, but has dynamically loaded #{loaded_libxml_version}"
65
+ end
66
+
67
+ if compiled_libxslt_version != loaded_libxslt_version
68
+ warnings << "Nokogiri was built against libxslt version #{compiled_libxslt_version}, but has dynamically loaded #{loaded_libxslt_version}"
69
+ end
70
+ end
71
+
72
+ warnings
73
+ end
74
+
75
+ def to_hash
76
+ header_directory = File.expand_path(File.join(File.dirname(__FILE__), "../../../ext/nokogiri"))
77
+ {}.tap do |vi|
78
+ vi["warnings"] = []
79
+ vi["nokogiri"] = {}.tap do |nokogiri|
80
+ nokogiri["version"] = Nokogiri::VERSION
81
+
82
+ unless jruby?
83
+ cppflags = ["-I#{header_directory.shellescape}"]
84
+ if libxml2_using_packaged?
85
+ cppflags << "-I#{File.join(header_directory, "include").shellescape}"
86
+ cppflags << "-I#{File.join(header_directory, "include/libxml2").shellescape}"
87
+ end
88
+ nokogiri["cppflags"] = cppflags
89
+ end
90
+ end
91
+ vi["ruby"] = {}.tap do |ruby|
92
+ ruby["version"] = ::RUBY_VERSION
93
+ ruby["platform"] = ::RUBY_PLATFORM
94
+ ruby["gem_platform"] = ::Gem::Platform.local.to_s
95
+ ruby["description"] = ::RUBY_DESCRIPTION
96
+ ruby["engine"] = engine
97
+ ruby["jruby"] = jruby? if jruby?
98
+ end
99
+
100
+ if libxml2?
101
+ vi["libxml"] = {}.tap do |libxml|
102
+ if libxml2_using_packaged?
103
+ libxml["source"] = "packaged"
104
+ libxml["precompiled"] = libxml2_precompiled?
105
+ libxml["patches"] = Nokogiri::LIBXML2_PATCHES
106
+
107
+ # this is for nokogumbo and shouldn't be forever
108
+ libxml["libxml2_path"] = header_directory
109
+ else
110
+ libxml["source"] = "system"
111
+ end
112
+ libxml["iconv_enabled"] = libxml2_has_iconv?
113
+ libxml["compiled"] = compiled_libxml_version.to_s
114
+ libxml["loaded"] = loaded_libxml_version.to_s
115
+ end
116
+
117
+ vi["libxslt"] = {}.tap do |libxslt|
118
+ if libxml2_using_packaged?
119
+ libxslt["source"] = "packaged"
120
+ libxslt["precompiled"] = libxml2_precompiled?
121
+ libxslt["patches"] = Nokogiri::LIBXSLT_PATCHES
122
+ else
123
+ libxslt["source"] = "system"
124
+ end
125
+ libxslt["compiled"] = compiled_libxslt_version.to_s
126
+ libxslt["loaded"] = loaded_libxslt_version.to_s
127
+ end
128
+
129
+ vi["warnings"] = warnings
130
+ end
131
+
132
+ if defined?(Nokogiri::OTHER_LIBRARY_VERSIONS)
133
+ # see extconf for how this string is assembled: "lib1name:lib1version,lib2name:lib2version"
134
+ vi["other_libraries"] = Hash[*Nokogiri::OTHER_LIBRARY_VERSIONS.split(/[,:]/)]
135
+ elsif jruby?
136
+ vi["other_libraries"] = {}.tap do |ol|
137
+ ol["xerces"] = Nokogiri::XERCES_VERSION
138
+ ol["nekohtml"] = Nokogiri::NEKO_VERSION
139
+ end
140
+ end
141
+ end
142
+ end
143
+
144
+ def to_markdown
145
+ begin
146
+ require "psych"
147
+ rescue LoadError
148
+ end
149
+ require "yaml"
150
+ "# Nokogiri (#{Nokogiri::VERSION})\n" +
151
+ YAML.dump(to_hash).each_line.map { |line| " #{line}" }.join
152
+ end
153
+
154
+ instance.warnings.each do |warning|
155
+ warn "WARNING: #{warning}"
156
+ end
157
+ end
158
+
159
+ def self.uses_libxml?(requirement = nil) # :nodoc:
160
+ return false unless VersionInfo.instance.libxml2?
161
+ return true unless requirement
162
+ Gem::Requirement.new(requirement).satisfied_by?(VersionInfo.instance.loaded_libxml_version)
163
+ end
164
+
165
+ def self.jruby? # :nodoc:
166
+ VersionInfo.instance.jruby?
167
+ end
168
+
169
+ # Ensure constants used in this file are loaded - see #1896
170
+ if Nokogiri.jruby?
171
+ require "nokogiri/jruby/dependencies"
172
+ end
173
+ begin
174
+ ::RUBY_VERSION =~ /(\d+\.\d+)/
175
+ require "nokogiri/#{Regexp.last_match(1)}/nokogiri"
176
+ rescue LoadError
177
+ require "nokogiri/nokogiri"
178
+ end
179
+
180
+ # More complete version information about libxml
181
+ VERSION_INFO = VersionInfo.instance.to_hash
182
+ end
@@ -1,4 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+
2
5
  module Nokogiri
3
6
  module XML
4
7
  ##
@@ -44,9 +47,11 @@ module Nokogiri
44
47
  #
45
48
  def self.parse string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML
46
49
  options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
47
- # Give the options to the user
50
+
48
51
  yield options if block_given?
49
52
 
53
+ url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
54
+
50
55
  if empty_doc?(string_or_io)
51
56
  if options.strict?
52
57
  raise Nokogiri::XML::SyntaxError.new("Empty document")
@@ -56,12 +61,17 @@ module Nokogiri
56
61
  end
57
62
 
58
63
  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
64
+ if string_or_io.is_a?(Pathname)
65
+ # resolve the Pathname to the file and open it as an IO object, see #2110
66
+ string_or_io = string_or_io.expand_path.open
67
+ url ||= string_or_io.path
68
+ end
69
+
70
+ read_io(string_or_io, url, encoding, options.to_i)
71
+ else
72
+ # read_memory pukes on empty docs
73
+ read_memory(string_or_io, url, encoding, options.to_i)
74
+ end
65
75
 
66
76
  # do xinclude processing
67
77
  doc.do_xinclude(options) if options.xinclude?
@@ -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
@@ -267,6 +267,8 @@ module Nokogiri
267
267
  #
268
268
  # Also see related method +swap+.
269
269
  def replace(node_or_tags)
270
+ raise("Cannot replace a node with no parent") unless parent
271
+
270
272
  # We cannot replace a text node directly, otherwise libxml will return
271
273
  # an internal error at parser.c:13031, I don't know exactly why
272
274
  # libxml is trying to find a parent node that is an element or document
@@ -278,7 +280,7 @@ module Nokogiri
278
280
  return replacee.replace node_or_tags
279
281
  end
280
282
 
281
- node_or_tags = coerce(node_or_tags)
283
+ node_or_tags = parent.coerce(node_or_tags)
282
284
 
283
285
  if node_or_tags.is_a?(XML::NodeSet)
284
286
  node_or_tags.each { |n| add_previous_sibling n }
@@ -819,13 +821,30 @@ module Nokogiri
819
821
 
820
822
  return Nokogiri::XML::NodeSet.new(document) if contents.empty?
821
823
 
822
- ##
823
- # This is a horrible hack, but I don't care. See #313 for background.
824
+ # libxml2 does not obey the `recover` option after encountering errors during `in_context`
825
+ # parsing, and so this horrible hack is here to try to emulate recovery behavior.
826
+ #
827
+ # Unfortunately, this means we're no longer parsing "in context" and so namespaces that
828
+ # would have been inherited from the context node won't be handled correctly. This hack was
829
+ # written in 2010, and I regret it, because it's silently degrading functionality in a way
830
+ # that's not easily prevented (or even detected).
831
+ #
832
+ # I think preferable behavior would be to either:
833
+ #
834
+ # a. add an error noting that we "fell back" and pointing the user to turning off the `recover` option
835
+ # b. don't recover, but raise a sensible exception
836
+ #
837
+ # For context and background: https://github.com/sparklemotion/nokogiri/issues/313
838
+ # FIXME bug report: https://github.com/sparklemotion/nokogiri/issues/2092
824
839
  error_count = document.errors.length
825
840
  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
841
+ if (node_set.empty? && (document.errors.length > error_count))
842
+ if options.recover?
843
+ fragment = Nokogiri::HTML::DocumentFragment.parse contents
844
+ node_set = fragment.children
845
+ else
846
+ raise document.errors[error_count]
847
+ end
829
848
  end
830
849
  node_set
831
850
  end
@@ -1128,6 +1147,28 @@ module Nokogiri
1128
1147
 
1129
1148
  # @!endgroup
1130
1149
 
1150
+ protected
1151
+
1152
+ def coerce(data)
1153
+ case data
1154
+ when XML::NodeSet
1155
+ return data
1156
+ when XML::DocumentFragment
1157
+ return data.children
1158
+ when String
1159
+ return fragment(data).children
1160
+ when Document, XML::Attr
1161
+ # unacceptable
1162
+ when XML::Node
1163
+ return data
1164
+ end
1165
+
1166
+ raise ArgumentError, <<-EOERR
1167
+ Requires a Node, NodeSet or String argument, and cannot accept a #{data.class}.
1168
+ (You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
1169
+ EOERR
1170
+ end
1171
+
1131
1172
  private
1132
1173
 
1133
1174
  def keywordify(keywords)
@@ -1142,10 +1183,12 @@ module Nokogiri
1142
1183
  end
1143
1184
 
1144
1185
  def add_sibling(next_or_previous, node_or_tags)
1186
+ raise("Cannot add sibling to a node with no parent") unless parent
1187
+
1145
1188
  impl = (next_or_previous == :next) ? :add_next_sibling_node : :add_previous_sibling_node
1146
1189
  iter = (next_or_previous == :next) ? :reverse_each : :each
1147
1190
 
1148
- node_or_tags = coerce node_or_tags
1191
+ node_or_tags = parent.coerce(node_or_tags)
1149
1192
  if node_or_tags.is_a?(XML::NodeSet)
1150
1193
  if text?
1151
1194
  pivot = Nokogiri::XML::Node.new "dummy", document
@@ -1182,26 +1225,6 @@ module Nokogiri
1182
1225
  [:name, :namespace, :attribute_nodes, :children]
1183
1226
  end
1184
1227
 
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
1228
  # @private
1206
1229
  IMPLIED_XPATH_CONTEXTS = [".//".freeze].freeze
1207
1230