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

Improved element register #531

Merged
merged 3 commits 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
6 changes: 3 additions & 3 deletions lib/phlex/element_clobbering_guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
module Phlex
module ElementClobberingGuard
def method_added(method_name)
if method_name[0] == "_" && private_instance_methods.include?(:"__phlex#{method_name}__")
if method_name[0] == "_" && element_method?(method_name[1..].to_sym)
raise NameError, "👋 Redefining the method `#{name}##{method_name}` is not a good idea."
else
super
end

super
end
end
end
29 changes: 13 additions & 16 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@
end

module Phlex::Elements
private def slow_registered_elements
private_instance_methods
.lazy
.map(&:to_s)
.select { |m| m.start_with?("__phlex_") }
.map { |m| m[8...-2].to_sym }
def registered_elements
@registered_elements ||= Concurrent::Map.new
end

def register_element(element, tag: element.name.tr("_", "-"))
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
# frozen_string_literal: true

def __phlex_#{element}__(**attributes, &block)

def #{element}(**attributes, &block)
target = @_context.target
#{' '}

if attributes.length > 0 # with attributes
if block_given? # with content block
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
Expand All @@ -41,21 +38,21 @@ def __phlex_#{element}__(**attributes, &block)
nil
end

alias_method :_#{element}, :__phlex_#{element}__
alias_method :#{element}, :__phlex_#{element}__
private :__phlex_#{element}__
alias_method :_#{element}, :#{element}
RUBY

registered_elements[element] = tag

element
end

def register_void_element(element, tag: element.name.tr("_", "-"))
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
# frozen_string_literal: true

def __phlex_#{element}__(**attributes)
def #{element}(**attributes)
target = @_context.target
#{' '}

if attributes.length > 0 # with attributes
target << "<#{tag}" << (Phlex::ATTRIBUTE_CACHE[respond_to?(:process_attributes) ? (attributes.hash + self.class.hash) : attributes.hash] || __attributes__(**attributes)) << ">"
else # without attributes
Expand All @@ -65,11 +62,11 @@ def __phlex_#{element}__(**attributes)
nil
end

alias_method :_#{element}, :__phlex_#{element}__
alias_method :#{element}, :__phlex_#{element}__
private :__phlex_#{element}__
alias_method :_#{element}, :#{element}
RUBY

registered_elements[element] = tag

element
end
end
11 changes: 11 additions & 0 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ def rendered_at_least_once!
alias_method :__attributes__, :__final_attributes__
alias_method :call, :__final_call__
end

# @api private
def element_method?(method_name)
return false unless instance_methods.include?(method_name)

owner = instance_method(method_name).owner

return true if owner.is_a?(Phlex::Elements) && owner.registered_elements[method_name]

false
end
end

# Renders the view and returns the buffer. The default buffer is a mutable String.
Expand Down
16 changes: 8 additions & 8 deletions test/phlex/view/svg_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@
describe Phlex::SVG do
extend ViewHelper

Phlex::SVG::StandardElements.send(:slow_registered_elements).each do |method_name|
with "<#{method_name}> called with an underscore prefix while overridden" do
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 :template do
send("_#{method_name}")
end

define_method method_name do
define_method tag do
super(class: "overridden")
end
end

