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

Remove ERB dependency #635

Merged
merged 4 commits into from
Jan 30, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Comment on lines +16 to +17
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up removing this because you need to require ERB anyway, and all versions of ERB have ERB::Util.

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