libs/web: various changes in template library

- rename sanitize_pcdata() and sanitize_utf8() to pcdata() and utf8()
	- implement striptags()
This commit is contained in:
Jo-Philipp Wich 2012-11-26 14:26:43 +00:00
parent e2f3b8923d
commit f7b4fd2979
4 changed files with 99 additions and 20 deletions

View file

@ -44,11 +44,11 @@ int template_L_parse(lua_State *L)
return rv;
}
int template_L_sanitize_utf8(lua_State *L)
int template_L_utf8(lua_State *L)
{
size_t len = 0;
const char *str = luaL_checklstring(L, 1, &len);
char *res = sanitize_utf8(str, len);
char *res = utf8(str, len);
if (res != NULL)
{
@ -61,11 +61,28 @@ int template_L_sanitize_utf8(lua_State *L)
return 0;
}
int template_L_sanitize_pcdata(lua_State *L)
int template_L_pcdata(lua_State *L)
{
size_t len = 0;
const char *str = luaL_checklstring(L, 1, &len);
char *res = sanitize_pcdata(str, len);
char *res = pcdata(str, len);
if (res != NULL)
{
lua_pushstring(L, res);
free(res);
return 1;
}
return 0;
}
int template_L_striptags(lua_State *L)
{
size_t len = 0;
const char *str = luaL_checklstring(L, 1, &len);
char *res = striptags(str, len);
if (res != NULL)
{
@ -129,8 +146,9 @@ static int template_L_hash(lua_State *L) {
/* module table */
static const luaL_reg R[] = {
{ "parse", template_L_parse },
{ "sanitize_utf8", template_L_sanitize_utf8 },
{ "sanitize_pcdata", template_L_sanitize_pcdata },
{ "utf8", template_L_utf8 },
{ "pcdata", template_L_pcdata },
{ "striptags", template_L_striptags },
{ "load_catalog", template_L_load_catalog },
{ "close_catalog", template_L_close_catalog },
{ "change_catalog", template_L_change_catalog },

View file

@ -244,7 +244,7 @@ template_format_chunk(struct template_parser *parser, size_t *sz)
switch (c->type)
{
case T_TYPE_TEXT:
escape_luastr(buf, c->s, c->e - c->s, 0);
luastr_escape(buf, c->s, c->e - c->s, 0);
break;
case T_TYPE_EXPR:
@ -254,15 +254,15 @@ template_format_chunk(struct template_parser *parser, size_t *sz)
break;
case T_TYPE_INCLUDE:
escape_luastr(buf, c->s, c->e - c->s, 0);
luastr_escape(buf, c->s, c->e - c->s, 0);
break;
case T_TYPE_I18N:
translate_luastr(buf, c->s, c->e - c->s, 1);
luastr_translate(buf, c->s, c->e - c->s, 1);
break;
case T_TYPE_I18N_RAW:
translate_luastr(buf, c->s, c->e - c->s, 0);
luastr_translate(buf, c->s, c->e - c->s, 0);
break;
case T_TYPE_CODE:

View file

@ -278,7 +278,7 @@ static int _validate_utf8(unsigned char **s, int l, struct template_buffer *buf)
}
/* sanitize given string and replace all invalid UTF-8 sequences with "?" */
char * sanitize_utf8(const char *s, unsigned int l)
char * utf8(const char *s, unsigned int l)
{
struct template_buffer *buf = buf_init(l);
unsigned char *ptr = (unsigned char *)s;
@ -312,7 +312,7 @@ char * sanitize_utf8(const char *s, unsigned int l)
/* Sanitize given string and strip all invalid XML bytes
* Validate UTF-8 sequences
* Escape XML control chars */
char * sanitize_pcdata(const char *s, unsigned int l)
char * pcdata(const char *s, unsigned int l)
{
struct template_buffer *buf = buf_init(l);
unsigned char *ptr = (unsigned char *)s;
@ -368,7 +368,67 @@ char * sanitize_pcdata(const char *s, unsigned int l)
return buf_destroy(buf);
}
void escape_luastr(struct template_buffer *out, const char *s, unsigned int l,
char * striptags(const char *s, unsigned int l)
{
struct template_buffer *buf = buf_init(l);
unsigned char *ptr = (unsigned char *)s;
unsigned char *end = ptr + l;
unsigned char *tag;
unsigned char prev;
char esq[8];
int esl;
for (prev = ' '; ptr < end; ptr++)
{
if ((*ptr == '<') && ((ptr + 2) < end) &&
((*(ptr + 1) == '/') || isalpha(*(ptr + 1))))
{
for (tag = ptr; tag < end; tag++)
{
if (*tag == '>')
{
if (!isspace(prev))
buf_putchar(buf, ' ');
ptr = tag;
prev = ' ';
break;
}
}
}
else if (isspace(*ptr))
{
if (!isspace(prev))
buf_putchar(buf, *ptr);
prev = *ptr;
}
else
{
switch(*ptr)
{
case '"':
case '\'':
case '<':
case '>':
case '&':
esl = snprintf(esq, sizeof(esq), "&#%i;", *ptr);
buf_append(buf, esq, esl);
break;
default:
buf_putchar(buf, *ptr);
break;
}
prev = *ptr;
}
}
return buf_destroy(buf);
}
void luastr_escape(struct template_buffer *out, const char *s, unsigned int l,
int escape_xml)
{
int esl;
@ -411,7 +471,7 @@ void escape_luastr(struct template_buffer *out, const char *s, unsigned int l,
}
}
void translate_luastr(struct template_buffer *out, const char *s, unsigned int l,
void luastr_translate(struct template_buffer *out, const char *s, unsigned int l,
int escape_xml)
{
char *tr;
@ -420,11 +480,11 @@ void translate_luastr(struct template_buffer *out, const char *s, unsigned int l
switch (lmo_translate(s, l, &tr, &trlen))
{
case 0:
escape_luastr(out, tr, trlen, escape_xml);
luastr_escape(out, tr, trlen, escape_xml);
break;
case -1:
escape_luastr(out, s, l, escape_xml);
luastr_escape(out, s, l, escape_xml);
break;
default:

View file

@ -39,10 +39,11 @@ int buf_append(struct template_buffer *buf, const char *s, int len);
int buf_length(struct template_buffer *buf);
char * buf_destroy(struct template_buffer *buf);
char * sanitize_utf8(const char *s, unsigned int l);
char * sanitize_pcdata(const char *s, unsigned int l);
char * utf8(const char *s, unsigned int l);
char * pcdata(const char *s, unsigned int l);
char * striptags(const char *s, unsigned int l);
void escape_luastr(struct template_buffer *out, const char *s, unsigned int l, int escape_xml);
void translate_luastr(struct template_buffer *out, const char *s, unsigned int l, int escape_xml);
void luastr_escape(struct template_buffer *out, const char *s, unsigned int l, int escape_xml);
void luastr_translate(struct template_buffer *out, const char *s, unsigned int l, int escape_xml);
#endif