Lines
87.79 %
Functions
85.09 %
Branches
100 %
//! Post-layout developer warnings for misused raw text nodes.
//!
//! Browsers wrap a raw text run in an ANONYMOUS BLOCK whenever it needs one;
//! azul does not. A bare `NodeType::Text` therefore has no box of its own —
//! no rect, no clip, no layout constraints — and every box-model CSS
//! property, every id or class a stylesheet could reach it by, and every
//! pointer or focus callback, `tab_index` or `dataset` attached to one is
//! silently inert. (Lifecycle and window listeners are not: they are
//! delivered by node identity, not through a box - see
//! `has_box_dependent_callback`.) That silence has shipped real bugs (text
//! escaping its widget, click targets that never fire), which is why the raw
//! constructor is named `create_text_do_not_use_without_block_level_wrapper`
//! and why this pass exists: after layout, every text node in a shape azul
//! cannot honor is reported to the developer, once per unique finding.
//! The checks are structural (DOM + computed display), deliberately not
//! geometric: they fire deterministically on the first layout of a DOM,
//! before any symptom is visible on screen.
//! This runs after EVERY layout pass, in release, so it is built to cost
//! nothing on a clean DOM: the scan allocates only once a finding exists,
//! message text (two allocations for the snippet alone) is rendered only for
//! a warning that is actually printed, and the per-parent questions ("is
//! there a block-level child?", "how many items?") are answered once per
//! parent instead of once per text child.
use std::{
collections::BTreeSet,
hash::{Hash, Hasher},
mem::discriminant,
sync::Mutex,
};
use azul_core::{
dom::{AttributeType, NodeType},
events::EventFilter,
id::NodeId,
styled_dom::{NodeHierarchyItem, StyledDom},
use azul_css::props::layout::LayoutDisplay;
use crate::solver3::getters::{get_display_property, MultiValue};
/// Distinct findings kept alive process-wide. A DOM that is wrong in a
/// hundred places has one bug, not a hundred; past this the developer is
/// told the tap was closed rather than having the log drowned.
const MAX_DISTINCT_WARNINGS: usize = 32;
/// Findings already reported, keyed by [`dedup_key`], process-wide. Layouts
/// re-run constantly (every DOM refresh); a warning that repeats 60 times a
/// second is a warning nobody reads.
struct Emitted {
keys: BTreeSet<u64>,
capped: bool,
}
static EMITTED: Mutex<Emitted> = Mutex::new(Emitted {
keys: BTreeSet::new(),
capped: false,
});
/// The suppression tag for this lint, honored from the `AZ_SUPPRESS`
/// environment variable (comma-separated list; the common misspelling
/// `AZ_SUPRESS` is accepted too). Every emitted warning names it.
pub const SUPPRESS_TAG: &str = "bare_text";
/// `AZ_SUPPRESS=bare_text` (checked once): the developer has read the
/// warnings and wants them off — e.g. a codebase that deliberately renders
/// raw text and accepts the differences from browser behavior.
fn is_suppressed() -> bool {
static SUPPRESSED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*SUPPRESSED.get_or_init(|| {
let v = std::env::var("AZ_SUPPRESS")
.or_else(|_| std::env::var("AZ_SUPRESS"))
.unwrap_or_default();
v.split(',')
.any(|t| t.trim().eq_ignore_ascii_case(SUPPRESS_TAG))
})
/// State on a text node that a text node cannot carry, as a bitset so the
/// scan can record a finding without allocating.
const INERT_CSS: u8 = 1 << 0;
const INERT_IDS_CLASSES: u8 = 1 << 1;
const INERT_CALLBACKS: u8 = 1 << 2;
const INERT_TAB_INDEX: u8 = 1 << 3;
const INERT_DATASET: u8 = 1 << 4;
const INERT_CHILDREN: u8 = 1 << 5;
/// One finding, kept as a tag until a message is known to be needed.
#[derive(Clone, Copy)]
enum Finding {
/// W1 — box-less node carrying state (bitset of the `INERT_*` flags).
Inert(u8),
/// W2 — the text run is the root: nothing owns its box.
NoParent,
/// W2 — text nested directly inside text.
TextParent,
/// W2 — one of several items in a flex/grid container.
FlexItem {
display: LayoutDisplay,
child_count: usize,
},
/// W2 — mixed inline/block content: no line box of its own.
BlockSibling,
impl Finding {
/// Orders findings on the same node the way the checks are written
/// (state first, placement second) — the scan visits a text node and its
/// parent in different iterations, so the sort needs the tie-break.
const fn rank(self) -> u8 {
match self {
Self::Inert(_) => 0,
Self::NoParent => 1,
Self::TextParent => 2,
Self::FlexItem { .. } => 3,
Self::BlockSibling => 4,
const fn is_text(node_type: &NodeType) -> bool {
matches!(node_type, NodeType::Text(_))
fn has_ids_or_classes(data: &azul_core::dom::NodeData) -> bool {
data.attributes()
.as_ref()
.iter()
.any(|a| matches!(a, AttributeType::Id(_) | AttributeType::Class(_)))
/// Does the node carry a callback that can only be reached THROUGH ITS BOX?
///
/// Pointer events find their target by hit-testing a rect, and focus events
/// need a node that can hold focus - a text node has neither, so a `Hover` or
/// `Focus` listener on one can never fire. `Window` listeners fire on every
/// node for a window-level event, and `Component` (lifecycle) listeners are
/// delivered by NODE IDENTITY from the reconcile diff - `AfterMount` on a text
/// node fires like on any other node (`dll/tests/headless_lifecycle` relies on
/// it). Calling those inert would be a lie, and the lie has a cost: it once
/// sent the diagnosis of a dead lifecycle pipeline chasing the test's DOM.
fn has_box_dependent_callback(data: &azul_core::dom::NodeData) -> bool {
data.get_callbacks()
.any(|cb| matches!(cb.event, EventFilter::Hover(_) | EventFilter::Focus(_)))
fn display_of(styled_dom: &StyledDom, node_id: NodeId) -> LayoutDisplay {
match get_display_property(styled_dom, Some(node_id)) {
MultiValue::Exact(d) => d,
_ => LayoutDisplay::Block,
const fn is_flex_or_grid(display: LayoutDisplay) -> bool {
matches!(
display,
LayoutDisplay::Flex
| LayoutDisplay::InlineFlex
| LayoutDisplay::Grid
| LayoutDisplay::InlineGrid
)
const fn is_block_level(display: LayoutDisplay) -> bool {
!matches!(
LayoutDisplay::Inline
| LayoutDisplay::InlineBlock
| LayoutDisplay::InlineTable
| LayoutDisplay::None
/// Walk `styled_dom` and tag every text node used in a shape azul cannot
/// honor. Allocation-free while nothing is wrong.
fn collect_findings(styled_dom: &StyledDom) -> Vec<(usize, Finding)> {
let node_data = styled_dom.node_data.as_container();
let hierarchy = styled_dom.node_hierarchy.as_container();
let mut found: Vec<(usize, Finding)> = Vec::new();
for idx in 0..node_data.len() {
let node_id = NodeId::new(idx);
let data = &node_data[node_id];
let Some(h) = hierarchy.get(node_id) else {
continue;
if is_text(data.get_node_type()) {
// W1 — state on a box-less node: every one of these is inert on
// a text node, because only the wrapping block box carries a
// rect. An id or a class counts: it is how a stylesheet reaches
// the node, and every rule it selects computes onto a node that
// never gets one.
let mut inert = 0u8;
if !data.get_style().rules.as_ref().is_empty() {
inert |= INERT_CSS;
if has_ids_or_classes(data) {
inert |= INERT_IDS_CLASSES;
if has_box_dependent_callback(data) {
inert |= INERT_CALLBACKS;
if data.get_tab_index().is_some() {
inert |= INERT_TAB_INDEX;
if data.get_dataset().is_some() {
inert |= INERT_DATASET;
if h.first_child_id(node_id).is_some() {
inert |= INERT_CHILDREN;
if inert != 0 {
found.push((idx, Finding::Inert(inert)));
// W2 — no containing block the text can live in. Browsers would
// generate an anonymous block here; azul does not. The remaining
// W2 shapes depend only on the PARENT, so they are decided once
// in the parent's own iteration below.
match h.parent_id() {
None => found.push((idx, Finding::NoParent)),
Some(parent_id) if is_text(node_data[parent_id].get_node_type()) => {
found.push((idx, Finding::TextParent));
Some(_) => {}
// Parent-major placement checks. Answering "does this parent have a
// block-level child / how many items does it have?" once per parent
// keeps the pass linear; asking it per text child made a wide parent
// with several text children quadratic.
let mut child_count = 0usize;
let mut has_text_child = false;
let mut child = h.first_child_id(node_id);
while let Some(cc) = child {
child_count += 1;
has_text_child |= is_text(node_data[cc].get_node_type());
child = hierarchy
.get(cc)
.and_then(NodeHierarchyItem::next_sibling_id);
if !has_text_child {
// The overwhelmingly common case: no display lookup, no second
// sweep, nothing allocated.
let parent_display = display_of(styled_dom, node_id);
let finding = if is_flex_or_grid(parent_display) {
// A text leaf as the SOLE child of a flex/grid box is the
// sanctioned wrapper pattern (the parent is the box that carries
// the styling — badge, the converted labels). The hazard is text
// as ONE OF SEVERAL items: it competes in item layout with no box
// of its own.
if child_count <= 1 {
Finding::FlexItem {
display: parent_display,
child_count,
} else {
// Mixed inline + block content under one parent: the text has no
// dedicated line box of its own next to block siblings.
let mut has_block_child = false;
let mut sibling = h.first_child_id(node_id);
while let Some(sib) = sibling {
if !is_text(node_data[sib].get_node_type())
&& is_block_level(display_of(styled_dom, sib))
{
has_block_child = true;
break;
sibling = hierarchy
.get(sib)
if !has_block_child {
Finding::BlockSibling
if is_text(node_data[cc].get_node_type()) {
found.push((cc.index(), finding));
// Node order, so a finding reads in the order the DOM was written.
found.sort_by_key(|(idx, finding)| (*idx, finding.rank()));
found
/// Walk `styled_dom` and return one message per text node that is used in a
/// shape azul cannot honor. Pure — the caller decides how to report.
#[must_use]
pub fn collect_text_placement_warnings(styled_dom: &StyledDom) -> Vec<String> {
let found = collect_findings(styled_dom);
if found.is_empty() {
return Vec::new();
.into_iter()
.map(|(idx, finding)| render(&node_data[NodeId::new(idx)], idx, finding))
.collect()
/// Build the developer-facing text. Only called for a finding that is about
/// to be printed — the snippet alone costs two allocations.
fn render(data: &azul_core::dom::NodeData, idx: usize, finding: Finding) -> String {
let snippet = match data.get_node_type() {
NodeType::Text(t) => snippet_of(t.as_str()),
_ => String::new(),
match finding {
Finding::Inert(flags) => {
let mut inert = Vec::new();
if flags & INERT_CSS != 0 {
inert.push("css properties");
if flags & INERT_IDS_CLASSES != 0 {
inert.push("ids/classes");
if flags & INERT_CALLBACKS != 0 {
inert.push("pointer/focus callbacks");
if flags & INERT_TAB_INDEX != 0 {
inert.push("a tab_index");
if flags & INERT_DATASET != 0 {
inert.push("a dataset");
if flags & INERT_CHILDREN != 0 {
inert.push("element children");
format!(
"text node {idx} ({snippet}) carries {} — INERT: a text node has no box. Move \
them onto a block wrapper (create_p_with_text / create_div_with_text) instead of \
the raw text node.",
inert.join(" + "),
Finding::NoParent => format!(
"text node {idx} ({snippet}) has no parent — a raw text run needs a block-level \
container (p / div / ...) to own its box.",
),
Finding::TextParent => format!(
"text node {idx} ({snippet}) is the child of another text node — wrap both in a \
block-level container (p / div / ...).",
} => format!(
"text node {idx} ({snippet}) is one of {child_count} items in a {display:?} container \
— a raw text run competes in flex/grid layout with no box of its own (browsers \
auto-wrap it in an anonymous block; azul does not). Wrap it: create_p_with_text / \
create_div_with_text.",
Finding::BlockSibling => format!(
"text node {idx} ({snippet}) sits NEXT TO block-level siblings — browsers would wrap \
it in an anonymous block, azul does not, so it has no line box of its own. Wrap it: \
create_p_with_text / create_div_with_text.",
/// Identify the PROBLEM, not the node.
/// Node indices shift on every structural edit — i.e. on every keystroke in
/// a live document — so an index-derived key re-hashes each frame: the same
/// finding re-prints forever AND the dedup set grows without bound. The kind
/// of finding plus the styling identity of the text node and its parent is
/// stable across edits, and it is what the developer actually fixes: one
/// construction site.
fn dedup_key(styled_dom: &StyledDom, node_id: NodeId, finding: Finding) -> u64 {
let mut hasher = std::collections::hash_map::DefaultHasher::new();
finding.rank().hash(&mut hasher);
Finding::Inert(flags) => flags.hash(&mut hasher),
// NOT the child_count: adding an item to the container is not a new
// bug.
Finding::FlexItem { display, .. } => display.hash(&mut hasher),
_ => {}
hash_identity(&node_data[node_id], &mut hasher);
match hierarchy
.get(node_id)
.and_then(NodeHierarchyItem::parent_id)
None => 0u8.hash(&mut hasher),
Some(parent_id) => {
1u8.hash(&mut hasher);
hash_identity(&node_data[parent_id], &mut hasher);
hasher.finish()
/// The part of a node that survives a DOM rebuild: what it is and how it is
/// selected. Deliberately not its text, which may be per-frame data.
fn hash_identity(data: &azul_core::dom::NodeData, hasher: &mut impl Hasher) {
discriminant(data.get_node_type()).hash(hasher);
for attr in data.attributes().as_ref() {
match attr {
AttributeType::Id(s) => {
0u8.hash(hasher);
s.as_str().hash(hasher);
AttributeType::Class(s) => {
1u8.hash(hasher);
/// Report every finding from [`collect_text_placement_warnings`] to stderr,
/// once per unique finding per process. Call after layout.
pub fn warn_text_without_block_container(styled_dom: &StyledDom) {
if is_suppressed() {
return;
let Ok(mut emitted) = EMITTED.lock() else {
if emitted.capped {
for (idx, finding) in found {
if !emitted.keys.insert(dedup_key(styled_dom, node_id, finding)) {
if emitted.keys.len() > MAX_DISTINCT_WARNINGS {
emitted.capped = true;
eprintln!(
"[azul][text-without-block] {MAX_DISTINCT_WARNINGS} distinct findings reported — \
further warnings suppressed (suppress the whole lint with \
AZ_SUPPRESS={SUPPRESS_TAG})"
);
"[azul][text-without-block] WARNING: {} (suppress with AZ_SUPPRESS={SUPPRESS_TAG})",
render(&node_data[node_id], idx, finding),
fn snippet_of(text: &str) -> String {
let mut s: String = text.chars().take(24).collect();
if text.chars().count() > 24 {
s.push_str("...");
format!("{s:?}")
#[cfg(test)]
mod autotest_generated {
dom::{Dom, IdOrClass, IdOrClassVec, TabIndex},
events::{ComponentEventFilter, EventFilter, HoverEventFilter},
refany::RefAny,
styled_dom::StyledDom,
use azul_css::css::Css;
use super::collect_text_placement_warnings;
fn styled(mut dom: Dom, css: &str) -> StyledDom {
let css = if css.is_empty() {
Css::empty()
Css::from_string(css.into())
StyledDom::create(&mut dom, css)
fn raw_text(s: &str) -> Dom {
Dom::create_text_do_not_use_without_block_level_wrapper(s)
#[test]
fn a_correctly_wrapped_text_produces_no_warning() {
let sd = styled(
Dom::create_body().with_child(Dom::create_p_with_text("hello")),
"",
assert_eq!(collect_text_placement_warnings(&sd), Vec::<String>::new());
fn text_inside_an_inline_span_inside_a_block_is_fine() {
Dom::create_body()
.with_child(Dom::create_p().with_child(Dom::create_span_with_text("hi"))),
fn state_on_a_text_node_is_reported_as_inert() {
let text = raw_text("styled").with_tab_index(TabIndex::Auto);
Dom::create_body().with_child(Dom::create_p().with_child(text)),
let w = collect_text_placement_warnings(&sd);
assert_eq!(w.len(), 1, "{w:?}");
assert!(w[0].contains("INERT"), "{w:?}");
assert!(w[0].contains("tab_index"), "{w:?}");
fn a_pointer_callback_on_a_text_node_is_reported_as_inert() {
let text = raw_text("clickable").with_callback(
EventFilter::Hover(HoverEventFilter::MouseUp),
RefAny::new(()),
0usize,
assert!(w[0].contains("pointer/focus callbacks"), "{w:?}");
fn a_lifecycle_callback_on_a_text_node_is_not_inert() {
// Delivered by node identity from the reconcile diff, not by hit-test:
// it fires, so the lint must not call it dead.
let text = raw_text("mounted").with_callback(
EventFilter::Component(ComponentEventFilter::AfterMount),
assert!(w.is_empty(), "{w:?}");
fn ids_and_classes_on_a_text_node_are_reported_as_inert() {
// The shipped menu_renderer defect: the checkmark's three classes
// ended up on the text node instead of the icon <div> that boxes it.
// The text is that div's only child — the shape every placement check
// calls sanctioned — so the classes are the only thing left to report.
let icon = raw_text("✓").with_ids_and_classes(IdOrClassVec::from_vec(vec![
IdOrClass::Class("menu-item-icon".into()),
IdOrClass::Class("menu-item-checkbox".into()),
IdOrClass::Class("menu-item-checkbox-checked".into()),
]));
Dom::create_body().with_child(Dom::create_div().with_child(icon)),
assert!(w[0].contains("ids/classes"), "{w:?}");
fn a_sole_text_leaf_in_a_flex_wrapper_is_the_sanctioned_pattern() {
// badge / the converted labels: the flex box IS the wrapper.
Dom::create_body().with_child(Dom::create_div().with_child(raw_text("flexed"))),
"div { display: flex; }",
fn text_competing_with_other_flex_items_is_reported() {
// The tree_view/radio_group shape: a raw label beside element items.
Dom::create_body().with_child(
Dom::create_div()
.with_child(Dom::create_div())
.with_child(raw_text("flexed")),
"body > div { display: flex; }",
assert!(w[0].contains("competes in flex/grid"), "{w:?}");
fn text_next_to_a_block_sibling_is_reported() {
// The audited frame.rs shape: a title wedged between two divs.
.with_child(raw_text("title"))
.with_child(Dom::create_div()),
assert!(w[0].contains("block-level siblings"), "{w:?}");
fn several_text_children_of_one_parent_are_all_reported() {
// The per-parent memo must not swallow the sibling text runs it was
// computed for.
.with_child(raw_text("a"))
.with_child(raw_text("b")),
assert_eq!(w.len(), 2, "{w:?}");
assert!(w[0].contains("\"a\""), "{w:?}");
assert!(w[1].contains("\"b\""), "{w:?}");
fn findings_are_reported_in_node_order() {
// The two text runs are found via DIFFERENT parents (one nested, one
// directly under body), so the scan reaches them out of order and the
// sort has to put them back.
.with_child(
.with_child(raw_text("deep")),
.with_child(raw_text("shallow"))
let indices: Vec<usize> = w
.map(|m| {
m.split_whitespace()
.nth(2)
.and_then(|n| n.parse().ok())
.unwrap_or_else(|| panic!("no node index in {m:?}"))
.collect();
assert!(indices[0] < indices[1], "{indices:?} / {w:?}");
fn every_widget_dom_is_warning_free() {
// The runtime twin of the widgets' label-convention test: none of the
// shipped widgets may trip the developer warning.
for (name, dom) in crate::widgets::all_widget_doms_for_lint() {
let sd = styled(Dom::create_body().with_child(dom), "");
assert_eq!(
w,
Vec::<String>::new(),
"widget {name} trips the text lint: {w:?}"
// ============================================================================
// Suppression, once, for every lint
/// Is `tag` suppressed via `AZ_SUPPRESS`?
/// Comma-separated; the common misspelling `AZ_SUPRESS` is accepted too.
/// `AZ_SUPPRESS=all` turns every engine lint off in one move — a shipped app
/// that has read its warnings and does not want them in a customer's console
/// should not have to enumerate them, and a new lint must not suddenly start
/// talking in that app's logs.
/// Read once per tag and cached: these run inside the layout pass.
pub fn lint_suppressed(tag: &str) -> bool {
collections::BTreeMap,
sync::{Mutex, OnceLock},
static CACHE: OnceLock<Mutex<BTreeMap<String, bool>>> = OnceLock::new();
let cache = CACHE.get_or_init(|| Mutex::new(BTreeMap::new()));
if let Ok(c) = cache.lock() {
if let Some(hit) = c.get(tag) {
return *hit;
let hit = v.split(',').any(|t| {
let t = t.trim();
t.eq_ignore_ascii_case(tag) || t.eq_ignore_ascii_case("all")
if let Ok(mut c) = cache.lock() {
c.insert(tag.to_string(), hit);
hit
// Lint: a <div> that holds nothing but text
/// Suppression tag for [`warn_div_used_as_text_container`].
pub const DIV_TEXT_SUPPRESS_TAG: &str = "div_as_text";
fn div_text_suppressed() -> bool {
lint_suppressed(DIV_TEXT_SUPPRESS_TAG)
/// A `<div>` whose only child is a text node is almost always the wrong
/// element.
/// `div` is a generic CONTAINER — it says "a box, meaning unspecified". Text
/// that is a label, a caption, or a control's text belongs in `<span>`
/// (inline), and text that is prose belongs in `<p>`. Reaching for `div` is
/// usually a sign that someone wanted "a box around this text" and took the
/// first constructor that provided one.
/// It matters beyond tidiness. Assistive technology reads the element to decide
/// what a thing IS: a `div` conveys nothing, while `p` announces a paragraph
/// and `span` stays inline inside its label. It also affects layout — a `div`
/// is block-level, so a label inside a button becomes a block box where an
/// inline one was meant.
/// Not an error: a `div` with text is legal and occasionally right (a sizing
/// box that happens to contain a word). Hence a warning, and a suppression tag.
pub fn warn_div_used_as_text_container(styled_dom: &StyledDom) {
if div_text_suppressed() {
let nodes = styled_dom.node_data.as_container();
let mut reported = 0usize;
for (node_id, node) in nodes
.linear_iter()
.filter_map(|id| nodes.get(id).map(|n| (id, n)))
if !matches!(node.get_node_type(), NodeType::Div) {
// Exactly one child, and that child is a text node.
let Some(item) = hierarchy.get(node_id) else {
let Some(first) = item.first_child_id(node_id) else {
let only_child = hierarchy
.get(first)
.is_some_and(|c| c.next_sibling_id().is_none());
if !only_child {
let Some(child) = nodes.get(first) else {
let NodeType::Text(text) = child.get_node_type() else {
reported += 1;
if reported > 8 {
break; // one screenful is enough to act on
let preview: String = text.as_str().chars().take(24).collect();
azul_core::diagnostics::emit(format!(
"[azul][div-as-text] node {} is a <div> whose only child is the text {preview:?}. A \
div is a generic container and says nothing about what the text IS. Use \
create_span_with_text for a label or a control's text (inline, stays inside its line \
box) or create_p_with_text for prose (block, announced as a paragraph). Assistive \
technology reads the element to decide what a thing is, and a div tells it nothing. \
(suppress with AZ_SUPPRESS={DIV_TEXT_SUPPRESS_TAG})",
node_id.index()
));
// Lint: an interactive node with no accessible name
/// Suppression tag for [`warn_interactive_without_accessibility`].
pub const A11Y_SUPPRESS_TAG: &str = "a11y";
fn a11y_suppressed() -> bool {
lint_suppressed(A11Y_SUPPRESS_TAG)
/// A node the user can CLICK, with nothing that names it to assistive
/// technology.
/// A screen reader announces a control by its accessible name. Without one it
/// reads "button" — or nothing at all — and the control is unusable without
/// sight. Azul builds its accessibility tree from `NodeData::accessibility`,
/// so a node with a click callback and no `AccessibilityInfo` is invisible to
/// that tree even though it is plainly visible on screen.
/// The fix is one call:
/// ```ignore
/// dom.with_accessibility_info(AccessibilityInfo {
/// accessibility_name: Some("Mute microphone".into()).into(),
/// ..Default::default()
/// })
/// ```
/// A node whose text child already spells out its label is NOT reported: azul
/// derives a name from the text in that case, so the control does announce
/// itself. What this catches is the icon-only button — the one whose label is a
/// glyph, where a sighted user infers "mute" from a picture and a screen reader
/// gets a private-use codepoint.
pub fn warn_interactive_without_accessibility(styled_dom: &StyledDom) {
if a11y_suppressed() {
// "Interactive" = the app attached a callback to it.
if node.get_callbacks().is_empty() {
if node.accessibility.is_some() {
continue; // already named
// Does a descendant text node spell out a usable label? Azul derives a
// name from it, so those controls DO announce themselves.
// Walk DESCENDANTS, not just direct children. A well-built control puts
// its label in a <span> — the shape this crate recommends — so the text
// is a grandchild. Scanning one level deep flagged every correctly
// written button, which a test caught before this shipped.
let mut has_readable_text = false;
let mut stack = vec![node_id];
let mut visited = 0usize;
while let Some(cur) = stack.pop() {
visited += 1;
if visited > 64 {
break; // a label is never buried this deep; bound the walk
if let Some(cn) = nodes.get(cur) {
if let NodeType::Text(t) = cn.get_node_type() {
// A private-use glyph is an ICON, not a name: it reads as a
// meaningless codepoint.
if t.as_str()
.chars()
.any(|ch| ch.is_alphanumeric() && !('\u{e000}'..='\u{f8ff}').contains(&ch))
has_readable_text = true;
if let Some(item) = hierarchy.get(cur) {
if let Some(first) = item.first_child_id(cur) {
let mut sib = Some(first);
while let Some(sid) = sib {
stack.push(sid);
sib = hierarchy
.get(sid)
if has_readable_text {
"[azul][a11y] node {} has a callback but no accessible name, and no text child that \
could serve as one — an icon-only control reads to a screen reader as a private-use \
codepoint, or as nothing. Add one: .with_accessibility_info(AccessibilityInfo {{ \
accessibility_name: Some(\"...\".into()).into(), ..Default::default() }}). (suppress \
with AZ_SUPPRESS={A11Y_SUPPRESS_TAG})",
#[cfg(all(test, feature = "std"))]
mod semantic_and_a11y_lint_tests {
use azul_core::dom::Dom;
use super::*;
// The lints share the global diagnostics ring, so they must not run
// concurrently. The lock is workspace-wide and lives beside the ring:
// see azul_core::diagnostics::test_lock.
fn styled(dom: Dom) -> StyledDom {
StyledDom::create_from_dom(dom)
fn a_div_holding_only_text_is_reported() {
let _g = azul_core::diagnostics::test_lock().lock();
azul_core::diagnostics::clear();
warn_div_used_as_text_container(&styled(
Dom::create_body().with_child(Dom::create_div_with_text("Unmute mic")),
assert!(
azul_core::diagnostics::any_contains("div-as-text"),
"a <div> whose only child is text must be reported: {:?}",
azul_core::diagnostics::recorded()
// ...and the alternatives must NOT be.
.with_child(Dom::create_span_with_text("Unmute mic"))
.with_child(Dom::create_p_with_text("A paragraph of prose.")),
!azul_core::diagnostics::any_contains("div-as-text"),
"span and p are the RECOMMENDED forms and must not be flagged: {:?}",
/// A div that contains other elements is a container doing its job.
fn a_div_with_element_children_is_not_reported() {
.with_child(Dom::create_span_with_text("a"))
.with_child(Dom::create_span_with_text("b")),
assert!(!azul_core::diagnostics::any_contains("div-as-text"));
/// An ICON-only control — its label is a private-use glyph — announces
/// nothing to a screen reader and must be reported.
fn an_icon_only_control_without_a_name_is_reported() {
let icon_button = Dom::create_div()
.with_child(Dom::create_span_with_text("\u{e161}")) // a Material icon
.with_callback(
azul_core::dom::EventFilter::Hover(
azul_core::dom::HoverEventFilter::MouseUp,
azul_core::refany::RefAny::new(()),
crate::callbacks::Callback::from_ptr(noop_cb),
warn_interactive_without_accessibility(&styled(Dom::create_body().with_child(icon_button)));
azul_core::diagnostics::any_contains("[azul][a11y]"),
"an icon-only control with no accessible name must be reported: {:?}",
/// A control whose text spells out its label DOES announce itself — azul
/// derives a name from it — so reporting it would be noise.
fn a_control_with_a_readable_label_is_not_reported() {
let text_button = Dom::create_div()
azul_core::dom::EventFilter::Hover(azul_core::dom::HoverEventFilter::MouseUp),
warn_interactive_without_accessibility(&styled(Dom::create_body().with_child(text_button)));
!azul_core::diagnostics::any_contains("[azul][a11y]"),
"a control with a readable text label already announces itself: {:?}",
extern "C" fn noop_cb(
_: azul_core::refany::RefAny,
_: crate::callbacks::CallbackInfo,
) -> azul_core::callbacks::Update {
azul_core::callbacks::Update::DoNothing
/// The azul widget a node came from, if it says so.
/// Widgets tag themselves `__azul-native-<name>`, which lets the FRAMEWORK lint
/// give WIDGET-specific advice — naming the type and the exact builder call —
/// without the widget having to warn at build time.
/// That distinction matters: a widget cannot warn about a missing name itself,
/// because the name arrives AFTER it is built. `Slider::new(..).dom()` runs
/// first, `.with_accessibility_name("Volume")` second. A build-time warning
/// would fire on every correctly-named slider in existence. The lint runs on
/// the finished DOM, so it sees the answer.
fn azul_widget_kind(node: &azul_core::dom::NodeData) -> Option<String> {
node.get_ids_and_classes().iter().find_map(|ic| {
let s = match ic {
azul_core::dom::IdOrClass::Class(c) => c.as_str(),
azul_core::dom::IdOrClass::Id(_) => return None,
let rest = s.strip_prefix("__azul-native-")?;
// "__azul-native-slider-thumb" -> "slider": the first segment is the
// widget; the rest names a part inside it.
let name = rest.split('-').next()?;
if name.is_empty() {
return None;
let mut c = name.chars();
Some(c.next()?.to_uppercase().collect::<String>() + c.as_str())
/// Does a readable text label exist at or beneath `node_id`?
/// "Readable" excludes private-use codepoints: an icon glyph is a picture, and
/// reads to a screen reader as a meaningless character rather than a name.
/// Shared by the a11y lints because both need the same question answered, and
/// answering it differently in two places is how a lint acquires false
/// positives — the shape lint reported every labelled checkbox as "anonymous"
/// until it used this.
fn has_readable_text_label(styled_dom: &StyledDom, node_id: NodeId) -> bool {
return true;
false
// Lint: what this node's SHAPE says it should declare
/// Suppression tag for [`warn_a11y_shape`].
pub const A11Y_SHAPE_SUPPRESS_TAG: &str = "a11y_shape";
/// Tell a developer, from the shape of the DOM, which accessibility attributes
/// this particular node is missing — and why each one matters.
/// ## The distinction this exists to teach
/// Azul builds its accessibility tree from `NodeData::accessibility`. A node
/// with `None` there is **not in that tree**: it is on screen and absent from
/// the interface a screen-reader user actually has. This is not "less
/// accessible", it is *invisible*, and it is the default for every node you
/// build. There is no partial credit and no automatic fallback beyond a name
/// derived from text.
/// `AccessibilityInfo` carries far more than a name — a role out of 118, a
/// value, a description, states, `labelled_by` / `described_by` relations, a
/// default action, an accelerator, and a live-region flag. Nobody discovers
/// that by reading `core/src/a11y.rs`, so this lint reads the node's shape and
/// names the specific field it is missing:
/// | shape observed | what to declare |
/// |-------------------------------------|------------------------|
/// | interactive, no accessibility at all | `accessibility_name`, plus a `role` |
/// | declared, but role is `Unknown` | `role` — "what IS this" |
/// | `Slider` / `ProgressBar` / `ScrollBar` | `accessibility_value` |
/// | `CheckButton` / `RadioButton` | a checked state |
/// | `PushButton` with no action named | `default_action` |
/// | an image with no name | `accessibility_name` or `description` |
/// | keyboard-focusable, no role | `role` |
/// Suppress with `AZ_SUPPRESS=a11y_shape`, or `AZ_SUPPRESS=all` for every lint.
pub fn warn_a11y_shape(styled_dom: &StyledDom) {
if lint_suppressed(A11Y_SHAPE_SUPPRESS_TAG) {
use azul_core::a11y::{AccessibilityRole, AccessibilityState};
if reported >= 8 {
let idx = node_id.index();
let interactive = !node.get_callbacks().is_empty();
let focusable = node.get_tab_index().is_some();
let Some(info) = node.accessibility.as_ref() else {
// The no-a11y case is handled by warn_interactive_without_accessibility,
// which already reports interactive nodes. Here, add the shapes that
// are worth naming even when they are not clickable.
if matches!(node.get_node_type(), NodeType::Image(_)) {
"[azul][a11y-shape] node {idx} is an IMAGE with no accessibility info, so it \
is absent from the accessibility tree entirely — a screen reader announces \
nothing where a sighted user sees a picture. Give it \
.with_accessibility_info(AccessibilityInfo {{ accessibility_name: \
Some(\"what it shows\".into()).into(), role: AccessibilityRole::Graphic, \
..Default::default() }}), or, when it only decorates something named \
elsewhere, mark it decorative with `role: AccessibilityRole::Nothing` and no \
name. (suppress with AZ_SUPPRESS={A11Y_SHAPE_SUPPRESS_TAG})"
} else if focusable {
"[azul][a11y-shape] node {idx} is KEYBOARD-FOCUSABLE (it has a tab_index) but \
declares no accessibility info, so tabbing lands on something the screen \
reader cannot describe. At minimum give it a `role` and an \
`accessibility_name`. (suppress with AZ_SUPPRESS={A11Y_SHAPE_SUPPRESS_TAG})"
// It DECLARED accessibility — now check that the declaration matches the
// shape. A half-filled AccessibilityInfo is the subtler failure: the node
// is in the tree, so nothing looks broken, and it still announces wrongly.
let role = info.role;
let has_name = info.accessibility_name.as_ref().is_some();
let has_value = info.accessibility_value.as_ref().is_some();
if matches!(role, AccessibilityRole::Unknown) && (interactive || focusable) {
"[azul][a11y-shape] node {idx} is interactive and declares accessibility, but its \
`role` is Unknown — a screen reader can say its name and not what it IS. Pick \
from AccessibilityRole (PushButton, CheckBox, ComboBox, Slider, Link, Tab, \
MenuItem, …). (suppress with AZ_SUPPRESS={A11Y_SHAPE_SUPPRESS_TAG})"
if matches!(
role,
AccessibilityRole::Slider
| AccessibilityRole::ProgressBar
| AccessibilityRole::ScrollBar
) && !has_value
"[azul][a11y-shape] node {idx} has role {role:?} but no `accessibility_value`. \
The value IS the content of these controls — without it a screen reader \
announces \"slider\" and never how far along it is. Set accessibility_value on \
every change, not once. (suppress with AZ_SUPPRESS={A11Y_SHAPE_SUPPRESS_TAG})"
AccessibilityRole::CheckButton | AccessibilityRole::RadioButton
) {
let states = info.states.as_ref();
let declares_checked = states.iter().any(|s| {
s,
AccessibilityState::CheckedTrue
| AccessibilityState::CheckedFalse
| AccessibilityState::Selected
if !declares_checked {
"[azul][a11y-shape] node {idx} has role {role:?} but declares no \
CheckedTrue/CheckedFalse/Selected state, so it always announces as unchecked \
no matter what it renders. Push the state into AccessibilityInfo::states \
when the control toggles. (suppress with \
AZ_SUPPRESS={A11Y_SHAPE_SUPPRESS_TAG})"
// An explicit `role: Nothing` with no name on something nobody can click
// or tab to is the DECORATIVE mark (ARIA's role="presentation"): the
// picture is part of a whole that is named elsewhere, like one tile of a
// map whose container carries the name. Demanding a name here would make
// a screen reader read out every tile.
if matches!(role, AccessibilityRole::Nothing) && !has_name && !interactive && !focusable {
// A control whose visible label is a text node DOES announce itself —
// azul derives the name from that text. Reporting those was a false
// positive on every correctly-built labelled checkbox and slider.
if !has_name
&& info.labelled_by.as_ref().is_none()
&& !has_readable_text_label(styled_dom, node_id)
// Name the WIDGET when the node admits which one it is. "Slider has
// no name; add .with_accessibility_name(..)" is a fix someone can
// apply; "node 40 is anonymous" is a puzzle they have to solve
// first.
let who = azul_widget_kind(node)
.map_or_else(|| format!("node {idx}"), |w| format!("node {idx} (a {w})"));
"[azul][a11y-shape] {who} declares accessibility (role {role:?}) but has neither \
an `accessibility_name` nor a `labelled_by`. It is in the tree and anonymous. \
This control has no text of its own to derive a name from, and only the CALL \
SITE knows what it is called: add `.with_accessibility_name(\"…\")` there — it \
MERGES, so the role, value and states above survive — or point at the label you \
already render with `.with_accessibility_labelled_by(node)`, which cannot drift \
out of sync the way a copied string does. (suppress with \