Skip to content

fix: keep DSL prop controls out of block children - #48

Merged
AdamMusa merged 2 commits into
mainfrom
fix/dsl-block-prop-children
Nov 2, 2026
Merged

AdamMusa merged 2 commits into
mainfrom
fix/dsl-block-prop-children

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Problem

Inside a DSL block, a control passed as a prop was also kept as a child of the enclosing control. Every helper attaches the control it builds to the current builder, and Ruby evaluates arguments first. So text("T") was attached before list_tile consumed it as title:.

Repro on main:

Code Children / roots before
column { list_tile(title: text("T"), subtitle: text("S")) } Text, Text, ListTile
column { container(content: text("x")) } Text, Container
Ruflet.column { row(controls: [text("a"), text("b")]) } Text, Text, Row
Ruflet.center { container(content: text("x")) } content is the Text, not the Container
page.mount { container(content: text("m")) } page controls Text, Container
top-level container(content: text("top")) app roots Text, Container
top-level page(appbar: app_bar(title: text("Hi")), floating_action_button: ...) app roots Text, AppBar, FloatingActionButton. They are added to the page body and set as page chrome.

Ruflet.column { Ruflet.container(content: Ruflet.text("x")) } was correct, because each Ruflet.* call uses a fresh builder.

The same control instance (same id) then appears twice in the tree.

Cause

There are two accumulation points, and both had the bug:

  1. ui/widget_builder.rb WidgetBuilder#control: @children << node for every helper call inside a block. This path is used by Ruflet.column { } and the other Ruflet.* block helpers, Page#mount, and center { }.
  2. ruflet/dsl.rb Ruflet::DSL::App#control / #attach: it appends to @roots, or to @stack.last.children inside a block. This path is used by the bare top-level helpers (Kernel forwarders → Ruflet::DSL → _pending_app), Ruflet::UI.*, and Ruflet::DSL.app { }. App#page merged control-valued props but left those controls in @roots.

Page#build_widget and the Ruflet.* forwarders use a fresh WidgetBuilder each time, so they do not accumulate. ruflet_rails' HTML DSL builds controls with ControlFactory.build directly and does not go through a builder.

