Lines
100 %
Functions
85.19 %
Branches
//! Label widget for displaying static text with platform-specific default styling.
use azul_core::dom::{Dom, IdOrClass, IdOrClass::Class, IdOrClassVec};
use azul_css::dynamic_selector::{
CssPropertyWithConditions, CssPropertyWithConditionsVec, OptionCssPropertyWithConditionsVec,
};
#[allow(clippy::wildcard_imports)]
// widget/render module pulls in the css property/value types it builds with
use azul_css::{
props::{
basic::*,
layout::*,
property::{CssProperty, *},
style::*,
},
*,
/// A static text label widget with platform-appropriate default styling.
#[derive(Debug, Clone)]
#[repr(C)]
pub struct Label {
pub string: AzString,
/// The label's CSS, or `None` for "no opinion" — in which case the
/// platform default applies.
///
/// `None` and `Some(empty)` are different answers: the first means the
/// widget picks, the second means the caller asked for no properties at all
/// and gets none.
pub label_style: OptionCssPropertyWithConditionsVec,
}
const SANS_SERIF_STR: &str = "system:ui";
const SANS_SERIF: AzString = AzString::from_const_str(SANS_SERIF_STR);
const SANS_SERIF_FAMILIES: &[StyleFontFamily] = &[StyleFontFamily::System(SANS_SERIF)];
const SANS_SERIF_FAMILY: StyleFontFamilyVec =
StyleFontFamilyVec::from_const_slice(SANS_SERIF_FAMILIES);
/// Standard label text color (#4C4C4C), matching platform UI defaults.
const COLOR_4C4C4C: ColorU = ColorU {
r: 76,
g: 76,
b: 76,
a: 255,
static LABEL_STYLE_DEFAULT: &[CssPropertyWithConditions] = &[
CssPropertyWithConditions::simple(CssProperty::const_display(LayoutDisplay::Flex)),
CssPropertyWithConditions::simple(CssProperty::const_flex_direction(
LayoutFlexDirection::Column,
)),
CssPropertyWithConditions::simple(CssProperty::const_justify_content(
LayoutJustifyContent::Center,
CssPropertyWithConditions::simple(CssProperty::const_align_items(LayoutAlignItems::Center)),
CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(1))),
CssPropertyWithConditions::simple(CssProperty::const_text_color(StyleTextColor {
inner: COLOR_4C4C4C,
})),
CssPropertyWithConditions::simple(CssProperty::const_font_size(StyleFontSize::const_px(13))),
CssPropertyWithConditions::simple(CssProperty::const_text_align(StyleTextAlign::Center)),
CssPropertyWithConditions::simple(CssProperty::const_font_family(SANS_SERIF_FAMILY)),
];
static LABEL_STYLE_MAC: &[CssPropertyWithConditions] = &[
CssPropertyWithConditions::simple(CssProperty::const_font_size(StyleFontSize::const_px(12))),
/// No default styling on unsupported platforms (e.g. WASM, FreeBSD);
/// callers should provide explicit styles via `label_style`.
static LABEL_STYLE_OTHER: &[CssPropertyWithConditions] = &[];
impl Label {
/// Creates a new label with the given text and platform-specific default styling.
#[inline]
#[must_use]
pub const fn create(string: AzString) -> Self {
Self {
string,
label_style: OptionCssPropertyWithConditionsVec::None,
/// The label CSS this widget renders with.
/// `None` means no opinion, so the platform default applies — the same
/// answer both themes give, asked in one place so they cannot drift. The
/// `cfg` fan-out that used to sit in the constructor lives here instead,
/// which is why a hand-built `Label` no longer has to know which platform
/// it was compiled for to look right.
pub fn resolved_label_style(&self) -> CssPropertyWithConditionsVec {
self.label_style.clone().into_option().unwrap_or_else(|| {
#[cfg(any(target_os = "windows", target_os = "linux"))]
let default = LABEL_STYLE_DEFAULT;
#[cfg(target_os = "macos")]
let default = LABEL_STYLE_MAC;
#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
let default = LABEL_STYLE_OTHER;
CssPropertyWithConditionsVec::from_const_slice(default)
})
/// Replaces `self` with an empty default label, returning the original.
pub const fn swap_with_default(&mut self) -> Self {
let mut s = Self::create(AzString::from_const_str(""));
core::mem::swap(&mut s, self);
s
/// Converts this label into a `<p>` block carrying the
/// `__azul-native-label` class and wrapping a bare text node.
/// The `<p>` is the styled box: a `NodeType::Text` node is always
/// inline-level and owns no rect, so every box-model property here would
/// be inert on a raw text node.
pub fn dom(self) -> Dom {
static LABEL_CLASS: &[IdOrClass] =
&[Class(AzString::from_const_str("__azul-native-label"))];
// Resolved before `self.string` is moved out below.
let label_style = self.resolved_label_style();
crate::widgets::widget_p_with_text(self.string)
.with_ids_and_classes(IdOrClassVec::from_const_slice(LABEL_CLASS))
.with_css_props(label_style)
impl From<Label> for Dom {
fn from(l: Label) -> Self {
l.dom()
#[cfg(test)]
mod autotest_generated {
use std::collections::HashSet;
use azul_core::dom::NodeType;
use azul_css::props::basic::{length::SizeMetric, pixel::PixelValue};
use super::*;
// ------------------------------------------------------------------
// Helpers
/// The number of declarations each *populated* platform table carries.
const DECL_COUNT: usize = 9;
/// The class `dom()` stamps onto the label `<p>`.
const LABEL_CLASS_NAME: &str = "__azul-native-label";
/// The style table `Label::create` is expected to pick on *this* target.
/// Uses `cfg!` rather than `#[cfg]` so every branch still type-checks on
/// every platform — a table that stopped compiling on macOS would otherwise
/// only be caught by a macOS CI run.
fn expected_table() -> &'static [CssPropertyWithConditions] {
if cfg!(target_os = "macos") {
LABEL_STYLE_MAC
} else if cfg!(any(target_os = "windows", target_os = "linux")) {
LABEL_STYLE_DEFAULT
} else {
LABEL_STYLE_OTHER
/// The font size `Label::create` bakes in on this target — `None` on the
/// platforms that are documented to get no default styling at all.
fn expected_font_size() -> Option<f32> {
Some(12.0)
Some(13.0)
None
/// True when this target is one of the platforms that gets a real table.
fn target_is_styled() -> bool {
!expected_table().is_empty()
/// The declared properties of a style vec, in declaration order.
fn properties(v: &CssPropertyWithConditionsVec) -> Vec<CssProperty> {
v.as_ref().iter().map(|p| p.property.clone()).collect()
/// The `f32` of a `PixelValue`, asserting it is an absolute `px` length. A
/// font size declared in `em`/`%` would resolve against the *parent* font
/// and make the "platform default" scale with whatever it is dropped into.
fn px(pv: &PixelValue) -> f32 {
assert_eq!(
pv.metric,
SizeMetric::Px,
"label lengths must be absolute px, got {:?}",
pv.metric
);
pv.number.get()
fn font_size_px(v: &CssPropertyWithConditionsVec) -> Option<f32> {
v.as_ref().iter().find_map(|p| match &p.property {
CssProperty::FontSize(f) => f.get_property().map(|f| px(&f.inner)),
_ => None,
fn text_color(v: &CssPropertyWithConditionsVec) -> Option<ColorU> {
CssProperty::TextColor(c) => c.get_property().map(|c| c.inner),
fn flex_grow(v: &CssPropertyWithConditionsVec) -> Option<f32> {
CssProperty::FlexGrow(f) => f.get_property().map(|f| f.inner.get()),
fn font_families(v: &CssPropertyWithConditionsVec) -> Option<Vec<StyleFontFamily>> {
CssProperty::FontFamily(f) => f.get_property().map(|f| f.as_ref().to_vec()),
/// True if `node` carries the CSS class `name`.
fn has_class(node: &Dom, name: &str) -> bool {
node.root
.get_ids_and_classes()
.as_ref()
.iter()
.any(|c| matches!(c, Class(s) if s.as_str() == name))
/// The properties of a rendered node's *inline* style, in declaration order.
fn inline_properties(node: &Dom) -> Vec<CssProperty> {
.style
.iter_inline_properties()
.map(|(p, _)| p.clone())
.collect()
/// The text carried by a text node, looking through the `<p>` block
/// wrapper the label convention mandates (`p > text`).
fn text_of(node: &Dom) -> Option<&str> {
match node.root.get_node_type() {
NodeType::Text(s) => Some(s.as_ref().as_str()),
NodeType::P => match node.children.as_ref() {
[only] => match only.root.get_node_type() {
/// `Label` derives neither `PartialEq` nor `Default`, so compare field-wise.
fn assert_same_label(a: &Label, b: &Label, ctx: &str) {
assert_eq!(a.string, b.string, "{ctx}: the string differs");
a.resolved_label_style(),
b.resolved_label_style(),
"{ctx}: the style differs"
/// Inputs a label must survive verbatim. Empty, whitespace-only, embedded
/// NUL, combining marks, ZWJ sequences, bidi overrides, strings that look
/// like this widget's own sentinels, and one very large allocation.
fn adversarial_strings() -> Vec<String> {
let mut v: Vec<String> = [
"",
"Label",
" ",
"\t\n\r",
"e\u{0301}", // e + combining acute
"\u{212B}", // ANGSTROM SIGN (NFC-folds to U+00C5)
"\u{C5}", // the folded form — must stay distinct
"\u{1F469}\u{200D}\u{1F469}\u{200D}\u{1F467}", // ZWJ family emoji
"\u{5E9}\u{5DC}\u{5D5}\u{5DD}", // RTL Hebrew
"\0", // a single NUL
"a\0b", // embedded NUL
"\u{FFFD}\u{202E}\u{200B}", // replacement, RTL override, ZWSP
"system:ui", // the font sentinel this file bakes in
"__azul-native-label", // this widget's own class name
"}\n#pwned { color: red; }", // CSS-injection shaped
"<div>&</div>", // markup shaped
"-9223372036854775808", // i64::MIN
]
.map(|s| (*s).to_string())
.collect();
v.push("x".repeat(200_000));
v.push("\0".repeat(1_000));
v
// Label::create (constructor)
#[test]
fn create_stores_every_adversarial_string_verbatim() {
// The string travels through `AzString` (an FFI `U8Vec`), so a
// C-string-style NUL truncation or a lossy UTF-8 round-trip would show
// up as a shortened or mangled label here first.
for s in adversarial_strings() {
let label = Label::create(AzString::from(s.clone()));
label.string.as_str(),
s.as_str(),
"the string was rewritten"
label.string.len(),
s.len(),
"byte length changed (NUL truncation?)"
label.string.chars().count(),
s.chars().count(),
"char count changed (lossy UTF-8 round-trip?)"
fn create_never_normalises_or_folds_the_string() {
// U+212B and U+00C5 render identically but are different code points;
// a normalisation pass hidden in the string plumbing would silently
// merge them and break byte-exact round-trips through FFI.
let angstrom = Label::create(AzString::from("\u{212B}".to_string()));
let a_ring = Label::create(AzString::from("\u{C5}".to_string()));
assert_ne!(
angstrom.string, a_ring.string,
"the two forms were normalised into one"
angstrom.string.len(),
3,
"U+212B must stay a 3-byte sequence"
assert_eq!(a_ring.string.len(), 2, "U+00C5 must stay a 2-byte sequence");
fn create_uses_the_platform_table_for_this_target() {
let label = Label::create(AzString::from_const_str("hello"));
properties(&label.resolved_label_style()),
expected_table()
.map(|p| p.property.clone())
.collect::<Vec<_>>(),
"the constructor picked the wrong platform style table"
font_size_px(&label.resolved_label_style()),
expected_font_size()
fn create_is_deterministic_and_independent_of_the_string() {
// The style must not depend on the text: two labels built from wildly
// different strings must carry byte-identical styling.
let a = Label::create(AzString::from_const_str(""));
let b = Label::create(AzString::from("x".repeat(100_000)));
"the style varies with the label text"
assert_same_label(
&a,
&Label::create(AzString::from_const_str("")),
"repeat construction",
fn constructed_style_vec_has_consistent_length_and_capacity() {
let v = Label::create(AzString::from_const_str("hi")).resolved_label_style();
v.len(),
v.as_ref().len(),
"len() disagrees with the slice view"
assert!(
v.capacity() >= v.len(),
"capacity {} < len {}",
v.capacity(),
v.len()
if target_is_styled() {
assert_eq!(v.len(), DECL_COUNT, "unexpected number of declarations");
v.is_empty(),
"unsupported platforms are documented to get no styling"
fn cloning_and_dropping_never_corrupts_the_shared_static_style() {
// `label_style` borrows a `'static` slice (`NoDestructor`) while
// `string` may be heap-owned. A clone that wrongly claims ownership of
// the static, or a `Drop` that frees it, would corrupt every future
// label — so churn hard and re-check both the original and a fresh build.
let base = Label::create(AzString::from("churn \u{1F600}".to_string()));
let expected_props = properties(&base.resolved_label_style());
for round in 0..1000 {
let c = base.clone();
c.string.as_str(),
"churn \u{1F600}",
"clone {round}: the string diverged"
properties(&c.resolved_label_style()),
expected_props,
"clone {round}: the style diverged"
drop(c);
base.string.as_str(),
"the original string was damaged"
properties(&base.resolved_label_style()),
"the original style was damaged"
properties(&Label::create(AzString::from_const_str("x")).resolved_label_style()),
"a freshly built label disagrees after 1000 clone/drop cycles"
fn a_const_str_label_survives_clone_churn_of_its_static_backing() {
// `AzString::from_const_str` is `NoDestructor`-backed; `clone_self`
// copies it. A shallow clone plus a `Drop` that freed the `'static`
// would be a use-after-free the very next read.
let base = Label::create(AzString::from_const_str(LABEL_CLASS_NAME));
LABEL_CLASS_NAME,
"clone {round}: static backing corrupted"
assert_eq!(base.string.as_str(), LABEL_CLASS_NAME);
// The platform style tables
fn the_two_populated_tables_differ_only_in_font_size() {
// They are hand-duplicated blocks: a fix applied to one and not the
// other is the failure mode this file is most exposed to.
assert_eq!(LABEL_STYLE_DEFAULT.len(), DECL_COUNT);
LABEL_STYLE_MAC.len(),
DECL_COUNT,
"the two tables have diverged in length"
for (i, (d, m)) in LABEL_STYLE_DEFAULT
.zip(LABEL_STYLE_MAC.iter())
.enumerate()
{
core::mem::discriminant(&d.property),
core::mem::discriminant(&m.property),
"declaration {i} is a different property on the two platforms"
if matches!(d.property, CssProperty::FontSize(_)) {
d.property, m.property,
"the two tables were expected to differ in font size"
"declaration {i} drifted between the two tables"
fn label_style_other_is_empty_as_documented() {
// The doc comment promises "no default styling on unsupported
// platforms"; a stray declaration here would style WASM/FreeBSD
// differently from everything the other two tables were tuned against.
LABEL_STYLE_OTHER.is_empty(),
"the fallback table is no longer empty"
fn every_declaration_is_unconditional() {
// A label is stateless — a declaration gated on `:hover`/`:active`
// would simply never paint.
for (name, table) in [("default", LABEL_STYLE_DEFAULT), ("mac", LABEL_STYLE_MAC)] {
for p in table {
p.apply_if.as_ref().is_empty(),
"{name}: {:?} is conditional on a stateless widget",
p.property
fn no_property_is_declared_twice() {
// A duplicated declaration is a last-one-wins ambiguity: two font sizes
// would make one of them silently dead.
let mut seen = HashSet::new();
seen.insert(core::mem::discriminant(&p.property)),
"{name}: duplicate declaration of {:?}",
assert_eq!(seen.len(), table.len());
fn the_text_colour_is_the_documented_opaque_grey() {
// The doc comment pins it to #4C4C4C; a translucent label would let the
// background bleed through the glyphs.
COLOR_4C4C4C,
ColorU {
a: 255
COLOR_4C4C4C.a, 255,
"a translucent label lets the background bleed through"
COLOR_4C4C4C.r, COLOR_4C4C4C.g,
"the label colour is not neutral grey"
COLOR_4C4C4C.g, COLOR_4C4C4C.b,
let v = CssPropertyWithConditionsVec::from_const_slice(table);
text_color(&v),
Some(COLOR_4C4C4C),
"{name}: wrong text colour"
fn every_font_size_is_a_finite_positive_absolute_px() {
// Guard the `isize` -> `PixelValue` fixed-point conversion: a NaN, an
// infinity, a zero or a negative size must never reach the shaper.
for (name, table, want) in [
("default", LABEL_STYLE_DEFAULT, 13.0),
("mac", LABEL_STYLE_MAC, 12.0),
] {
let size = font_size_px(&v).expect("a label must declare a font size");
assert!(size.is_finite(), "{name}: non-finite font size {size}");
assert!(!size.is_nan(), "{name}: NaN font size");
assert!(size > 0.0, "{name}: a {size}px label is invisible");
size <= 128.0,
"{name}: {size}px is implausible for a UI label"
assert_eq!(size, want, "{name}: unexpected font size");
fn the_fixed_point_length_encoding_round_trips() {
// encode == decode for every numeric constant this file bakes in.
assert_eq!(PixelValue::const_px(13).number.get(), 13.0);
assert_eq!(PixelValue::const_px(12).number.get(), 12.0);
assert_eq!(StyleFontSize::const_px(13).inner.number.get(), 13.0);
assert_eq!(StyleFontSize::const_px(12).inner.number.get(), 12.0);
assert_eq!(LayoutFlexGrow::const_new(1).inner.get(), 1.0);
// ...and the values that actually landed in the tables are the ones the
// declarations asked for.
let d: Vec<CssProperty> = LABEL_STYLE_DEFAULT
assert!(d.contains(&CssProperty::const_font_size(StyleFontSize::const_px(13))));
let m: Vec<CssProperty> = LABEL_STYLE_MAC.iter().map(|p| p.property.clone()).collect();
assert!(m.contains(&CssProperty::const_font_size(StyleFontSize::const_px(12))));
fn flex_grow_is_exactly_one_and_not_a_rounding_artefact() {
// `FloatValue` stores a fixed-point `isize`; a botched encode/decode
// would show up as 0.999 or -0.0 rather than a clean 1.
let g = flex_grow(&v).expect("flex-grow must be declared");
assert!(g.is_finite(), "{name}: non-finite flex-grow {g}");
assert_eq!(g, 1.0, "{name}: flex-grow is {g}, not 1");
assert!(g.is_sign_positive(), "{name}: flex-grow decoded as -0.0");
fn the_font_family_is_a_single_system_ui_entry() {
// `SANS_SERIF` / `SANS_SERIF_FAMILY` are `const`, so each mention
// materialises a fresh value — bind them once and read them back.
let sentinel: AzString = SANS_SERIF;
let family_vec: StyleFontFamilyVec = SANS_SERIF_FAMILY;
assert_eq!(SANS_SERIF_STR, "system:ui");
sentinel.as_str(),
SANS_SERIF_STR,
"the const AzString lost its backing str"
assert_eq!(sentinel.as_str().len(), SANS_SERIF_STR.len());
SANS_SERIF_FAMILIES.len(),
1,
"the fallback chain changed length"
family_vec.as_ref().len(),
"the const vec disagrees with its slice"
let fams = font_families(&v).expect("a label must declare a font family");
assert_eq!(fams.len(), 1, "{name}: unexpected fallback chain length");
match &fams[0] {
// Pinned as-is: this file spells the UI font as a *named*
// family whose name happens to be the `system:ui` sentinel,
// not as `StyleFontFamily::SystemType(SystemFontType::Ui)`.
StyleFontFamily::System(s) => {
assert_eq!(s.as_str(), "system:ui", "{name}: wrong family name")
other => panic!("{name}: unexpected font family variant {other:?}"),
fn the_centering_declarations_are_mutually_consistent() {
// A label centres its text both ways; each half of that contract lives
// in a different declaration, so assert them together.
let props: Vec<CssProperty> = table.iter().map(|p| p.property.clone()).collect();
let has = |p: &CssProperty| props.contains(p);
has(&CssProperty::const_display(LayoutDisplay::Flex)),
"{name}: not a flex box"
has(&CssProperty::const_flex_direction(
LayoutFlexDirection::Column
"{name}: not a column"
has(&CssProperty::const_justify_content(
LayoutJustifyContent::Center
"{name}: not centred on the main axis"
has(&CssProperty::const_align_items(LayoutAlignItems::Center)),
"{name}: not centred on the cross axis"
has(&CssProperty::const_text_align(StyleTextAlign::Center)),
"{name}: the text itself is not centred"
// Label::swap_with_default
fn swap_with_default_returns_the_original_and_leaves_an_empty_label() {
let mut label = Label::create(AzString::from("original \u{1F600}".to_string()));
let expected_style = label.resolved_label_style();
let taken = label.swap_with_default();
// The returned value is the *original*, intact.
taken.string.as_str(),
"original \u{1F600}",
"the wrong value was returned"
taken.resolved_label_style(),
expected_style,
"the returned label lost its style"
// What is left behind is a freshly created empty label — not a hollowed
// out husk with a dangling string or an empty style vec.
"what was left behind is not empty"
assert!(label.string.is_empty());
&label,
"left-behind label",
fn swap_with_default_is_idempotent_on_an_already_empty_label() {
let mut label = Label::create(AzString::from_const_str(""));
let first = label.swap_with_default();
let second = label.swap_with_default();
assert_eq!(first.string.as_str(), "");
assert_eq!(second.string.as_str(), "");
"after two swaps",
fn repeated_swaps_never_corrupt_the_static_backed_style() {
// `mem::swap` moves a vec that borrows a `'static` slice; 200 rounds of
// swap-and-drop would surface a double free or a dangling `ptr`.
let mut label = Label::create(AzString::from("swap me".to_string()));
for round in 0..200 {
if round == 0 {
"swap me",
"round 0: wrong value returned"
"round {round}: the emptied slot was not empty"
properties(&taken.resolved_label_style()),
"round {round}"
"round {round}: what was left behind is not empty"
drop(taken);
"after 200 swaps",
fn swap_with_default_returns_a_custom_style_untouched() {
// Both fields are `pub`, so a caller can hand-build a label. The swap
// must hand that style back rather than rewriting it on the way out.
let custom =
CssPropertyWithConditionsVec::from_vec(vec![CssPropertyWithConditions::simple(
CssProperty::const_font_size(StyleFontSize::const_px(42)),
)]);
let mut label = Label {
string: AzString::from_const_str("custom"),
label_style: OptionCssPropertyWithConditionsVec::Some(custom),
assert_eq!(taken.string.as_str(), "custom");
taken.resolved_label_style().len(),
"the custom style was rewritten on the way out"
assert_eq!(font_size_px(&taken.resolved_label_style()), Some(42.0));
// ...and the slot is refilled with the platform default, not the custom one.
expected_font_size(),
"the 42px override survived"
fn swap_with_default_hands_back_a_huge_string_without_truncating_it() {
let huge = "\u{1F600}".repeat(50_000);
let mut label = Label::create(AzString::from(huge.clone()));
taken.string.len(),
huge.len(),
"the huge string was truncated on the way out"
assert_eq!(taken.string.as_str(), huge.as_str());
// Label::dom (round-trip: label -> DOM)
fn dom_is_a_single_classed_text_node_carrying_the_computed_style() {
let label = Label::create(AzString::from_const_str("Hello"));
let expected = properties(&label.resolved_label_style());
let dom = label.dom();
text_of(&dom),
Some("Hello"),
"the label is not a text node, or was mangled"
has_class(&dom, LABEL_CLASS_NAME),
"missing the widget class"
dom.root.get_ids_and_classes().as_ref().len(),
"expected exactly one class and no ids"
dom.children.as_ref().len(),
"a label is a <p> wrapping exactly one bare text node"
dom.children.as_ref()[0].children.as_ref().is_empty(),
"the text node itself is a leaf, not a subtree"
dom.estimated_total_children, 1,
"the <p> owns exactly its text node"
// The ONLY sheet a <p> widget carries is the UA-margin reset
// (`widgets::widget_p_margin_reset`), scoped to the node itself.
dom.css.as_ref().len(),
"a label attaches exactly the <p> margin reset, nothing else"
dom.root.callbacks.as_ref().is_empty(),
"a static widget must not bind callbacks"
inline_properties(&dom),
expected,
"the label lost its computed style"
fn dom_preserves_adversarial_labels_verbatim() {
let dom = Label::create(AzString::from(s.clone())).dom();
let t = text_of(&dom).expect("expected a text node");
assert_eq!(t, s.as_str(), "the label changed on its way into the DOM");
assert_eq!(t.len(), s.len(), "byte length changed (NUL truncation?)");
"the class was lost for {s:?}"
// The text must never leak into the style, and the style must never
// vary with the text.
inline_properties(&dom).len(),
expected_table().len(),
"style varies with the text"
fn dom_of_an_empty_label_is_still_a_classed_text_node() {
// The empty label is what `swap_with_default` leaves behind, so it is
// the one input guaranteed to be rendered somewhere.
let dom = Label::create(AzString::from_const_str("")).dom();
Some(""),
"the empty label is not a text node"
assert!(has_class(&dom, LABEL_CLASS_NAME));
assert_eq!(inline_properties(&dom).len(), expected_table().len());
fn dom_renders_the_style_field_verbatim_even_when_hand_built() {
// `dom()` consumes `label_style` as-is. A hand-built label must be
// rendered with exactly the style it was given — no re-derivation.
let custom = CssPropertyWithConditionsVec::from_vec(vec![
CssPropertyWithConditions::simple(CssProperty::const_font_size(
StyleFontSize::const_px(99),
CssPropertyWithConditions::simple(CssProperty::const_text_align(StyleTextAlign::Left)),
]);
let dom = Label {
string: AzString::from_const_str("hand built"),
label_style: OptionCssPropertyWithConditionsVec::Some(custom.clone()),
.dom();
properties(&custom),
"the custom style was rewritten"
"a hand-built label lost the widget class"
assert_eq!(text_of(&dom), Some("hand built"));
fn dom_of_a_style_less_label_carries_no_inline_declarations() {
// The `LABEL_STYLE_OTHER` shape: an empty style vec must produce an
// empty inline style, not a phantom rule block.
string: AzString::from_const_str("bare"),
label_style: OptionCssPropertyWithConditionsVec::Some(
CssPropertyWithConditionsVec::from_const_slice(LABEL_STYLE_OTHER),
),
inline_properties(&dom).is_empty(),
"an empty style vec produced declarations"
assert_eq!(text_of(&dom), Some("bare"));
fn the_widget_class_is_a_namespaced_ascii_css_identifier() {
let dom = Label::create(AzString::from_const_str("x")).dom();
let classes = dom.root.get_ids_and_classes();
let name = classes
.find_map(|c| match c {
Class(s) => Some(s.as_str().to_string()),
IdOrClass::Id(_) => None,
.expect("the label must carry a class");
assert_eq!(name, LABEL_CLASS_NAME);
assert!(!name.is_empty(), "empty class name");
assert!(name.is_ascii(), "non-ASCII class name {name:?}");
name.starts_with("__azul-native-"),
"unnamespaced class {name:?}"
// A space, a dot or a `#` would silently split/re-target the selector.
name.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'),
"class name {name:?} contains a CSS-significant character"
fn a_label_whose_text_is_the_class_name_does_not_gain_a_second_class() {
// `dom()` sets ids-and-classes *after* the text node is built; a
// confusion between the two would show up as an extra attribute here.
let dom = Label::create(AzString::from_const_str(LABEL_CLASS_NAME)).dom();
"the text leaked into the class list"
assert_eq!(text_of(&dom), Some(LABEL_CLASS_NAME));
fn from_label_for_dom_is_exactly_dom() {
for s in ["", "ok", "\u{1F600}\0"] {
let label = Label::create(AzString::from(s.to_string()));
let via_into: Dom = label.clone().into();
let via_dom = label.dom();
assert_eq!(via_into, via_dom, "{s:?}: `From` diverges from `dom()`");
fn building_many_doms_never_corrupts_the_shared_static_class_list() {
// `dom()` hands a `'static`-backed `IdOrClassVec` to each node it
// builds; 1000 build-and-drop rounds would surface a double free.
let dom = Label::create(AzString::from_const_str("churn")).dom();
"round {round}: the static class list was corrupted"
drop(dom);
"the static class list did not survive the churn"