aboutsummaryrefslogtreecommitdiff
path: root/html.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-09-27 20:48:09 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-09-27 20:48:09 +0200
commitbdfbd076f2d3e7212f300bf5edbbad1934fae324 (patch)
tree1e89ea2cd4bfd4e36c74b88840b5c3b342a87339 /html.c
parent5a6f30440b66fe6713acb9d979dc3e6624e4c36a (diff)
html.c: Do not recurse on siblings
It is not required to do so. Otherwise, nodes with many siblings could lead to a very deep call stack for no reason.
Diffstat (limited to 'html.c')
-rw-r--r--html.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/html.c b/html.c
index 0277498..094b00c 100644
--- a/html.c
+++ b/html.c
@@ -20,6 +20,9 @@ struct html_node
struct html_node *child, *sibling;
};
+static int serialize_siblings(struct dynstr *, const struct html_node *,
+ unsigned);
+
static char *html_encode(const char *s)
{
struct dynstr d;
@@ -210,9 +213,9 @@ static int serialize_node(struct dynstr *const d,
{
dynstr_append_or_ret_nonzero(d, "\n");
- if (serialize_node(d, n->child, level + 1))
+ if (serialize_siblings(d, n->child, level + 1))
{
- fprintf(stderr, "%s: serialize_node failed\n", __func__);
+ fprintf(stderr, "%s: serialize_siblings failed\n", __func__);
return -1;
}
@@ -223,19 +226,30 @@ static int serialize_node(struct dynstr *const d,
dynstr_append_or_ret_nonzero(d, "</%s>", n->element);
}
- /* TODO: print siblings */
-
dynstr_append_or_ret_nonzero(d, "\n");
+ return 0;
+}
+
+static int serialize_siblings(struct dynstr *const d,
+ const struct html_node *n, const unsigned level)
+{
+ struct html_node *sibling;
+
+ do
+ {
+ sibling = n->sibling;
+
+ if (serialize_node(d, n, level))
+ return -1;
- if (n->sibling)
- return serialize_node(d, n->sibling, level);
+ } while ((n = sibling));
return 0;
}
int html_serialize(const struct html_node *const n, struct dynstr *const d)
{
- return serialize_node(d, n, 0);
+ return serialize_siblings(d, n, 0);
}
static void html_attribute_free(struct html_attribute *const a)