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
data/lib/rexml/element.rb CHANGED
@@ -1,4 +1,4 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
2
  require_relative "parent"
3
3
  require_relative "namespace"
4
4
  require_relative "attribute"
@@ -24,9 +24,9 @@ module REXML
24
24
  # If you're somewhat familiar with XML
25
25
  # and have a particular task in mind,
26
26
  # you may want to see the
27
- # {tasks pages}[../doc/rexml/tasks/tocs/master_toc_rdoc.html],
27
+ # {tasks pages}[../doc/rexml/tasks/rdoc/toc_rdoc.html],
28
28
  # and in particular, the
29
- # {tasks page for elements}[../doc/rexml/tasks/tocs/element_toc_rdoc.html].
29
+ # {tasks for elements}[../doc/rexml/tasks/rdoc/toc_rdoc.html#label-Element].
30
30
  #
31
31
  # === Name
32
32
  #
@@ -356,7 +356,7 @@ module REXML
356
356
  # e.inspect # => "<foo bar='0' baz='1'> ... </>"
357
357
  #
358
358
  def inspect
359
- rv = "<#@expanded_name"
359
+ rv = +"<#@expanded_name"
360
360
 
361
361
  @attributes.each_attribute do |attr|
362
362
  rv << " "
@@ -588,12 +588,7 @@ module REXML
588
588
  # d.elements['//c'].namespaces # => {"x"=>"1", "y"=>"2", "z"=>"3"}
589
589
  #
590
590
  def namespaces
591
- namespaces_cache = document&.__send__(:namespaces_cache)
592
- if namespaces_cache
593
- namespaces_cache[self] ||= calculate_namespaces
594
- else
595
- calculate_namespaces
596
- end
591
+ _calculate_namespaces
597
592
  end
598
593
 
599
594
  # :call-seq:
@@ -618,13 +613,10 @@ module REXML
618
613
  #
619
614
  def namespace(prefix=nil)
620
615
  if prefix.nil?
621
- prefix = prefix()
616
+ _namespace_internal
617
+ else
618
+ _namespace_lookup_internal(prefix)
622
619
  end
623
- prefix = (prefix == '') ? 'xmlns' : prefix.delete_prefix("xmlns:")
624
- ns = namespaces[prefix]
625
-
626
- ns = '' if ns.nil? and prefix == 'xmlns'
627
- ns
628
620
  end
629
621
 
630
622
  # :call-seq:
@@ -1276,21 +1268,41 @@ module REXML
1276
1268
  # document.root.attribute("x") # => x='x'
1277
1269
  # document.root.attribute("x", "a") # => a:x='a:x'
1278
1270
  #
1271
+ # This method matches +namespace+ as the XML Namespaces specification says
1272
+ # first, then falls back to a looser match kept for compatibility: an
1273
+ # unprefixed attribute is taken to be in the default namespace, and a
1274
+ # +namespace+ that no prefix is bound to falls back to the unprefixed
1275
+ # attribute:
1276
+ #
1277
+ # xml_string = "<root xmlns='ns0' a='a'/>"
1278
+ # document = REXML::Document.new(xml_string)
1279
+ # document.root.attribute("a", "ns0") # => a='a'
1280
+ # document.root.attribute("a", "nosuch") # => a='a'
1281
+ #
1282
+ # The XML Namespaces specification says that an unprefixed attribute has
1283
+ # no namespace, so neither of those should match; the fallback answers
1284
+ # them. The fallback only answers when nothing matches strictly, so an
1285
+ # attribute that does is returned instead of the unprefixed one:
1286
+ #
1287
+ # xml_string = "<root xmlns='ns0' xmlns:p='ns0' a='A' p:a='PA'/>"
1288
+ # document = REXML::Document.new(xml_string)
1289
+ # document.root.attribute("a", "ns0") # => p:a='PA'
1290
+ #
1291
+ # REXML::Attributes#get_attribute_ns matches strictly and only strictly,
1292
+ # returning +nil+ where the fallback would answer.
1293
+ #
1279
1294
  def attribute( name, namespace=nil )
1280
- prefix = namespaces.key(namespace) if namespace
1281
- prefix = nil if prefix == 'xmlns'
1282
-
1283
- ret_val =
1284
- attributes.get_attribute( prefix ? "#{prefix}:#{name}" : name )
1295
+ return attributes.get_attribute( name ) if namespace.nil?
1285
1296
 
