Skip to content

Commit

Permalink
Support child nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Mar 2, 2024
1 parent 0e6814f commit 947772a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/phlex/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ def initialize
@buffer = +""
@capturing = false
@fragment = nil
@found_fragment = false
end

attr_accessor :buffer, :capturing, :fragment
attr_accessor :buffer, :capturing, :fragment, :found_fragment

# Added for backwards compatibility with phlex-rails. We can remove this with 2.0
def target
Expand Down
23 changes: 18 additions & 5 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,26 @@ def register_element(method_name, tag: method_name.name.tr("_", "-"), deprecated
# frozen_string_literal: true
def #{method_name}(**attributes, &block)
if (fragment = @_context.fragment)
unless attributes[:id] == fragment
yield
return nil
context = @_context
buffer = context.buffer
fragment = context.fragment
end_find = false
if fragment
found_fragment = context.found_fragment
if !found_fragment
if attributes[:id] == fragment
context.found_fragment = true
end_find = true
else
yield if block
return nil
end
end
end
#{deprecation}
buffer = @_context.buffer
if attributes.length > 0 # with attributes
if block # with content block
Expand All @@ -71,6 +82,8 @@ def #{method_name}(**attributes, &block)
#{'flush' if tag == 'head'}
context.found_fragment = false if end_find
nil
end
Expand Down
9 changes: 7 additions & 2 deletions test/phlex/selective_rendering.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
class Example < Phlex::HTML
def view_template
div {
h1(id: "target") { "Hello" }
h1 { "Before" }
h1(id: "target") {
plain "Hello"
strong { "World" }
}
h1 { "After" }
}
end
end

describe Phlex::HTML do
it "renders the view template" do
expect(Example.new.call(fragment: "target")).to be == ("<h1 id=\"target\">Hello</h1>")
expect(Example.new.call(fragment: "target")).to be == ("<h1 id=\"target\">Hello<strong>World</strong></h1>")
end
end

0 comments on commit 947772a

Please sign in to comment.