ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
StyleParser.hpp File Reference

CSS-like stylesheet parser and applier. More...

#include "Common.hpp"
#include "Style.hpp"
#include "Theme.hpp"
#include "Media.hpp"
#include "Diagnostics.hpp"
#include <filesystem>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

Go to the source code of this file.

Classes

struct  ZenitUI::ZMarkup::Declaration
 One parsed key: value; declaration, with source location. More...
struct  ZenitUI::ZMarkup::StyleSheet
 Intermediate representation: parsed rules, keyframes and root vars. More...
struct  ZenitUI::ZMarkup::StyleSheet::Rule
 One selector-plus-declarations entry. More...
struct  ZenitUI::ZMarkup::StyleSheet::KfFrame
 One from/to/N% frame inside @keyframes. More...
struct  ZenitUI::ZMarkup::StyleSheet::KfRule
 One @keyframes name { ... } block. More...
class  ZenitUI::ZMarkup::StyleParser
 Hand-written recursive-descent CSS-like parser. More...
struct  ZenitUI::ZMarkup::StyleLintOptions
 Options controlling static semantic linting in lintStyleSheet(). More...

Functions

std::string_view ZenitUI::ZMarkup::trim (std::string_view s)
 Trim leading and trailing ASCII whitespace.
std::string ZenitUI::ZMarkup::stripComments (std::string_view src)
 Strip // line and block comments from src.
std::vector< std::string_view > ZenitUI::ZMarkup::splitBy (std::string_view s, char sep)
 Split s on every occurrence of sep.
std::vector< std::string_view > ZenitUI::ZMarkup::splitWs (std::string_view s)
 Split s on any ASCII whitespace.
float ZenitUI::ZMarkup::parseDurationSec (std::string_view s)
 Parse a CSS duration ("200ms" → 0.2, "1.5s" → 1.5).
void ZenitUI::ZMarkup::applyStyleDeclaration (Style &st, std::string_view keyRaw, std::string_view valRaw, const SourceSpan &loc, DiagnosticSink *sink=nullptr, bool strict=false)
 Apply one declaration to st.
void ZenitUI::ZMarkup::normalizeKeyframes (KeyframeAnimation &anim, const SourceSpan &loc)
 Fill missing properties in every keyframe from neighbouring frames.
void ZenitUI::ZMarkup::applyStyleSheet (const StyleSheet &sheet, Theme &theme, DiagnosticSink *sink=nullptr, bool strict=false)
 Apply a parsed StyleSheet to a Theme.
void ZenitUI::ZMarkup::lintStyleSheet (const StyleSheet &sheet, DiagnosticSink &sink, const StyleLintOptions &opts={})
 Run static semantic checks on a parsed StyleSheet.
bool ZenitUI::ZMarkup::loadStyleString (std::string_view src, Theme &theme, const std::string &fileName="<string>", DiagnosticSink *sink=nullptr, bool strict=false, const Registry *registry=nullptr)
 Parse src and apply it to theme.
bool ZenitUI::ZMarkup::loadStyleFile (const std::filesystem::path &path, Theme &theme, DiagnosticSink *sink=nullptr, bool strict=false, const Registry *registry=nullptr)
 Load a stylesheet from a file and apply it to theme.

Detailed Description

CSS-like stylesheet parser and applier.

Parses CSS-style source into a StyleSheet, then merges it into a Theme:

:root {
--accent: #4a90e2;
font: "Inter", sans-serif;
}
Button {
background: var(--accent);
padding: 8px 16px;
transition: background 0.2s ease;
}
Button:hover { background: #5aa0f2; }
Button:disabled { opacity: 0.5; }
@keyframes pulse {
from { opacity: 1; }
to { opacity: 0.5; }
}
@media (min-width: 800px) {
Button { padding: 12px 24px; }
}

Supported features:

  • Type / class / id selectors, universal *, compound selectors, and combinators: descendant (A B), direct child (A > B), adjacent sibling (A + B), and general sibling (A ~ B).
  • Pseudo-classes :hover, :pressed, :focus, :disabled, :checked, and negation :not(...) (including CSS4 selector lists like :not(.a, .b)).
  • Pseudo-elements ::part (e.g. Slider::knob).
  • :root for the global inherited style.
  • @keyframes with from, to, % frames.
  • @media (min-width: N) and (orientation: landscape) etc.
  • --custom-props and var(--name) resolution.
  • Property-level transitions and animations (see Style.hpp).

Not supported: @import, :nth-child(), @supports, nested :not().

Error handling: the parser produces diagnostics through an optional DiagnosticSink. Without a sink, warnings are logged via logWarn. loadStyleString / loadStyleFile return false if any error was reported (errors only — warnings do not fail the load).

Implementation lives in src/StyleParser.cpp (compiled into libZenitUI).