Lavash.Optimistic.DisplaySpanTransformer (Lavash v0.4.0-rc.4)

Copy Markdown View Source

Layer-4 token transformer: wraps bare {@field} expressions in <span data-lavash-display="field"> when :field is an optimistic state field or an optimistic calculation.

The JS hook reads data-lavash-display to know which DOM node to swap on optimistic state change. Without the wrapping span, the bare interpolation just emits text — the optimistic patch can't find a stable element to update.

What it skips

  • metadata[:layer] == :base — layer-2-only modules don't need optimistic display wrapping; the warning suppressed in Base mode is paired with no-op wrapping here.
  • Non-optimistic refs — {@count} for a state :count, ... that doesn't set optimistic: true is left alone. (The old Lavash.Template.TokenTransformer emitted a diagnostic warning here; that warning lives in Lavash.Optimistic.NonOptimisticWarner now.)
  • Bare-var refs ({item} from a :for loop) — never wrapped, since loop vars aren't assigns.
  • Body_exprs whose immediate parent is already a display wrapper or a data-lavash-manual block.
  • Body_exprs inside <textarea>, <option>, <title> — the browser treats body content as form values / page title text, so injecting a <span> would corrupt the payload.