From 56d1fa18ea1cfb64866a39a07e8cdc7d28c88039 Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Sat, 23 Mar 2024 10:35:29 +0000 Subject: [PATCH 1/4] Filter output helpers with Nokogiri --- lib/phlex/rails/helper_macros.rb | 22 ++++++++++++++----- ...filter_spec.rb => fragment_finder_spec.rb} | 10 +++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) rename spec/{fragment_filter_spec.rb => fragment_finder_spec.rb} (82%) diff --git a/lib/phlex/rails/helper_macros.rb b/lib/phlex/rails/helper_macros.rb index 45617e65..323b11c5 100644 --- a/lib/phlex/rails/helper_macros.rb +++ b/lib/phlex/rails/helper_macros.rb @@ -7,8 +7,6 @@ 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) } @@ -16,9 +14,16 @@ def #{method_name}(*args, **kwargs, &block) 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 @@ -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 @@ -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 diff --git a/spec/fragment_filter_spec.rb b/spec/fragment_finder_spec.rb similarity index 82% rename from spec/fragment_filter_spec.rb rename to spec/fragment_finder_spec.rb index 7505ca01..5c899525 100644 --- a/spec/fragment_filter_spec.rb +++ b/spec/fragment_finder_spec.rb @@ -2,6 +2,16 @@ describe Phlex::Rails::FragmentFinder do include Phlex::Rails::FragmentFinder + it "find nothing" do + expect( + extract(<<~HTML, ["d"]) +
A
+
B
+
c
+ HTML + ).to be == "" + end + it "find one" do expect( extract(<<~HTML, ["b"]) From cbe2a882327d2d5812ea77ffc1cff89c8cdfd47f Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Sat, 23 Mar 2024 10:40:35 +0000 Subject: [PATCH 2/4] Update helper_macros.rb --- lib/phlex/rails/helper_macros.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/phlex/rails/helper_macros.rb b/lib/phlex/rails/helper_macros.rb index 323b11c5..5519719a 100644 --- a/lib/phlex/rails/helper_macros.rb +++ b/lib/phlex/rails/helper_macros.rb @@ -14,17 +14,16 @@ def #{method_name}(*args, **kwargs, &block) helpers.#{method_name}(*args, **kwargs) end + return unless ActiveSupport::SafeBuffer === output + context = @_context fragments = context.fragments if fragments && !context.in_target_fragment - output = Phlex::Rails::FragmentFinder.extract(output, fragments).html_safe + output = Phlex::Rails::FragmentFinder.extract(output, fragments) end - case output - when ActiveSupport::SafeBuffer - context.target << output - end + context.target << output nil end @@ -66,17 +65,16 @@ def #{method_name}(*args, **kwargs) helpers.#{method_name}(*args, **kwargs) end - context = @_context + return unless ActiveSupport::SafeBuffer === output + + context = @_context fragments = context.fragments if fragments && !context.in_target_fragment - output = Phlex::Rails::FragmentFinder.extract(output, fragments).html_safe + output = Phlex::Rails::FragmentFinder.extract(output, fragments) end - case output - when ActiveSupport::SafeBuffer - context.target << output - end + context.target << output nil end From d227bbb3543a6b9c85dced4c11e848639bab2006 Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Sat, 23 Mar 2024 10:40:38 +0000 Subject: [PATCH 3/4] Update sgml.rb --- lib/phlex/rails/sgml.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/phlex/rails/sgml.rb b/lib/phlex/rails/sgml.rb index fdabfb64..a55a8dee 100644 --- a/lib/phlex/rails/sgml.rb +++ b/lib/phlex/rails/sgml.rb @@ -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 From 1d152aa7b581e51fef5efc4d959dc70d84a74490 Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Sat, 23 Mar 2024 11:02:37 +0000 Subject: [PATCH 4/4] Update link_to.rb --- lib/phlex/rails/helpers/link_to.rb | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/phlex/rails/helpers/link_to.rb b/lib/phlex/rails/helpers/link_to.rb index 274ec4f5..48f5ca32 100644 --- a/lib/phlex/rails/helpers/link_to.rb +++ b/lib/phlex/rails/helpers/link_to.rb @@ -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) + 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