4#include <cjson/cJSON.h>
62 if (!el || !key || !value || !el->
attributes)
return;
64 if (cJSON_HasObjectItem(el->
attributes, key)) {
65 cJSON_ReplaceItemInObject(el->
attributes, key, cJSON_CreateString(value));
67 cJSON_AddStringToObject(el->
attributes, key, value);
86 if (!el || !class_name || !el->
attributes)
return;
88 cJSON *cls = cJSON_GetObjectItem(el->
attributes,
"class");
89 if (cls && cls->valuestring && *cls->valuestring) {
95 cJSON_ReplaceItemInObject(el->
attributes,
"class",
96 cJSON_CreateString(s->
data ? s->
data :
""));
110 const char *actual_text = text ? text :
"";
125 if (!el || !child)
return;
142static bool is_void_element(
const char *tag) {
143 static const char *
const void_tags[] = {
"area",
"base",
"br",
"col",
"embed",
"hr",
"img",
144 "input",
"link",
"meta",
"source",
"track",
"wbr"};
145 for (
size_t i = 0; i <
sizeof(void_tags) /
sizeof(void_tags[0]); i++) {
146 const char *a = tag, *b = void_tags[i];
147 while (*a && (*a | 0x20) == *b) {
151 if (*a ==
'\0' && *b ==
'\0')
return true;
162 if (!el || !out)
return;
169 if (!attr || !attr->string)
continue;
170 if (cJSON_IsString(attr) && attr->valuestring) {
176 }
else if (cJSON_IsTrue(attr)) {
179 }
else if (cJSON_IsNumber(attr)) {
181 snprintf(num_buf,
sizeof(num_buf),
"%g", attr->valuedouble);
193 if (is_void_element(el->
tag->
data))
return;
201 render_element(el->
children[i], out);
220 if (!el)
return NULL;
223 render_element(el, out);
Memory allocation wrappers backed by libttak.
void cwist_free(void *ptr)
Free memory obtained via cwist_alloc/cwist_strdup/etc.
void * cwist_realloc(void *ptr, size_t new_size)
Resize an existing allocation (libttak tracked).
void * cwist_alloc(size_t size)
Allocate zeroed memory tracked by libttak.
void cwist_html_element_add_child(cwist_html_element_t *el, cwist_html_element_t *child)
Append a child node to an element's ordered children list.
void cwist_html_element_add_attr(cwist_html_element_t *el, const char *key, const char *value)
Insert or replace an HTML attribute on an element.
void cwist_html_element_set_id(cwist_html_element_t *el, const char *id)
Set the DOM id attribute for an element.
void cwist_html_element_destroy(cwist_html_element_t *el)
Recursively destroy an element and every descendant it owns.
void cwist_html_element_add_class(cwist_html_element_t *el, const char *class_name)
Append a CSS class token to an element.
cwist_sstring * cwist_html_render(cwist_html_element_t *el)
Render an HTML tree to a freshly allocated string buffer.
cwist_html_element_t * cwist_html_element_create(const char *tag)
Allocate a new HTML element node for the supplied tag name.
void cwist_html_element_set_text(cwist_html_element_t *el, const char *text)
Replace the element's inner text payload.
HTML element builder and renderer.
Safe string type with helper methods.
cwist_sstring * cwist_sstring_create(void)
Create a new sstring instance.
cwist_error_t cwist_sstring_append(cwist_sstring *str, const char *data)
Append a C string.
cwist_error_t cwist_sstring_assign(cwist_sstring *str, const char *data)
Assign a C string.
void cwist_sstring_destroy(cwist_sstring *str)
Destroy an sstring instance.
cwist_error_t cwist_sstring_append_escaped(cwist_sstring *str, const char *data)
Append an escaped C string.
cwist_sstring * inner_text
struct cwist_html_element ** children
char * data
Access directly only when raw handling is necessary.