1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
.TH HTML_NODE_ADD_CHILD 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
html_node_add_child \- add child to a HTML node
.SH SYNOPSIS
.LP
.nf
#include <libweb/html.h>
.P
struct html_node *html_node_add_child(struct html_node *\fIn\fP, const char *\fIelem\fP);
.fi
.SH DESCRIPTION
The
.IR html_node_add_child (3)
function allocates a child
.I struct html_node
object into another
.I struct html_node
object, previously allocated by a call to
.IR html_node_alloc (3)
or
.IR html_node_add_child (3),
and pointed to by
.IR n .
The tag name for the child node is defined by
.IR elem .
.SH RETURN VALUE
On success,
.IR html_node_add_child (3)
returns zero. On failure, a negative integer is returned.
.SH EXAMPLE
The following example shall print a tree structure with a root node
and one child node:
.PP
.in +4n
.EX
#include <dynstr.h>
#include <libweb/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\en");
goto end;
}
else if (!(child = html_node_add_child(root, "child")))
{
fprintf(stderr, "html_node_add_child failed\en");
goto end;
}
else if (html_serialize(root, &d))
{
fprintf(stderr, "html_serialize failed\en");
goto end;
}
printf("%s", d.str);
ret = EXIT_SUCCESS;
end:
html_node_free(root);
dynstr_free(&d);
return ret;
}
.EE
.in
.PP
The following results shall be written to the standard output:
.PP
.in +4n
.EX
<root>
<child/>
</root>
.EE
.in
.PP
.SH ERRORS
No errors are defined.
.SH NOTES
Adding a child to an existing
.I struct html_node
object is an atomic operation, which means no changes are applied to it
if
.IR html_node_add_child (3)
fails.
Internally,
.I libweb
calls
.IR html_node_add_sibling (3)
from a child node when a node already has one.
.SH SEE ALSO
.BR html_node_alloc (3),
.BR html_node_add_sibling (3),
.BR libweb_http (7).
.SH COPYRIGHT
Copyright (C) 2023-2024 libweb contributors
.P
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.
|