Skip to content

Commit

Permalink
Add basic escape when ERB not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Jan 30, 2024
1 parent c26a761 commit 99908b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/phlex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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

0 comments on commit 99908b8

Please sign in to comment.