diff options
| author | 2023-11-20 18:33:24 +0100 | |
|---|---|---|
| committer | 2023-11-20 18:33:24 +0100 | |
| commit | bc5d0d4ed09833640856ee0193a53553dbb1eb20 (patch) | |
| tree | afd97135fb606445cb8f5348ae2857cb3088dbd8 /xs_html.h | |
| parent | Version 2.43 RELEASED. (diff) | |
| download | penes-snac2-bc5d0d4ed09833640856ee0193a53553dbb1eb20.tar.gz penes-snac2-bc5d0d4ed09833640856ee0193a53553dbb1eb20.tar.xz penes-snac2-bc5d0d4ed09833640856ee0193a53553dbb1eb20.zip | |
Replaced encode_html_strict() with xs_html_encode().
Diffstat (limited to 'xs_html.h')
| -rw-r--r-- | xs_html.h | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/xs_html.h b/xs_html.h new file mode 100644 index 0000000..744df5b --- /dev/null +++ b/xs_html.h | |||
| @@ -0,0 +1,240 @@ | |||
| 1 | /* copyright (c) 2022 - 2023 grunfink et al. / MIT license */ | ||
| 2 | |||
| 3 | #ifndef _XS_HTML_H | ||
| 4 | |||
| 5 | #define _XS_HTML_H | ||
| 6 | |||
| 7 | typedef struct xs_html xs_html; | ||
| 8 | |||
| 9 | xs_str *xs_html_encode(char *str); | ||
| 10 | |||
| 11 | xs_html *xs_html_attr(char *key, char *value); | ||
| 12 | xs_html *xs_html_text(char *content); | ||
| 13 | xs_html *xs_html_raw(char *content); | ||
| 14 | |||
| 15 | xs_html *xs_html_add(xs_html *tag, xs_html *data); | ||
| 16 | |||
| 17 | xs_html *_xs_html_tag(char *tag, xs_html *var[]); | ||
| 18 | #define xs_html_tag(tag, ...) _xs_html_tag(tag, (xs_html *[]) { __VA_ARGS__, NULL }) | ||
| 19 | xs_html *_xs_html_sctag(char *tag, xs_html *var[]); | ||
| 20 | #define xs_html_sctag(tag, ...) _xs_html_sctag(tag, (xs_html *[]) { __VA_ARGS__, NULL }) | ||
| 21 | xs_str *_xs_html_render(xs_html *h, xs_str *s); | ||
| 22 | #define xs_html_render(h) _xs_html_render(h, xs_str_new(NULL)) | ||
| 23 | |||
| 24 | #ifdef XS_IMPLEMENTATION | ||
| 25 | |||
| 26 | typedef enum { | ||
| 27 | XS_HTML_TAG, | ||
| 28 | XS_HTML_SCTAG, | ||
| 29 | XS_HTML_ATTR, | ||
| 30 | XS_HTML_TEXT | ||
| 31 | } xs_html_type; | ||
| 32 | |||
| 33 | struct xs_html { | ||
| 34 | xs_html_type type; | ||
| 35 | xs_str *content; | ||
| 36 | xs_html *f_attr; | ||
| 37 | xs_html *l_attr; | ||
| 38 | xs_html *f_tag; | ||
| 39 | xs_html *l_tag; | ||
| 40 | xs_html *next; | ||
| 41 | }; | ||
| 42 | |||
| 43 | xs_str *xs_html_encode(char *str) | ||
| 44 | /* encodes str using HTML entities */ | ||
| 45 | { | ||
| 46 | xs_str *s = xs_str_new(NULL); | ||
| 47 | int o = 0; | ||
| 48 | char *e = str + strlen(str); | ||
| 49 | |||
| 50 | for (;;) { | ||
| 51 | char *ec = "<>\"'&"; /* characters to escape */ | ||
| 52 | char *q = e; | ||
| 53 | int z; | ||
| 54 | |||
| 55 | /* find the nearest happening of a char */ | ||
| 56 | while (*ec) { | ||
| 57 | char *m = memchr(str, *ec++, q - str); | ||
| 58 | if (m) | ||
| 59 | q = m; | ||
| 60 | } | ||
| 61 | |||
| 62 | /* copy string to here */ | ||
| 63 | z = q - str; | ||
| 64 | s = xs_insert_m(s, o, str, z); | ||
| 65 | o += z; | ||
| 66 | |||
| 67 | /* if q points to the end, nothing more to do */ | ||
| 68 | if (q == e) | ||
| 69 | break; | ||
| 70 | |||
| 71 | /* insert the escaped char */ | ||
| 72 | char tmp[8]; | ||
| 73 | snprintf(tmp, sizeof(tmp), "&#%d;", *q); | ||
| 74 | |||
| 75 | z = strlen(tmp); | ||
| 76 | s = xs_insert_m(s, o, tmp, z); | ||
| 77 | o += z; | ||
| 78 | |||
| 79 | str = q + 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | return s; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | #define XS_HTML_NEW() memset(xs_realloc(NULL, sizeof(xs_html)), '\0', sizeof(xs_html)) | ||
| 87 | |||
| 88 | xs_html *xs_html_attr(char *key, char *value) | ||
| 89 | /* creates an HTML block with an attribute */ | ||
| 90 | { | ||
| 91 | xs_html *a = XS_HTML_NEW(); | ||
| 92 | |||
| 93 | a->type = XS_HTML_ATTR; | ||
| 94 | |||
| 95 | if (value) { | ||
| 96 | xs *ev = xs_html_encode(value); | ||
| 97 | a->content = xs_fmt("%s=\"%s\"", key, ev); | ||
| 98 | } | ||
| 99 | else | ||
| 100 | a->content = xs_dup(key); | ||
| 101 | |||
| 102 | return a; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | xs_html *xs_html_text(char *content) | ||
| 107 | /* creates an HTML block of text, escaping it previously */ | ||
| 108 | { | ||
| 109 | xs_html *a = XS_HTML_NEW(); | ||
| 110 | |||
| 111 | a->type = XS_HTML_TEXT; | ||
| 112 | a->content = xs_html_encode(content); | ||
| 113 | |||
| 114 | return a; | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | xs_html *xs_html_raw(char *content) | ||
| 119 | /* creates an HTML block without escaping (for pre-formatted HTML, comments, etc) */ | ||
| 120 | { | ||
| 121 | xs_html *a = XS_HTML_NEW(); | ||
| 122 | |||
| 123 | a->type = XS_HTML_TEXT; | ||
| 124 | a->content = xs_dup(content); | ||
| 125 | |||
| 126 | return a; | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | xs_html *xs_html_add(xs_html *tag, xs_html *data) | ||
| 131 | /* add data (attrs, tags or text) to a tag */ | ||
| 132 | { | ||
| 133 | xs_html **first; | ||
| 134 | xs_html **last; | ||
| 135 | |||
| 136 | if (data->type == XS_HTML_ATTR) { | ||
| 137 | first = &tag->f_attr; | ||
| 138 | last = &tag->l_attr; | ||
| 139 | } | ||
| 140 | else { | ||
| 141 | first = &tag->f_tag; | ||
| 142 | last = &tag->l_tag; | ||
| 143 | } | ||
| 144 | |||
| 145 | if (*first == NULL) | ||
| 146 | *first = data; | ||
| 147 | |||
| 148 | if (*last != NULL) | ||
| 149 | (*last)->next = data; | ||
| 150 | |||
| 151 | *last = data; | ||
| 152 | |||
| 153 | return tag; | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | static xs_html *_xs_html_tag_t(xs_html_type type, char *tag, xs_html *var[]) | ||
| 158 | /* creates a tag with a variable list of attributes and subtags */ | ||
| 159 | { | ||
| 160 | xs_html *a = XS_HTML_NEW(); | ||
| 161 | |||
| 162 | a->type = type; | ||
| 163 | a->content = xs_dup(tag); | ||
| 164 | |||
| 165 | while (*var) | ||
| 166 | xs_html_add(a, *var++); | ||
| 167 | |||
| 168 | return a; | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | xs_html *_xs_html_tag(char *tag, xs_html *var[]) | ||
| 173 | { | ||
| 174 | return _xs_html_tag_t(XS_HTML_TAG, tag, var); | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | xs_html *_xs_html_sctag(char *tag, xs_html *var[]) | ||
| 179 | { | ||
| 180 | return _xs_html_tag_t(XS_HTML_SCTAG, tag, var); | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | xs_str *_xs_html_render(xs_html *h, xs_str *s) | ||
| 185 | /* renders the tag and its subtags */ | ||
| 186 | { | ||
| 187 | xs_html *st; | ||
| 188 | |||
| 189 | switch (h->type) { | ||
| 190 | case XS_HTML_TAG: | ||
| 191 | case XS_HTML_SCTAG: | ||
| 192 | s = xs_str_cat(s, "<", h->content); | ||
| 193 | |||
| 194 | /* render the attributes */ | ||
| 195 | st = h->f_attr; | ||
| 196 | while (st) { | ||
| 197 | xs_html *nst = st->next; | ||
| 198 | s = _xs_html_render(st, s); | ||
| 199 | st = nst; | ||
| 200 | } | ||
| 201 | |||
| 202 | if (h->type == XS_HTML_SCTAG) { | ||
| 203 | /* self-closing tags should not have subtags */ | ||
| 204 | s = xs_str_cat(s, "/>"); | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | s = xs_str_cat(s, ">"); | ||
| 208 | |||
| 209 | /* render the subtags */ | ||
| 210 | st = h->f_tag; | ||
| 211 | while (st) { | ||
| 212 | xs_html *nst = st->next; | ||
| 213 | s = _xs_html_render(st, s); | ||
| 214 | st = nst; | ||
| 215 | } | ||
| 216 | |||
| 217 | s = xs_str_cat(s, "</", h->content, ">"); | ||
| 218 | } | ||
| 219 | |||
| 220 | break; | ||
| 221 | |||
| 222 | case XS_HTML_ATTR: | ||
| 223 | s = xs_str_cat(s, " ", h->content); | ||
| 224 | break; | ||
| 225 | |||
| 226 | case XS_HTML_TEXT: | ||
| 227 | s = xs_str_cat(s, h->content); | ||
| 228 | break; | ||
| 229 | } | ||
| 230 | |||
| 231 | xs_free(h->content); | ||
| 232 | xs_free(h); | ||
| 233 | |||
| 234 | return s; | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | #endif /* XS_IMPLEMENTATION */ | ||
| 239 | |||
| 240 | #endif /* _XS_HTML_H */ | ||