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
@@ -3,6 +3,8 @@ require_relative '../parseexception'
3
3
  require_relative '../undefinednamespaceexception'
4
4
  require_relative '../security'
5
5
  require_relative '../source'
6
+ require_relative '../text'
7
+ require_relative '../xmltokens'
6
8
  require 'set'
7
9
  require "strscan"
8
10
 
@@ -58,8 +60,10 @@ module REXML
58
60
  LETTER = '[:alpha:]'
59
61
  DIGIT = '[:digit:]'
60
62
 
61
- COMBININGCHAR = '' # TODO
62
- EXTENDER = '' # TODO
63
+ # Only the ones that aren't [:alpha:], to avoid duplicated ranges in
64
+ # the NCNAME_STR character class
65
+ COMBININGCHAR = '[\p{M}&&[^[:alpha:]]]'
66
+ EXTENDER = '\u00B7\u0387'
63
67
 
64
68
  NCNAME_STR= "[#{LETTER}_][-[:alnum:]._#{COMBININGCHAR}#{EXTENDER}]*"
65
69
  QNAME_STR= "(?:(#{NCNAME_STR}):)?(#{NCNAME_STR})"
@@ -69,8 +73,8 @@ module REXML
69
73
  # It's not used in REXML.
70
74
  UNAME_STR= "(?:#{NCNAME_STR}:)?#{NCNAME_STR}"
71
75
 
72
- NAMECHAR = '[\-\w\.:]'
73
- NAME = "([\\w:]#{NAMECHAR}*)"
76
+ NAMECHAR = XMLTokens::NAME_CHAR
77
+ NAME = XMLTokens::NAME
74
78
  NMTOKEN = "(?:#{NAMECHAR})+"
75
79
  NMTOKENS = "#{NMTOKEN}(\\s+#{NMTOKEN})*"
76
80
  REFERENCE = "&(?:#{NAME};|#\\d+;|#x[0-9a-fA-F]+;)"
