? encoding.patch
Index: lib/builder/xchar.rb
===================================================================
RCS file: /var/cvs/builder/builder/lib/builder/xchar.rb,v
retrieving revision 1.2
diff -u -r1.2 xchar.rb
--- lib/builder/xchar.rb 31 Jan 2006 02:25:16 -0000 1.2
+++ lib/builder/xchar.rb 11 May 2006 03:01:21 -0000
@@ -105,7 +105,12 @@
class String
# XML escaped version of to_s
def to_xs
- unpack('U*').map {|n| n.xchr}.join # ASCII, UTF-8
+ if methods.include? 'jlength' and [?u,?U].include? $KCODE[0]
+ unpack('U*')
+ gsub('&','&').gsub('<','<').gsub('>','>')
+ else
+ unpack('U*').map {|n| n.xchr}.join # ASCII, UTF-8
+ end
rescue
unpack('C*').map {|n| n.xchr}.join # ISO-8859-1, WIN-1252
end
Index: lib/builder/xmlbase.rb
===================================================================
RCS file: /var/cvs/builder/builder/lib/builder/xmlbase.rb,v
retrieving revision 1.4
diff -u -r1.4 xmlbase.rb
--- lib/builder/xmlbase.rb 31 Jan 2006 00:18:46 -0000 1.4
+++ lib/builder/xmlbase.rb 11 May 2006 03:01:21 -0000
@@ -19,9 +19,10 @@
# indentation and no line breaks).
# initial:: Level of initial indentation.
#
- def initialize(indent=0, initial=0)
- @indent = indent
- @level = initial
+ def initialize(indent=0, initial=0, encoding='utf-8')
+ @indent = indent
+ @level = initial
+ @encoding = encoding
end
# Create a tag named +sym+. Other than the first argument which
@@ -113,7 +114,11 @@
require 'builder/xchar'
def _escape(text)
- text.to_xs
+ if @encoding == 'utf-8':
+ text.to_xs
+ else
+ text
+ end
end
def _escape_quote(text)
Index: lib/builder/xmlmarkup.rb
===================================================================
RCS file: /var/cvs/builder/builder/lib/builder/xmlmarkup.rb,v
retrieving revision 1.6
diff -u -r1.6 xmlmarkup.rb
--- lib/builder/xmlmarkup.rb 30 Mar 2006 19:14:27 -0000 1.6
+++ lib/builder/xmlmarkup.rb 11 May 2006 03:01:21 -0000
@@ -240,6 +240,7 @@
if directive_tag == :xml
a = { :version=>"1.0", :encoding=>"UTF-8" }
attrs = a.merge attrs
+ @encoding = attrs[:encoding].downcase
end
_special(
"#{directive_tag}",
Index: test/testmarkupbuilder.rb
===================================================================
RCS file: /var/cvs/builder/builder/test/testmarkupbuilder.rb,v
retrieving revision 1.7
diff -u -r1.7 testmarkupbuilder.rb
--- test/testmarkupbuilder.rb 31 Jan 2006 00:18:50 -0000 1.7
+++ test/testmarkupbuilder.rb 11 May 2006 03:01:21 -0000
@@ -441,3 +441,66 @@
end
end
+
+class TestXmlChar < Test::Unit::TestCase
+ def setup
+ @xml = Builder::XmlMarkup.new(:indent=>2)
+ @kcode = $KCODE
+ end
+
+ # mock up usage of jcode
+ def _require_jcode
+ $KCODE = 'u'
+ String.class_eval {define_method(:jlength) {length}}
+ end
+
+ # undo mock jcode
+ def teardown
+ $KCODE = @kcode
+ if String.instance_methods.include? "jlength"
+ String.class_eval {undef_method :jlength}
+ end
+ end
+
+ def test_source_utf_8_target_utf_8
+ @xml.foo "\xC2\xA9"
+ assert_equal "©\n", @xml.target!
+ end
+
+ def test_source_iso_8859_1_target_utf_8
+ @xml.instruct! :xml, :encoding=>'utf-8'
+ @xml.foo "\xA9"
+ assert_equal "\n©\n", @xml.target!
+ end
+
+ def test_source_iso_8859_1_target_iso_8859_1
+ @xml.instruct! :xml, :encoding=>'iso-8859-1'
+ @xml.foo "\xC2\xA9"
+ assert_equal "\n\302\251\n", @xml.target!
+ end
+
+ def test_source_iso_8859_1_target_utf_8
+ @xml.instruct! :xml, :encoding=>'utf-8'
+ @xml.foo "\xA9"
+ assert_equal "\n©\n", @xml.target!
+ end
+
+ def test_jcode_valid_utf_8
+ _require_jcode
+ @xml.foo "\xC2\xA9"
+ assert_equal "\xC2\xA9\n", @xml.target!
+ end
+
+ def test_jcode_invalid_utf_8
+ _require_jcode
+ @xml.foo "\xA9"
+ assert_equal "©\n", @xml.target!
+ end
+
+ def test_jcode_escaping
+ _require_jcode
+ @xml.foo "<&>"
+ assert_equal "<&>\n", @xml.target!
+ end
+
+end