1297
+ ret_val = attributes.get_attribute_ns( namespace, name )
1286
1298
  return ret_val unless ret_val.nil?
1287
- return nil if prefix.nil?
1288
1299
 
1289
- # now check that prefix'es namespace is not the same as the
1290
- # default namespace
1291
- return nil unless ( namespaces[ prefix ] == namespaces[ 'xmlns' ] )
1292
-
1293
- attributes.get_attribute( name )
1300
+ # Kept for compatibility: an unprefixed attribute also matches when the
1301
+ # requested URI is the default namespace or is not declared on this
1302
+ # element, even though such an attribute is not in that namespace.
1303
+ if namespace == namespaces[ 'xmlns' ] or !namespaces.has_value?( namespace )
1304
+ attributes.get_attribute( name )
1305
+ end
1294
1306
  end
1295
1307
 
1296
1308
  # :call-seq:
@@ -1507,15 +1519,32 @@ module REXML
1507
1519
  formatter.write( self, output )
1508
1520
  end
1509
1521
 
1510
- private
1511
- def calculate_namespaces
1512
- if parent
1513
- parent.namespaces.merge(attributes.namespaces)
1514
- else
1515
- attributes.namespaces
1516
- end
1522
+ # Returns namespace of the element
1523
+ def _namespace_internal(namespaces = _calculate_namespaces) # :nodoc:
1524
+ _namespace_lookup_internal(prefix, namespaces)
1525
+ end
1526
+
1527
+ # Lookup namespace for the given prefix in the context of the element
1528
+ def _namespace_lookup_internal(prefix, namespaces = _calculate_namespaces) # :nodoc:
1529
+ prefix = (prefix == '') ? 'xmlns' : prefix.delete_prefix("xmlns:")
1530
+ ns = Namespace.resolve_prefix(prefix, namespaces)
1531
+ ns = +'' if ns.nil? and prefix == 'xmlns'
1532
+ ns
1533
+ end
1534
+
1535
+ def _calculate_namespaces(cache_hash = nil, attlist_mappings = nil) # :nodoc:
1536
+ return cache_hash[self] if cache_hash && cache_hash.key?(self)
1537
+
1538
+ attlist_mappings ||= document&.doctype&._attlist_mappings || {}
1539
+ inherited_namespaces = parent ? parent._calculate_namespaces(cache_hash, attlist_mappings) : {}
1540
+ attr_namespaces = attributes._calculate_namespaces(attlist_mappings)
1541
+ namespaces = attr_namespaces.any? ? inherited_namespaces.merge(attr_namespaces) : inherited_namespaces
1542
+ cache_hash[self] = namespaces if cache_hash
1543
+ namespaces
1517
1544
  end
1518
1545
 
1546
+ private
1547
+
1519
1548
  def __to_xpath_helper node
1520
1549
  rv = node.expanded_name.clone
1521
1550
  if node.parent
@@ -2240,15 +2269,10 @@ module REXML
2240
2269
  # [REXML::Attribute, bar:att='2']
2241
2270
  # [REXML::Attribute, att='&lt;']
2242
2271
  #
2243
- def each_attribute # :yields: attribute
2244
- return to_enum(__method__) unless block_given?
2245
- each_value do |val|
2246
- if val.kind_of? Attribute
2247
- yield val
2248
- else
2249
- val.each_value { |atr| yield atr }
2250
- end
2251
- end
2272
+ # This method doesn't iterate attributes in the DTD. This may be a bug.
2273
+ #
2274
+ def each_attribute(&block) # :yields: attribute
2275
+ each_own_attribute(&block)
2252
2276
  end
2253
2277
 
2254
2278
  # :call-seq:
@@ -2273,6 +2297,8 @@ module REXML
2273
2297
  # ["bar:att", "2"]
2274
2298
  # ["att", "<"]
2275
2299
  #
2300
+ # This method doesn't iterate attributes in the DTD. This may be a bug.
2301
+ #
2276
2302
  def each
2277
2303
  return to_enum(__method__) unless block_given?
2278
2304
  each_attribute do |attr|
@@ -2300,36 +2326,7 @@ module REXML
2300
2326
  # attrs.get_attribute('nosuch') # => nil
2301
2327
  #
2302
2328
  def get_attribute( name )
