Skip to content

Commit

Permalink
Add dynamic tag method
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Aug 12, 2024
1 parent 37700b2 commit fc08e03
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,38 @@ def capture(*args, &block)
end
end

def tag(name, ...)
name = case name
when Symbol then name.name.downcase
when String then name.downcase
else raise ArgumentError.new("Expected the tag namea as a Symbol or String.")
end

if name == "script"
raise ArgumentError.new("You can’t use the `<script>` tag from the `tag` method. Use `unsafe_tag` instead.")
end

if registered_elements[name]
public_send(name, ...)
else
raise ArgumentError.new("Unknown tag: #{name}")
end
end

def unsafe_tag(name, ...)
name = case name
when Symbol then name.name.downcase
when String then name.downcase
else raise ArgumentError.new("Expected the tag namea as a Symbol or String.")
end

if registered_elements[name]
public_send(name, ...)
else
raise ArgumentError.new("Unknown tag: #{name}")
end
end

private

# @api private
Expand Down

0 comments on commit fc08e03

Please sign in to comment.