Skip to content

Commit

Permalink
Avoid redundant @_context.target calls (#532)
Browse files Browse the repository at this point in the history
We don’t need to call `@_context.target` multiple times in the same
method.
  • Loading branch information
joeldrapper authored Mar 10, 2023
2 parents 68e9d2b + 3f4f107 commit 642bbbf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
20 changes: 12 additions & 8 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@ def register_element(element, tag: element.name.tr("_", "-"))
# frozen_string_literal: true
def __phlex_#{element}__(**attributes, &block)
target = @_context.target
#{' '}
if attributes.length > 0 # with attributes
if block_given? # with content block
@_context.target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
yield_content(&block)
@_context.target << "</#{tag}>"
target << "</#{tag}>"
else # without content block
@_context.target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << "></#{tag}>"
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << "></#{tag}>"
end
else # without attributes
if block_given? # with content block
@_context.target << "<#{tag}>"
target << "<#{tag}>"
yield_content(&block)
@_context.target << "</#{tag}>"
target << "</#{tag}>"
else # without content block
@_context.target << "<#{tag}></#{tag}>"
target << "<#{tag}></#{tag}>"
end
end
Expand All @@ -52,10 +54,12 @@ def register_void_element(element, tag: element.name.tr("_", "-"))
# frozen_string_literal: true
def __phlex_#{element}__(**attributes)
target = @_context.target
#{' '}
if attributes.length > 0 # with attributes
@_context.target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
else # without attributes
@_context.target << "<#{tag}>"
target << "<#{tag}>"
end
nil
Expand Down
25 changes: 16 additions & 9 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ def plain(content)
# Output a whitespace character. This is useful for getting inline elements to wrap. If you pass a block, a whitespace will be output before and after yielding the block.
# @return [nil]
def whitespace
@_context.target << " "
target = @_context.target

target << " "

if block_given?
yield
@_context.target << " "
target << " "
end

nil
Expand All @@ -129,9 +131,11 @@ def whitespace
# Output an HTML comment.
# @return [nil]
def comment(&block)
@_context.target << "<!-- "
target = @_context.target

target << "<!-- "
yield_content(&block)
@_context.target << " -->"
target << " -->"

nil
end
Expand Down Expand Up @@ -208,10 +212,11 @@ def capture(&block)
private def yield_content
return unless block_given?

original_length = @_context.target.length
content = yield(self)
target = @_context.target

plain(content) if original_length == @_context.target.length
original_length = target.length
content = yield(self)
plain(content) if original_length == target.length

nil
end
Expand All @@ -221,9 +226,11 @@ def capture(&block)
private def yield_content_with_args(*args)
return unless block_given?

original_length = @_context.target.length
target = @_context.target

original_length = target.length
content = yield(*args)
plain(content) if original_length == @_context.target.length
plain(content) if original_length == target.length

nil
end
Expand Down

0 comments on commit 642bbbf

Please sign in to comment.