CSS¶
Version: 1.4.0
Path:
source/lexbor/cssBase Includes:
lexbor/css/css.hExamples:
examples/lexbor/cssSpecification: CSS
Overview¶
CSS itself is a modular system. Unlike a single monolithic specification, CSS is defined as a collection of independent modules, each covering a specific aspect of styling: syntax, selectors, colors, layout, fonts, text, and many others. Each CSS module has its own specification, its own level (version), and evolves at its own pace. The W3C actively develops and extends these modules, so the CSS landscape is constantly growing.
The lexbor CSS module mirrors this modular approach. It is a CSS parser that processes CSS text — stylesheets, individual rules, declarations, selectors, and values — and builds corresponding in-memory data structures. Support for CSS specification modules is being actively developed; new modules and properties are added regularly.
Important: This module handles parsing only. It builds CSS data structures (rule trees, selector lists, declaration lists) from CSS text. If you need to find HTML elements matching CSS selectors, see the Selectors module documentation.
Key Features¶
Specification Compliant — implements CSS Syntax Module Level 3
CSS Selectors Level 4 — full parsing support for CSS Selectors
Modular Design — parser, selectors, properties, and at-rules are separate subsystems
Stylesheet Parsing — parses complete CSS stylesheets into a rule tree (CSSOM)
Property Parsing — parses 100+ CSS properties with typed values
Serialization — serialize any CSS structure back to text
Error Logging — detailed parse error reporting
Memory Efficient — shared memory pools, parser reuse
Quick Start¶
Parse a Stylesheet¶
#include <lexbor/css/css.h>
static lxb_status_t
callback(const lxb_char_t *data, size_t len, void *ctx)
{
printf("%.*s", (int) len, (const char *) data);
return LXB_STATUS_OK;
}
int main(void)
{
const lxb_char_t css[] = "div { color: red; font-size: 16px; }";
/* Create and initialize the parser */
lxb_css_parser_t *parser = lxb_css_parser_create();
lxb_status_t status = lxb_css_parser_init(parser, NULL);
if (status != LXB_STATUS_OK) {
return EXIT_FAILURE;
}
/* Create a stylesheet object */
lxb_css_stylesheet_t *sst = lxb_css_stylesheet_create(NULL);
if (sst == NULL) {
lxb_css_parser_destroy(parser, true);
return EXIT_FAILURE;
}
/* Parse CSS into the stylesheet */
status = lxb_css_stylesheet_parse(sst, parser, css, sizeof(css) - 1);
if (status != LXB_STATUS_OK) {
lxb_css_stylesheet_destroy(sst, true);
lxb_css_parser_destroy(parser, true);
return EXIT_FAILURE;
}
/* Serialize the rule tree back to text */
lxb_css_rule_serialize(sst->root, callback, NULL);
printf("\n");
/* Clean up */
lxb_css_stylesheet_destroy(sst, true);
lxb_css_parser_destroy(parser, true);
return EXIT_SUCCESS;
}
Output:
div {color: red; font-size: 16px}
CSS Specification Modules¶
The table below lists CSS specification modules and their support status in lexbor. The modules marked as supported have full or near-complete parsing implementation. Work on adding support for new modules is ongoing.
Supported¶
CSS Module |
Specification |
What It Covers |
|---|---|---|
CSS Syntax |
Tokenization and grammar rules — the foundation for all other modules |
|
CSS Selectors |
Selector parsing: type, class, ID, attribute, pseudo-classes, pseudo-elements, combinators |
|
CSS Cascading and Inheritance |
Keywords |
|
CSS Color |
Color values: named colors, hex, |
|
CSS Values and Units |
Lengths, angles, durations, frequencies, resolutions, percentages |
|
CSS Box Model |
|
|
CSS Flexible Box Layout |
|
|
CSS Positioned Layout |
|
|
CSS Fonts |
|
|
CSS Text |
|
|
CSS Text Decoration |
|
|
CSS Writing Modes |
|
|
CSS Overflow |
|
|
CSS Inline Layout |
|
|
CSS Display |
|
Not Yet Supported¶
These modules are not yet implemented but may be added in the future:
CSS Module |
Specification |
|---|---|
CSS Grid Layout |
|
CSS Multi-column Layout |
|
CSS Transitions |
|
CSS Animations |
|
CSS Transforms |
|
CSS Backgrounds and Borders |
|
CSS Images |
|
CSS Shapes |
|
CSS Containment |
|
CSS Custom Properties |
|
CSS Scroll Snap |
|
CSS Conditional Rules - @media |
|
CSS Fonts — @font-face |
|
CSS Namespaces — @namespace |