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

Inline yield_content and __text__ #764

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Inline yield_content and __text__
  • Loading branch information
joeldrapper committed Sep 4, 2024
commit 2bf0488911a8de8dda3da8be208133fd7ccbe230
53 changes: 46 additions & 7 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ def register_element(method_name, tag: method_name.name.tr("_", "-"))
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
# frozen_string_literal: true

def #{method_name}(content = Phlex::Null, **attributes, &block)
def #{method_name}(content = Phlex::Null, **attributes)
context = @_context
buffer = context.buffer
fragment = context.fragments
target_found = false
block_given = block_given?

if content != Phlex::Null && !(String === content)
raise ArgumentError.new("Only String allowed for inline tags content")
end

if block && content != Phlex::Null
if block_given && content != Phlex::Null
raise ArgumentError.new("Using inline and block syntax at same time is forbidden")
end

Expand All @@ -56,16 +57,35 @@ def #{method_name}(content = Phlex::Null, **attributes, &block)
context.begin_target(id)
target_found = true
else
yield(self) if block
yield(self) if block_given
return nil
end
end
end

if attributes.length > 0 # with attributes
if block # with content block
if block_given # with content block
buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">"
yield_content(&block)

original_length = buffer.bytesize
content = yield(self)
if original_length == buffer.bytesize
case content
when String
buffer << Phlex::Escape.html_escape(content)
when Symbol
buffer << Phlex::Escape.html_escape(content.name)
when nil
nil
else
if (formatted_object = format_object(content))
buffer << Phlex::Escape.html_escape(formatted_object)
else
return false
end
end
end

buffer << "</#{tag}>"
elsif content != Phlex::Null # with inline content
buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << ">"
Expand All @@ -75,9 +95,28 @@ def #{method_name}(content = Phlex::Null, **attributes, &block)
buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[attributes] ||= __attributes__(attributes)) << "></#{tag}>"
end
else # without attributes
if block # with content block
if block_given # with content block
buffer << "<#{tag}>"
yield_content(&block)

original_length = buffer.bytesize
content = yield(self)
if original_length == buffer.bytesize
case content
when String
buffer << Phlex::Escape.html_escape(content)
when Symbol
buffer << Phlex::Escape.html_escape(content.name)
when nil
nil
else
if (formatted_object = format_object(content))
buffer << Phlex::Escape.html_escape(formatted_object)
else
return false
end
end
end

buffer << "</#{tag}>"
elsif content != Phlex::Null # with inline content
buffer << "<#{tag}>"
Expand Down