[前][次][番号順一覧][スレッド一覧]

ruby-changes:25303

From: kou <ko1@a...>
Date: Sun, 28 Oct 2012 15:43:48 +0900 (JST)
Subject: [ruby-changes:25303] kou:r37355 (trunk): * lib/rexml/document.rb (REXML::Document#write): Add :encoding option

kou	2012-10-28 15:43:38 +0900 (Sun, 28 Oct 2012)

  New Revision: 37355

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37355

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/lib/rexml/document.rb
    trunk/test/rexml/test_document.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 37354)
+++ ChangeLog	(revision 37355)
@@ -1,3 +1,10 @@
+Sun Oct 28 15:41:50 2012  Kouhei Sutou  <kou@c...>
+
+	* 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@c...>
 
 	* lib/rexml/document.rb (REXML::Document#write): Remove needless
Index: lib/rexml/document.rb
===================================================================
--- lib/rexml/document.rb	(revision 37354)
+++ lib/rexml/document.rb	(revision 37355)
@@ -207,17 +207,19 @@
         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
Index: test/rexml/test_document.rb
===================================================================
--- test/rexml/test_document.rb	(revision 37354)
+++ test/rexml/test_document.rb	(revision 37355)
@@ -159,6 +159,19 @@
         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 @@
         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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]