Skip to content

Commit

Permalink
Filter output helpers with Nokogiri
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Mar 23, 2024
1 parent fab2934 commit 56d1fa1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/phlex/rails/helper_macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ def register_output_helper(method_name)
# frozen_string_literal: true
def #{method_name}(*args, **kwargs, &block)
context = @_context
return if context.fragments && !context.in_target_fragment
output = if block
helpers.#{method_name}(*args, **kwargs) { capture(&block) }
else
helpers.#{method_name}(*args, **kwargs)
end
context = @_context
fragments = context.fragments
if fragments && !context.in_target_fragment
output = Phlex::Rails::FragmentFinder.extract(output, fragments).html_safe
end
case output
when ActiveSupport::SafeBuffer
@_context.target << output
context.target << output
end
nil
Expand Down Expand Up @@ -47,8 +52,6 @@ def register_builder_yielding_helper(method_name, builder)
# frozen_string_literal: true
def #{method_name}(*args, **kwargs)
context = @_context
return if context.fragments && !context.in_target_fragment
output = if block_given?
helpers.#{method_name}(*args, **kwargs) { |form|
capture do
Expand All @@ -63,9 +66,16 @@ def #{method_name}(*args, **kwargs)
helpers.#{method_name}(*args, **kwargs)
end
context = @_context
fragments = context.fragments
if fragments && !context.in_target_fragment
output = Phlex::Rails::FragmentFinder.extract(output, fragments).html_safe
end
case output
when ActiveSupport::SafeBuffer
@_context.target << output
context.target << output
end
nil
Expand Down
10 changes: 10 additions & 0 deletions spec/fragment_filter_spec.rb → spec/fragment_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

describe Phlex::Rails::FragmentFinder do
include Phlex::Rails::FragmentFinder
it "find nothing" do
expect(
extract(<<~HTML, ["d"])
<div id="a">A</div>
<div id="b">B</div>
<div id="c">c</div>
HTML
).to be == ""
end

it "find one" do
expect(
extract(<<~HTML, ["b"])
Expand Down

0 comments on commit 56d1fa1

Please sign in to comment.