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

Fix for components that render content before a fragment is found #691

Merged
merged 5 commits into from
Mar 22, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ def context
# @return [nil]
# @see #format_object
def plain(content)
context = @_context
return if context.fragments && !context.in_target_fragment

unless __text__(content)
raise ArgumentError, "You've passed an object to plain that is not handled by format_object. See https://rubydoc.info/gems/phlex/Phlex/SGML#format_object-instance_method for more information"
end
Expand Down Expand Up @@ -374,6 +371,9 @@ def yield_content_with_args(*args)
# Performs the same task as the public method #plain, but does not raise an error if an unformattable object is passed
# @api private
def __text__(content)
context = @_context
return true if context.fragments && !context.in_target_fragment

case content
when String
@_context.buffer << Phlex::Escape.html_escape(content)
Expand Down
7 changes: 7 additions & 0 deletions test/phlex/selective_rendering.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# frozen_string_literal: true

class ExampleComponent < Phlex::HTML
def view_template(&block)
div(&block)
end
end

class StandardElementExample < Phlex::HTML
def initialize(execution_checker = -> {})
@execution_checker = execution_checker
Expand All @@ -11,6 +17,7 @@ def view_template
comment { h1(id: "target") }
h1 { "Before" }
img(src: "before.jpg")
render ExampleComponent.new { "Should not render" }
whitespace
comment { "This is a comment" }
h1(id: "target") {
Expand Down