2303
- attr = fetch( name, nil )
2304
- if attr.nil?
2305
- return nil if name.nil?
2306
- # Look for prefix
2307
- name =~ Namespace::NAMESPLIT
2308
- prefix, n = $1, $2
2309
- if prefix
2310
- attr = fetch( n, nil )
2311
- # check prefix
2312
- if attr == nil
2313
- elsif attr.kind_of? Attribute
2314
- return attr if prefix == attr.prefix
2315
- else
2316
- attr = attr[ prefix ]
2317
- return attr
2318
- end
2319
- end
2320
- doctype = @element.document&.doctype
2321
- if doctype
2322
- expn = @element.expanded_name
2323
- expn = doctype.name if expn.size == 0
2324
- attr_val = doctype.attribute_of(expn, name)
2325
- return Attribute.new( name, attr_val ) if attr_val
2326
- end
2327
- return nil
2328
- end
2329
- if attr.kind_of? Hash
2330
- attr = attr[ @element.prefix ]
2331
- end
2332
- attr
2329
+ fetch(name, nil) || attlist_attributes[name]
2333
2330
  end
2334
2331
 
2335
2332
  # :call-seq:
@@ -2357,12 +2354,15 @@ module REXML
2357
2354
  #
2358
2355
  def []=( name, value )
2359
2356
  if value.nil? # Delete the named attribute
2360
- attr = get_attribute(name)
2361
- delete attr
2357
+ delete name
2362
2358
  return
2363
2359
  end
2364
2360
 
2365
- unless value.kind_of? Attribute
2361
+ if value.kind_of? Attribute
2362
+ # Use attribute's expanded name to avoid inconsistency.
2363
+ # TODO: Explicitly document that inconsistent names are allowed, or raise/warn about inconsistency.
2364
+ name = value.expanded_name
2365
+ else
2366
2366
  doctype = @element.document&.doctype
2367
2367
  if doctype
2368
2368
  value = Text::normalize( value, doctype )
@@ -2372,17 +2372,7 @@ module REXML
2372
2372
  value = Attribute.new(name, value)
2373
2373
  end
2374
2374
  value.element = @element
2375
- old_attr = fetch(value.name, nil)
2376
- if old_attr.nil?
2377
- store(value.name, value)
2378
- elsif old_attr.kind_of? Hash
2379
- old_attr[value.prefix] = value
2380
- elsif old_attr.prefix != value.prefix
2381
- store value.name, {old_attr.prefix => old_attr,
2382
- value.prefix => value}
2383
- else
2384
- store value.name, value
2385
- end
2375
+ store name, value
2386
2376
  @element
2387
2377
  end
2388
2378
 
@@ -2398,20 +2388,7 @@ module REXML
2398
2388
  # d.root.attributes.prefixes # => ["x", "y"]
2399
2389
  #
2400
2390
  def prefixes
2401
- ns = []
2402
- each_attribute do |attribute|
2403
- ns << attribute.name if attribute.prefix == 'xmlns'
2404
- end
2405
- doctype = @element.document&.doctype
2406
- if doctype
2407
- expn = @element.expanded_name
2408
- expn = doctype.name if expn.size == 0
2409
- doctype.attributes_of(expn).each {
2410
- |attribute|
2411
- ns << attribute.name if attribute.prefix == 'xmlns'
2412
- }
2413
- end
2414
- ns
2391
+ namespaces.keys - ['xmlns']
2415
2392
  end
2416
2393
 
2417
2394
  # :call-seq:
@@ -2424,20 +2401,7 @@ module REXML
2424
2401
  # d.root.attributes.namespaces # => {"xmlns"=>"foo", "x"=>"bar", "y"=>"twee"}
2425
2402
  #
2426
2403
  def namespaces
2427
- namespaces = {}
2428
- each_attribute do |attribute|
2429
- namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns'
2430
- end
2431
- doctype = @element.document&.doctype
2432
- if doctype
2433
- expn = @element.expanded_name
2434
- expn = doctype.name if expn.size == 0
2435
- doctype.attributes_of(expn).each {
2436
- |attribute|
2437
- namespaces[attribute.name] = attribute.value if attribute.prefix == 'xmlns' or attribute.name == 'xmlns'
2438
- }
2439
- end
2440
- namespaces
2404
+ _calculate_namespaces
2441
2405
  end
2442
2406
 
2443
2407
  # :call-seq:
@@ -2468,28 +2432,11 @@ module REXML
2468
2432
  # attrs.delete(attr) # => <ele att='&lt;'/> # => <ele att='&lt;'/>
2469
2433
  # attrs.delete(attr) # => <ele att='&lt;'/> # => <ele/>
2470
2434
  #
