mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19173 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d90ab4c09b
commit
091171a286
3 changed files with 82 additions and 11 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
Sat Sep 6 12:19:36 2008 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* 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@dotrb.org>
|
Sat Sep 6 07:54:36 2008 Tadayoshi Funaba <tadf@dotrb.org>
|
||||||
|
|
||||||
* complex.c: uses f_real_p macro.
|
* complex.c: uses f_real_p macro.
|
||||||
|
|
|
@ -53,11 +53,7 @@ fun_so_escape_html_chref(void *statep, const unsigned char *s, size_t l, unsigne
|
||||||
transcode_generate_node(ActionMap.parse(map_html_text), "escape_html_text")
|
transcode_generate_node(ActionMap.parse(map_html_text), "escape_html_text")
|
||||||
|
|
||||||
map_html_attr = {}
|
map_html_attr = {}
|
||||||
map_html_attr["{00-21,23-25,27-3B,3D,3F-FF}"] = :nomap
|
map_html_attr["{00-FF}"] = :func_so
|
||||||
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_generate_node(ActionMap.parse(map_html_attr), "escape_html_attr")
|
||||||
%>
|
%>
|
||||||
|
|
||||||
|
@ -87,16 +83,68 @@ rb_escape_html_text = {
|
||||||
NULL, NULL, NULL, &fun_so_escape_html_chref
|
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
|
static const rb_transcoder
|
||||||
rb_escape_html_attr = {
|
rb_escape_html_attr = {
|
||||||
"", "html-attr-escaped", escape_html_attr,
|
"", "html-attr-escaped", escape_html_attr,
|
||||||
TRANSCODE_TABLE_INFO,
|
TRANSCODE_TABLE_INFO,
|
||||||
1, /* input_unit_length */
|
1, /* input_unit_length */
|
||||||
1, /* max_input */
|
1, /* max_input */
|
||||||
6, /* max_output */
|
7, /* max_output */
|
||||||
stateless_converter, /* stateful_type */
|
stateful_encoder, /* stateful_type */
|
||||||
0, NULL, NULL,
|
1, escape_html_attr_init, escape_html_attr_init,
|
||||||
NULL, NULL, NULL, &fun_so_escape_html_chref
|
NULL, NULL, NULL, fun_so_escape_html_attr,
|
||||||
|
escape_html_attr_finish
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -727,14 +727,30 @@ class TestEncodingConverter < Test::Unit::TestCase
|
||||||
assert_equal("&", ec.convert("&"))
|
assert_equal("&", ec.convert("&"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_html_escape
|
def test_html_escape_text
|
||||||
ec = Encoding::Converter.new("", "amp-escaped")
|
ec = Encoding::Converter.new("", "amp-escaped")
|
||||||
assert_equal('&<>"', ec.convert("&<>\""))
|
assert_equal('&<>"', ec.convert("&<>\""))
|
||||||
|
assert_equal('', ec.finish)
|
||||||
|
|
||||||
ec = Encoding::Converter.new("", "html-text-escaped")
|
ec = Encoding::Converter.new("", "html-text-escaped")
|
||||||
assert_equal('&<>"', ec.convert("&<>\""))
|
assert_equal('&<>"', ec.convert("&<>\""))
|
||||||
|
assert_equal('', ec.finish)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_html_escape_attr
|
||||||
|
ec = Encoding::Converter.new("", "html-attr-escaped")
|
||||||
|
assert_equal('""', ec.finish)
|
||||||
|
|
||||||
ec = Encoding::Converter.new("", "html-attr-escaped")
|
ec = Encoding::Converter.new("", "html-attr-escaped")
|
||||||
assert_equal('&<>"', ec.convert("&<>\""))
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue