Skip to content

Commit

Permalink
Remove ERB dependency (#635)
Browse files Browse the repository at this point in the history
ERB is bundled with Ruby already
  • Loading branch information
joeldrapper committed Jan 30, 2024
2 parents df3d961 + b23b4d8 commit 14505e4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
8 changes: 8 additions & 0 deletions lib/phlex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ module Phlex
# rescue Phlex::Error
module Error; end

if defined?(ERB::Escape)
Escape = ERB::Escape
elsif defined?(ERB::Util)
Escape = ERB::Util
else
Escape = BasicEscape
end

# A specialised ArgumentError for Phlex.
class ArgumentError < ::ArgumentError
include Error
Expand Down
16 changes: 16 additions & 0 deletions lib/phlex/basic_escape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Phlex
module BasicEscape
def html_escape(string)
string
.dup
.gsub!("&", "&amp;")
.gsub!("<", "&lt;")
.gsub!(">", "&gt;")
.gsub!('"', "&quot;")
.gsub!("'", "&#39;")
.freeze
end
end
end
16 changes: 8 additions & 8 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,14 @@ def yield_content_with_args(*args)
def __text__(content)
case content
when String
@_context.target << ERB::Escape.html_escape(content)
@_context.target << Phlex::Escape.html_escape(content)
when Symbol
@_context.target << ERB::Escape.html_escape(content.name)
@_context.target << Phlex::Escape.html_escape(content.name)
when nil
nil
else
if (formatted_object = format_object(content))
@_context.target << ERB::Escape.html_escape(formatted_object)
@_context.target << Phlex::Escape.html_escape(formatted_object)
else
return false
end
Expand Down Expand Up @@ -402,9 +402,9 @@ def __build_attributes__(attributes, buffer:)
when true
buffer << " " << name
when String
buffer << " " << name << '="' << ERB::Escape.html_escape(v) << '"'
buffer << " " << name << '="' << Phlex::Escape.html_escape(v) << '"'
when Symbol
buffer << " " << name << '="' << ERB::Escape.html_escape(v.name) << '"'
buffer << " " << name << '="' << Phlex::Escape.html_escape(v.name) << '"'
when Integer, Float
buffer << " " << name << '="' << v.to_s << '"'
when Hash
Expand All @@ -417,11 +417,11 @@ def __build_attributes__(attributes, buffer:)
}, buffer: buffer
)
when Array
buffer << " " << name << '="' << ERB::Escape.html_escape(v.compact.join(" ")) << '"'
buffer << " " << name << '="' << Phlex::Escape.html_escape(v.compact.join(" ")) << '"'
when Set
buffer << " " << name << '="' << ERB::Escape.html_escape(v.to_a.compact.join(" ")) << '"'
buffer << " " << name << '="' << Phlex::Escape.html_escape(v.to_a.compact.join(" ")) << '"'
else
buffer << " " << name << '="' << ERB::Escape.html_escape(v.to_str) << '"'
buffer << " " << name << '="' << Phlex::Escape.html_escape(v.to_str) << '"'
end
end

Expand Down
1 change: 0 additions & 1 deletion phlex.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Gem::Specification.new do |spec|
end
spec.require_paths = ["lib"]

spec.add_dependency "erb", ">= 4"
spec.add_dependency "zeitwerk", "~> 2.6"

spec.metadata["rubygems_mfa_required"] = "true"
Expand Down

0 comments on commit 14505e4

Please sign in to comment.