HTML_NODE_ADD_CHILD(3)

NAME
html_node_add_child - add child to a HTML node

SYNOPSIS

#include <slweb/html.h>

struct html_node *html_node_add_child(struct html_node *n, const char *elem);

DESCRIPTION
The html_node_add_child(3) function allocates a child struct html_node object into another struct html_node object, previously allocated by a call to html_node_alloc(3) or html_node_add_child(3), and pointed to by n. The tag name for the child node is defined by elem.

RETURN VALUE
On success, html_node_add_child(3) returns zero. On failure, a negative integer is returned.

EXAMPLE
The following example shall print a tree structure with a root node and one child node:

    #include <dynstr.h>
    #include <slweb/html.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
        int ret = EXIT_FAILURE;
        struct html_node *const root = html_node_alloc("root"), *child;
        struct dynstr d;
        dynstr_init(&d);
        if (!root)
        {
            fprintf(stderr, "html_node_alloc failed\n");
            goto end;
        }
        else if (!(child = html_node_add_child(root, "child")))
        {
            fprintf(stderr, "html_node_add_child failed\n");
            goto end;
        }
        else if (html_serialize(root, &d))
        {
            fprintf(stderr, "html_serialize failed\n");
            goto end;
        }
        printf("%s", d.str);
        ret = EXIT_SUCCESS;
    end:
        html_node_free(root);
        dynstr_free(&d);
        return ret;
    }

The following results shall be written to the standard output:

    <root>
        <child/>
    </root>

ERRORS
No errors are defined.

NOTES
Adding a child to an existing struct html_node object is an atomic operation, which means no changes are applied to it if html_node_add_child(3) fails. Internally, slweb calls html_node_add_sibling(3) from a child node when a node already has one.

SEE ALSO
html_node_alloc(3), html_node_add_sibling(3), slweb_http(7).

COPYRIGHT
Copyright (C) 2023 Xavier Del Campo Romero.

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.