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

Avoid redundant @_context.target calls #532

Merged
merged 1 commit into from
Mar 10, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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