cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
builder.c File Reference

Minimal HTML tree construction and rendering helpers. More...

#include <cwist/core/html/builder.h>
#include <cwist/core/sstring/sstring.h>
#include <cwist/core/mem/alloc.h>
#include <cjson/cJSON.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
Include dependency graph for builder.c:

Go to the source code of this file.

Functions

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_destroy (cwist_html_element_t *el)
 Recursively destroy an element and every descendant it owns.
 
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_add_class (cwist_html_element_t *el, const char *class_name)
 Append a CSS class token to an element.
 
void cwist_html_element_set_text (cwist_html_element_t *el, const char *text)
 Replace the element's inner text payload.
 
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.
 
cwist_sstring * cwist_html_render (cwist_html_element_t *el)
 Render an HTML tree to a freshly allocated string buffer.
 

Detailed Description

Minimal HTML tree construction and rendering helpers.

Definition in file builder.c.

Function Documentation

◆ cwist_html_element_add_attr()

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.

Add an attribute to an element.

Parameters
elElement to update.
keyAttribute name, for example "class".
valueAttribute value stored as a cJSON string node.

Definition at line 61 of file builder.c.

References cwist_html_element::attributes.

Referenced by cwist_html_element_add_class(), cwist_html_element_set_id(), and cwist_http_response_add_oob().

◆ cwist_html_element_add_child()

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.

Add a child element.

Parameters
elParent element that will own the child.
childChild node to append.

Definition at line 124 of file builder.c.

References cwist_html_element::child_count, cwist_html_element::children, and cwist_realloc().

◆ cwist_html_element_add_class()

void cwist_html_element_add_class ( cwist_html_element_t *  el,
const char *  class_name 
)

Append a CSS class token to an element.

Add a CSS class.

Parameters
elElement to update.
class_nameClass token to append or initialise.

Definition at line 85 of file builder.c.

References cwist_html_element::attributes, cwist_html_element_add_attr(), cwist_sstring_append(), cwist_sstring_assign(), cwist_sstring_create(), cwist_sstring_destroy(), and cwist_sstring::data.

◆ cwist_html_element_create()

cwist_html_element_t * cwist_html_element_create ( const char *  tag)

Allocate a new HTML element node for the supplied tag name.

Create a new HTML element.

Parameters
tagTag name to copy into the node, such as "div" or "span".
Returns
Newly allocated element, or NULL when allocation fails.

Definition at line 19 of file builder.c.

References cwist_html_element::attributes, cwist_html_element::child_count, cwist_html_element::children, cwist_alloc(), cwist_sstring_assign(), cwist_sstring_create(), cwist_html_element::inner_text, and cwist_html_element::tag.

◆ cwist_html_element_destroy()

void cwist_html_element_destroy ( cwist_html_element_t *  el)

Recursively destroy an element and every descendant it owns.

Destroy an HTML element and its children.

Parameters
elElement tree root to release. NULL is ignored.

Definition at line 37 of file builder.c.

References cwist_html_element::attributes, cwist_html_element::child_count, cwist_html_element::children, cwist_free(), cwist_html_element_destroy(), cwist_sstring_destroy(), cwist_html_element::inner_text, and cwist_html_element::tag.

Referenced by cwist_html_component_instantiate(), cwist_html_element_destroy(), cwist_http_response_add_oob(), cwist_http_response_set_html(), and cwist_http_response_set_view().

◆ cwist_html_element_set_id()

void cwist_html_element_set_id ( cwist_html_element_t *  el,
const char *  id 
)

Set the DOM id attribute for an element.

Set the id attribute.

Parameters
elElement to update.
idIdentifier value to assign.

Definition at line 76 of file builder.c.

References cwist_html_element_add_attr().

◆ cwist_html_element_set_text()

void cwist_html_element_set_text ( cwist_html_element_t *  el,
const char *  text 
)

Replace the element's inner text payload.

Set inner text.

Parameters
elElement to update.
textPlain-text payload to store.

Definition at line 108 of file builder.c.

References cwist_sstring_assign(), cwist_sstring_create(), and cwist_html_element::inner_text.

◆ cwist_html_render()

cwist_sstring * cwist_html_render ( cwist_html_element_t *  el)

Render an HTML tree to a freshly allocated string buffer.

Render the element tree to an HTML string.

Parameters
elRoot element to serialise.
Returns
Rendered HTML buffer, or NULL when the root is invalid.

Definition at line 219 of file builder.c.

References cwist_sstring_assign(), and cwist_sstring_create().