|
cwist
Modern C web framework with libttak runtime
|
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>
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. | |
Minimal HTML tree construction and rendering helpers.
Definition in file builder.c.
| 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.
| el | Element to update. |
| key | Attribute name, for example "class". |
| value | Attribute 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().
| 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.
| el | Parent element that will own the child. |
| child | Child node to append. |
Definition at line 124 of file builder.c.
References cwist_html_element::child_count, cwist_html_element::children, and cwist_realloc().
| 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.
| el | Element to update. |
| class_name | Class 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_t * cwist_html_element_create | ( | const char * | tag | ) |
Allocate a new HTML element node for the supplied tag name.
Create a new HTML element.
| tag | Tag name to copy into the node, such as "div" or "span". |
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.
| 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.
| el | Element 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().
| 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.
| el | Element to update. |
| id | Identifier value to assign. |
Definition at line 76 of file builder.c.
References cwist_html_element_add_attr().
| void cwist_html_element_set_text | ( | cwist_html_element_t * | el, |
| const char * | text | ||
| ) |
Replace the element's inner text payload.
Set inner text.
| el | Element to update. |
| text | Plain-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_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.
| el | Root element to serialise. |
Definition at line 219 of file builder.c.
References cwist_sstring_assign(), and cwist_sstring_create().