diff options
author | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-01-31 04:03:14 +0000 |
---|---|---|
committer | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-01-31 04:03:14 +0000 |
commit | 6425b4ba561cc5e3efe7f7c56896ea3edb1427ea (patch) | |
tree | b016e1542a1d37d67dd9bd88ca8ac6d346569227 /lib/rexml | |
parent | 1b1b5c2bc36a009b143c9fb67fc418aa47e616eb (diff) | |
download | ruby-6425b4ba561cc5e3efe7f7c56896ea3edb1427ea.tar.gz |
* lib/rexml/document.rb (REXML::Document#add): fix duplicate XMLDecls
and bad DocTypes in REXML::Document. (Bug #19058) [ruby-core:27979]
based on the patch by Federico Builes.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26513 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rexml')
-rw-r--r-- | lib/rexml/document.rb | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/lib/rexml/document.rb b/lib/rexml/document.rb index 0fde6df1a1..0337553a2e 100644 --- a/lib/rexml/document.rb +++ b/lib/rexml/document.rb @@ -66,25 +66,27 @@ module REXML # of the document def add( child ) if child.kind_of? XMLDecl - @children.unshift child + if @children[0].kind_of? XMLDecl + @children[0] = child + else + @children.unshift child + end child.parent = self elsif child.kind_of? DocType # Find first Element or DocType node and insert the decl right # before it. If there is no such node, just insert the child at the # end. If there is a child and it is an DocType, then replace it. - insert_before_index = 0 - @children.find { |x| - insert_before_index += 1 + insert_before_index = @children.find_index { |x| x.kind_of?(Element) || x.kind_of?(DocType) } - if @children[ insert_before_index ] # Not null = not end of list - if @children[ insert_before_index ].kind_of DocType + if insert_before_index # Not null = not end of list + if @children[ insert_before_index ].kind_of? DocType @children[ insert_before_index ] = child else - @children[ index_before_index-1, 0 ] = child + @children[ insert_before_index-1, 0 ] = child end else # Insert at end of list - @children[insert_before_index] = child + @children << child end child.parent = self else |