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

HTML element builder and renderer. More...

#include <cwist/core/sstring/sstring.h>
#include <cjson/cJSON.h>
Include dependency graph for builder.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  cwist_html_element
 

Typedefs

typedef struct cwist_html_element cwist_html_element_t
 

Functions

Element creation and destruction
cwist_html_element_t * cwist_html_element_create (const char *tag)
 Create a new HTML element.
 
void cwist_html_element_destroy (cwist_html_element_t *el)
 Destroy an HTML element and its children.
 
Attribute manipulation
void cwist_html_element_add_attr (cwist_html_element_t *el, const char *key, const char *value)
 Add an attribute to an element.
 
void cwist_html_element_set_id (cwist_html_element_t *el, const char *id)
 Set the id attribute.
 
void cwist_html_element_add_class (cwist_html_element_t *el, const char *class_name)
 Add a CSS class.
 
Content and children
void cwist_html_element_set_text (cwist_html_element_t *el, const char *text)
 Set inner text.
 
void cwist_html_element_add_child (cwist_html_element_t *el, cwist_html_element_t *child)
 Add a child element.
 
Rendering
cwist_sstring * cwist_html_render (cwist_html_element_t *el)
 Render the element tree to an HTML string.
 

Detailed Description

HTML element builder and renderer.

Definition in file builder.h.

Typedef Documentation

◆ cwist_html_element_t

Function Documentation

◆ cwist_html_element_add_attr()

void cwist_html_element_add_attr ( cwist_html_element_t *  el,
const char *  key,
const char *  value 
)

Add an attribute to 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 
)

Add a child element.

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 
)

Add a CSS class.

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)

Create a new HTML element.

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)

◆ cwist_html_element_set_id()

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

Set the id attribute.

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 
)

Set inner text.

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 the element tree to an HTML string.

Void elements (area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr; any letter case) are written as a start tag only: they get no end tag, and text or children attached to them are not rendered.

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().