Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/herb/engine/slots/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def initialize(markers: Markers.new, mode: :server, identifier: :path, mark: tru

@fragment_nodes = fragment_nodes.compare_by_identity
@fragment_fallbacks = fragment_fallbacks.compare_by_identity
@exposed_fallbacks = [] #: Array[untyped]
@deferred_nodes = deferred_nodes.compare_by_identity
@assignment_nodes = assignment_nodes.compare_by_identity
end
Expand Down Expand Up @@ -661,6 +662,7 @@ def visit_document_node(node)
@states.apply_states

wrap_bound_renders
expose_fallbacks(node)
end

#: () -> Hash[String, String]?
Expand All @@ -670,6 +672,7 @@ def statics

#: (untyped) -> void
def finish(node)
withdraw_fallbacks(node)
follow_replacements

return unless @mark
Expand Down Expand Up @@ -1982,6 +1985,29 @@ def append_statics(document_node)
document_node.children.concat(nodes)
end

#: (Herb::AST::DocumentNode) -> void
def expose_fallbacks(document_node)
return if @fragment_fallbacks.empty?

@exposed_fallbacks = @fragment_fallbacks.each_value.flat_map { |body| body } #: Array[untyped]

document_node.children.concat(@exposed_fallbacks)

nil
end

#: (Herb::AST::DocumentNode) -> void
def withdraw_fallbacks(document_node)
exposed = @exposed_fallbacks

return if exposed.empty?

document_node.children.reject! { |child| exposed.any? { |node| node.equal?(child) } }
@exposed_fallbacks = [] #: Array[untyped]

nil
end

#: () -> Array[[String, Array[untyped]]]
def fallback_statics_entries
@fragment_fallbacks.filter_map { |node, body|
Expand Down
6 changes: 6 additions & 0 deletions sig/herb/engine/slots/visitor.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/engine/scoped_style/visitor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,5 +394,14 @@ def hoisted(source, filename: TEMPLATE)
refute_nil diagnostic
assert_equal "app/views/posts/_dynamic.html.erb", diagnostic.template
end

test "carries the scope into the fallback a client stands in while content is stale" do
source = %(<%# herb:slots client %><%# herb:state (album: "") %><style scoped>.g { color: red; }</style><Fragment on="album"><b>Loaded</b><Fallback><i class="g">Loading</i></Fallback></Fragment>)

assert_compiled_snapshot(
source,
options(visitors: [Herb::Engine::Slots::Visitor.new(mode: :client)], project_path: PROJECT_PATH)
)
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading