nokogiri 1.12.5 → 1.13.0

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 (91) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/README.md +9 -7
  4. data/bin/nokogiri +63 -50
  5. data/dependencies.yml +5 -6
  6. data/ext/nokogiri/extconf.rb +47 -35
  7. data/ext/nokogiri/xml_document.c +35 -35
  8. data/ext/nokogiri/xml_document_fragment.c +0 -2
  9. data/ext/nokogiri/xml_dtd.c +2 -2
  10. data/ext/nokogiri/xml_encoding_handler.c +25 -11
  11. data/ext/nokogiri/xml_node.c +638 -333
  12. data/ext/nokogiri/xml_reader.c +37 -11
  13. data/ext/nokogiri/xml_xpath_context.c +72 -49
  14. data/gumbo-parser/src/parser.c +0 -11
  15. data/lib/nokogiri/class_resolver.rb +67 -0
  16. data/lib/nokogiri/css/node.rb +9 -8
  17. data/lib/nokogiri/css/parser.rb +11 -3
  18. data/lib/nokogiri/css/parser.y +10 -2
  19. data/lib/nokogiri/css/parser_extras.rb +20 -20
  20. data/lib/nokogiri/css/syntax_error.rb +1 -0
  21. data/lib/nokogiri/css/tokenizer.rb +2 -1
  22. data/lib/nokogiri/css/tokenizer.rex +2 -1
  23. data/lib/nokogiri/css/xpath_visitor.rb +174 -75
  24. data/lib/nokogiri/css.rb +38 -6
  25. data/lib/nokogiri/decorators/slop.rb +8 -7
  26. data/lib/nokogiri/extension.rb +1 -1
  27. data/lib/nokogiri/gumbo.rb +1 -0
  28. data/lib/nokogiri/html.rb +16 -10
  29. data/lib/nokogiri/html4/builder.rb +1 -0
  30. data/lib/nokogiri/html4/document.rb +84 -75
  31. data/lib/nokogiri/html4/document_fragment.rb +11 -7
  32. data/lib/nokogiri/html4/element_description.rb +1 -0
  33. data/lib/nokogiri/html4/element_description_defaults.rb +426 -520
  34. data/lib/nokogiri/html4/entity_lookup.rb +2 -1
  35. data/lib/nokogiri/html4/sax/parser.rb +2 -1
  36. data/lib/nokogiri/html4/sax/parser_context.rb +1 -0
  37. data/lib/nokogiri/html4/sax/push_parser.rb +7 -7
  38. data/lib/nokogiri/html4.rb +11 -5
  39. data/lib/nokogiri/html5/document.rb +24 -10
  40. data/lib/nokogiri/html5/document_fragment.rb +5 -2
  41. data/lib/nokogiri/html5/node.rb +6 -3
  42. data/lib/nokogiri/html5.rb +68 -64
  43. data/lib/nokogiri/jruby/dependencies.rb +10 -9
  44. data/lib/nokogiri/syntax_error.rb +1 -0
  45. data/lib/nokogiri/version/constant.rb +2 -1
  46. data/lib/nokogiri/version/info.rb +19 -13
  47. data/lib/nokogiri/version.rb +1 -0
  48. data/lib/nokogiri/xml/attr.rb +5 -3
  49. data/lib/nokogiri/xml/attribute_decl.rb +2 -1
  50. data/lib/nokogiri/xml/builder.rb +32 -32
  51. data/lib/nokogiri/xml/cdata.rb +2 -1
  52. data/lib/nokogiri/xml/character_data.rb +1 -0
  53. data/lib/nokogiri/xml/document.rb +139 -103
  54. data/lib/nokogiri/xml/document_fragment.rb +41 -38
  55. data/lib/nokogiri/xml/dtd.rb +3 -2
  56. data/lib/nokogiri/xml/element_content.rb +1 -0
  57. data/lib/nokogiri/xml/element_decl.rb +2 -1
  58. data/lib/nokogiri/xml/entity_decl.rb +3 -2
  59. data/lib/nokogiri/xml/entity_reference.rb +1 -0
  60. data/lib/nokogiri/xml/namespace.rb +2 -0
  61. data/lib/nokogiri/xml/node/save_options.rb +6 -3
  62. data/lib/nokogiri/xml/node.rb +512 -348
  63. data/lib/nokogiri/xml/node_set.rb +46 -54
  64. data/lib/nokogiri/xml/notation.rb +12 -0
  65. data/lib/nokogiri/xml/parse_options.rb +11 -7
  66. data/lib/nokogiri/xml/pp/character_data.rb +8 -6
  67. data/lib/nokogiri/xml/pp/node.rb +24 -26
  68. data/lib/nokogiri/xml/pp.rb +1 -0
  69. data/lib/nokogiri/xml/processing_instruction.rb +2 -1
  70. data/lib/nokogiri/xml/reader.rb +17 -19
  71. data/lib/nokogiri/xml/relax_ng.rb +1 -0
  72. data/lib/nokogiri/xml/sax/document.rb +20 -19
  73. data/lib/nokogiri/xml/sax/parser.rb +36 -34
  74. data/lib/nokogiri/xml/sax/parser_context.rb +7 -3
  75. data/lib/nokogiri/xml/sax/push_parser.rb +5 -5
  76. data/lib/nokogiri/xml/sax.rb +1 -0
  77. data/lib/nokogiri/xml/schema.rb +7 -6
  78. data/lib/nokogiri/xml/searchable.rb +42 -22
  79. data/lib/nokogiri/xml/syntax_error.rb +4 -4
  80. data/lib/nokogiri/xml/text.rb +1 -0
  81. data/lib/nokogiri/xml/xpath/syntax_error.rb +2 -1
  82. data/lib/nokogiri/xml/xpath.rb +12 -0
  83. data/lib/nokogiri/xml/xpath_context.rb +2 -3
  84. data/lib/nokogiri/xml.rb +3 -3
  85. data/lib/nokogiri/xslt/stylesheet.rb +1 -0
  86. data/lib/nokogiri/xslt.rb +3 -2
  87. data/lib/nokogiri.rb +19 -16
  88. data/lib/xsd/xmlparser/nokogiri.rb +25 -24
  89. data/patches/libxml2/0008-htmlParseComment-handle-abruptly-closed-comments.patch +61 -0
  90. data/patches/libxml2/0009-allow-wildcard-namespaces.patch +77 -0
  91. metadata +101 -27
