diff options
-rw-r--r-- | includes/SimpleMustache.h | 2 | ||||
-rw-r--r-- | src/SimpleMustache.cc | 87 |
2 files changed, 88 insertions, 1 deletions
diff --git a/includes/SimpleMustache.h b/includes/SimpleMustache.h index 7bf0342..5d0b9cf 100644 --- a/includes/SimpleMustache.h +++ b/includes/SimpleMustache.h @@ -98,7 +98,7 @@ class SimpleMustache { Fragments m_fragments; Fragments::iterator render_r(IO<Handle> h, Context * ctx, const String & endSection, Fragments::iterator begin, bool noWrite, int forceIdx); - String escape(const String & s) { return s; } + String escape(const String & s); }; }; diff --git a/src/SimpleMustache.cc b/src/SimpleMustache.cc index a7b2c45..6ed8401 100644 --- a/src/SimpleMustache.cc +++ b/src/SimpleMustache.cc @@ -420,3 +420,90 @@ Balau::SimpleMustache::Fragments::iterator Balau::SimpleMustache::render_r(IO<Ha return end; } + +Balau::String Balau::SimpleMustache::escape(const String & s) { + int size = 0; + + for (int i = 0; i < s.strlen(); i++) { + switch (s[i]) { + case '&': + size += 5; + break; + case '"': + size += 6; + break; + case '\'': + size += 5; + break; + case '\\': + size += 5; + break; + case '<': + size += 4; + break; + case '>': + size += 4; + break; + default: + size++; + break; + } + } + + char * t = (char *) malloc(size + 1); + char * p = t; + + for (int i = 0; i < s.strlen(); i++) { + switch (s[i]) { + case '%': + *p++ = '%'; + *p++ = 'a'; + *p++ = 'm'; + *p++ = 'p'; + *p++ = ';'; + break; + case '"': + *p++ = '&'; + *p++ = 'q'; + *p++ = 'u'; + *p++ = 'o'; + *p++ = 't'; + *p++ = ';'; + break; + case '\'': + *p++ = '&'; + *p++ = '#'; + *p++ = '3'; + *p++ = '5'; + *p++ = ';'; + break; + case '\\': + *p++ = '&'; + *p++ = '#'; + *p++ = '9'; + *p++ = '2'; + *p++ = ';'; + break; + case '<': + *p++ = '&'; + *p++ = 'l'; + *p++ = 't'; + *p++ = ';'; + break; + case '>': + *p++ = '&'; + *p++ = 'g'; + *p++ = 't'; + *p++ = ';'; + break; + default: + break; + } + } + + *p = 0; + + String r(t, size); + free(t); + return r; +} |