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

ruby-changes:7644

From: akr <ko1@a...>
Date: Sat, 6 Sep 2008 06:29:27 +0900 (JST)
Subject: [ruby-changes:7644] Ruby:r19165 (trunk): * enc/trans/escape.trans: new file.

akr	2008-09-06 06:29:12 +0900 (Sat, 06 Sep 2008)

  New Revision: 19165

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

  Log:
    * enc/trans/escape.trans: new file.

  Added files:
    trunk/enc/trans/escape.trans
  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_econv.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 19164)
+++ ChangeLog	(revision 19165)
@@ -1,3 +1,7 @@
+Sat Sep  6 06:28:46 2008  Tanaka Akira  <akr@f...>
+
+	* enc/trans/escape.trans: new file.
+
 Sat Sep  6 06:23:27 2008  Tanaka Akira  <akr@f...>
 
 	* tool/transcode-tblgen.rb (StrSet.parse): accept upper case
Index: enc/trans/escape.trans
===================================================================
--- enc/trans/escape.trans	(revision 0)
+++ enc/trans/escape.trans	(revision 19165)
@@ -0,0 +1,109 @@
+#include "transcode_data.h"
+
+static int
+fun_so_escape_html_chref(void *statep, const unsigned char *s, size_t l, unsigned char *o)
+{
+    switch (*s) {
+      case '&':
+        o[0] = '&';
+        o[1] = 'a';
+        o[2] = 'm';
+        o[3] = 'p';
+        o[4] = ';';
+        return 5;
+
+      case '<':
+        o[0] = '&';
+        o[1] = 'l';
+        o[2] = 't';
+        o[3] = ';';
+        return 4;
+
+      case '>':
+        o[0] = '&';
+        o[1] = 'g';
+        o[2] = 't';
+        o[3] = ';';
+        return 4;
+
+      case '"':
+        o[0] = '&';
+        o[1] = 'q';
+        o[2] = 'u';
+        o[3] = 'o';
+        o[4] = 't';
+        o[5] = ';';
+        return 6;
+
+      default:
+        rb_bug("unexpected char");
+    }
+} 
+<%
+  map_amp = {}
+  map_amp["{00-25,27-FF}"] = :nomap
+  map_amp["26"] = :func_so
+  transcode_generate_node(ActionMap.parse(map_amp), "escape_amp_as_chref")
+
+  map_html_text = {}
+  map_html_text["{00-25,27-3B,3D,3F-FF}"] = :nomap
+  map_html_text["26"] = :func_so
+  map_html_text["3C"] = :func_so
+  map_html_text["3E"] = :func_so
+  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
+  transcode_generate_node(ActionMap.parse(map_html_attr), "escape_html_attr")
+%>
+
+<%= transcode_generated_code %>
+
+static const rb_transcoder
+rb_escape_amp_as_chref = {
+    "", "amp-escaped", escape_amp_as_chref,
+    TRANSCODE_TABLE_INFO,
+    1, /* input_unit_length */
+    1, /* max_input */
+    5, /* max_output */
+    stateless_converter, /* stateful_type */
+    0, NULL, NULL,
+    NULL, NULL, NULL, &fun_so_escape_html_chref
+};
+
+static const rb_transcoder
+rb_escape_html_text = {
+    "", "html-text-escaped", escape_html_text,
+    TRANSCODE_TABLE_INFO,
+    1, /* input_unit_length */
+    1, /* max_input */
+    5, /* max_output */
+    stateless_converter, /* stateful_type */
+    0, NULL, NULL,
+    NULL, NULL, NULL, &fun_so_escape_html_chref
+};
+
+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
+};
+
+void
+Init_escape(void)
+{
+    rb_register_transcoder(&rb_escape_amp_as_chref);
+    rb_register_transcoder(&rb_escape_html_text);
+    rb_register_transcoder(&rb_escape_html_attr);
+}
+
Index: test/ruby/test_econv.rb
===================================================================
--- test/ruby/test_econv.rb	(revision 19164)
+++ test/ruby/test_econv.rb	(revision 19165)
@@ -692,5 +692,19 @@
     ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1", Encoding::Converter::UNDEF_HEX_CHARREF)
     assert_equal("&#x4EA4;&#x63DB;&#x6CD5;&#x5247;: n\xD7m=m\xD7n".force_encoding("ISO-8859-1"),
                  ec.convert("\xB8\xF2\xB4\xB9\xCB\xA1\xC2\xA7: n\xA1\xDFm=m\xA1\xDFn"))
+
+    ec = Encoding::Converter.new("UTF-8", "US-ASCII", Encoding::Converter::UNDEF_HEX_CHARREF)
+    assert_equal("&", ec.convert("&"))
   end
+
+  def test_html_escape
+    ec = Encoding::Converter.new("", "amp-escaped")
+    assert_equal('&amp;<>"', ec.convert("&<>\""))
+
+    ec = Encoding::Converter.new("", "html-text-escaped")
+    assert_equal('&amp;&lt;&gt;"', ec.convert("&<>\""))
+
+    ec = Encoding::Converter.new("", "html-attr-escaped")
+    assert_equal('&amp;&lt;&gt;&quot;', ec.convert("&<>\""))
+  end
 end

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

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