summaryrefslogtreecommitdiff
path: root/lib/rexml/text.rb
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-02-22 09:37:09 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-02-22 09:37:09 +0000
commite559b24b526bd4d2559283ed2640681595b5ff3d (patch)
treebc159ccce6d776fa04a3ecbfa7c115872e327bc1 /lib/rexml/text.rb
parent857a2ceef5960dc5c526172690eab7a40a532e2c (diff)
merge revision(s) 39384:
* lib/rexml/document.rb (REXML::Document.entity_expansion_text_limit): new attribute to read/write entity expansion text limit. the default limit is 10Kb. * lib/rexml/text.rb (REXML::Text.unnormalize): check above attribute. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@39385 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rexml/text.rb')
-rw-r--r--lib/rexml/text.rb40
1 files changed, 25 insertions, 15 deletions
diff --git a/lib/rexml/text.rb b/lib/rexml/text.rb
index 6623c0c03b..878d13b8e8 100644
--- a/lib/rexml/text.rb
+++ b/lib/rexml/text.rb
@@ -380,25 +380,35 @@ module REXML
# Unescapes all possible entities
def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil )
+ sum = 0
string.gsub( /\r\n?/, "\n" ).gsub( REFERENCE ) {
- ref = $&
- if ref[1] == ?#
- if ref[2] == ?x
- [ref[3...-1].to_i(16)].pack('U*')
- else
- [ref[2...-1].to_i].pack('U*')
- end
- elsif ref == '&amp;'
- '&'
- elsif filter and filter.include?( ref[1...-1] )
- ref
- elsif doctype
- doctype.entity( ref[1...-1] ) or ref
+ s = Text.expand($&, doctype, filter)
+ if sum + s.bytesize > Document.entity_expansion_text_limit
+ raise "entity expansion has grown too large"
else
- entity_value = DocType::DEFAULT_ENTITIES[ ref[1...-1] ]
- entity_value ? entity_value.value : ref
+ sum += s.bytesize
end
+ s
}
end
+
+ def Text.expand(ref, doctype, filter)
+ if ref[1] == ?#
+ if ref[2] == ?x
+ [ref[3...-1].to_i(16)].pack('U*')
+ else
+ [ref[2...-1].to_i].pack('U*')
+ end
+ elsif ref == '&amp;'
+ '&'
+ elsif filter and filter.include?( ref[1...-1] )
+ ref
+ elsif doctype
+ doctype.entity( ref[1...-1] ) or ref
+ else
+ entity_value = DocType::DEFAULT_ENTITIES[ ref[1...-1] ]
+ entity_value ? entity_value.value : ref
+ end
+ end
end
end