it "is not overridden" do
expect(output).to be == %(<#{method_name}></#{method_name}>)
expect(output).to be == %(<#{tag}></#{tag}>)
end
end

with "<#{method_name}> with block content and attributes" do
with "<#{tag}> with block content and attributes" do
svg_view do
define_method :template do
send(method_name, class: "class", id: "id", disabled: true, selected: false) { text { "Hello" } }
end
end

it "produces the correct output" do
expect(output).to be == %(<#{method_name} class="class" id="id" disabled><text>Hello</text></#{method_name}>)
expect(output).to be == %(<#{tag} class="class" id="id" disabled><text>Hello</text></#{tag}>)
end
end

with "<#{method_name}> with block text content and attributes" do
with "<#{tag}> with block text content and attributes" do
svg_view do
define_method :template do
send(method_name, class: "class", id: "id", disabled: true, selected: false) { "content" }
end
end

it "produces the correct output" do
expect(output).to be == %(<#{method_name} class="class" id="id" disabled>content</#{method_name}>)
expect(output).to be == %(<#{tag} class="class" id="id" disabled>content</#{tag}>)
end
end
end
Expand Down
69 changes: 13 additions & 56 deletions test/phlex/view/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
describe Phlex::HTML do
extend ViewHelper

Phlex::HTML::StandardElements.send(:slow_registered_elements).each do |method_name|
# template_tag is a special case because the method_name != the tag name
next if method_name == :template_tag

with "<#{method_name}> called with an underscore prefix while overridden" do
Phlex::HTML::StandardElements.registered_elements.each do |method_name, tag|
with "<#{tag}> called with an underscore prefix while overridden" do
view do
define_method :template do
send("_#{method_name}")
Expand All @@ -19,101 +16,61 @@
end

it "is not overridden" do
expect(output).to be == %(<#{method_name}></#{method_name}>)
expect(output).to be == %(<#{tag}></#{tag}>)
end
end

with "<#{method_name}> with block content and attributes" do
with "<#{tag}> with block content and attributes" do
view do
define_method :template do
send(method_name, class: "class", id: "id", disabled: true, selected: false) { h1 { "Hello" } }
end
end

it "produces the correct output" do
expect(output).to be == %(<#{method_name} class="class" id="id" disabled><h1>Hello</h1></#{method_name}>)
expect(output).to be == %(<#{tag} class="class" id="id" disabled><h1>Hello</h1></#{tag}>)
end
end

with "<#{method_name}> with block text content and attributes" do
with "<#{tag}> with block text content and attributes" do
view do
define_method :template do
send(method_name, class: "class", id: "id", disabled: true, selected: false) { "content" }
end
end

it "produces the correct output" do
expect(output).to be == %(<#{method_name} class="class" id="id" disabled>content</#{method_name}>)
end
end
end

with "<template> called with an underscore prefix while overridden" do
view do
define_method :template do
send("_template_tag")
end

define_method :template_tag do
super(class: "overridden")
end
end

it "is not overridden" do
expect(output).to be == %(<template></template>)
end
end

with "<template> with block content and attributes" do
view do
define_method :template do
template_tag(class: "class", id: "id", disabled: true, selected: false) { h1 { "Hello" } }
expect(output).to be == %(<#{tag} class="class" id="id" disabled>content</#{tag}>)
end
end

it "produces the correct output" do
expect(output).to be == %(<template class="class" id="id" disabled><h1>Hello</h1></template>)
end
end

with "<template> with block text content and attributes" do
view do
define_method :template do
template_tag(class: "class", id: "id", disabled: true, selected: false) { "content" }
end
end

it "produces the correct output" do
expect(output).to be == %(<template class="class" id="id" disabled>content</template>)
end
end

Phlex::HTML::VoidElements.send(:slow_registered_elements).each do |method_name|
with "<#{method_name}> called with an underscore prefix while overridden" do
Phlex::HTML::VoidElements.registered_elements.each do |method_name, tag|
with "<#{tag}> called with an underscore prefix while overridden" do
view do
define_method :template do
send("_#{method_name}")
end

define_method method_name do
define_method tag do
super(class: "overridden")
end
end

it "is not overridden" do
expect(output).to be == %(<#{method_name}>)
expect(output).to be == %(<#{tag}>)
end
end

with "<#{method_name}> with attributes" do
with "<#{tag}> with attributes" do
view do
define_method :template do
send(method_name, class: "class", id: "id", disabled: true, selected: false)
end
end

it "produces the correct output" do
expect(output).to be == %(<#{method_name} class="class" id="id" disabled>)
expect(output).to be == %(<#{tag} class="class" id="id" disabled>)
end
end
end
Expand Down