Commit Diff


commit - af8b29d8cf480fa20a09387923b39cb3b5394b8a
commit + ac67fee49c78c55c9b711b975fbf8d481e521c74
blob - 5f1093f5488dc59cedb4551421207df0fb7e025d
blob + a462a35fdd53ed1e7997e205fd621e462ad511bc
--- template/tmpl.c
+++ template/tmpl.c
@@ -97,37 +97,46 @@ tp_urlescape(struct template *tp, const char *str)
 	return (0);
 }
 
+static inline int
+htmlescape(struct template *tp, char c)
+{
+	switch (c) {
+	case '<':
+		return tp_write(tp, "&lt;", 4);
+	case '>':
+		return tp_write(tp, "&gt;", 4);
+	case '&':
+		return tp_write(tp, "&amp;", 5);
+	case '"':
+		return tp_write(tp, "&quot;", 6);
+	case '\'':
+		return tp_write(tp, "&apos;", 6);
+	default:
+		return tp_write(tp, &c, 1);
+	}
+}
+
 int
 tp_htmlescape(struct template *tp, const char *str)
 {
-	int r;
-
 	if (str == NULL)
 		return (0);
 
 	for (; *str; ++str) {
-		switch (*str) {
-		case '<':
-			r = tp_write(tp, "&lt;", 4);
-			break;
-		case '>':
-			r = tp_write(tp, "&gt;", 4);
-			break;
-		case '&':
-			r = tp_write(tp, "&amp;", 5);
-			break;
-		case '"':
-			r = tp_write(tp, "&quot;", 6);
-			break;
-		case '\'':
-			r = tp_write(tp, "&apos;", 6);
-			break;
-		default:
-			r = tp_write(tp, str, 1);
-			break;
-		}
+		if (htmlescape(tp, *str) == -1)
+			return (-1);
+	}
 
-		if (r == -1)
+	return (0);
+}
+
+int
+tp_write_htmlescape(struct template *tp, const char *str, size_t len)
+{
+	size_t i;
+
+	for (i = 0; i < len; ++i) {
+		if (htmlescape(tp, str[i]) == -1)
 			return (-1);
 	}
 
blob - df3b74c2696e16f2a591fb656220ac7957c59a56
blob + 3246735b8276b8bcf62702651f4c61e069bf7158
--- template/tmpl.h
+++ template/tmpl.h
@@ -36,6 +36,7 @@ int	 tp_writef(struct template *, const char *, ...)
 	    __attribute__((__format__ (printf, 2, 3)));
 int	 tp_urlescape(struct template *, const char *);
 int	 tp_htmlescape(struct template *, const char *);
+int	 tp_write_htmlescape(struct template *, const char *, size_t);
 
 struct template	*template(void *, tmpl_write, char *, size_t);
 int		 template_flush(struct template *);