2471
- def delete( attribute )
2472
- name = nil
2473
- prefix = nil
2474
- if attribute.kind_of? Attribute
2475
- name = attribute.name
2476
- prefix = attribute.prefix
2477
- else
2478
- attribute =~ Namespace::NAMESPLIT
2479
- prefix, name = $1, $2
2480
- prefix = '' unless prefix
2481
- end
2482
- old = fetch(name, nil)
2483
- if old.kind_of? Hash # the supplied attribute is one of many
2484
- old.delete(prefix)
2485
- if old.size == 1
2486
- repl = nil
2487
- old.each_value{|v| repl = v}
2488
- store name, repl
2489
- end
2490
- elsif old # the supplied attribute is a top-level one
2491
- super(name)
2492
- end
2435
+ def delete(attribute_or_name)
2436
+ key = attribute_or_name
2437
+ key = attribute_or_name.expanded_name if attribute_or_name.kind_of? Attribute
2438
+ super(key)
2439
+
2493
2440
  @element
2494
2441
  end
2495
2442
 
@@ -2508,13 +2455,13 @@ module REXML
2508
2455
  # d = REXML::Document.new(xml_string)
2509
2456
  # ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='&lt;'/>
2510
2457
  # attrs = ele.attributes
2511
- # attrs # => {"att"=>{"foo"=>foo:att='1', "bar"=>bar:att='2', ""=>att='&lt;'}}
2458
+ # attrs # => {"foo:att" => foo:att='1', "bar:att" => bar:att='2', "att" => att='&lt;'}
2512
2459
  # attrs.add(REXML::Attribute.new('foo:att', '2')) # => foo:att='2'
2513
2460
  # attrs.add(REXML::Attribute.new('baz', '3')) # => baz='3'
2514
2461
  # attrs.include?('baz') # => true
2515
2462
  #
2516
2463
  def add( attribute )
2517
- self[attribute.name] = attribute
2464
+ self[attribute.expanded_name] = attribute
2518
2465
  end
2519
2466
 
2520
2467
  alias :<< :add
@@ -2527,21 +2474,26 @@ module REXML
2527
2474
  #
2528
2475
  # xml_string = <<-EOT
2529
2476
  # <root xmlns:foo="http://foo" xmlns:bar="http://bar">
2530
- # <ele foo:att='1' bar:att='2' att='&lt;'/>
2477
+ # <ele foo:att='1' att='&lt;' foo:other='2' other='3'/>
2531
2478
  # </root>
2532
2479
  # EOT
2533
2480
  # d = REXML::Document.new(xml_string)
2534
- # ele = d.root.elements['//ele'] # => <a foo:att='1' bar:att='2' att='&lt;'/>
2481
+ # ele = d.root.elements['//ele'] # => <a foo:att='1' att='&lt;' foo:other='2' other='3'/>
2535
2482
  # attrs = ele.attributes
2536
- # attrs.delete_all('att') # => [att='&lt;']
2483
+ # attrs.delete_all('att') # => [foo:att='1', att='&lt;']
2484
+ # attrs.each_attribute.map(&:expanded_name) #=> ['foo:other', 'other']
2537
2485
  #
2538
2486
  def delete_all( name )
2539
- rv = []
2540
- each_attribute { |attribute|
2541
- rv << attribute if attribute.expanded_name == name
2542
- }
2543
- rv.each{ |attr| attr.remove }
2544
- rv
2487
+ attributes = each_attribute.select do |attribute|
2488
+ # For <element xmlns:foo="url" ns:foo="value"/>
2489
+ # delete_all('foo') should not delete xmlns:foo="url"
2490
+ # because it is a namespace declaration, not a normal attribute.
2491
+ (!attribute.namespace_declaration? && attribute.name == name) || attribute.expanded_name == name
2492
+ end
2493
+ attributes.each do |attribute|
2494
+ delete attribute.expanded_name
2495
+ end
2496
+ attributes
2545
2497
  end
2546
2498
 
2547
2499
  # :call-seq:
@@ -2561,18 +2513,72 @@ module REXML
2561
2513
  # attrs.get_attribute_ns('http://foo', 'att') # => foo:att='1'
2562
2514
  # attrs.get_attribute_ns('http://foo', 'nosuch') # => nil
2563
2515
  #
