rexml 3.4.4 → 3.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/NEWS.md +304 -0
  3. data/doc/rexml/tasks/rdoc/child.rdoc +1 -1
  4. data/doc/rexml/tasks/rdoc/document.rdoc +1 -1
  5. data/doc/rexml/tasks/rdoc/element.rdoc +1 -1
  6. data/doc/rexml/tasks/rdoc/node.rdoc +1 -1
  7. data/doc/rexml/tasks/rdoc/parent.rdoc +1 -1
  8. data/doc/rexml/tutorial.rdoc +4 -2
  9. data/lib/rexml/attribute.rb +11 -2
  10. data/lib/rexml/cdata.rb +1 -1
  11. data/lib/rexml/comment.rb +1 -1
  12. data/lib/rexml/doctype.rb +21 -18
  13. data/lib/rexml/document.rb +6 -21
  14. data/lib/rexml/element.rb +164 -158
  15. data/lib/rexml/entity.rb +19 -10
  16. data/lib/rexml/formatters/default.rb +35 -19
  17. data/lib/rexml/formatters/pretty.rb +2 -3
  18. data/lib/rexml/functions.rb +164 -147
  19. data/lib/rexml/instruction.rb +3 -3
  20. data/lib/rexml/namespace.rb +9 -1
  21. data/lib/rexml/output.rb +1 -1
  22. data/lib/rexml/parent.rb +1 -3
  23. data/lib/rexml/parsers/baseparser.rb +60 -73
  24. data/lib/rexml/parsers/sax2parser.rb +9 -20
  25. data/lib/rexml/parsers/xpathparser.rb +87 -58
  26. data/lib/rexml/rexml.rb +2 -2
  27. data/lib/rexml/security.rb +6 -6
  28. data/lib/rexml/source.rb +9 -8
  29. data/lib/rexml/text.rb +39 -35
  30. data/lib/rexml/validation/relaxng.rb +11 -0
  31. data/lib/rexml/xmldecl.rb +2 -2
  32. data/lib/rexml/xmltokens.rb +8 -8
  33. data/lib/rexml/xpath.rb +0 -1
  34. data/lib/rexml/xpath_parser.rb +745 -467
  35. metadata +16 -17
  36. data/lib/rexml/quickpath.rb +0 -267
  37. /data/doc/rexml/tasks/{tocs → includes}/child_toc.rdoc +0 -0
  38. /data/doc/rexml/tasks/{tocs → includes}/document_toc.rdoc +0 -0
  39. /data/doc/rexml/tasks/{tocs → includes}/element_toc.rdoc +0 -0
  40. /data/doc/rexml/tasks/{tocs → includes}/node_toc.rdoc +0 -0
  41. /data/doc/rexml/tasks/{tocs → includes}/parent_toc.rdoc +0 -0
  42. /data/doc/rexml/tasks/{tocs/master_toc.rdoc → rdoc/toc.rdoc} +0 -0
@@ -1,4 +1,7 @@
1
1
  # frozen_string_literal: false
2
+
3
+ require 'set'
4
+
2
5
  module REXML
3
6
  # If you add a method, keep in mind two things:
4
7
  # (1) the first argument will always be a list of nodes from which to
@@ -7,97 +10,92 @@ module REXML
7
10
  # (2) all method calls from XML will have "-" replaced with "_".
8
11
  # Therefore, in XML, "local-name()" is identical (and actually becomes)
9
12
  # "local_name()"
10
- module Functions
11
- @@available_functions = {}
12
- @@context = nil
13
- @@namespace_context = {}
14
- @@variables = {}
15
-
16
- INTERNAL_METHODS = [
17
- :namespace_context,
18
- :namespace_context=,
19
- :variables,
20
- :variables=,
21
- :context=,
22
- :get_namespace,
23
- :send,
24
- ]
25
- class << self
26
- def singleton_method_added(name)
27
- unless INTERNAL_METHODS.include?(name)
28
- @@available_functions[name] = true
29
- end
30
- end
31
- end
32
-
33
- def Functions::namespace_context=(x) ; @@namespace_context=x ; end
34
- def Functions::variables=(x) ; @@variables=x ; end
35
- def Functions::namespace_context ; @@namespace_context ; end
36
- def Functions::variables ; @@variables ; end
13
+ class FunctionsClass # :nodoc:
14
+ def initialize
15
+ @context = nil
16
+ @node_indexes = nil
17
+ end
18
+
19
+ AVAILABLE_FUNCTIONS = %w[
20
+ boolean
21
+ ceiling
22
+ concat
23
+ contains
24
+ count
25
+ false
26
+ floor
27
+ id
28
+ lang
29
+ last
30
+ local-name
31
+ name
32
+ namespace-uri
33
+ normalize-space
34
+ not
35
+ number
36
+ position
37
+ round
38
+ starts-with
39
+ string
40
+ string-length
41
+ substring
42
+ substring-after
43
+ substring-before
44
+ sum
45
+ translate
46
+ true
47
+ ].to_set.freeze
37
48
 
