diff options
author | kou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-10-28 06:43:38 +0000 |
---|---|---|
committer | kou <kou@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-10-28 06:43:38 +0000 |
commit | af83fcfe4de5d0497e78789efdfb7e7daf251805 (patch) | |
tree | 96c6190cd863b7597cdb57aae78fd15f878a5fe5 | |
parent | 292c659b5ce2b8e01d59f87713450e6c66ca14a0 (diff) | |
download | ruby-af83fcfe4de5d0497e78789efdfb7e7daf251805.tar.gz |
* lib/rexml/document.rb (REXML::Document#write): Add :encoding option
to support custom XML encoding.
[Feature #4872] (work in progress)
* test/rexml/test_document.rb: Add tests for the above change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37355 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | lib/rexml/document.rb | 12 | ||||
-rw-r--r-- | test/rexml/test_document.rb | 22 |
3 files changed, 36 insertions, 5 deletions
@@ -1,3 +1,10 @@ +Sun Oct 28 15:41:50 2012 Kouhei Sutou <kou@cozmixng.org> + + * lib/rexml/document.rb (REXML::Document#write): Add :encoding option + to support custom XML encoding. + [Feature #4872] (work in progress) + * test/rexml/test_document.rb: Add tests for the above change. + Sun Oct 28 15:00:19 2012 Kouhei Sutou <kou@cozmixng.org> * lib/rexml/document.rb (REXML::Document#write): Remove needless diff --git a/lib/rexml/document.rb b/lib/rexml/document.rb index 6ac527fa83..5508a60be4 100644 --- a/lib/rexml/document.rb +++ b/lib/rexml/document.rb @@ -207,17 +207,19 @@ module REXML indent = options[:indent] transitive = options[:transitive] ie_hack = options[:ie_hack] + encoding = options[:encoding] else - output, indent, transitive, ie_hack, = *arguments + output, indent, transitive, ie_hack, encoding, = *arguments end - output ||= $stdout - indent ||= -1 + output ||= $stdout + indent ||= -1 transitive = false if transitive.nil? ie_hack = false if ie_hack.nil? + encoding ||= xml_decl.encoding - if xml_decl.encoding != 'UTF-8' && !output.kind_of?(Output) - output = Output.new( output, xml_decl.encoding ) + if encoding != 'UTF-8' && !output.kind_of?(Output) + output = Output.new( output, encoding ) end formatter = if indent > -1 if transitive diff --git a/test/rexml/test_document.rb b/test/rexml/test_document.rb index e8ca2e1c24..72d0ff696f 100644 --- a/test/rexml/test_document.rb +++ b/test/rexml/test_document.rb @@ -159,6 +159,19 @@ EOX document.write(output, indent, transitive, ie_hack) assert_equal("<empty />", output) end + + def test_encoding + output = "" + indent = -1 + transitive = false + ie_hack = false + encoding = "Shift_JIS" + @document.write(output, indent, transitive, ie_hack, encoding) + assert_equal(<<-EOX, output) +<?xml version='1.0' encoding='SHIFT_JIS'?> +<message>Hello world!</message> +EOX + end end class OptionsTest < self @@ -199,6 +212,15 @@ EOX document.write(:output => output, :ie_hack => true) assert_equal("<empty />", output) end + + def test_encoding + output = "" + @document.write(:output => output, :encoding => "Shift_JIS") + assert_equal(<<-EOX, output) +<?xml version='1.0' encoding='SHIFT_JIS'?> +<message>Hello world!</message> +EOX + end end end end |