@@ -134,11 +138,11 @@ module REXML
134
138
  EREFERENCE = /&(?!#{NAME};)/
135
139
 
136
140
  DEFAULT_ENTITIES = {
137
- 'gt' => [/>/, '>', '>', />/],
138
- 'lt' => [/&lt;/, '&lt;', '<', /</],
139
- 'quot' => [/&quot;/, '&quot;', '"', /"/],
140
- "apos" => [/&apos;/, "&apos;", "'", /'/]
141
- }
141
+ 'gt' => [/&gt;/, '&gt;', '>', />/].freeze,
142
+ 'lt' => [/&lt;/, '&lt;', '<', /</].freeze,
143
+ 'quot' => [/&quot;/, '&quot;', '"', /"/].freeze,
144
+ "apos" => [/&apos;/, "&apos;", "'", /'/].freeze
145
+ }.freeze
142
146
 
143
147
  module Private
144
148
  PEREFERENCE_PATTERN = /#{PEREFERENCE}/um
@@ -157,7 +161,11 @@ module REXML
157
161
  default_entities.each do |term|
158
162
  DEFAULT_ENTITIES_PATTERNS[term] = /&#{term};/
159
163
  end
164
+ DEFAULT_ENTITIES_PATTERNS.freeze
160
165
  XML_PREFIXED_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
166
+ EXTERNAL_ID_PUBLIC_PATTERN = /\s+#{PUBIDLITERAL}\s+#{SYSTEMLITERAL}/um
167
+ EXTERNAL_ID_SYSTEM_PATTERN = /\s+#{SYSTEMLITERAL}/um
168
+ PUBLIC_ID_PATTERN = /\s+#{PUBIDLITERAL}/um
161
169
  end
162
170
  private_constant :Private
163
171
 
@@ -307,7 +315,6 @@ module REXML
307
315
  @source.ensure_buffer
308
316
  else
309
317
  id = parse_id(base_error_message,
310
- accept_external_id: true,
311
318
  accept_public_id: false)
312
319
  if id[0] == "SYSTEM"
313
320
  # For backward compatibility
@@ -409,7 +416,6 @@ module REXML
409
416
  end
410
417
  name = parse_name(base_error_message)
411
418
  id = parse_id(base_error_message,
412
- accept_external_id: true,
413
419
  accept_public_id: true)
414
420
  @source.skip_spaces
415
421
  unless @source.match?(">", true)
@@ -422,6 +428,8 @@ module REXML
422
428
  else
423
429
  raise REXML::ParseException.new("Malformed node: Started with '<!' but not a comment nor ELEMENT,ENTITY,ATTLIST,NOTATION", @source)
424
430
  end
431
+ elsif @source.match?("<?", true)
432
+ return process_instruction
425
433
  elsif match = @source.match(/(%.*?;)\s*/um, true)
426
434
  return [ :externalentity, match[1] ]
427
435
  elsif @source.match?(/\]\s*>/um, true)
@@ -534,14 +542,20 @@ module REXML
534
542
  end
535
543
  private :pull_event
536
544
 
537
- def entity( reference, entities )
545
+ def entity( reference, entities, expanding: nil )
538
546
  return unless entities
539
547
 
540
548
  value = entities[ reference ]
541
549
  return if value.nil?
542
550
 
551
+ expanding ||= Set.new
552
+ raise REXML::ParseException.new("Detected an entity reference loop: #{reference}", @source) if expanding.include?(reference)
553
+
543
554
  record_entity_expansion
544
- unnormalize( value, entities )
555
+ expanding.add(reference)
556
+ result = unnormalize( value, entities, nil, expanding: expanding )
557
+ expanding.delete(reference)
558
+ result
545
559
  end
546
560
 
547
561
  # Escapes all possible entities
@@ -561,7 +575,7 @@ module REXML
561
575
  end
562
576
 
563
577
  # Unescapes all possible entities
564
- def unnormalize( string, entities=nil, filter=nil )
578
+ def unnormalize( string, entities=nil, filter=nil, expanding: nil )
565
579
  if string.include?("\r")
566
580
  rv = string.gsub( Private::CARRIAGE_RETURN_NEWLINE_PATTERN, "\n" )
567
581
  else
@@ -576,7 +590,7 @@ module REXML
576
590
  else
577
591
  code_point = Integer(m, 10)
578
592
  end
579
- [code_point].pack('U*')
593
+ Text.expand_character_reference(code_point, "&##{m};")
580
594
  }
581
595
  matches.collect!{|x|x[0]}.compact!
582
596
  if filter
@@ -587,7 +601,7 @@ module REXML
587
601
  if matches.size > 0
588
602
  matches.tally.each do |entity_reference, n|
589
603
  entity_expansion_count_before = @entity_expansion_count
590
- entity_value = entity( entity_reference, entities )
604
+ entity_value = entity( entity_reference, entities, expanding: expanding )
591
605
  if entity_value
592
606
  if n > 1
593
607
  entity_expansion_count_delta =
@@ -667,68 +681,41 @@ module REXML
667
681
  end
668
682
 
669
683
  def parse_id(base_error_message,
670
- accept_external_id:,
671
684
  accept_public_id:)
672
- if accept_external_id and (md = @source.match(EXTERNAL_ID_PUBLIC, true))
673
- pubid = system = nil
674
- pubid_literal = md[1]
675
- pubid = pubid_literal[1..-2] if pubid_literal # Remove quote
676
- system_literal = md[2]
677
- system = system_literal[1..-2] if system_literal # Remove quote
678
- ["PUBLIC", pubid, system]
679
- elsif accept_public_id and (md = @source.match(PUBLIC_ID, true))
680
- pubid = system = nil
681
- pubid_literal = md[1]
682
- pubid = pubid_literal[1..-2] if pubid_literal # Remove quote
683
- ["PUBLIC", pubid, nil]
684
- elsif accept_external_id and (md = @source.match(EXTERNAL_ID_SYSTEM, true))
685
- system = nil
686
- system_literal = md[1]
687
- system = system_literal[1..-2] if system_literal # Remove quote
688
- ["SYSTEM", nil, system]
689
- else
690
- details = parse_id_invalid_details(accept_external_id: accept_external_id,
691
- accept_public_id: accept_public_id)
692
- message = "#{base_error_message}: #{details}"
693
- raise REXML::ParseException.new(message, @source)
694
- end
695
- end
696
-
697
- def parse_id_invalid_details(accept_external_id:,
698
- accept_public_id:)
699
- public = /\A\s*PUBLIC/um
700
- system = /\A\s*SYSTEM/um
701
- if (accept_external_id or accept_public_id) and @source.match?(/#{public}/um)
702
- if @source.match?(/#{public}(?:\s+[^'"]|\s*[\[>])/um)
703
- return "public ID literal is missing"
704
- end
705
- unless @source.match?(/#{public}\s+#{PUBIDLITERAL}/um)
706
- return "invalid public ID literal"
707
- end
708
- if accept_public_id
709
- if @source.match?(/#{public}\s+#{PUBIDLITERAL}\s+[^'"]/um)
710
- return "system ID literal is missing"
711
- end
712
- unless @source.match?(/#{public}\s+#{PUBIDLITERAL}\s+#{SYSTEMLITERAL}/um)
713
- return "invalid system literal"
714
- end
715
- "garbage after system literal"
685
+ @source.skip_spaces
686
+ if @source.match?("PUBLIC", true)
687
+ if (md = @source.match(Private::EXTERNAL_ID_PUBLIC_PATTERN, true))
688
+ pubid = system = nil
689
+ pubid_literal = md[1]
690
+ pubid = pubid_literal[1..-2] if pubid_literal # Remove quote
691
+ system_literal = md[2]
692
+ system = system_literal[1..-2] if system_literal # Remove quote
693
+ ["PUBLIC", pubid, system]
694
+ elsif accept_public_id and (md = @source.match(Private::PUBLIC_ID_PATTERN, true))
695
+ pubid = system = nil
696
+ pubid_literal = md[1]
697
+ pubid = pubid_literal[1..-2] if pubid_literal # Remove quote
698
+ ["PUBLIC", pubid, nil]
699
+ elsif @source.match?(/(?:\s+[^'"]|\s*[\[>])/um)
700
+ raise REXML::ParseException.new("#{base_error_message}: public ID literal is missing", @source)
701
+ elsif !@source.match?(Private::PUBLIC_ID_PATTERN)
702
+ raise REXML::ParseException.new("#{base_error_message}: invalid public ID literal", @source)
716
703
  else
717
- "garbage after public ID literal"
704
+ raise REXML::ParseException.new("#{base_error_message}: garbage after public ID literal", @source)
718
705
  end
719
- elsif accept_external_id and @source.match?(/#{system}/um)
720
- if @source.match?(/#{system}(?:\s+[^'"]|\s*[\[>])/um)
721
- return "system literal is missing"
722
- end
723
- unless @source.match?(/#{system}\s+#{SYSTEMLITERAL}/um)
724
- return "invalid system literal"
706
+ elsif @source.match?("SYSTEM", true)
707
+ if (md = @source.match(Private::EXTERNAL_ID_SYSTEM_PATTERN, true))
708
+ system = nil
709
+ system_literal = md[1]
710
+ system = system_literal[1..-2] if system_literal # Remove quote
711
+ ["SYSTEM", nil, system]
712
+ elsif @source.match?(/(?:\s+[^'"]|\s*[\[>])/um)
713
+ raise REXML::ParseException.new("#{base_error_message}: system literal is missing", @source)
714
+ else
715
+ raise REXML::ParseException.new("#{base_error_message}: invalid system literal", @source)
725
716
  end
726
- "garbage after system literal"
727
717
  else
728
- unless @source.match?(/\A\s*(?:PUBLIC|SYSTEM)\s/um)
729
- return "invalid ID type"
730
- end
731
- "ID type is missing"
718
+ raise REXML::ParseException.new("#{base_error_message}: invalid ID type", @source)
732
719
  end
733
720
  end
734
721
 
@@ -12,7 +12,7 @@ module REXML
12
12
  @parser = BaseParser.new(source)
13
13
  @listeners = []
14
14
  @procs = []
15
- @namespace_stack = []
15
+ @namespace_stack = [{}]
16
16
  @has_listeners = false
17
17
  @tag_stack = []
18
18
  @entities = {}
@@ -122,7 +122,7 @@ module REXML
122
122
  event[2].each {|n, v| event[2][n] = @parser.normalize(v)}
123
123
  nsdecl = event[2].find_all { |n, value| n =~ /^xmlns(:|$)/ }
124
124
  nsdecl.collect! { |n, value| [ n[6..-1], value ] }
125
- @namespace_stack.push({})
125
+ @namespace_stack.push(@namespace_stack[-1].dup)
126
126
  nsdecl.each do |n,v|
127
127
  @namespace_stack[-1][n] = v
128
128
  # notify observers of namespaces
@@ -157,7 +157,7 @@ module REXML
157
157
  ob.end_element( uri, local, event[1] )
158
158
  } if listeners
159
159
 
160
- namespace_mapping = @namespace_stack.pop
160
+ namespace_mapping = @namespace_stack.pop if procs or listeners
161
161
  # find the observers for namespaces
162
162
  procs = get_procs( :end_prefix_mapping, event[1] )
163
163
  listeners = get_listeners( :end_prefix_mapping, event[1] )
@@ -226,12 +226,8 @@ module REXML
226
226
  return nil if @procs.size == 0
227
227
  @procs.find_all do |sym, match, block|
228
228
  (
229
- (sym.nil? or symbol == sym) and
230
- ((name.nil? and match.nil?) or match.nil? or (
231
- (name == match) or
232
- (match.kind_of? Regexp and name =~ match)
233
- )
234
- )
229
+ (sym.nil? or (symbol == sym)) and
230
+ (match.nil? or (name == match) or (match.kind_of? Regexp and match.match?(name)))
235
231
  )
236
232
  end.collect{|x| x[-1]}
237
233
  end
@@ -239,12 +235,8 @@ module REXML
239
235
  return nil if @listeners.size == 0
240
236
  @listeners.find_all do |sym, match, block|
241
237
  (
242
- (sym.nil? or symbol == sym) and
243
- ((name.nil? and match.nil?) or match.nil? or (
244
- (name == match) or
245
- (match.kind_of? Regexp and name =~ match)
246
- )
247
- )
238
+ (sym.nil? or (symbol == sym)) and
239
+ (match.nil? or (name == match) or (match.kind_of? Regexp and match.match?(name)))
248
240
  )
249
241
  end.collect{|x| x[-1]}
250
242
  end
@@ -259,11 +251,8 @@ module REXML
259
251
  end
260
252
 
261
253
  def get_namespace( prefix )
262
- return nil if @namespace_stack.empty?
263
-
264
- uris = (@namespace_stack.find_all { |ns| not ns[prefix].nil? }) ||
265
- (@namespace_stack.find { |ns| not ns[nil].nil? })
266
- uris[-1][prefix] unless uris.nil? or 0 == uris.size
254
+ current_namespace = @namespace_stack[-1]
255
+ current_namespace[prefix] || current_namespace[nil]
267
256
  end
268
257
  end
269
258
  end
@@ -9,19 +9,15 @@ module REXML
9
9
  # for this class. Believe me. You don't want to poke around in here.
10
10
  # There is strange, dark magic at work in this code. Beware. Go back! Go
11
11
  # back while you still can!
12
- class XPathParser
12
+ class XPathParser # :nodoc:
13
13
  include XMLTokens
14
- LITERAL = /^'([^']*)'|^"([^"]*)"/u
14
+ LITERAL = /\A'([^']*)'|\A"([^"]*)"/u
15
15
 
16
16
  def namespaces=( namespaces )
17
- Functions::namespace_context = namespaces
18
17
  @namespaces = namespaces
19
18
  end
20
19
 
21
20
  def parse path
22
- path = path.dup
23
- path.gsub!(/([\(\[])\s+/, '\1') # Strip ignorable spaces
24
- path.gsub!( /\s+([\]\)])/, '\1')
25
21
  parsed = []
26
22
  rest = OrExpr(path, parsed)
27
23
  if rest
@@ -106,14 +102,16 @@ module REXML
106
102
  component << ']'
107
103
  when :document
108
104
  components << ""
109
- when :function
110
- component << parsed.shift
111
- component << "( "
112
- component << predicate_to_path(parsed.shift[0]) {|x| abbreviate(x)}
113
- component << " )"
114
- when :literal
115
- component << quote_literal(parsed.shift)
105
+ when :function, :literal, :group, :neg, :and, :or, :mult, :plus, :minus, :neq, :eq, :lt, :gt, :lteq, :gteq, :div, :mod, :union
106
+ component = ''
107
+ components << component
108
+ parsed.unshift(op)
109
+ component << predicate_to_path(parsed) {|x| abbreviate(x)}
116
110
  else
111
+ unless component
112
+ component = ''
113
+ components << component
114
+ end
117
115
  component << "UNKNOWN("
118
116
  component << op.inspect
119
117
  component << ")"
@@ -191,6 +189,12 @@ module REXML
191
189
  op = "!="
192
190
  when :union
193
191
  op = "|"
192
+ when :plus
193
+ op = "+"
194
+ when :minus
195
+ op = "-"
196
+ when :mult
197
+ op = "*"
194
198
  end
195
199
  left = predicate_to_path( parsed.shift, &block )
196
200
  right = predicate_to_path( parsed.shift, &block )
@@ -199,6 +203,10 @@ module REXML
199
203
  path << op.to_s
200
204
  path << " "
201
205
  path << right
206
+ when :neg
207
+ parsed.shift
208
+ path << "-"
209
+ path << predicate_to_path( parsed, &block )
202
210
  when :function
203
211
  parsed.shift
204
212
  name = parsed.shift
@@ -212,6 +220,11 @@ module REXML
212
220
  when :literal
213
221
  parsed.shift
214
222
  path << quote_literal(parsed.shift)
223
+ when :group
224
+ parsed.shift
225
+ path << "("
226
+ path << yield(parsed.shift)
227
+ path << ")"
215
228
  else
216
229
  path << yield( parsed )
217
230
  end
@@ -252,7 +265,7 @@ module REXML
252
265
  path = path[1..-1]
253
266
  end
254
267
  end
255
- RelativeLocationPath( path, parsed ) if path.size > 0
268
+ RelativeLocationPath( path, parsed )
256
269
  end
257
270
 
258
271
  #RelativeLocationPath
@@ -263,7 +276,7 @@ module REXML
263
276
  # | '.' | '..' AbbreviatedStep
264
277
  # | RelativeLocationPath '/' Step
265
278
  # | RelativeLocationPath '//' Step
266
- AXIS = /^(ancestor|ancestor-or-self|attribute|child|descendant|descendant-or-self|following|following-sibling|namespace|parent|preceding|preceding-sibling|self)::/
279
+ AXIS = /\A(ancestor|ancestor-or-self|attribute|child|descendant|descendant-or-self|following|following-sibling|namespace|parent|preceding|preceding-sibling|self)::/
267
280
  def RelativeLocationPath path, parsed
268
281
  loop do
269
282
  original_path = path
@@ -335,11 +348,11 @@ module REXML
335
348
  # | NODE_TYPE '(' ')' NodeType
336
349
  # | PI '(' LITERAL ')' PI
337
350
  # | '[' expr ']' Predicate
338
- PREFIX_WILDCARD = /^\*:(#{NCNAME_STR})/u
339
- LOCAL_NAME_WILDCARD = /^(#{NCNAME_STR}):\*/u
351
+ PREFIX_WILDCARD = /\A\*:(#{NCNAME_STR})/u
352
+ LOCAL_NAME_WILDCARD = /\A(#{NCNAME_STR}):\*/u
340
353
  QNAME = Namespace::NAMESPLIT
341
- NODE_TYPE = /^(comment|text|node)\(\s*\)/m
342
- PI = /^processing-instruction\(/
354
+ NODE_TYPE = /\A(comment|text|node)\(\s*\)/m
355
+ PI = /\Aprocessing-instruction\(/
343
356
  def NodeTest path, parsed
344
357
  original_path = path
345
358
  path = path.lstrip
@@ -351,7 +364,7 @@ module REXML
351
364
  parsed << :qname
352
365
  parsed << prefix
353
366
  parsed << name
354
- when /^\*/
367
+ when /\A\*/
355
368
  path = $'
356
369
  parsed << :any
357
370
  when NODE_TYPE
@@ -359,17 +372,15 @@ module REXML
359
372
  path = $'
360
373
  parsed << type.tr('-', '_').intern
361
374
  when PI
362
- path = $'
375
+ path = $'.lstrip
363
376
  literal = nil
364
- if path =~ /^\s*\)/
365
- path = $'
366
- else
377
+ unless path.start_with?(')')
367
378
  path =~ LITERAL
368
379
  literal = $1
369
- path = $'
380
+ path = $'.lstrip
370
381
  raise ParseException.new("Missing ')' after processing instruction") if path[0] != ?)
371
- path = path[1..-1]
372
382
  end
383
+ path = path[1..-1]
373
384
  parsed << :processing_instruction
374
385
  parsed << (literal || '')
375
386
  when LOCAL_NAME_WILDCARD
@@ -400,6 +411,8 @@ module REXML
400
411
  while path[0] == ?[
401
412
  path, expr = get_group(path)
402
413
  predicates << expr[1..-2] if expr
414
+ stripped_path = path.lstrip
415
+ path = stripped_path if stripped_path.start_with?("[")
403
416
  end
404
417
  predicates.each{ |pred|
405
418
  preds = []
@@ -420,7 +433,7 @@ module REXML
420
433
  n = []
421
434
  rest = AndExpr( path, n )
422
435
  if rest != path
423
- while rest =~ /^\s*( or )/
436
+ while rest =~ /\A\s+(or)\s+/
424
437
  n = [ :or, n, [] ]
425
438
  rest = AndExpr( $', n[-1] )
426
439
  end
@@ -439,7 +452,7 @@ module REXML
439
452
  n = []
440
453
  rest = EqualityExpr( path, n )
441
454
  if rest != path
442
- while rest =~ /^\s*( and )/
455
+ while rest =~ /\A\s+(and)\s+/
443
456
  n = [ :and, n, [] ]
444
457
  rest = EqualityExpr( $', n[-1] )
445
458
  end
@@ -458,7 +471,7 @@ module REXML
458
471
  n = []
459
472
  rest = RelationalExpr( path, n )
460
473
  if rest != path
461
- while rest =~ /^\s*(!?=)\s*/
474
+ while rest =~ /\A\s*(!?=)\s*/
462
475
  if $1[0] == ?!
463
476
  n = [ :neq, n, [] ]
464
477
  else
@@ -481,7 +494,7 @@ module REXML
481
494
  n = []
482
495
  rest = AdditiveExpr( path, n )
483
496
  if rest != path
484
- while rest =~ /^\s*([<>]=?)\s*/
497
+ while rest =~ /\A\s*([<>]=?)\s*/
485
498
  if $1[0] == ?<
486
499
  sym = "lt"
487
500
  else
@@ -506,7 +519,7 @@ module REXML
506
519
  n = []
507
520
  rest = MultiplicativeExpr( path, n )
508
521
  if rest != path
509
- while rest =~ /^\s*(\+|-)\s*/
522
+ while rest =~ /\A\s*(\+|-)\s*/
510
523
  if $1[0] == ?+
511
524
  n = [ :plus, n, [] ]
512
525
  else
@@ -529,7 +542,7 @@ module REXML
529
542
  n = []
530
543
  rest = UnaryExpr( path, n )
531
544
  if rest != path
532
- while rest =~ /^\s*(\*| div | mod )\s*/
545
+ while rest =~ /\A\s*(\*|\s(?:div|mod)\s)\s*/
533
546
  if $1[0] == ?*
534
547
  n = [ :mult, n, [] ]
535
548
  elsif $1.include?( "div" )
@@ -551,19 +564,18 @@ module REXML
551
564
  #| '-' UnaryExpr
552
565
  #| UnionExpr
553
566
  def UnaryExpr path, parsed
554
- path =~ /^(\-*)/
555
- path = $'
556
- if $1 and (($1.size % 2) != 0)
557
- mult = -1
558
- else
559
- mult = 1
560
- end
561
- parsed << :neg if mult < 0
567
+ original_path = path
568
+ path = path.lstrip
569
+ match_data = /\A((?:-\s*)*)/.match(path)
570
+ negative = match_data[1].count("-").odd?
571
+ path = match_data.post_match
562
572
 
563
573
  n = []
564
- path = UnionExpr( path, n )
574
+ rest = UnionExpr( path, n )
575
+ return original_path if rest == path
576
+ parsed << :neg if negative
565
577
  parsed.concat( n )
566
- path
578
+ rest
567
579
  end
568
580
 
569
581
  #| UnionExpr '|' PathExpr
@@ -572,7 +584,7 @@ module REXML
572
584
  n = []
573
585
  rest = PathExpr( path, n )
574
586
  if rest != path
575
- while rest =~ /^\s*(\|)\s*/
587
+ while rest =~ /\A\s*(\|)\s*/
576
588
  n = [ :union, n, [] ]
577
589
  rest = PathExpr( $', n[-1] )
578
590
  end
@@ -588,17 +600,26 @@ module REXML
588
600
  #| LocationPath
589
601
  #| FilterExpr ('/' | '//') RelativeLocationPath
590
602
  def PathExpr path, parsed
603
+ original_path = path
591
604
  path = path.lstrip
592
605
  n = []
593
606
  rest = FilterExpr( path, n )
594
- if rest != path
595
- if rest and rest[0] == ?/
607
+ if rest == path
608
+ rest = LocationPath(rest, n)
609
+ return original_path if rest == path
610
+ else
611
+ if /\A\s*\//.match?(rest)
612
+ rest = rest.lstrip
613
+ if rest.start_with?('//')
614
+ n << :descendant_or_self
615
+ n << :node
616
+ rest = rest[2..-1]
617
+ else # starts with '/'
618
+ rest = rest[1..-1]
619
+ end
596
620
  rest = RelativeLocationPath(rest, n)
597
- parsed.concat(n)
598
- return rest
599
621
  end
600
622
  end
601
- rest = LocationPath(rest, n) if rest =~ /\A[\/\.\@\[\w*]/
602
623
  parsed.concat(n)
603
624
  rest
604
625
  end
@@ -620,8 +641,8 @@ module REXML
620
641
  #| LITERAL
621
642
  #| NUMBER
622
643
  #| FunctionCall
623
- VARIABLE_REFERENCE = /^\$(#{NAME_STR})/u
624
- NUMBER = /^(\d*\.?\d+)/
644
+ VARIABLE_REFERENCE = /\A\$(#{NAME_STR})/u
645
+ NUMBER = /\A(\d*\.?\d+)/
625
646
  NT = /^comment|text|processing-instruction|node$/
626
647
  def PrimaryExpr path, parsed
627
648
  case path
@@ -631,7 +652,7 @@ module REXML
631
652
  parsed << :variable
632
653
  parsed << varname
633
654
  #arry << @variables[ varname ]
634
- when /^(\w[-\w]*)(?:\()/
655
+ when /\A(\w[-\w]*)(?:\()/
635
656
  fname = $1
636
657
  tmp = $'
637
658
  return path if fname =~ NT
@@ -649,12 +670,12 @@ module REXML
649
670
  path = $'
650
671
  parsed << :literal
651
672
  parsed << varname
652
- when /^\(/ #/
673
+ when /\A\(/ #/
653
674
  path, contents = get_group(path)
654
675
  contents = contents[1..-2]
655
676
  n = []
656
677
  OrExpr( contents, n )
657
- parsed.concat(n)
678
+ parsed.push(:group, n)
658
679
  end
659
680
  path
660
681
  end
@@ -678,12 +699,20 @@ module REXML
678
699
  depth = 0
679
700
  st = string[0,1]
680
701
  en = (st == "(" ? ")" : "]")
702
+ quote = nil
681
703
  begin
682
- case string[ind,1]
683
- when st
684
- depth += 1
685
- when en
686
- depth -= 1
704
+ if quote
705
+ # ignore () [] inside quotes
706
+ quote = nil if string[ind] == quote
707
+ else
708
+ case string[ind]
709
+ when st
710
+ depth += 1
711
+ when en
712
+ depth -= 1
713
+ when '"', "'"
714
+ quote = string[ind]
715
+ end
687
716
  end
688
717
  ind += 1
689
718
  end while depth > 0 and ind < string.length
data/lib/rexml/rexml.rb CHANGED
@@ -18,7 +18,7 @@
18
18
  #
19
19
  # If you're somewhat familiar with XML
20
20
  # and have a particular task in mind,
21
- # you may want to see {the tasks pages}[doc/rexml/tasks/tocs/master_toc_rdoc.html].
21
+ # you may want to see {the tasks pages}[doc/rexml/tasks/rdoc/toc_rdoc.html].
22
22
  #
23
23
  # == API
24
24
  #
@@ -31,7 +31,7 @@
31
31
  module REXML
32
32
  COPYRIGHT = "Copyright © 2001-2008 Sean Russell <ser@germane-software.com>"
33
33
  DATE = "2008/019"
34
- VERSION = "3.4.4"
34
+ VERSION = "3.4.5"
35
35
  REVISION = ""
36
36
 
37
37
  Copyright = COPYRIGHT