38
- def Functions::context=(value); @@context = value; end
49
+ def context=(value); @context = value; end
39
50
 
40
- def Functions::text( )
41
- if @@context[:node].node_type == :element
42
- @@context[:node].find_all{|n| n.node_type == :text}.collect{|n| n.value}
43
- elsif @@context[:node].node_type == :text
44
- @@context[:node].value
45
- else
46
- false
47
- end
48
- end
51
+ # The evaluation's shared index cache, set by XPathParser so that sorting
52
+ # here does not re-index the same children for every call. It is
53
+ # nil for the shared Functions instance, which is reachable without going
54
+ # through XPathParser; sort then caches for one call only.
55
+ def node_indexes=(value); @node_indexes = value; end
49
56
 
50
57
  # Returns the last node of the given list of nodes.
51
- def Functions::last( )
52
- @@context[:size]
58
+ def last( )
59
+ @context[:size]
53
60
  end
54
61
 
55
- def Functions::position( )
56
- @@context[:index]
62
+ def position( )
63
+ @context[:position]
57
64
  end
58
65
 
59
66
  # Returns the size of the given list of nodes.
60
- def Functions::count( node_set )
67
+ def count( node_set )
61
68
  node_set.size
62
69
  end
63
70
 
64
71
  # Since REXML is non-validating, this method is not implemented as it
65
72
  # requires a DTD
66
- def Functions::id( object )
73
+ def id( object )
74
+ []
67
75
  end
68
76
 
69
- def Functions::local_name(node_set=nil)
70
- get_namespace(node_set) do |node|
71
- return node.local_name
72
- end
73
- ""
77
+ def local_name(node_set=nil)
78
+ target_named_node(node_set)&.local_name || ""
74
79
  end
75
80
 
76
- def Functions::namespace_uri( node_set=nil )
77
- get_namespace( node_set ) {|node| node.namespace}
81
+ def namespace_uri( node_set=nil )
82
+ target_named_node(node_set)&.namespace || ""
78
83
  end
79
84
 
80
- def Functions::name( node_set=nil )
81
- get_namespace( node_set ) do |node|
82
- node.expanded_name
83
- end
85
+ def name( node_set=nil )
86
+ target_named_node(node_set)&.expanded_name || ""
84
87
  end
85
88
 
86
89
  # Helper method.
87
- def Functions::get_namespace( node_set = nil )
88
- if node_set == nil
89
- yield @@context[:node] if @@context[:node].respond_to?(:namespace)
90
- else
91
- if node_set.respond_to? :each
92
- result = []
93
- node_set.each do |node|
94
- result << yield(node) if node.respond_to?(:namespace)
95
- end
96
- result
97
- elsif node_set.respond_to? :namespace
98
- yield node_set
90
+ def target_named_node(node_set = nil)
91
+ node =
92
+ case node_set
93
+ when nil
94
+ @context[:node]
95
+ when Array
96
+ XPathParser.sort(node_set, @node_indexes).first
99
97
  end
100
- end
98
+ node if node.respond_to?(:namespace)
101
99
  end
102
100
 
103
101
  # A node-set is converted to a string by returning the string-value of the
@@ -135,7 +133,7 @@ module REXML
135
133
  #
136
134
  # An object of a type other than the four basic types is converted to a
137
135
  # string in a way that is dependent on that type.
138
- def Functions::string( object=@@context[:node] )
136
+ def string( object=@context[:node] )
139
137
  if object.respond_to?(:node_type)
140
138
  case object.node_type
141
139
  when :attribute
@@ -152,16 +150,26 @@ module REXML
152
150
  else
153
151
  case object
154
152
  when Array
155
- string(object[0])
153
+ string(XPathParser.sort(object, @node_indexes).first)
156
154
  when Float
157
155
  if object.nan?
158
156
  "NaN"
157
+ elsif object.infinite?
158
+ object > 0 ? "Infinity" : "-Infinity"
159
159
  else
160
160
  integer = object.to_i
161
161
  if object == integer
162
162
  "%d" % integer
163
163
  else
164
- object.to_s
164
+ str = object.to_s
165
+ if str.include?("e-")
166
+ sign = str.start_with?("-") ? "-" : ""
167
+ mantissa, exp = str.delete_prefix("-").split("e-", 2)
168
+ digits = mantissa.delete(".").sub(/0+\z/, "")
169
+ "#{sign}0.#{'0' * (exp.to_i - 1)}#{digits}"
170
+ else
171
+ str
172
+ end
165
173
  end
166
174
  end
167
175
  else
@@ -175,7 +183,7 @@ module REXML
175
183
  # of each of the children of the node in the
176
184
  # node-set that is first in document order.
177
185
  # If the node-set is empty, an empty string is returned.
178
- def Functions::string_value( o )
186
+ def string_value( o )
179
187
  rv = ""
180
188
  o.children.each { |e|
181
189
  if e.node_type == :text
@@ -187,7 +195,7 @@ module REXML
187
195
  rv
188
196
  end
189
197
 
190
- def Functions::concat( *objects )
198
+ def concat( *objects )
191
199
  concatenated = ""
192
200
  objects.each do |object|
193
201
  concatenated << string(object)
@@ -196,17 +204,17 @@ module REXML
196
204
  end
197
205
 
198
206
  # Fixed by Mike Stok
199
- def Functions::starts_with( string, test )
207
+ def starts_with( string, test )
200
208
  string(string).index(string(test)) == 0
201
209
  end
202
210
 
203
211
  # Fixed by Mike Stok
204
- def Functions::contains( string, test )
212
+ def contains( string, test )
205
213
  string(string).include?(string(test))
206
214
  end
207
215
 
208
216
  # Kouhei fixed this
209
- def Functions::substring_before( string, test )
217
+ def substring_before( string, test )
210
218
  ruby_string = string(string)
211
219
  ruby_index = ruby_string.index(string(test))
212
220
  if ruby_index.nil?
@@ -217,62 +225,52 @@ module REXML
217
225
  end
218
226
 
219
227
  # Kouhei fixed this too
220
- def Functions::substring_after( string, test )
228
+ def substring_after( string, test )
221
229
  ruby_string = string(string)
222
- return $1 if ruby_string =~ /#{test}(.*)/
223
- ""
230
+ ruby_test = string(test)
231
+ ruby_index = ruby_string.index(ruby_test)
232
+ if ruby_index.nil?
233
+ ""
234
+ else
235
+ ruby_string[(ruby_index + ruby_test.length)..-1]
236
+ end
224
237
  end
225
238
 
226
239
  # Take equal portions of Mike Stok and Sean Russell; mix
227
240
  # vigorously, and pour into a tall, chilled glass. Serves 10,000.
228
- def Functions::substring( string, start, length=nil )
241
+ def substring( string, start, length=nil )
229
242
  ruby_string = string(string)
230
- ruby_length = if length.nil?
231
- ruby_string.length.to_f
232
- else
233
- number(length)
234
- end
235
- ruby_start = number(start)
236
-
237
- # Handle the special cases
238
- return '' if (
239
- ruby_length.nan? or
240
- ruby_start.nan? or
241
- ruby_start.infinite?
242
- )
243
-
244
- infinite_length = ruby_length.infinite? == 1
245
- ruby_length = ruby_string.length if infinite_length
246
-
247
- # Now, get the bounds. The XPath bounds are 1..length; the ruby bounds
248
- # are 0..length. Therefore, we have to offset the bounds by one.
249
- ruby_start = round(ruby_start) - 1
250
- ruby_length = round(ruby_length)
251
-
252
- if ruby_start < 0
253
- ruby_length += ruby_start unless infinite_length
254
- ruby_start = 0
243
+ # The returned substring contains each character whose position p
244
+ # satisfies: round(start) <= p < round(start) + round(length)
245
+ first = round(start)
246
+ last = if length.nil?
247
+ Float::INFINITY
248
+ else
249
+ first + round(length)
250
+ end
251
+ # This is also false when first or last is NaN.
252
+ return '' unless first < last
253
+
254
+ ruby_start = first < 1 ? 0 : first - 1
255
+ if last > ruby_string.length
256
+ ruby_string[ruby_start..-1] || ''
257
+ elsif last - 1 > ruby_start
258
+ ruby_string[ruby_start...(last - 1)]
259
+ else
260
+ ''
255
261
  end
256
- return '' if ruby_length <= 0
257
- ruby_string[ruby_start,ruby_length]
258
262
  end
259
263
 
260
- # UNTESTED
261
- def Functions::string_length( string )
262
- string(string).length
264
+ def string_length( object=@context[:node] )
265
+ string(object).length
263
266
  end
264
267
 
265
- def Functions::normalize_space( string=nil )
266
- string = string(@@context[:node]) if string.nil?
267
- if string.kind_of? Array
268
- string.collect{|x| x.to_s.strip.gsub(/\s+/um, ' ') if x}
269
- else
270
- string.to_s.strip.gsub(/\s+/um, ' ')
271
- end
268
+ def normalize_space( object=@context[:node] )
269
+ string(object).strip.gsub(/\s+/um, ' ')
272
270
  end
273
271
 
274
272
  # This is entirely Mike Stok's beast
275
- def Functions::translate( string, tr1, tr2 )
273
+ def translate( string, tr1, tr2 )
276
274
  from = string(tr1)
277
275
  to = string(tr2)
278
276
 
@@ -314,7 +312,7 @@ module REXML
314
312
  end
315
313
  end
316
314
 
317
- def Functions::boolean(object=@@context[:node])
315
+ def boolean(object=@context[:node])
318
316
  case object
319
317
  when true, false
320
318
  object
@@ -334,24 +332,24 @@ module REXML
334
332
  end
335
333
 
336
334
  # UNTESTED
337
- def Functions::not( object )
335
+ def not( object )
338
336
  not boolean( object )
339
337
  end
340
338
 
341
339
  # UNTESTED
342
- def Functions::true( )
340
+ def true( )
343
341
  true
344
342
  end
345
343
 
346
344
  # UNTESTED
347
- def Functions::false( )
345
+ def false( )
348
346
  false
349
347
  end
350
348
 
351
349
  # UNTESTED
352
- def Functions::lang( language )
350
+ def lang( language )
353
351
  lang = false
354
- node = @@context[:node]
352
+ node = @context[:node]
355
353
  attr = nil
356
354
  until node.nil?
357
355
  if node.node_type == :element
@@ -367,8 +365,10 @@ module REXML
367
365
  lang
368
366
  end
369
367
 
370
- def Functions::compare_language lang1, lang2
371
- lang2.downcase.index(lang1.downcase) == 0
368
+ def compare_language lang1, lang2
369
+ lang1 = lang1.downcase
370
+ lang2 = lang2.downcase
371
+ lang2 == lang1 or lang2.start_with?("#{lang1}-")
372
372
  end
373
373
 
374
374
  # a string that consists of optional whitespace followed by an optional
@@ -384,7 +384,7 @@ module REXML
384
384
  #
385
385
  # an object of a type other than the four basic types is converted to a
386
386
  # number in a way that is dependent on that type
387
- def Functions::number(object=@@context[:node])
387
+ def number(object=@context[:node])
388
388
  case object
389
389
  when true
390
390
  Float(1)
@@ -405,42 +405,59 @@ module REXML
405
405
  end
406
406
  end
407
407
 
408
- def Functions::sum( nodes )
408
+ def sum( nodes )
409
409
  nodes = [nodes] unless nodes.kind_of? Array
410
410
  nodes.inject(0) { |r,n| r + number(string(n)) }
411
411
  end
412
412
 
413
- def Functions::floor( number )
414
- number(number).floor
415
- end
416
-
417
- def Functions::ceiling( number )
418
- number(number).ceil
419
- end
420
-
421
- def Functions::round( number )
413
+ def floor( number )
422
414
  number = number(number)
423
415
  begin
424
- neg = number.negative?
425
- number = number.abs.round
426
- neg ? -number : number
416
+ number.floor
427
417
  rescue FloatDomainError
428
418
  number
429
419
  end
430
420
  end
431
421
 
432
- def Functions::processing_instruction( node )
433
- node.node_type == :processing_instruction
422
+ def ceiling( number )
423
+ number = number(number)
424
+ begin
425
+ number.ceil
426
+ rescue FloatDomainError
427
+ number
428
+ end
434
429
  end
435
430
 
436
- def Functions::send(name, *args)
437
- if @@available_functions[name.to_sym]
438
- super
431
+ def round( number )
432
+ number = number(number)
433
+ return number if number.nan? or number.infinite?
434
+ # If there are two integers closest to the number, the one that is
435
+ # closest to positive infinity is returned. If the number is less than
436
+ # zero but greater than or equal to -0.5, negative zero is returned.
437
+ return -0.0 if number < 0 and number >= -0.5
438
+ rounded = number.floor
439
+ rounded += 1 if number - rounded >= 0.5
440
+ rounded
441
+ end
442
+
443
+ def call(name, args:, fallback: nil)
444
+ name = name.to_s
445
+ if AVAILABLE_FUNCTIONS.include?(name)
446
+ __send__(name.tr('-', '_'), *args)
447
+ elsif fallback.nil?
448
+ raise ArgumentError, "Unknown XPath function: #{name}"
439
449
  else
440
- # TODO: Maybe, this is not XPath spec behavior.
441
- # This behavior must be reconsidered.
442
- XPath.match(@@context[:node], name.to_s)
450
+ fallback
443
451
  end
444
452
  end
453
+
454
+ # For compatibility. Use `call` instead.
455
+ def send(name, *args)
456
+ call(name.to_s.tr('_', '-'), args: args, fallback: [])
457
+ end
445
458
  end
459
+
460
+ # Using this singleton instance may cause thread-safety issues.
461
+ # Consider instantiating your own FunctionsClass object.
462
+ Functions = FunctionsClass.new # :nodoc:
446
463
  end
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
 
3
3
  require_relative "child"
4
4
  require_relative "source"
@@ -34,8 +34,8 @@ module REXML
34
34
  @content = target.content
35
35
  else
36
36
  message =
37
- "processing instruction target must be String or REXML::Instruction: "
38
- message << "<#{target.inspect}>"
37
+ "processing instruction target must be String or REXML::Instruction: " \
38
+ "<#{target.inspect}>"
39
39
  raise ArgumentError, message
40
40
  end
41
41
  @content.strip! if @content
@@ -11,7 +11,15 @@ module REXML
11
11
  attr_accessor :prefix
12
12
  include XMLTokens
13
13
  NAME_WITHOUT_NAMESPACE = /\A#{NCNAME_STR}\z/
14
- NAMESPLIT = /^(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u
14
+ NAMESPLIT = /\A(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})/u
15
+
16
+ XML_URI = "http://www.w3.org/XML/1998/namespace"
17
+ private_constant :XML_URI
18
+
19
+ def self.resolve_prefix(prefix, namespaces) # :nodoc:
20
+ # XML Namespaces reserves this binding even without an xmlns declaration.
21
+ prefix == "xml" ? XML_URI : namespaces[prefix]
22
+ end
15
23
 
16
24
  # Sets the name and the expanded name
17
25
  def name=( name )
data/lib/rexml/output.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  require_relative 'encoding'
3
3
 
4
4
  module REXML
5
- class Output
5
+ class Output # :nodoc:
6
6
  include Encoding
7
7
 
8
8
  attr_reader :encoding
data/lib/rexml/parent.rb CHANGED
@@ -121,9 +121,7 @@ module REXML
121
121
  # @return the index of the child, or nil if the object is not a child
122
122
  # of this parent.
123
123
  def index( child )
124
- count = -1
125
- @children.find { |i| count += 1 ; i.hash == child.hash }
126
- count
124
+ @children.index { |c| c.equal?(child) }
127
125
  end
128
126
 
129
127
  # @return the number of children of this parent