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,28 +1,28 @@
1
1
  # frozen_string_literal: false
2
2
  module REXML
3
3
  module Security
4
- @@entity_expansion_limit = 10_000
4
+ @entity_expansion_limit = 10_000
5
5
 
6
6
  # Set the entity expansion limit. By default the limit is set to 10000.
7
7
  def self.entity_expansion_limit=( val )
8
- @@entity_expansion_limit = val
8
+ @entity_expansion_limit = val
9
9
  end
10
10
 
11
11
  # Get the entity expansion limit. By default the limit is set to 10000.
12
12
  def self.entity_expansion_limit
13
- @@entity_expansion_limit
13
+ @entity_expansion_limit
14
14
  end
15
15
 
16
- @@entity_expansion_text_limit = 10_240
16
+ @entity_expansion_text_limit = 10_240
17
17
 
18
18
  # Set the entity expansion limit. By default the limit is set to 10240.
19
19
  def self.entity_expansion_text_limit=( val )
20
- @@entity_expansion_text_limit = val
20
+ @entity_expansion_text_limit = val
21
21
  end
22
22
 
23
23
  # Get the entity expansion limit. By default the limit is set to 10240.
24
24
  def self.entity_expansion_text_limit
25
- @@entity_expansion_text_limit
25
+ @entity_expansion_text_limit
26
26
  end
27
27
  end
28
28
  end
data/lib/rexml/source.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # coding: US-ASCII
2
- # frozen_string_literal: false
2
+ # frozen_string_literal: true
3
3
 
4
4
  require "stringio"
5
5
  require "strscan"
@@ -8,7 +8,7 @@ require_relative 'encoding'
8
8
 
9
9
  module REXML
10
10
  if StringScanner::Version < "1.0.0"
11
- module StringScannerCheckScanString
11
+ module StringScannerCheckScanString # :nodoc:
12
12
  refine StringScanner do
13
13
  def check(pattern)
14
14
  pattern = /#{Regexp.escape(pattern)}/ if pattern.is_a?(String)
@@ -35,7 +35,7 @@ module REXML
35
35
  end
36
36
 
37
37
  # Generates Source-s. USE THIS CLASS.
38
- class SourceFactory
38
+ class SourceFactory # :nodoc:
39
39
  # Generates a Source object
40
40
  # @param arg Either a String, or an IO
41
41
  # @return a Source, or nil if a bad argument was given
@@ -58,7 +58,7 @@ module REXML
58
58
 
59
59
  # A Source can be searched for patterns, and wraps buffers and other
60
60
  # objects and provides consumption of text
61
- class Source
61
+ class Source # :nodoc: all
62
62
  include Encoding
63
63
  # The line number of the last consumed text
64
64
  attr_reader :line
@@ -78,6 +78,7 @@ module REXML
78
78
  PRE_DEFINED_TERM_PATTERNS[term] = term
79
79
  end
80
80
  end
81
+ PRE_DEFINED_TERM_PATTERNS.freeze
81
82
  end
82
83
  private_constant :Private
83
84
 
@@ -217,7 +218,7 @@ module REXML
217
218
 
218
219
  # A Source that wraps an IO. See the Source class for method
219
220
  # documentation
220
- class IOSource < Source
221
+ class IOSource < Source # :nodoc:
221
222
  #attr_reader :block_size
222
223
 
223
224
  # block_size has been deprecated
@@ -227,9 +228,9 @@ module REXML
227
228
  @pending_buffer = nil
228
229
 
229
230
  if encoding
230
- super("", encoding)
231
+ super(+"", encoding)
231
232
  else
232
- super(@source.read(3) || "")
233
+ super(@source.read(3) || +"")
233
234
  end
234
235
 
235
236
  if !@to_utf and
@@ -380,7 +381,7 @@ module REXML
380
381
  @source.set_encoding(@encoding, @encoding)
381
382
  end
382
383
  @line_break = encode(">")
383
- @pending_buffer, @scanner.string = @scanner.rest, ""
384
+ @pending_buffer, @scanner.string = @scanner.rest, +""
384
385
  @pending_buffer.force_encoding(@encoding)
