ruby-changes:7652
From: akr <ko1@a...>
Date: Sat, 6 Sep 2008 12:21:12 +0900 (JST)
Subject: [ruby-changes:7652] Ruby:r19173 (trunk): * enc/trans/escape.trans (escape_html_attr_init): new function.
akr 2008-09-06 12:20:51 +0900 (Sat, 06 Sep 2008) New Revision: 19173 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19173 Log: * enc/trans/escape.trans (escape_html_attr_init): new function. (fun_so_escape_html_attr): new function. (escape_html_attr_finish): new function. (rb_escape_html_attr): use them to quote the converted result. Modified files: trunk/ChangeLog trunk/enc/trans/escape.trans trunk/test/ruby/test_econv.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 19172) +++ ChangeLog (revision 19173) @@ -1,3 +1,10 @@ +Sat Sep 6 12:19:36 2008 Tanaka Akira <akr@f...> + + * enc/trans/escape.trans (escape_html_attr_init): new function. + (fun_so_escape_html_attr): new function. + (escape_html_attr_finish): new function. + (rb_escape_html_attr): use them to quote the converted result. + Sat Sep 6 07:54:36 2008 Tadayoshi Funaba <tadf@d...> * complex.c: uses f_real_p macro. Index: enc/trans/escape.trans =================================================================== --- enc/trans/escape.trans (revision 19172) +++ enc/trans/escape.trans (revision 19173) @@ -53,11 +53,7 @@ transcode_generate_node(ActionMap.parse(map_html_text), "escape_html_text") map_html_attr = {} - map_html_attr["{00-21,23-25,27-3B,3D,3F-FF}"] = :nomap - map_html_attr["22"] = :func_so - map_html_attr["26"] = :func_so - map_html_attr["3C"] = :func_so - map_html_attr["3E"] = :func_so + map_html_attr["{00-FF}"] = :func_so transcode_generate_node(ActionMap.parse(map_html_attr), "escape_html_attr") %> @@ -87,16 +83,68 @@ NULL, NULL, NULL, &fun_so_escape_html_chref }; +#define END 0 +#define NORMAL 1 + +static int +escape_html_attr_init(void *statep) +{ + unsigned char *sp = statep; + *sp = END; + return 0; +} + +static VALUE +fun_so_escape_html_attr(void *statep, const unsigned char *s, size_t l, unsigned char *o) +{ + unsigned char *sp = statep; + int n = 0; + if (*sp == END) { + *sp = NORMAL; + o[n++] = '"'; + } + switch (s[0]) { + case '&': + case '<': + case '>': + case '"': + n += fun_so_escape_html_chref(statep, s, l, o+n); + break; + + default: + o[n++] = s[0]; + break; + } + return n; +} + +static int +escape_html_attr_finish(void *statep, unsigned char *o) +{ + unsigned char *sp = statep; + int n = 0; + + if (*sp == END) { + o[n++] = '"'; + } + + o[n++] = '"'; + *sp = END; + + return n; +} + static const rb_transcoder rb_escape_html_attr = { "", "html-attr-escaped", escape_html_attr, TRANSCODE_TABLE_INFO, 1, /* input_unit_length */ 1, /* max_input */ - 6, /* max_output */ - stateless_converter, /* stateful_type */ - 0, NULL, NULL, - NULL, NULL, NULL, &fun_so_escape_html_chref + 7, /* max_output */ + stateful_encoder, /* stateful_type */ + 1, escape_html_attr_init, escape_html_attr_init, + NULL, NULL, NULL, fun_so_escape_html_attr, + escape_html_attr_finish }; void Index: test/ruby/test_econv.rb =================================================================== --- test/ruby/test_econv.rb (revision 19172) +++ test/ruby/test_econv.rb (revision 19173) @@ -727,14 +727,30 @@ assert_equal("&", ec.convert("&")) end - def test_html_escape + def test_html_escape_text ec = Encoding::Converter.new("", "amp-escaped") assert_equal('&<>"', ec.convert("&<>\"")) + assert_equal('', ec.finish) ec = Encoding::Converter.new("", "html-text-escaped") assert_equal('&<>"', ec.convert("&<>\"")) + assert_equal('', ec.finish) + end + def test_html_escape_attr ec = Encoding::Converter.new("", "html-attr-escaped") - assert_equal('&<>"', ec.convert("&<>\"")) + assert_equal('""', ec.finish) + + ec = Encoding::Converter.new("", "html-attr-escaped") + assert_equal('', ec.convert("")) + assert_equal('""', ec.finish) + + ec = Encoding::Converter.new("", "html-attr-escaped") + assert_equal('""', ec.convert('"')) + assert_equal('"', ec.finish) + + ec = Encoding::Converter.new("", "html-attr-escaped") + assert_equal('"&<>"', ec.convert("&<>\"")) + assert_equal('"', ec.finish) end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/