Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/phlex-ruby/phlex
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Sep 14, 2024
2 parents cd85d5f + f59e2b3 commit 2937be1
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 62 deletions.
6 changes: 3 additions & 3 deletions lib/phlex/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def content_type

def column(header = nil, value)
if @_first
@_headers << escape(header)
@_headers << __escape__(header)
elsif header != @_headers[@_current_column_index]
raise "Inconsistent header."
end

@_current_row << escape(value)
@_current_row << __escape__(value)
@_current_column_index += 1
end

Expand Down Expand Up @@ -109,7 +109,7 @@ def helpers
@_view_context
end

def escape(value)
def __escape__(value)
value = trim_whitespace? ? value.to_s.strip : value.to_s
first_char = value[0]
last_char = value[-1]
Expand Down
34 changes: 5 additions & 29 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
# frozen_string_literal: true

# Extending this module provides the {register_element} macro for registering your own custom elements. It's already extended by {HTML} and {SVG}.
# @example
# module MyCustomElements
# extend Phlex::Elements
#
# register_element :trix_editor
# end
#
# class MyComponent < Phlex::HTML
# include MyCustomElements
#
# def view_template
# trix_editor
# end
# end
module Phlex::Elements
# @api private
def registered_elements
@registered_elements ||= {}
def __registered_elements__
@__registered_elements__ ||= {}
end

# Register a custom element. This macro defines an element method for the current class and descendents only. There is no global element registry.
# @param method_name [Symbol]
# @param tag [String] the name of the tag, otherwise this will be the method name with underscores replaced with dashes.
# @return [Symbol] the name of the method created
# @note The methods defined by this macro depend on other methods from {SGML} so they should always be mixed into an {HTML} or {SVG} component.
# @example Register the custom element `<trix-editor>`
# register_element :trix_editor
def register_element(method_name, tag: method_name.name.tr("_", "-"))
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
# frozen_string_literal: true
Expand Down Expand Up @@ -121,13 +98,12 @@ def #{method_name}(**attributes)
alias_method :_#{method_name}, :#{method_name}
RUBY

registered_elements[method_name] = tag
__registered_elements__[method_name] = tag

method_name
end

# @api private
def register_void_element(method_name, tag: method_name.name.tr("_", "-"))
def __register_void_element__(method_name, tag: method_name.name.tr("_", "-"))
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
# frozen_string_literal: true
Expand Down Expand Up @@ -165,7 +141,7 @@ def #{method_name}(**attributes)
alias_method :_#{method_name}, :#{method_name}
RUBY

registered_elements[method_name] = tag
__registered_elements__[method_name] = tag

method_name
end
Expand Down
1 change: 1 addition & 0 deletions lib/phlex/error.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# @api private
module Phlex::Error
end
1 change: 1 addition & 0 deletions lib/phlex/fifo.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# @api private
class Phlex::FIFO
def initialize(max_bytesize: 2_000, max_value_bytesize: 2_000)
@store = {}
Expand Down
22 changes: 11 additions & 11 deletions lib/phlex/html/void_elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,65 @@ module Phlex::HTML::VoidElements
# Outputs an `<area>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/area
register_void_element :area
__register_void_element__ :area

# @!method br(**attributes, &content)
# Outputs a `<br>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/br
register_void_element :br
__register_void_element__ :br

# @!method col(**attributes, &content)
# Outputs a `<col>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/col
register_void_element :col
__register_void_element__ :col

# @!method embed(**attributes, &content)
# Outputs an `<embed>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/embed
register_void_element :embed
__register_void_element__ :embed

# @!method hr(**attributes, &content)
# Outputs an `<hr>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/hr
register_void_element :hr
__register_void_element__ :hr

# @!method img(**attributes, &content)
# Outputs an `<img>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/img
register_void_element :img
__register_void_element__ :img

# @!method input(**attributes, &content)
# Outputs an `<input>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/input
register_void_element :input
__register_void_element__ :input

# @!method link(**attributes, &content)
# Outputs a `<link>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/link
register_void_element :link
__register_void_element__ :link

# @!method meta(**attributes, &content)
# Outputs a `<meta>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/meta
register_void_element :meta
__register_void_element__ :meta

# @!method source(**attributes, &content)
# Outputs a `<source>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/source
register_void_element :source
__register_void_element__ :source

# @!method track(**attributes, &content)
# Outputs a `<track>` tag.
# @return [nil]
# @see https://developer.mozilla.org/docs/Web/HTML/Element/track
register_void_element :track
__register_void_element__ :track
end
23 changes: 7 additions & 16 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ def new(*, **, &block)
end
end

# @api private
def __element_method__?(method_name)
if instance_methods.include?(method_name)
owner = instance_method(method_name).owner

