Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter ActionView output with Nokogiri #179

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
28 changes: 18 additions & 10 deletions lib/phlex/rails/helper_macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ 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

case output
when ActiveSupport::SafeBuffer
@_context.target << output
return unless ActiveSupport::SafeBuffer === output

context = @_context

fragments = context.fragments
if fragments && !context.in_target_fragment
output = Phlex::Rails::FragmentFinder.extract(output, fragments)
end

context.target << output

nil
end
RUBY
Expand All @@ -47,8 +51,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,11 +65,17 @@ def #{method_name}(*args, **kwargs)
helpers.#{method_name}(*args, **kwargs)
end

case output
when ActiveSupport::SafeBuffer
@_context.target << output
return unless ActiveSupport::SafeBuffer === output

context = @_context

fragments = context.fragments
if fragments && !context.in_target_fragment
output = Phlex::Rails::FragmentFinder.extract(output, fragments)
end

context.target << output

nil
end
RUBY
Expand Down
20 changes: 17 additions & 3 deletions lib/phlex/rails/helpers/link_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
module Phlex::Rails::Helpers::LinkTo
extend Phlex::Rails::HelperMacros

# @!method link_to(...)
# @return [nil]
register_output_helper :link_to
def link_to(*args, **kwargs, &block)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is probably worth making a macro for this, since there are a lot of helpers that take an id as a keyword argument. A few other helpers, such as turbo_frame_tag take an id as a positional argument.

context = @_context

if context.fragments && !context.in_target_fragment && !context.fragments[kwargs[:id]]
return nil
end

output = if block
helpers.link_to(*args, **kwargs) { capture(&block) }
else
helpers.link_to(*args, **kwargs)
end

return unless ActiveSupport::SafeBuffer === output

context.target << output
end
end
11 changes: 10 additions & 1 deletion lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ def render(*args, **kwargs, &block)
return super unless renderable.is_a?(ActiveRecord::Relation)
else
captured_block = -> { capture(&block) } if block
@_context.target << @_view_context.render(*args, **kwargs, &captured_block)
output = @_view_context.render(*args, **kwargs, &captured_block)

context = @_context
fragments = context.fragments

if fragments && !context.in_target_fragment
output = Phlex::Rails::FragmentFinder.extract(output, fragments)
end

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