2516
+ # +namespace+ is matched as the XML Namespaces specification defines it,
2517
+ # where an unprefixed attribute has no namespace of its own and does not
2518
+ # take the default one:
2519
+ #
2520
+ # xml_string = "<root xmlns='ns0' a='a'/>"
2521
+ # attrs = REXML::Document.new(xml_string).root.attributes
2522
+ # attrs.get_attribute_ns('ns0', 'a') # => nil
2523
+ # attrs.get_attribute_ns('', 'a') # => a='a'
2524
+ #
2525
+ # REXML::Element#attribute matches more loosely and returns the attribute
2526
+ # for both of those.
2527
+ #
2564
2528
  def get_attribute_ns(namespace, name)
2565
- result = nil
2566
- each_attribute() { |attribute|
2567
- if name == attribute.name &&
2568
- namespace == attribute.namespace() &&
2569
- ( !namespace.empty? || !attribute.fully_expanded_name.index(':') )
2570
- # foo will match xmlns:foo, but only if foo isn't also an attribute
2571
- result = attribute if !result or !namespace.empty? or
2572
- !attribute.fully_expanded_name.index(':')
2529
+ each_effective_attribute.find do |attribute|
2530
+ if attribute.namespace_declaration?
2531
+ # namespace declarations are not considered as attributes in this method.
2532
+ # For example: <elem1 xmlns="" xmlns:foo="url" /><elem2 xmlns="bar" xmlns:foo="url" />
2533
+ # Both elem1.get_attribute_ns('', 'foo') and elem2.get_attribute_ns('bar', 'foo')
2534
+ # should not match.
2535
+ false
2536
+ elsif namespace.empty? && !attribute.prefix.empty?
2537
+ # If prefix is present, namespace url shouldn't be empty, so it never matches.
2538
+ # For example, <elem foo:att="value" />, even if attribute.namespace returns '',
2539
+ # it just means that namespace lookup failed, it shouldn't match.
2540
+ false
2541
+ else
2542
+ name == attribute.name && namespace == attribute.namespace()
2573
2543
  end
2574
- }
2575
- result
2544
+ end
2545
+ end
2546
+
2547
+ def _calculate_namespaces(attlist_mappings = nil) # :nodoc:
2548
+ namespaces = {}
2549
+ each_effective_attribute(attlist_mappings) do |attribute|
2550
+ namespaces[attribute.name] = attribute.value if attribute.namespace_declaration?
2551
+ end
2552
+ namespaces
2553
+ end
2554
+
2555
+ private
2556
+
2557
+ # Iterates over the attributes of the element and those in the DTD.
2558
+ # If the element has an attribute with the same name as one in the DTD,
2559
+ # the element's attribute takes precedence.
2560
+ def each_effective_attribute(attlist_mappings = nil)
2561
+ return to_enum(__method__, attlist_mappings) unless block_given?
2562
+ attlist = attlist_attributes(attlist_mappings)
2563
+ attributes = attlist.empty? ? self : attlist.merge(self)
2564
+ attributes.each_value do |attribute|
2565
+ yield attribute
2566
+ end
2567
+ end
2568
+
2569
+ alias each_own_attribute each_value
2570
+ private :each_own_attribute
2571
+
2572
+ def attlist_attributes(attlist_mappings = nil)
2573
+ attlist_mappings ||= @element.document&.doctype&._attlist_mappings
2574
+ return {} unless attlist_mappings
2575
+
2576
+ expn = @element.is_a?(Document) ? @element.doctype&.name : @element.expanded_name
2577
+ attlist_mappings[expn]&.map do |key, value|
2578
+ attr = Attribute.new(key, value)
2579
+ attr.element = @element
2580
+ [key, attr]
2581
+ end.to_h
2576
2582
  end
2577
2583
  end
2578
2584
  end
data/lib/rexml/entity.rb CHANGED
@@ -1,5 +1,7 @@
1
- # frozen_string_literal: false
1
+ # frozen_string_literal: true
2
+ require 'set'
2
3
  require_relative 'child'
4
+ require_relative 'parseexception'
3
5
  require_relative 'source'
4
6
  require_relative 'xmltokens'
5
7
 
@@ -70,13 +72,20 @@ module REXML
70
72
 
71
73
  # Evaluates to the unnormalized value of this entity; that is, replacing
72
74
  # &ent; entities.
73
- def unnormalized
75
+ def unnormalized(expanding: nil)
74
76
  document&.record_entity_expansion
75
77
 
76
78
  return nil if @value.nil?
77
79
 