Fix

  • Ruflet::UI::PropControls (ui/prop_controls.rb) is the single place where implicit DSL children are attached and taken back.
    • each_control(value) walks prop values recursively through Hash and Array and yields every Ruflet::Control. Examples it finds: content, title, actions: [...], children:/controls:, and data: { rows: [{ cells: [...] }] }. It stops at controls, because their own props were handled when they were built.
    • attach(list, control) appends an implicit child and records the owning list on the control (Control#dsl_owner, internal and never serialized).
    • reclaim(props) takes each prop control back out of its owner by identity (rindex + equal?), whichever builder or enclosing block attached it.
  • WidgetBuilder#control and build_service, and DSL::App#control, #service and #page, call reclaim(props) for every control they build or page props they merge. They attach their own implicit children through attach.
  • Unchanged: genuine block children and their order, the block-result child in WidgetBuilder, and the children:/controls: handling. Explicit children never get an owner, so they are never taken back. A control used as a prop and also passed explicitly as a child appears exactly once as a child.
  • Reuse after a WidgetBuilder block has finished does not reach back into the finished tree. Its implicit list has already been copied into the parent control. Reclaim covers the current block and the blocks that enclose it.

Update after QA review: cross-level reuse

QA found that each nested WidgetBuilder only took back controls it had attached itself. A control created in an outer block and used as a prop one or more levels deeper stayed a child of the outer block. For example, Ruflet.column { t = text("x"); row { container(content: t) } } gave column [Text, Row].

The second commit, fix: reclaim DSL prop controls from enclosing blocks, replaces the per-builder @attached maps with the owner recorded on the control, as described above. That also covers a WidgetBuilder nested inside a DSL app block, such as app.column { t = text("x"); center { container(content: t) } }.

mruby: only core methods are used (is_a?, each_value, rindex with a block, delete_at, equal?, attr_accessor, module_function). There is no defined?, no regex and no Set.

Tests

New packages/ruflet_core/test/dsl_block_prop_children_test.rb, 22 tests.

  • WidgetBuilder blocks:
    • Covers content, title/subtitle, an actions array, a deeply nested hash, controls:/children: inside a block, and a multi-level container(content: row(controls: [text])).
    • Reuse from the parent block one level deeper, and from an ancestor two levels deeper.
    • Real block children are kept in order.
    • A prop control returned as the block child, and a prop control that is also an explicit controls: child, each appear exactly once as a child.
    • center { } content and Page#mount roots are correct.
  • Ruflet::DSL app: block children, reuse one and two levels deeper, a nested WidgetBuilder (center) reclaiming from the enclosing app block, top-level roots, page(appbar:, floating_action_button:) not becoming roots, order kept, and an explicit controls: child kept once.
  • Bare Kernel DSL: the lead's repro (column { list_tile(...) }, column { container(...) }, top-level container(content: text)).

The original 17 tests: 16 failed on main. Of the 5 cross-level tests added after QA: the 2 WidgetBuilder cases and the App+center case failed on the first commit, and the 2 App-only cases already passed.

verify_pr.sh "" <ruby-wt> with RUBY_PKGS="ruflet_core ruflet ruflet_server ruflet_rails ruflet_record", all PASS:

  • ruflet_core: 146/146 files, 640 runs (baseline 145/618, plus this file's 22)
  • ruflet: 13/13 files, 160 runs
  • ruflet_server: 11/11 files, 40 runs
  • ruflet_rails: 15/15 files, 278 runs, per file via bundle exec (its HTML DSL builds through the registry)
  • ruflet_record: 5/5 files, 24 runs

Explorer tree check, using QA's r48_tree_dump.rb over all 67 ruflet_explorer standalone apps plus the explorer main app, main against this branch with object addresses ignored:

  • 67/68 identical.
  • The exception is animation, which uses Random and differs between two runs of main too.
  • These apps build their trees with page.add(...), so they contained no duplicates to remove.

Usage (now correct)

column do
  list_tile(title: text("Inbox"), subtitle: text("3 unread"))   # one ListTile child
  container(content: text("Body"), padding: 12)                  # one Container child
  app_bar(title: text("T"), actions: [icon_button("add")])       # one AppBar child
end

page(appbar: app_bar(title: text("Home")))   # page chrome only, not also a body control

Inside a DSL block every helper attaches the control it builds to the
enclosing builder. Arguments are evaluated first, so in
column { list_tile(title: text("T"), subtitle: text("S")) } both Text
controls were attached as column children before the ListTile consumed them,
and the column rendered Text, Text, ListTile. The same happened for content:,
actions: [...], children:/controls: and nested hashes, in WidgetBuilder blocks
(Ruflet.column { ... }, Page#mount, center) and in the Ruflet::DSL app used by
the bare top-level helpers. There, top-level props and page(appbar: ...) also
leaked into the app's root controls.

Both builders now record the controls they attach. After building a control
(or merging page props), they walk its prop values through Hash and Array and
take back, by identity, any control they had attached. Explicit block children,
the block-result child, and children:/controls: handling are unchanged.
Each nested WidgetBuilder only took back prop controls that it had
attached itself. A control created in an outer block and used as a prop one or
more levels deeper stayed a child of the outer block:
column { t = text("x"); row { container(content: t) } } still gave
column [Text, Row].

Builders now attach implicit children through PropControls.attach, which
records the owning list on the control (Control#dsl_owner, internal and never
serialized). PropControls.reclaim takes a prop control back from that owner,
matching by identity, whichever builder or enclosing block attached it. This
also covers a WidgetBuilder nested in a DSL app block (center { } inside
app.column { }). Explicit children are never given an owner, so they are never
taken back.
@AdamMusa
AdamMusa merged commit db9647f into main Nov 2, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant