Backport from xs.

This commit is contained in:
default 2023-12-03 17:26:50 +01:00
parent a658e5d1c7
commit 5f047d46c0
3 changed files with 28 additions and 13 deletions

2
html.c
View file

@ -1000,7 +1000,7 @@ static xs_html *html_button(char *clss, char *label, char *hint)
xs *c = xs_fmt("snac-btn-%s", clss); xs *c = xs_fmt("snac-btn-%s", clss);
/* use an NULL tag to separate non-css-classed buttons from one another */ /* use an NULL tag to separate non-css-classed buttons from one another */
return xs_html_tag(NULL, return xs_html_container(
xs_html_sctag("input", xs_html_sctag("input",
xs_html_attr("type", "submit"), xs_html_attr("type", "submit"),
xs_html_attr("name", "action"), xs_html_attr("name", "action"),

View file

@ -21,6 +21,9 @@ xs_html *_xs_html_tag(char *tag, xs_html *var[]);
xs_html *_xs_html_sctag(char *tag, xs_html *var[]); xs_html *_xs_html_sctag(char *tag, xs_html *var[]);
#define xs_html_sctag(tag, ...) _xs_html_sctag(tag, (xs_html *[]) { __VA_ARGS__, NULL }) #define xs_html_sctag(tag, ...) _xs_html_sctag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
xs_html *_xs_html_container(xs_html *var[]);
#define xs_html_container(...) _xs_html_container((xs_html *[]) { __VA_ARGS__, NULL })
void xs_html_render_f(xs_html *h, FILE *f); void xs_html_render_f(xs_html *h, FILE *f);
xs_str *xs_html_render_s(xs_html *tag, char *prefix); xs_str *xs_html_render_s(xs_html *tag, char *prefix);
#define xs_html_render(tag) xs_html_render_s(tag, NULL) #define xs_html_render(tag) xs_html_render_s(tag, NULL)
@ -31,6 +34,7 @@ xs_str *xs_html_render_s(xs_html *tag, char *prefix);
typedef enum { typedef enum {
XS_HTML_TAG, XS_HTML_TAG,
XS_HTML_SCTAG, XS_HTML_SCTAG,
XS_HTML_CONTAINER,
XS_HTML_ATTR, XS_HTML_ATTR,
XS_HTML_TEXT XS_HTML_TEXT
} xs_html_type; } xs_html_type;
@ -75,9 +79,7 @@ xs_str *xs_html_encode(char *str)
/* insert the escaped char */ /* insert the escaped char */
char tmp[8]; char tmp[8];
snprintf(tmp, sizeof(tmp), "&#%d;", *q); z = snprintf(tmp, sizeof(tmp), "&#%d;", *q);
z = strlen(tmp);
s = xs_insert_m(s, o, tmp, z); s = xs_insert_m(s, o, tmp, z);
o += z; o += z;
@ -192,6 +194,12 @@ xs_html *_xs_html_sctag(char *tag, xs_html *var[])
} }
xs_html *_xs_html_container(xs_html *var[])
{
return _xs_html_tag_t(XS_HTML_CONTAINER, NULL, var);
}
void xs_html_render_f(xs_html *h, FILE *f) void xs_html_render_f(xs_html *h, FILE *f)
/* renders the tag and its subtags into a file */ /* renders the tag and its subtags into a file */
{ {
@ -200,7 +208,6 @@ void xs_html_render_f(xs_html *h, FILE *f)
switch (h->type) { switch (h->type) {
case XS_HTML_TAG: case XS_HTML_TAG:
case XS_HTML_SCTAG: case XS_HTML_SCTAG:
if (h->content)
fprintf(f, "<%s", h->content); fprintf(f, "<%s", h->content);
/* render the attributes */ /* render the attributes */
@ -213,11 +220,9 @@ void xs_html_render_f(xs_html *h, FILE *f)
if (h->type == XS_HTML_SCTAG) { if (h->type == XS_HTML_SCTAG) {
/* self-closing tags should not have subtags */ /* self-closing tags should not have subtags */
if (h->content)
fprintf(f, "/>"); fprintf(f, "/>");
} }
else { else {
if (h->content)
fprintf(f, ">"); fprintf(f, ">");
/* render the subtags */ /* render the subtags */
@ -228,12 +233,22 @@ void xs_html_render_f(xs_html *h, FILE *f)
st = nst; st = nst;
} }
if (h->content)
fprintf(f, "</%s>", h->content); fprintf(f, "</%s>", h->content);
} }
break; break;
case XS_HTML_CONTAINER:
/* render the subtags and nothing more */
st = h->f_tag;
while (st) {
xs_html *nst = st->next;
xs_html_render_f(st, f);
st = nst;
}
break;
case XS_HTML_ATTR: case XS_HTML_ATTR:
fprintf(f, " %s", h->content); fprintf(f, " %s", h->content);
break; break;

View file

@ -1 +1 @@
/* b26300d01136fad22ee774d20a7da5b299f30d13 2023-12-03T11:38:09+01:00 */ /* f27e092c79ca6e2e96f83e0d4c3dbc73d737ffaa 2023-12-03T17:12:47+01:00 */