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)
{
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)
{
/* 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 },
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:
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:
}
/* 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;
/* 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;
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;
}
}
-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;
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:
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