Skip to content

Commit

Permalink
Rename targetbuffer (#666)
Browse files Browse the repository at this point in the history
If we're going to do selective rendering with a target ID, we should
rename the buffer to something other than `target`.
  • Loading branch information
joeldrapper authored Mar 2, 2024
1 parent e6b9d1e commit 199c731
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
19 changes: 12 additions & 7 deletions lib/phlex/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
# @api private
class Phlex::Context
def initialize
@target = +""
@buffer = +""
@capturing = false
end

attr_accessor :target, :capturing
attr_accessor :buffer, :capturing

def capturing_into(new_target)
original_target = @target
# Added for backwards compatibility with phlex-rails. We can remove this with 2.0
def target
@buffer
end

def capturing_into(new_buffer)
original_buffer = @buffer
original_capturing = @capturing

begin
@target = new_target
@buffer = new_buffer
@capturing = true
yield
ensure
@target = original_target
@buffer = original_buffer
@capturing = original_capturing
end

new_target
new_buffer
end
end
20 changes: 10 additions & 10 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ def register_element(method_name, tag: method_name.name.tr("_", "-"), deprecated
def #{method_name}(**attributes, &block)
#{deprecation}
target = @_context.target
buffer = @_context.buffer
if attributes.length > 0 # with attributes
if block # with content block
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
yield_content(&block)
target << "</#{tag}>"
buffer << "</#{tag}>"
else # without content block
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << "></#{tag}>"
buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << "></#{tag}>"
end
else # without attributes
if block # with content block
target << "<#{tag}>"
buffer << "<#{tag}>"
yield_content(&block)
target << "</#{tag}>"
buffer << "</#{tag}>"
else # without content block
target << "<#{tag}></#{tag}>"
buffer << "<#{tag}></#{tag}>"
end
end
Expand Down Expand Up @@ -90,12 +90,12 @@ def register_void_element(method_name, tag: method_name.name.tr("_", "-"), depre
def #{method_name}(**attributes)
#{deprecation}
target = @_context.target
buffer = @_context.buffer
if attributes.length > 0 # with attributes
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
buffer << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
else # without attributes
target << "<#{tag}>"
buffer << "<#{tag}>"
end
nil
Expand Down
2 changes: 1 addition & 1 deletion lib/phlex/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __unbuffered_class__

# Output an HTML doctype.
def doctype
@_context.target << "<!DOCTYPE html>"
@_context.buffer << "<!DOCTYPE html>"
nil
end

Expand Down
46 changes: 23 additions & 23 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil,
end
end

buffer << context.target unless parent
buffer << context.buffer unless parent
end

# Output text content. The text will be HTML-escaped.
Expand All @@ -150,13 +150,13 @@ def plain(content)
# @return [nil]
# @yield If a block is given, it yields the block with no arguments.
def whitespace(&block)
target = @_context.target
buffer = @_context.buffer

target << " "
buffer << " "

if block_given?
yield_content(&block)
target << " "
buffer << " "
end

nil
Expand All @@ -165,11 +165,11 @@ def whitespace(&block)
# Output an HTML comment.
# @return [nil]
def comment(&block)
target = @_context.target
buffer = @_context.buffer

target << "<!-- "
buffer << "<!-- "
yield_content(&block)
target << " -->"
buffer << " -->"

nil
end
Expand All @@ -180,7 +180,7 @@ def comment(&block)
def unsafe_raw(content = nil)
return nil unless content

@_context.target << content
@_context.buffer << content
nil
end

Expand All @@ -199,9 +199,9 @@ def capture(&block)
def flush
return if @_context.capturing

target = @_context.target
@_buffer << target.dup
target.clear
buffer = @_context.buffer
@_buffer << buffer.dup
buffer.clear
end

# Render another component, block or enumerable
Expand Down Expand Up @@ -302,11 +302,11 @@ def after_template
def yield_content
return unless block_given?

target = @_context.target
buffer = @_context.buffer

original_length = target.length
original_length = buffer.length
content = yield(self)
__text__(content) if original_length == target.length
__text__(content) if original_length == buffer.length

nil
end
Expand All @@ -316,11 +316,11 @@ def yield_content
def yield_content_with_no_args
return unless block_given?

target = @_context.target
buffer = @_context.buffer

original_length = target.length
original_length = buffer.length
content = yield
__text__(content) if original_length == target.length
__text__(content) if original_length == buffer.length

nil
end
Expand All @@ -331,11 +331,11 @@ def yield_content_with_no_args
def yield_content_with_args(*args)
return unless block_given?

target = @_context.target
buffer = @_context.buffer

original_length = target.length
original_length = buffer.length
content = yield(*args)
__text__(content) if original_length == target.length
__text__(content) if original_length == buffer.length

nil
end
Expand All @@ -345,14 +345,14 @@ def yield_content_with_args(*args)
def __text__(content)
case content
when String
@_context.target << Phlex::Escape.html_escape(content)
@_context.buffer << Phlex::Escape.html_escape(content)
when Symbol
@_context.target << Phlex::Escape.html_escape(content.name)
@_context.buffer << Phlex::Escape.html_escape(content.name)
when nil
nil
else
if (formatted_object = format_object(content))
@_context.target << Phlex::Escape.html_escape(formatted_object)
@_context.buffer << Phlex::Escape.html_escape(formatted_object)
else
return false
end
Expand Down

0 comments on commit 199c731

Please sign in to comment.