78
- @unnormalized = Text::unnormalize(@value, parent,
79
- entity_expansion_text_limit: document&.entity_expansion_text_limit)
80
+ expanding ||= Set.new
81
+ raise REXML::ParseException.new("Detected an entity reference loop: #{@name}") if expanding.include?(@name)
82
+
83
+ expanding.add(@name)
84
+ result = Text::unnormalize(@value, parent,
85
+ entity_expansion_text_limit: document&.entity_expansion_text_limit,
86
+ expanding: expanding)
87
+ expanding.delete(@name)
88
+ result
80
89
  end
81
90
 
82
91
  #once :unnormalized
@@ -118,7 +127,7 @@ module REXML
118
127
 
119
128
  # Returns this entity as a string. See write().
120
129
  def to_s
121
- rv = ''
130
+ rv = +''
122
131
  write rv
123
132
  rv
124
133
  end
@@ -129,14 +138,14 @@ module REXML
129
138
  # CAUTION: these entities does not have parent and document
130
139
  module EntityConst
131
140
  # +>+
132
- GT = Entity.new( 'gt', '>' )
141
+ GT = Entity.new( 'gt', '>' ).freeze
133
142
  # +<+
134
- LT = Entity.new( 'lt', '<' )
143
+ LT = Entity.new( 'lt', '<' ).freeze
135
144
  # +&+
136
- AMP = Entity.new( 'amp', '&' )
145
+ AMP = Entity.new( 'amp', '&' ).freeze
137
146
  # +"+
138
- QUOT = Entity.new( 'quot', '"' )
147
+ QUOT = Entity.new( 'quot', '"' ).freeze
139
148
  # +'+
140
- APOS = Entity.new( 'apos', "'" )
149
+ APOS = Entity.new( 'apos', "'" ).freeze
141
150
  end
142
151
  end
@@ -62,27 +62,32 @@ module REXML
62
62
  node.children.each { |child| write( child, output ) }
63
63
  end
64
64
 
65
+ # Descendants are walked with an explicit stack instead of recursion so
66
+ # that a deeply nested document doesn't exhaust the machine stack. A
67
+ # String on the stack is an end tag waiting to be written.
65
68
  def write_element( node, output )
66
- output << "<#{node.expanded_name}"
67
-
68
- node.attributes.to_a.map { |a|
69
- Hash === a ? a.values : a
70
- }.flatten.sort_by {|attr| attr.name}.each do |attr|
71
- output << " "
72
- attr.write( output )
73
- end unless node.attributes.empty?
74
-
75
- if node.children.empty?
76
- output << " " if @ie_hack
77
- output << "/"
78
- else
79
- output << ">"
80
- node.children.each { |child|
81
- write( child, output )
82
- }
83
- output << "</#{node.expanded_name}"
69
+ stack = [node]
70
+ until stack.empty?
71
+ current = stack.pop
72
+ case current
73
+ when String
74
+ output << current
75
+ when Element
76
+ output << "<#{current.expanded_name}"
77
+ write_element_attributes( current, output )
78
+ children = current.children
79
+ if children.empty?
80
+ output << " " if @ie_hack
81
+ output << "/>"
82
+ else
83
+ output << ">"
84
+ stack << "</#{current.expanded_name}>"
85
+ stack.concat(children.reverse)
86
+ end
87
+ else
88
+ write( current, output )
89
+ end
84
90
  end
85
- output << ">"
86
91
  end
87
92
 
88
93
  def write_text( node, output )
@@ -111,6 +116,17 @@ module REXML
111
116
  end
112
117
  output << Instruction::STOP
113
118
  end
119
+
120
+ private
121
+ def write_element_attributes( node, output )
122
+ return if node.attributes.empty?
123
+ node.attributes.to_a.map { |a|
124
+ Hash === a ? a.values : a
125
+ }.flatten.sort_by {|attr| attr.name}.each do |attr|
126
+ output << " "
127
+ attr.write( output )
128
+ end
129
+ end
114
130
  end
115
131
  end
116
132
  end
@@ -86,9 +86,8 @@ module REXML
86
86
  end
87
87
 
88
88
  def write_text( node, output )
89
- s = node.to_s()
90
- s.gsub!(/\s/,' ')
91
- s.squeeze!(" ")
89
+ # Not gsub!/squeeze!: Text#to_s returns the node's own string.
90
+ s = node.to_s().gsub(/\s/, ' ').squeeze(" ")
92
91
  s = wrap(s, @width - @level)
93
92
  s = indent_text(s, @level, " ", true)
94
93
  output << (' '*@level + s)