@@ -1,42 +1,116 @@
1
+ # coding: utf-8
1
2
  # frozen_string_literal: true
3
+
2
4
  module Nokogiri
3
5
  module CSS
4
- class XPathVisitor # :nodoc:
5
- def visit_function node
6
- msg = :"visit_function_#{node.value.first.gsub(/[(]/, '')}"
7
- return self.send(msg, node) if self.respond_to?(msg)
6
+ # When translating CSS selectors to XPath queries with Nokogiri::CSS.xpath_for, the XPathVisitor
7
+ # class allows for changing some of the behaviors related to builtin xpath functions and quirks
8
+ # of HTML5.
9
+ class XPathVisitor
10
+ WILDCARD_NAMESPACES = Nokogiri.libxml2_patches.include?("0009-allow-wildcard-namespaces.patch") # :nodoc:
11
+
12
+ # Enum to direct XPathVisitor when to use Nokogiri builtin XPath functions.
13
+ module BuiltinsConfig
14
+ # Never use Nokogiri builtin functions, always generate vanilla XPath 1.0 queries. This is
15
+ # the default when calling Nokogiri::CSS.xpath_for directly.
16
+ NEVER = :never
17
+
18
+ # Always use Nokogiri builtin functions whenever possible. This is probably only useful for testing.
19
+ ALWAYS = :always
20
+
21
+ # Only use Nokogiri builtin functions when they will be faster than vanilla XPath. This is
22
+ # the behavior chosen when searching for CSS selectors on a Nokogiri document, fragment, or
23
+ # node.
24
+ OPTIMAL = :optimal
25
+
26
+ # :nodoc: array of values for validation
27
+ VALUES = [NEVER, ALWAYS, OPTIMAL]
28
+ end
29
+
30
+ # Enum to direct XPathVisitor when to tweak the XPath query to suit the nature of the document
31
+ # being searched. Note that searches for CSS selectors from a Nokogiri document, fragment, or
32
+ # node will choose the correct option automatically.
33
+ module DoctypeConfig
34
+ # The document being searched is an XML document. This is the default.
35
+ XML = :xml
36
+
37
+ # The document being searched is an HTML4 document.
38
+ HTML4 = :html4
39
+
40
+ # The document being searched is an HTML5 document.
41
+ HTML5 = :html5
42
+
43
+ # :nodoc: array of values for validation
44
+ VALUES = [XML, HTML4, HTML5]
45
+ end
46
+
47
+ # :call-seq:
48
+ # new() → XPathVisitor
49
+ # new(builtins:, doctype:) → XPathVisitor
50
+ #
51
+ # [Parameters]
52
+ # - +builtins:+ (BuiltinsConfig) Determine when to use Nokogiri's built-in xpath functions for performance improvements.
53
+ # - +doctype:+ (DoctypeConfig) Make document-type-specific accommodations for CSS queries.
54
+ #
55
+ # [Returns] XPathVisitor
56
+ #
57
+ def initialize(builtins: BuiltinsConfig::NEVER, doctype: DoctypeConfig::XML)
58
+ unless BuiltinsConfig::VALUES.include?(builtins)
59
+ raise(ArgumentError, "Invalid values #{builtins.inspect} for builtins: keyword parameter")
60
+ end
61
+ unless DoctypeConfig::VALUES.include?(doctype)
62
+ raise(ArgumentError, "Invalid values #{doctype.inspect} for doctype: keyword parameter")
63
+ end
64
+
65
+ @builtins = builtins
66
+ @doctype = doctype
67
+ end
68
+
69
+ # :call-seq: config() → Hash
70
+ #
71
+ # [Returns]
72
+ # a Hash representing the configuration of the XPathVisitor, suitable for use as
73
+ # part of the CSS cache key.
74
+ def config
75
+ { builtins: @builtins, doctype: @doctype }
76
+ end
77
+
78
+ # :stopdoc:
79
+ def visit_function(node)
80
+ msg = :"visit_function_#{node.value.first.gsub(/[(]/, "")}"
81
+ return send(msg, node) if respond_to?(msg)
8
82
 
