diff --git a/ChangeLog b/ChangeLog index efb3342152..07973f94c6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sun Oct 28 15:41:50 2012 Kouhei Sutou + + * 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 * 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("", 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) + +Hello world! +EOX + end end class OptionsTest < self @@ -199,6 +212,15 @@ EOX document.write(:output => output, :ie_hack => true) assert_equal("", output) end + + def test_encoding + output = "" + @document.write(:output => output, :encoding => "Shift_JIS") + assert_equal(<<-EOX, output) + +Hello world! +EOX + end end end end