aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-09-27 20:48:55 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-09-28 00:12:57 +0200
commit94ddbec88e57fe742f219a30145650165887f22c (patch)
treeb605b87b003927eb86cf8d78bae934b06b50ec5a
parentbdfbd076f2d3e7212f300bf5edbbad1934fae324 (diff)
html: Make html_encode public
Among other reasons, this function can be useful to sanitize user-generated content before assigning it do a node.
-rw-r--r--doc/man3/html_encode.345
-rw-r--r--html.c2
-rw-r--r--include/libweb/html.h1
3 files changed, 47 insertions, 1 deletions
diff --git a/doc/man3/html_encode.3 b/doc/man3/html_encode.3
new file mode 100644
index 0000000..bf7695b
--- /dev/null
+++ b/doc/man3/html_encode.3
@@ -0,0 +1,45 @@
+.TH HTML_ENCODE 3 2025-09-28 0.4.0 "libweb Library Reference"
+
+.SH NAME
+html_encode \- Encodes a string as HTML
+
+.SH SYNOPSIS
+.LP
+.nf
+#include <libweb/html.h>
+.P
+char *html_encode(const char *\fIs\fP);
+.fi
+
+.SH DESCRIPTION
+The
+.IR html_encode ()
+function encodes the null-terminated string given by
+.IR s ,
+replacing HTML reserved symbols, such as
+.I <
+or
+.IR > ,
+into their equivalents, such as
+.I &gt;
+or
+.IR &lt; .
+
+.SH RETURN VALUE
+On success, a valid pointer to a HTML-encoded, null-terminated
+string is returned. On failure, a null pointer is returned.
+
+.SH ERRORS
+No errors are defined.
+
+.SH SEE ALSO
+.BR html_node_set_value (3),
+.BR libweb_html (7).
+
+.SH COPYRIGHT
+Copyright (C) 2023-2025 libweb contributors
+.P
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
diff --git a/html.c b/html.c
index 094b00c..4af536e 100644
--- a/html.c
+++ b/html.c
@@ -23,7 +23,7 @@ struct html_node
static int serialize_siblings(struct dynstr *, const struct html_node *,
unsigned);
-static char *html_encode(const char *s)
+char *html_encode(const char *s)
{
struct dynstr d;
diff --git a/include/libweb/html.h b/include/libweb/html.h
index e62575e..37be86e 100644
--- a/include/libweb/html.h
+++ b/include/libweb/html.h
@@ -5,6 +5,7 @@
struct html_node *html_node_alloc(const char *element);
void html_node_free(struct html_node *n);
+char *html_encode(const char *s);
int html_node_set_value(struct html_node *n, const char *val);
int html_node_set_value_unescaped(struct html_node *n, const char *val);
int html_node_add_attr(struct html_node *n, const char *attr, const char *val);