if Phlex::Elements === owner && owner.registered_elements[method_name]
if Phlex::Elements === owner && owner.__registered_elements__[method_name]
true
else
false
Expand Down Expand Up @@ -120,7 +119,7 @@ def call(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: n
else
view_template do |*args|
if args.length > 0
yield_content_with_args(*args, &block)
__yield_content_with_args__(*args, &block)
else
yield_content(&block)
end
Expand Down Expand Up @@ -217,7 +216,7 @@ def capture(*args, &block)
return "" unless block

if args.length > 0
@_context.capturing_into(+"") { yield_content_with_args(*args, &block) }
@_context.capturing_into(+"") { __yield_content_with_args__(*args, &block) }
else
@_context.capturing_into(+"") { yield_content(&block) }
end
Expand All @@ -234,7 +233,7 @@ def tag(name, ...)
raise Phlex::ArgumentError.new("You can’t use the `<script>` tag from the `tag` method. Use `unsafe_tag` instead, but be careful if using user input.")
end

if registered_elements[normalized_name]
if __registered_elements__[normalized_name]
public_send(normalized_name, ...)
else
raise Phlex::ArgumentError.new("Unknown tag: #{normalized_name}")
Expand Down Expand Up @@ -267,7 +266,7 @@ def render(renderable = nil, &)
renderable.each { |r| render(r, &) }
when Proc, Method
if renderable.arity == 0
yield_content_with_no_args(&renderable)
__yield_content_with_no_args__(&renderable)
else
yield_content(&renderable)
end
Expand Down Expand Up @@ -351,7 +350,7 @@ def yield_content

# Same as {#yield_content} but yields no arguments.
# @yield Yields the block with no arguments.
def yield_content_with_no_args
def __yield_content_with_no_args__
return unless block_given?

buffer = @_context.buffer
Expand All @@ -366,7 +365,7 @@ def yield_content_with_no_args
# Same as {#yield_content} but accepts a splat of arguments to yield. This is slightly slower than {#yield_content}.
# @yield [*args] Yields the given arguments.
# @return [nil]
def yield_content_with_args(*)
def __yield_content_with_args__(*)
return unless block_given?

buffer = @_context.buffer
Expand All @@ -378,8 +377,6 @@ def yield_content_with_args(*)
nil
end

# Performs the same task as the public method #plain, but does not raise an error if an unformattable object is passed
# @api private
def __text__(content)
context = @_context
return true if context.fragments && !context.in_target_fragment
Expand All @@ -402,7 +399,6 @@ def __text__(content)
true
end

# @api private
def __attributes__(attributes, buffer = +"")
attributes.each do |k, v|
next unless v
Expand Down Expand Up @@ -483,8 +479,6 @@ def __attributes__(attributes, buffer = +"")
buffer
end

# @api private
#
# Provides the nested-attributes case for serializing out attributes.
# This allows us to skip many of the checks the `__attributes__` method must perform.
def __nested_attributes__(attributes, base_name, buffer = +"")
Expand Down Expand Up @@ -527,7 +521,6 @@ def __nested_attributes__(attributes, base_name, buffer = +"")
end
end

# @api private
def __nested_tokens__(tokens)
buffer = +""

Expand Down Expand Up @@ -566,7 +559,6 @@ def __nested_tokens__(tokens)
buffer
end

# @api private
def __classes__(c)
case c
when String
Expand Down Expand Up @@ -597,7 +589,6 @@ def __classes__(c)
end
end

# @api private
def __styles__(s)
style = case s
when String
Expand Down
4 changes: 2 additions & 2 deletions quickdraw/html/elements.test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

Phlex::HTML::StandardElements.registered_elements.each do |method_name, tag|
Phlex::HTML::StandardElements.__registered_elements__.each do |method_name, tag|
test "<#{tag}> called with an underscore prefix while overridden" do
example = Class.new(Phlex::HTML) do
define_method :view_template do
Expand Down Expand Up @@ -56,7 +56,7 @@
end
end

Phlex::HTML::VoidElements.registered_elements.each do |method_name, tag|
Phlex::HTML::VoidElements.__registered_elements__.each do |method_name, tag|
test "<#{tag}> called with an underscore prefix while overridden" do
example = Class.new(Phlex::HTML) do
define_method :view_template do
Expand Down
2 changes: 1 addition & 1 deletion test/phlex/view/svg_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe Phlex::SVG do
extend ViewHelper

Phlex::SVG::StandardElements.registered_elements.each do |method_name, tag|
Phlex::SVG::StandardElements.__registered_elements__.each do |method_name, tag|
with "<#{tag}> called with an underscore prefix while overridden" do
svg_view do
define_method :view_template do
Expand Down

0 comments on commit 2937be1

Please sign in to comment.