Skip to content

Commit

Permalink
__implicit_output__
Browse files Browse the repository at this point in the history
  • Loading branch information
Vagab committed Sep 25, 2024
1 parent 8cb4b58 commit 92f6f1a
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def context
end

def plain(content)
unless __text__(content)
unless __text__(content)
raise Phlex::ArgumentError.new("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 @@ -271,7 +271,7 @@ def yield_content

original_length = buffer.bytesize
content = yield(self)
__text__(content) if original_length == buffer.bytesize
__implicit_output__(content) if original_length == buffer.bytesize

nil
end
Expand All @@ -283,7 +283,7 @@ def __yield_content_with_no_args__

original_length = buffer.bytesize
content = yield
__text__(content) if original_length == buffer.bytesize
__implicit_output__(content) if original_length == buffer.bytesize

nil
end
Expand All @@ -295,12 +295,12 @@ def __yield_content_with_args__(*)

original_length = buffer.bytesize
content = yield(*)
__text__(content) if original_length == buffer.bytesize
__implicit_output__(content) if original_length == buffer.bytesize

nil
end

def __text__(content)
def __implicit_output__(content)
context = @_context
return true if context.fragments && !context.in_target_fragment

Expand All @@ -324,6 +324,29 @@ def __text__(content)
true
end

# same as __implicit_output__ but escapes even `safe` objects
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)
when Symbol
context.buffer << Phlex::Escape.html_escape(content.name)
when nil
nil
else
if (formatted_object = format_object(content))
context.buffer << Phlex::Escape.html_escape(formatted_object)
else
return false
end
end

true
end

def __attributes__(attributes, buffer = +"")
attributes.each do |k, v|
next unless v
Expand Down

0 comments on commit 92f6f1a

Please sign in to comment.