Skip to content

Commit

Permalink
safe values should be raw (#792)
Browse files Browse the repository at this point in the history
@joeldrapper @willcosgrove 
so I made `html_safe` and `safe` consistent in this pr and also created
`__implicit_output__` and made `plain` use old `__text__` instead so we
can render `html_safe` as `raw` by default, unless it's `plain`. Does
that look good?
  • Loading branch information
Vagab authored Oct 1, 2024
1 parent c534a98 commit afafc0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
31 changes: 28 additions & 3 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
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,11 +295,36 @@ 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 __implicit_output__(content)
context = @_context
return true if context.fragments && !context.in_target_fragment

case content
when Phlex::SGML::SafeObject
context.buffer << content.to_s
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

# same as __implicit_output__ but escapes even `safe` objects
def __text__(content)
context = @_context
return true if context.fragments && !context.in_target_fragment
Expand Down
8 changes: 4 additions & 4 deletions lib/phlex/sgml/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ def #{method_name}(**attributes)
content = yield(self)
if original_length == buffer.bytesize
case content
when ::Phlex::SGML::SafeObject
buffer << content.to_s
when String
buffer << ::Phlex::Escape.html_escape(content)
when Symbol
buffer << ::Phlex::Escape.html_escape(content.name)
when nil
nil
when ::Phlex::SGML::SafeObject
buffer << content.to_s
else
if (formatted_object = format_object(content))
buffer << ::Phlex::Escape.html_escape(formatted_object)
Expand All @@ -67,14 +67,14 @@ def #{method_name}(**attributes)
content = yield(self)
if original_length == buffer.bytesize
case content
when ::Phlex::SGML::SafeObject
buffer << content.to_s
when String
buffer << ::Phlex::Escape.html_escape(content)
when Symbol
buffer << ::Phlex::Escape.html_escape(content.name)
when nil
nil
when ::Phlex::SGML::SafeObject
buffer << content.to_s
else
if (formatted_object = format_object(content))
buffer << ::Phlex::Escape.html_escape(formatted_object)
Expand Down

0 comments on commit afafc0e

Please sign in to comment.