ruby-changes:7658
From: akr <ko1@a...>
Date: Sat, 6 Sep 2008 14:47:48 +0900 (JST)
Subject: [ruby-changes:7658] Ruby:r19179 (trunk): * include/ruby/encoding.h (ECONV_HTML_TEXT_ENCODER): new constant.
akr 2008-09-06 14:47:30 +0900 (Sat, 06 Sep 2008) New Revision: 19179 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19179 Log: * include/ruby/encoding.h (ECONV_HTML_TEXT_ENCODER): new constant. (ECONV_HTML_ATTR_ENCODER): ditto. * transcode.c (rb_econv_open): check ECONV_HTML_TEXT_ENCODER and ECONV_HTML_ATTR_ENCODER. (Init_transcode): Encoding::Converter::HTML_TEXT_ENCODER and Encoding::Converter::HTML_ATTR_ENCODER defined. Modified files: trunk/ChangeLog trunk/include/ruby/encoding.h trunk/test/ruby/test_econv.rb trunk/transcode.c Index: include/ruby/encoding.h =================================================================== --- include/ruby/encoding.h (revision 19178) +++ include/ruby/encoding.h (revision 19179) @@ -263,6 +263,8 @@ /* usable only if source encoding is ascii compatible */ #define ECONV_CRLF_NEWLINE_ENCODER 0x0200 #define ECONV_CR_NEWLINE_ENCODER 0x0400 +#define ECONV_HTML_TEXT_ENCODER 0x0800 +#define ECONV_HTML_ATTR_ENCODER 0x1000 /* end of flags for rb_econv_open */ Index: ChangeLog =================================================================== --- ChangeLog (revision 19178) +++ ChangeLog (revision 19179) @@ -1,3 +1,13 @@ +Sat Sep 6 14:46:12 2008 Tanaka Akira <akr@f...> + + * include/ruby/encoding.h (ECONV_HTML_TEXT_ENCODER): new constant. + (ECONV_HTML_ATTR_ENCODER): ditto. + + * transcode.c (rb_econv_open): check ECONV_HTML_TEXT_ENCODER and + ECONV_HTML_ATTR_ENCODER. + (Init_transcode): Encoding::Converter::HTML_TEXT_ENCODER and + Encoding::Converter::HTML_ATTR_ENCODER defined. + Sat Sep 6 14:15:25 2008 Tanaka Akira <akr@f...> * transcode.c (struct trans_open_t): defined to pass num_additional. Index: test/ruby/test_econv.rb =================================================================== --- test/ruby/test_econv.rb (revision 19178) +++ test/ruby/test_econv.rb (revision 19179) @@ -754,4 +754,18 @@ assert_equal('"&<>"', ec.convert("&<>\"")) assert_equal('"', ec.finish) end + + def test_html_escape_with_charref + ec = Encoding::Converter.new("utf-8", "euc-jp", Encoding::Converter::HTML_TEXT_ENCODER|Encoding::Converter::UNDEF_HEX_CHARREF) + assert_equal('<♥>&"♡"', ec.convert("<\u2665>&\"\u2661\"")) + assert_equal('', ec.finish) + + ec = Encoding::Converter.new("utf-8", "euc-jp", Encoding::Converter::HTML_ATTR_ENCODER|Encoding::Converter::UNDEF_HEX_CHARREF) + assert_equal('"<♥>&"♡"', ec.convert("<\u2665>&\"\u2661\"")) + assert_equal('"', ec.finish) + + ec = Encoding::Converter.new("utf-8", "iso-2022-jp", Encoding::Converter::HTML_TEXT_ENCODER) + assert_equal("&\e$B$&\e(B&".force_encoding("iso-2022-jp"), ec.convert("&\u3046&")) + assert_equal('', ec.finish) + end end Index: transcode.c =================================================================== --- transcode.c (revision 19178) +++ transcode.c (revision 19179) @@ -895,6 +895,10 @@ (ecflags & ECONV_UNIVERSAL_NEWLINE_DECODER)) return NULL; + if ((ecflags & ECONV_HTML_TEXT_ENCODER) && + (ecflags & ECONV_HTML_ATTR_ENCODER)) + return NULL; + num_encoders = 0; if (ecflags & ECONV_CRLF_NEWLINE_ENCODER) if (!(encoders[num_encoders++] = get_transcoder_entry("", "crlf_newline"))) @@ -902,6 +906,12 @@ if (ecflags & ECONV_CR_NEWLINE_ENCODER) if (!(encoders[num_encoders++] = get_transcoder_entry("", "cr_newline"))) return NULL; + if (ecflags & ECONV_HTML_TEXT_ENCODER) + if (!(encoders[num_encoders++] = get_transcoder_entry("", "html-text-escaped"))) + return NULL; + if (ecflags & ECONV_HTML_ATTR_ENCODER) + if (!(encoders[num_encoders++] = get_transcoder_entry("", "html-attr-escaped"))) + return NULL; num_decoders = 0; if (ecflags & ECONV_UNIVERSAL_NEWLINE_DECODER) @@ -3510,6 +3520,8 @@ rb_define_const(rb_cEncodingConverter, "UNIVERSAL_NEWLINE_DECODER", INT2FIX(ECONV_UNIVERSAL_NEWLINE_DECODER)); rb_define_const(rb_cEncodingConverter, "CRLF_NEWLINE_ENCODER", INT2FIX(ECONV_CRLF_NEWLINE_ENCODER)); rb_define_const(rb_cEncodingConverter, "CR_NEWLINE_ENCODER", INT2FIX(ECONV_CR_NEWLINE_ENCODER)); + rb_define_const(rb_cEncodingConverter, "HTML_TEXT_ENCODER", INT2FIX(ECONV_HTML_TEXT_ENCODER)); + rb_define_const(rb_cEncodingConverter, "HTML_ATTR_ENCODER", INT2FIX(ECONV_HTML_ATTR_ENCODER)); rb_define_method(rb_eConversionUndefined, "source_encoding_name", ecerr_source_encoding_name, 0); rb_define_method(rb_eConversionUndefined, "destination_encoding_name", ecerr_destination_encoding_name, 0); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/