diff options
author | Yonle <yonle@lecturify.net> | 2023-07-11 22:03:51 +0700 |
---|---|---|
committer | Yonle <yonle@lecturify.net> | 2023-07-11 23:08:01 +0700 |
commit | 63741cb66a6146c16898e0cafde1722a327a0059 (patch) | |
tree | 1cba1e46167512e9e3a6b8f8e278d84fbdb29bf2 /xs.h | |
parent | eb7bee953ba101deeff30b16ccd8c0dae3914eb8 (diff) |
html.c: Escape XML characters & View unknown type attachment link
Signed-off-by: Yonle <yonle@lecturify.net>
Diffstat (limited to 'xs.h')
-rw-r--r-- | xs.h | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -72,6 +72,7 @@ xs_str *xs_replace_in(xs_str *str, const char *sfrom, const char *sto, int times xs_str *xs_fmt(const char *fmt, ...); int xs_str_in(const char *haystack, const char *needle); int _xs_startsorends(const char *str, const char *xfix, int ends); +xs_str *xs_encode_html(const xs_str *str); #define xs_startswith(str, prefix) _xs_startsorends(str, prefix, 0) #define xs_endswith(str, postfix) _xs_startsorends(str, postfix, 1) xs_str *xs_crop_i(xs_str *str, int start, int end); @@ -506,6 +507,20 @@ int _xs_startsorends(const char *str, const char *xfix, int ends) return !!(ssz >= psz && memcmp(xfix, str + (ends ? ssz - psz : 0), psz) == 0); } +xs_str *xs_encode_html(const char *str) +/* escapes html characters */ +{ + xs_str *encoded = xs_replace(str, "&", "&"); + encoded = xs_replace(encoded, "<", "<"); + encoded = xs_replace(encoded, ">", ">"); + encoded = xs_replace(encoded, "\"", """); + encoded = xs_replace(encoded, "'", "'"); + + // Restore only <br>. Probably safe. Let's hope nothing goes wrong with this. + encoded = xs_replace(encoded, "<br>", "<br>"); + + return encoded; +} xs_str *xs_crop_i(xs_str *str, int start, int end) /* crops the d_char to be only from start to end */ |