9
83
  case node.value.first
10
84
  when /^text\(/
11
- 'child::text()'
85
+ "child::text()"
12
86
  when /^self\(/
13
87
  "self::#{node.value[1]}"
14
88
  when /^eq\(/
15
89
  "position()=#{node.value[1]}"
16
90
  when /^(nth|nth-of-type)\(/
17
- if node.value[1].is_a?(Nokogiri::CSS::Node) and node.value[1].type == :NTH
91
+ if node.value[1].is_a?(Nokogiri::CSS::Node) && (node.value[1].type == :NTH)
18
92
  nth(node.value[1])
19
93
  else
20
94
  "position()=#{node.value[1]}"
21
95
  end
22
96
  when /^nth-child\(/
23
- if node.value[1].is_a?(Nokogiri::CSS::Node) and node.value[1].type == :NTH
24
- nth(node.value[1], :child => true)
97
+ if node.value[1].is_a?(Nokogiri::CSS::Node) && (node.value[1].type == :NTH)
98
+ nth(node.value[1], child: true)
25
99
  else
26
- "count(preceding-sibling::*)=#{node.value[1].to_i-1}"
100
+ "count(preceding-sibling::*)=#{node.value[1].to_i - 1}"
27
101
  end
28
102
  when /^nth-last-of-type\(/
29
- if node.value[1].is_a?(Nokogiri::CSS::Node) and node.value[1].type == :NTH
30
- nth(node.value[1], :last => true)
103
+ if node.value[1].is_a?(Nokogiri::CSS::Node) && (node.value[1].type == :NTH)
104
+ nth(node.value[1], last: true)
31
105
  else
32
106
  index = node.value[1].to_i - 1
33
107
  index == 0 ? "position()=last()" : "position()=last()-#{index}"
34
108
  end
35
109
  when /^nth-last-child\(/
36
- if node.value[1].is_a?(Nokogiri::CSS::Node) and node.value[1].type == :NTH
37
- nth(node.value[1], :last => true, :child => true)
110
+ if node.value[1].is_a?(Nokogiri::CSS::Node) && (node.value[1].type == :NTH)
111
+ nth(node.value[1], last: true, child: true)
38
112
  else
39
- "count(following-sibling::*)=#{node.value[1].to_i-1}"
113
+ "count(following-sibling::*)=#{node.value[1].to_i - 1}"
40
114
  end
41
115
  when /^(first|first-of-type)\(/
42
116
  "position()=1"
@@ -52,15 +126,15 @@ module Nokogiri
52
126
  "comment()"
53
127
  when /^has\(/
54
128
  is_direct = node.value[1].value[0].nil? # e.g. "has(> a)", "has(~ a)", "has(+ a)"
55
- ".#{"//" if !is_direct}#{node.value[1].accept(self)}"
129
+ ".#{"//" unless is_direct}#{node.value[1].accept(self)}"
56
130
  else
57
131
  # non-standard. this looks like a function call.
58
- args = ['.'] + node.value[1..-1]
59
- "#{node.value.first}#{args.join(',')})"
132
+ args = ["."] + node.value[1..-1]
133
+ "#{node.value.first}#{args.join(",")})"
60
134
  end
61
135
  end
62
136
 
63
- def visit_not node
137
+ def visit_not(node)
64
138
  child = node.value.first
65
139
  if :ELEMENT_NAME == child.type
66
140
  "not(self::#{child.accept(self)})"
@@ -69,29 +143,29 @@ module Nokogiri
69
143
  end
70
144
  end
71
145
 
72
- def visit_id node
146
+ def visit_id(node)
73
147
  node.value.first =~ /^#(.*)$/
74
- "@id='#{$1}'"
148
+ "@id='#{Regexp.last_match(1)}'"
75
149
  end
76
150
 
77
- def visit_attribute_condition node
78
- attribute = if (node.value.first.type == :FUNCTION) or (node.value.first.value.first =~ /::/)
79
- ''
80
- else
81
- '@'
82
- end
151
+ def visit_attribute_condition(node)
152
+ attribute = if (node.value.first.type == :FUNCTION) || (node.value.first.value.first =~ /::/)
153
+ ""
154
+ else
155
+ "@"
156
+ end
83
157
  attribute += node.value.first.accept(self)
84
158
 
85
159
  # non-standard. attributes starting with '@'
86
- attribute.gsub!(/^@@/, '@')
160
+ attribute.gsub!(/^@@/, "@")
87
161
 
88
162
  return attribute unless node.value.length == 3
89
163
 
90
164
  value = node.value.last
91
- value = "'#{value}'" if value !~ /^['"]/
165
+ value = "'#{value}'" unless /^['"]/.match?(value)
92
166
 
93
167
  # quoted values - see test_attribute_value_with_quotes in test/css/test_parser.rb
94
- if (value[0]==value[-1]) && %q{"'}.include?(value[0])
168
+ if (value[0] == value[-1]) && %q{"'}.include?(value[0])
95
169
  str_value = value[1..-2]
96
170
  if str_value.include?(value[0])
97
171
  value = 'concat("' + str_value.split('"', -1).join(%q{",'"',"}) + '","")'
@@ -100,9 +174,9 @@ module Nokogiri
100
174
 
101
175
  case node.value[1]
102
176
  when :equal
103
- attribute + "=" + "#{value}"
177
+ attribute + "=" + value.to_s
104
178
  when :not_equal
105
- attribute + "!=" + "#{value}"
179
+ attribute + "!=" + value.to_s
106
180
  when :substring_match
107
181
  "contains(#{attribute},#{value})"
108
182
  when :prefix_match
@@ -115,16 +189,16 @@ module Nokogiri
115
189
  when :suffix_match
116
190
  "substring(#{attribute},string-length(#{attribute})-string-length(#{value})+1,string-length(#{value}))=#{value}"
117
191
  else
118
- attribute + " #{node.value[1]} " + "#{value}"
192
+ attribute + " #{node.value[1]} " + value.to_s
119
193
  end
120
194
  end
121
195
 
122
- def visit_pseudo_class node
123
- if node.value.first.is_a?(Nokogiri::CSS::Node) and node.value.first.type == :FUNCTION
196
+ def visit_pseudo_class(node)
197
+ if node.value.first.is_a?(Nokogiri::CSS::Node) && (node.value.first.type == :FUNCTION)
124
198
  node.value.first.accept(self)
125
199
  else
126
- msg = :"visit_pseudo_class_#{node.value.first.gsub(/[(]/, '')}"
127
- return self.send(msg, node) if self.respond_to?(msg)
200
+ msg = :"visit_pseudo_class_#{node.value.first.gsub(/[(]/, "")}"
201
+ return send(msg, node) if respond_to?(msg)
128
202
 
129
203
  case node.value.first
130
204
  when "first" then "position()=1"
@@ -144,24 +218,24 @@ module Nokogiri
144
218
  end
145
219
  end
146
220
 
147
- def visit_class_condition node
221
+ def visit_class_condition(node)
148
222
  css_class("@class", node.value.first)
149
223
  end
150
224
 
151
- def visit_combinator node
225
+ def visit_combinator(node)
152
226
  if is_of_type_pseudo_class?(node.value.last)
153
- "#{node.value.first.accept(self) if node.value.first}][#{node.value.last.accept(self)}"
227
+ "#{node.value.first&.accept(self)}][#{node.value.last.accept(self)}"
154
228
  else
155
- "#{node.value.first.accept(self) if node.value.first} and #{node.value.last.accept(self)}"
229
+ "#{node.value.first&.accept(self)} and #{node.value.last.accept(self)}"
156
230
  end
157
231
  end
158
232
 
159
233
  {
160
- 'direct_adjacent_selector' => "/following-sibling::*[1]/self::",
161
- 'following_selector' => "/following-sibling::",
162
- 'descendant_selector' => '//',
163
- 'child_selector' => '/',
164
- }.each do |k,v|
234
+ "direct_adjacent_selector" => "/following-sibling::*[1]/self::",
235
+ "following_selector" => "/following-sibling::",
236
+ "descendant_selector" => "//",
237
+ "child_selector" => "/",
238
+ }.each do |k, v|
165
239
  class_eval %{
166
240
  def visit_#{k} node
167
241
  "\#{node.value.first.accept(self) if node.value.first}#{v}\#{node.value.last.accept(self)}"
@@ -169,25 +243,46 @@ module Nokogiri
169
243
  }
170
244
  end
171
245
 
172
- def visit_conditional_selector node
173
- node.value.first.accept(self) + '[' +
174
- node.value.last.accept(self) + ']'
246
+ def visit_conditional_selector(node)
247
+ node.value.first.accept(self) + "[" +
248
+ node.value.last.accept(self) + "]"
249
+ end
250
+
251
+ def visit_element_name(node)
252
+ if @doctype == DoctypeConfig::HTML5 && node.value.first != "*"
253
+ # if there is already a namespace, use it as normal
254
+ return node.value.first if node.value.first.include?(":")
255
+
256
+ # HTML5 has namespaces that should be ignored in CSS queries
257
+ # https://github.com/sparklemotion/nokogiri/issues/2376
258
+ if @builtins == BuiltinsConfig::ALWAYS || (@builtins == BuiltinsConfig::OPTIMAL && Nokogiri.uses_libxml?)
259
+ if WILDCARD_NAMESPACES
260
+ "*:#{node.value.first}"
261
+ else
262
+ "*[nokogiri-builtin:local-name-is('#{node.value.first}')]"
263
+ end
264
+ else
265
+ "*[local-name()='#{node.value.first}']"
266
+ end
267
+ else
268
+ node.value.first
269
+ end
175
270
  end
176
271
 
177
- def visit_element_name node
272
+ def visit_attrib_name(node)
178
273
  node.value.first
179
274
  end
180
275
 
181
- def accept node
276
+ def accept(node)
182
277
  node.accept(self)
183
278
  end
184
279
 
185
280
  private
186
281
 
187
- def nth node, options={}
282
+ def nth(node, options = {})
188
283
  raise ArgumentError, "expected an+b node to contain 4 tokens, but is #{node.value.inspect}" unless node.value.size == 4
189
284
 
190
- a, b = read_a_and_positive_b node.value
285
+ a, b = read_a_and_positive_b(node.value)
191
286
  position = if options[:child]
192
287
  options[:last] ? "(count(following-sibling::*)+1)" : "(count(preceding-sibling::*)+1)"
193
288
  else
@@ -206,7 +301,7 @@ module Nokogiri
206
301
  end
207
302
  end
208
303
 
209
- def read_a_and_positive_b values
304
+ def read_a_and_positive_b(values)
210
305
  op = values[2]
211
306
  if op == "+"
212
307
  a = values[0].to_i
@@ -220,9 +315,9 @@ module Nokogiri
220
315
  [a, b]
221
316
  end
222
317
 
223
- def is_of_type_pseudo_class? node
224
- if node.type==:PSEUDO_CLASS
225
- if node.value[0].is_a?(Nokogiri::CSS::Node) and node.value[0].type == :FUNCTION
318
+ def is_of_type_pseudo_class?(node) # rubocop:disable Naming/PredicateName
319
+ if node.type == :PSEUDO_CLASS
320
+ if node.value[0].is_a?(Nokogiri::CSS::Node) && (node.value[0].type == :FUNCTION)
226
321
  node.value[0].value[0]
227
322
  else
228
323
  node.value[0]
@@ -230,30 +325,34 @@ module Nokogiri
230
325
  end
231
326
  end
232
327
 
233
- # use only ordinary xpath functions
234
- def css_class_standard(hay, needle)
235
- "contains(concat(' ',normalize-space(#{hay}),' '),' #{needle} ')"
236
- end
237
-
238
- # use the builtin implementation
239
- def css_class_builtin(hay, needle)
240
- "nokogiri-builtin:css-class(#{hay},'#{needle}')"
328
+ def css_class(hay, needle)
329
+ if @builtins == BuiltinsConfig::ALWAYS || (@builtins == BuiltinsConfig::OPTIMAL && Nokogiri.uses_libxml?)
330
+ # use the builtin implementation
331
+ "nokogiri-builtin:css-class(#{hay},'#{needle}')"
332
+ else
333
+ # use only ordinary xpath functions
334
+ "contains(concat(' ',normalize-space(#{hay}),' '),' #{needle} ')"
335
+ end
241
336
  end
242
-
243
- alias_method :css_class, :css_class_standard
244
337
  end
245
338
 
246
- class XPathVisitorAlwaysUseBuiltins < XPathVisitor # :nodoc:
247
- private
248
- alias_method :css_class, :css_class_builtin
339
+ module XPathVisitorAlwaysUseBuiltins # :nodoc:
340
+ def self.new
341
+ warn(
342
+ "Nokogiri::CSS::XPathVisitorAlwaysUseBuiltins is deprecated and will be removed in a future version of Nokogiri",
343
+ { uplevel: 1 },
344
+ )
345
+ XPathVisitor.new(builtins: :always)
346
+ end
249
347
  end
250
348
 
251
- class XPathVisitorOptimallyUseBuiltins < XPathVisitor # :nodoc:
252
- private
253
- if Nokogiri.uses_libxml?
254
- alias_method :css_class, :css_class_builtin
255
- else
256
- alias_method :css_class, :css_class_standard
349
+ module XPathVisitorOptimallyUseBuiltins # :nodoc:
350
+ def self.new
351
+ warn(
352
+ "Nokogiri::CSS::XPathVisitorOptimallyUseBuiltins is deprecated and will be removed in a future version of Nokogiri",
353
+ { uplevel: 1 },
354
+ )
355
+ XPathVisitor.new(builtins: :optimal)
257
356
  end
258
357
  end
259
358
  end
data/lib/nokogiri/css.rb CHANGED
@@ -1,17 +1,49 @@
1
+ # coding: utf-8
1
2
  # frozen_string_literal: true
3
+
2
4
  module Nokogiri
5
+ # Translate a CSS selector into an XPath 1.0 query
3
6
  module CSS
4
7
  class << self
5
- ###
6
- # Parse this CSS selector in +selector+. Returns an AST.
7
- def parse(selector)
8
+ # TODO: Deprecate this method ahead of 2.0 and delete it in 2.0.
9
+ # It is not used by Nokogiri and shouldn't be part of the public API.
10
+ def parse(selector) # :nodoc:
8
11
  Parser.new.parse(selector)
9
12
  end
10
13
 
11
- ###
12
- # Get the XPath for +selector+.
14
+ # :call-seq:
15
+ # xpath_for(selector) String
16
+ # xpath_for(selector [, prefix:] [, visitor:] [, ns:]) → String
17
+ #
18
+ # Translate a CSS selector to the equivalent XPath query.
19
+ #
20
+ # [Parameters]
21
+ # - +selector+ (String) The CSS selector to be translated into XPath
22
+ #
23
+ # - +prefix:+ (String)
24
+ #
25
+ # The XPath prefix for the query, see Nokogiri::XML::XPath for some options. Default is
26
+ # +XML::XPath::GLOBAL_SEARCH_PREFIX+.
27
+ #
28
+ # - +visitor:+ (Nokogiri::CSS::XPathVisitor)
29
+ #
30
+ # The visitor class to use to transform the AST into XPath. Default is
31
+ # +Nokogiri::CSS::XPathVisitor.new+.
32
+ #
33
+ # - +ns:+ (Hash<String ⇒ String>)
34
+ #
35
+ # The namespaces that are referenced in the query, if any. This is a hash where the keys are
36
+ # the namespace prefix and the values are the namespace URIs. Default is an empty Hash.
37
+ #
38
+ # [Returns] (String) The equivalent XPath query for +selector+
39
+ #
40
+ # 💡 Note that translated queries are cached for performance concerns.
41
+ #
13
42
  def xpath_for(selector, options = {})
14
- Parser.new(options[:ns] || {}).xpath_for(selector, options)
43
+ prefix = options.fetch(:prefix, Nokogiri::XML::XPath::GLOBAL_SEARCH_PREFIX)
44
+ visitor = options.fetch(:visitor) { Nokogiri::CSS::XPathVisitor.new }
45
+ ns = options.fetch(:ns, {})
46
+ Parser.new(ns).xpath_for(selector, prefix, visitor)
15
47
  end
16
48
  end
17
49
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Nokogiri
3
4
  module Decorators
4
5
  ###
@@ -10,21 +11,21 @@ module Nokogiri
10
11
 
11
12
  ###
12
13
  # look for node with +name+. See Nokogiri.Slop
13
- def method_missing name, *args, &block
14
+ def method_missing(name, *args, &block)
14
15
  if args.empty?
15
- list = xpath("#{XPATH_PREFIX}#{name.to_s.sub(/^_/, '')}")
16
- elsif args.first.is_a? Hash
16
+ list = xpath("#{XPATH_PREFIX}#{name.to_s.sub(/^_/, "")}")
17
+ elsif args.first.is_a?(Hash)
17
18
  hash = args.first
18
19
  if hash[:css]
19
20
  list = css("#{name}#{hash[:css]}")
20
21
  elsif hash[:xpath]
21
- conds = Array(hash[:xpath]).join(' and ')
22
+ conds = Array(hash[:xpath]).join(" and ")
22
23
  list = xpath("#{XPATH_PREFIX}#{name}[#{conds}]")
23
24
  end
24
25
  else
25
26
  CSS::Parser.without_cache do
26
27
  list = xpath(
27
- *CSS.xpath_for("#{name}#{args.first}", :prefix => XPATH_PREFIX)
28
+ *CSS.xpath_for("#{name}#{args.first}", prefix: XPATH_PREFIX)
28
29
  )
29
30
  end
30
31
  end
@@ -33,8 +34,8 @@ module Nokogiri
33
34
  list.length == 1 ? list.first : list
34
35
  end
35
36
 
36
- def respond_to_missing? name, include_private = false
37
- list = xpath("#{XPATH_PREFIX}#{name.to_s.sub(/^_/, '')}")
37
+ def respond_to_missing?(name, include_private = false)
38
+ list = xpath("#{XPATH_PREFIX}#{name.to_s.sub(/^_/, "")}")
38
39
 
39
40
  !list.empty?
40
41
  end
@@ -6,7 +6,7 @@ begin
6
6
  ::RUBY_VERSION =~ /(\d+\.\d+)/
7
7
  require_relative "#{Regexp.last_match(1)}/nokogiri"
8
8
  rescue LoadError => e
9
- if e.message =~ /GLIBC/
9
+ if /GLIBC/.match?(e.message)
10
10
  warn(<<~EOM)
11
11
 
12
12
  ERROR: It looks like you're trying to use Nokogiri as a precompiled native gem on a system with glibc < 2.17:
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Nokogiri
3
4
  module Gumbo
4
5
  # The default maximum number of attributes per element.
data/lib/nokogiri/html.rb CHANGED
@@ -1,40 +1,46 @@
1
+ # coding: utf-8
1
2
  # frozen_string_literal: true
3
+
2
4
  require_relative "html4"
3
5
 
4
6
  module Nokogiri
7
+ # Alias for Nokogiri::HTML4
5
8
  HTML = Nokogiri::HTML4
6
9
 
7
- # @!method HTML(input, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block)
10
+ # :singleton-method: HTML
11
+ # :call-seq: HTML(input, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block) → Nokogiri::HTML4::Document
12
+ #
8
13
  # Parse HTML. Convenience method for Nokogiri::HTML4::Document.parse
9
- # @!scope class
14
+
15
+ # :nodoc:
10
16
  define_singleton_method(:HTML, Nokogiri.method(:HTML4))
11
17
 
12
- # @note This module/namespace is an alias for {Nokogiri::HTML4} as of v1.12.0. Before v1.12.0,
13
- # {Nokogiri::HTML4} did not exist, and this was the module/namespace for all HTML-related
18
+ # 💡 This module/namespace is an alias for Nokogiri::HTML4 as of v1.12.0. Before v1.12.0,
19
+ # Nokogiri::HTML4 did not exist, and this was the module/namespace for all HTML-related
14
20
  # classes.
15
21
  module HTML
16
- # @note This class is an alias for {Nokogiri::HTML4::Document} as of v1.12.0.
22
+ # 💡 This class is an alias for Nokogiri::HTML4::Document as of v1.12.0.
17
23
  class Document < Nokogiri::XML::Document
18
24
  end
19
25
 
20
- # @note This class is an alias for {Nokogiri::HTML4::DocumentFragment} as of v1.12.0.
26
+ # 💡 This class is an alias for Nokogiri::HTML4::DocumentFragment as of v1.12.0.
21
27
  class DocumentFragment < Nokogiri::XML::DocumentFragment
22
28
  end
23
29
 
24
- # @note This class is an alias for {Nokogiri::HTML4::Builder} as of v1.12.0.
30
+ # 💡 This class is an alias for Nokogiri::HTML4::Builder as of v1.12.0.
25
31
  class Builder < Nokogiri::XML::Builder
26
32
  end
27
33
 
28
34
  module SAX
29
- # @note This class is an alias for {Nokogiri::HTML4::SAX::Parser} as of v1.12.0.
35
+ # 💡 This class is an alias for Nokogiri::HTML4::SAX::Parser as of v1.12.0.
30
36
  class Parser < Nokogiri::XML::SAX::Parser
31
37
  end
32
38
 
33
- # @note This class is an alias for {Nokogiri::HTML4::SAX::ParserContext} as of v1.12.0.
39
+ # 💡 This class is an alias for Nokogiri::HTML4::SAX::ParserContext as of v1.12.0.
34
40
  class ParserContext < Nokogiri::XML::SAX::ParserContext
35
41
  end
36
42
 
37
- # @note This class is an alias for {Nokogiri::HTML4::SAX::PushParser} as of v1.12.0.
43
+ # 💡 This class is an alias for Nokogiri::HTML4::SAX::PushParser as of v1.12.0.
38
44
  class PushParser
39
45
  end
40
46
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Nokogiri
3
4
  module HTML4
4
5
  ###