Repository navigation
fix: keep DSL prop controls out of block children - #48
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 beforelist_tileconsumed it astitle:.Repro on
main:column { list_tile(title: text("T"), subtitle: text("S")) }Text, Text, ListTilecolumn { container(content: text("x")) }Text, ContainerRuflet.column { row(controls: [text("a"), text("b")]) }Text, Text, RowRuflet.center { container(content: text("x")) }page.mount { container(content: text("m")) }Text, Containercontainer(content: text("top"))Text, Containerpage(appbar: app_bar(title: text("Hi")), floating_action_button: ...)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 eachRuflet.*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:
ui/widget_builder.rbWidgetBuilder#control:@children << nodefor every helper call inside a block. This path is used byRuflet.column { }and the otherRuflet.*block helpers,Page#mount, andcenter { }.ruflet/dsl.rbRuflet::DSL::App#control/#attach: it appends to@roots, or to@stack.last.childreninside a block. This path is used by the bare top-level helpers (Kernel forwarders →Ruflet::DSL→_pending_app),Ruflet::UI.*, andRuflet::DSL.app { }.App#pagemerged control-valued props but left those controls in@roots.Page#build_widgetand theRuflet.*forwarders use a freshWidgetBuildereach time, so they do not accumulate. ruflet_rails' HTML DSL builds controls withControlFactory.builddirectly 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 everyRuflet::Control. Examples it finds:content,title,actions: [...],children:/controls:, anddata: { 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#controlandbuild_service, andDSL::App#control,#serviceand#page, callreclaim(props)for every control they build or page props they merge. They attach their own implicit children throughattach.WidgetBuilder, and thechildren:/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.WidgetBuilderblock 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
WidgetBuilderonly 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) } }gavecolumn [Text, Row].The second commit,
fix: reclaim DSL prop controls from enclosing blocks, replaces the per-builder@attachedmaps with the owner recorded on the control, as described above. That also covers aWidgetBuildernested inside a DSL app block, such asapp.column { t = text("x"); center { container(content: t) } }.mruby: only core methods are used (
is_a?,each_value,rindexwith a block,delete_at,equal?,attr_accessor,module_function). There is nodefined?, no regex and noSet.Tests
New
packages/ruflet_core/test/dsl_block_prop_children_test.rb, 22 tests.content,title/subtitle, anactionsarray, a deeply nested hash,controls:/children:inside a block, and a multi-levelcontainer(content: row(controls: [text])).controls:child, each appear exactly once as a child.center { }content andPage#mountroots are correct.Ruflet::DSLapp: block children, reuse one and two levels deeper, a nestedWidgetBuilder(center) reclaiming from the enclosing app block, top-level roots,page(appbar:, floating_action_button:)not becoming roots, order kept, and an explicitcontrols:child kept once.column { list_tile(...) },column { container(...) }, top-levelcontainer(content: text)).The original 17 tests: 16 failed on
main. Of the 5 cross-level tests added after QA: the 2WidgetBuildercases and the App+centercase failed on the first commit, and the 2 App-only cases already passed.verify_pr.sh "" <ruby-wt>withRUBY_PKGS="ruflet_core ruflet ruflet_server ruflet_rails ruflet_record", all PASS:bundle exec(its HTML DSL builds through the registry)Explorer tree check, using QA's
r48_tree_dump.rbover all 67ruflet_explorerstandalone apps plus the explorer main app,mainagainst this branch with object addresses ignored:animation, which usesRandomand differs between two runs ofmaintoo.page.add(...), so they contained no duplicates to remove.Usage (now correct)