385
386
  super
386
387
  end
data/lib/rexml/text.rb CHANGED
@@ -11,23 +11,24 @@ module REXML
11
11
  class Text < Child
12
12
  include Comparable
13
13
  # The order in which the substitutions occur
14
- SPECIALS = [ /&(?!#?[\w-]+;)/u, /</u, />/u, /"/u, /'/u, /\r/u ]
15
- SUBSTITUTES = ['&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#13;']
14
+ SPECIALS = [ /&(?!#?[\w-]+;)/u, /</u, />/u, /"/u, /'/u, /\r/u ].freeze
15
+ SUBSTITUTES = ['&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#13;'].freeze
16
16
  # Characters which are substituted in written strings
17
- SLAICEPS = [ '<', '>', '"', "'", '&' ]
18
- SETUTITSBUS = [ /&lt;/u, /&gt;/u, /&quot;/u, /&apos;/u, /&amp;/u ]
17
+ SLAICEPS = [ '<', '>', '"', "'", '&' ].freeze
18
+ SETUTITSBUS = [ /&lt;/u, /&gt;/u, /&quot;/u, /&apos;/u, /&amp;/u ].freeze
19
19
 
20
20
  # If +raw+ is true, then REXML leaves the value alone
21
21
  attr_accessor :raw
22
22
 
23
23
  NEEDS_A_SECOND_CHECK = /(<|&((#{Entity::NAME});|(#0*((?:\d+)|(?:x[a-fA-F0-9]+)));)?)/um
24
+ ENTITY_NAME_PATTERN = /\A#{Entity::NAME}\z/um
24
25
  NUMERICENTITY = /&#0*((?:\d+)|(?:x[a-fA-F0-9]+));/
25
26
  VALID_CHAR = [
26
27
  0x9, 0xA, 0xD,
27
28
  (0x20..0xD7FF),
28
29
  (0xE000..0xFFFD),
29
30
  (0x10000..0x10FFFF)
30
- ]
31
+ ].freeze
31
32
 
32
33
  VALID_XML_CHARS = Regexp.new('^['+
33
34
  VALID_CHAR.map { |item|
@@ -38,7 +39,7 @@ module REXML
38
39
  [item.first, '-'.ord, item.last].pack('UUU').force_encoding('utf-8')
39
40
  end
40
41
  }.join +
41
- ']*$')
42
+ ']*$').freeze
42
43
 
43
44
  # Constructor
44
45
  # +arg+ if a String, the content is set to the String. If a Text,
@@ -157,7 +158,7 @@ module REXML
157
158
  else
158
159
  raise "Illegal character #{string.inspect} in raw string #{string.inspect}"
159
160
  end
160
- elsif !(/\A#{Entity::NAME}\z/um.match?(value))
161
+ elsif !(ENTITY_NAME_PATTERN.match?(value))
161
162
  raise "Illegal character \"#{string[index]}\" in raw string #{string.inspect}"
162
163
  end
163
164
 
@@ -334,28 +335,16 @@ module REXML
334
335
  end
335
336
 
336
337
  # Reads text, substituting entities
338
+ #
339
+ # Deprecated since 3.4.5. Use Text.unnormalize instead.
337
340
  def Text::read_with_substitution( input, illegal=nil )
338
- copy = input.clone
339
-
340
- if copy =~ illegal
341
+ Kernel.warn("#{name}.read_with_substitution is deprecated. " +
342
+ "Use #{name}.unnormalize instead.", uplevel: 1)
343
+ copy = input.to_s
344
+ if illegal and copy =~ illegal
341
345
  raise ParseException.new( "malformed text: Illegal character #$& in \"#{copy}\"" )
342
- end if illegal
343
-
344
- copy.gsub!( /\r\n?/, "\n" )
345
- if copy.include? ?&
346
- copy.gsub!( SETUTITSBUS[0], SLAICEPS[0] )
347
- copy.gsub!( SETUTITSBUS[1], SLAICEPS[1] )
348
- copy.gsub!( SETUTITSBUS[2], SLAICEPS[2] )
349
- copy.gsub!( SETUTITSBUS[3], SLAICEPS[3] )
350
- copy.gsub!( SETUTITSBUS[4], SLAICEPS[4] )
351
- copy.gsub!( /&#0*((?:\d+)|(?:x[a-f0-9]+));/ ) {
352
- m=$1
353
- #m='0' if m==''
354
- m = "0#{m}" if m[0] == ?x
355
- [Integer(m)].pack('U*')
356
- }
357
346
  end
358
- copy
347
+ unnormalize(copy)
359
348
  end
360
349
 
361
350
  EREFERENCE = /&(?!#{Entity::NAME};)/
@@ -368,9 +357,10 @@ module REXML
368
357
  if doctype
369
358
  # Replace all ampersands that aren't part of an entity
370
359
  doctype.entities.each_value do |entity|
371
- copy = copy.gsub( entity.value,
372
- "&#{entity.name};" ) if entity.value and
373
- not( entity_filter and entity_filter.include?(entity.name) )
360
+ # Skip an empty value because String#gsub("") matches at every position
361
+ next if entity.value.nil? or entity.value.empty?
362
+ next if entity_filter and entity_filter.include?(entity.name)
363
+ copy = copy.gsub( entity.value, "&#{entity.name};" )
374
364
  end
375
365
  else
376
366
  # Replace all ampersands that aren't part of an entity
@@ -384,11 +374,11 @@ module REXML
384
374
  end
385
375
 
386
376
  # Unescapes all possible entities
387
- def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil, entity_expansion_text_limit: nil )
377
+ def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil, entity_expansion_text_limit: nil, expanding: nil )
388
378
  entity_expansion_text_limit ||= Security.entity_expansion_text_limit
389
379
  sum = 0
390
380
  string.gsub( /\r\n?/, "\n" ).gsub( REFERENCE ) {
391
- s = Text.expand($&, doctype, filter)
381
+ s = Text.expand($&, doctype, filter, expanding: expanding)
392
382
  if sum + s.bytesize > entity_expansion_text_limit
393
383
  raise "entity expansion has grown too large"
394
384
  else
@@ -398,19 +388,33 @@ module REXML
398
388
  }
399
389
  end
400
390
 
401
- def Text.expand(ref, doctype, filter)
391
+ # Expands a numeric character reference's code point to a String.
392
+ # Code points that are not valid XML characters are rejected, matching
393
+ # the validation Text.check applies to literal references. This keeps the
394
+ # entity-expansion path from emitting XML-forbidden characters (NUL,
395
+ # control chars, U+FFFE/U+FFFF, code points beyond U+10FFFF, ...).
396
+ def Text.expand_character_reference(code_point, reference)
397
+ case code_point
398
+ when *VALID_CHAR
399
+ [code_point].pack('U*')
400
+ else
401
+ raise ParseException.new("Illegal character reference: <#{reference}>")
402
+ end
403
+ end
404
+
405
+ def Text.expand(ref, doctype, filter, expanding: nil)
402
406
  if ref[1] == ?#
403
407
  if ref[2] == ?x
404
- [ref[3...-1].to_i(16)].pack('U*')
408
+ expand_character_reference(ref[3...-1].to_i(16), ref)
405
409
  else
406
- [ref[2...-1].to_i].pack('U*')
410
+ expand_character_reference(ref[2...-1].to_i, ref)
407
411
  end
408
412
  elsif ref == '&amp;'
409
413
  '&'
410
414
  elsif filter and filter.include?( ref[1...-1] )
411
415
  ref
412
416
  elsif doctype
413
- doctype.entity( ref[1...-1] ) or ref
417
+ doctype.entity( ref[1...-1], expanding: expanding ) or ref
414
418
  else
415
419
  entity_value = DocType::DEFAULT_ENTITIES[ ref[1...-1] ]
416
420
  entity_value ? entity_value.value : ref
@@ -46,6 +46,7 @@ module REXML
46
46
  def initialize source
47
47
  parser = REXML::Parsers::BaseParser.new( source )
48
48
 
49
+ @attr_stack = []
49
50
  @count = 0
50
51
  @references = {}
51
52
  @root = @current = Sequence.new(self)
@@ -120,6 +121,16 @@ module REXML
120
121
  end
121
122
 
122
123
  def receive event
124
+ case event[0]
125
+ when :xmldecl, :comment, :processing_instruction,
126
+ :start_doctype, :end_doctype, :elementdecl, :entitydecl,
127
+ :attlistdecl, :notationdecl, :externalentity
128
+ # These events carry no RELAX NG document content.
129
+ return
130
+ when :text
131
+ # Whitespace outside the root is not element content.
132
+ return if @attr_stack.empty? and /\A[ \t\r\n]*\z/.match?(event[1])
133
+ end
123
134
  validate( event )
124
135
  end
125
136
  end
data/lib/rexml/xmldecl.rb CHANGED
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
 
3
3
  require_relative 'encoding'
4
4
  require_relative 'source'
@@ -117,7 +117,7 @@ module REXML
117
117
  quote = "'"
118
118
  end
119
119
 
120
- rv = "version=#{quote}#{@version}#{quote}"
120
+ rv = +"version=#{quote}#{@version}#{quote}"
121
121
  if @writeencoding or enc !~ /\Autf-8\z/i
122
122
  rv << " encoding=#{quote}#{enc}#{quote}"
123
123
  end
@@ -58,8 +58,8 @@ module REXML
58
58
  "\\u0300-\\u036F",
59
59
  "\\u203F-\\u2040",
60
60
  ]
61
- NAME_START_CHAR = "[#{name_start_chars.join('')}]"
62
- NAME_CHAR = "[#{name_chars.join('')}]"
61
+ NAME_START_CHAR = "[#{name_start_chars.join('')}]".freeze
62
+ NAME_CHAR = "[#{name_chars.join('')}]".freeze
63
63
  NAMECHAR = NAME_CHAR # deprecated. Use NAME_CHAR instead.
64
64
 
65
65
  # From http://www.w3.org/TR/xml-names11/#NT-NCName
@@ -70,13 +70,13 @@ module REXML
70
70
  #
71
71
  # [5] NCNameChar ::= NameChar - ':'
72
72
  ncname_chars = name_chars - [":"]
73
- NCNAME_STR = "[#{ncname_start_chars.join('')}][#{ncname_chars.join('')}]*"
74
- NAME_STR = "(?:#{NCNAME_STR}:)?#{NCNAME_STR}"
73
+ NCNAME_STR = "[#{ncname_start_chars.join('')}][#{ncname_chars.join('')}]*".freeze
74
+ NAME_STR = "(?:#{NCNAME_STR}:)?#{NCNAME_STR}".freeze
75
75
 
76
- NAME = "(#{NAME_START_CHAR}#{NAME_CHAR}*)"
77
- NMTOKEN = "(?:#{NAME_CHAR})+"
78
- NMTOKENS = "#{NMTOKEN}(\\s+#{NMTOKEN})*"
79
- REFERENCE = "(?:&#{NAME};|&#\\d+;|&#x[0-9a-fA-F]+;)"
76
+ NAME = "(#{NAME_START_CHAR}#{NAME_CHAR}*)".freeze
77
+ NMTOKEN = "(?:#{NAME_CHAR})+".freeze
78
+ NMTOKENS = "#{NMTOKEN}(\\s+#{NMTOKEN})*".freeze
79
+ REFERENCE = "(?:&#{NAME};|&#\\d+;|&#x[0-9a-fA-F]+;)".freeze
80
80
 
81
81
  #REFERENCE = "(?:#{ENTITYREF}|#{CHARREF})"
82
82
  #ENTITYREF = "&#{NAME};"
data/lib/rexml/xpath.rb CHANGED
@@ -5,7 +5,6 @@ require_relative 'xpath_parser'
5
5
  module REXML
6
6
  # Wrapper class. Use this class to access the XPath functions.
7
7
  class XPath
8
- include Functions
9
8
  # A base Hash object, supposing to be used when initializing a
10
9
  # default empty namespaces set, but is currently unused.
11
10
  # TODO: either set the namespaces=EMPTY_HASH, or deprecate this.