summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2011-11-28 00:47:12 -0800
committerPixel <pixel@nobis-crew.org>2011-11-28 00:47:12 -0800
commit50f08cb0b43a7d9fee1d4b3047d71f569d741380 (patch)
treec9bc861a547ee9c1dbf820ce58ebdc0c067f3825
parentdcbf05840a6b38dd56367fd68182db9c61f9e61d (diff)
Whoops: forgot to implement the escaping function.
-rw-r--r--includes/SimpleMustache.h2
-rw-r--r--src/SimpleMustache.cc87
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;
+}