lib/erubi.rb in erubi-1.11.0 vs lib/erubi.rb in erubi-1.12.0

- old
+ new

@@ -1,9 +1,9 @@ # frozen_string_literal: true module Erubi - VERSION = '1.11.0' + VERSION = '1.12.0' # :nocov: if RUBY_VERSION >= '1.9' RANGE_FIRST = 0 RANGE_LAST = -1 @@ -12,37 +12,45 @@ RANGE_LAST = -1..-1 end MATCH_METHOD = RUBY_VERSION >= '2.4' ? :match? : :match SKIP_DEFINED_FOR_INSTANCE_VARIABLE = RUBY_VERSION > '3' + FREEZE_TEMPLATE_LITERALS = !eval("''").frozen? && RUBY_VERSION >= '2.1' # :nocov: begin - require 'cgi/escape' + require 'erb/escape' # :nocov: - unless CGI.respond_to?(:escapeHTML) # work around for JRuby 9.1 - CGI = Object.new - CGI.extend(defined?(::CGI::Escape) ? ::CGI::Escape : ::CGI::Util) - end + define_singleton_method(:h, ERB::Escape.instance_method(:html_escape)) # :nocov: - # Escape characters with their HTML/XML equivalents. - def self.h(value) - CGI.escapeHTML(value.to_s) - end rescue LoadError - # :nocov: - ESCAPE_TABLE = {'&' => '&amp;'.freeze, '<' => '&lt;'.freeze, '>' => '&gt;'.freeze, '"' => '&quot;'.freeze, "'" => '&#39;'.freeze}.freeze - if RUBY_VERSION >= '1.9' - def self.h(value) - value.to_s.gsub(/[&<>"']/, ESCAPE_TABLE) + begin + require 'cgi/escape' + # :nocov: + unless CGI.respond_to?(:escapeHTML) # work around for JRuby 9.1 + CGI = Object.new + CGI.extend(defined?(::CGI::Escape) ? ::CGI::Escape : ::CGI::Util) end - else + # :nocov: + # Escape characters with their HTML/XML equivalents. def self.h(value) - value.to_s.gsub(/[&<>"']/){|s| ESCAPE_TABLE[s]} + CGI.escapeHTML(value.to_s) end + rescue LoadError + # :nocov: + ESCAPE_TABLE = {'&' => '&amp;'.freeze, '<' => '&lt;'.freeze, '>' => '&gt;'.freeze, '"' => '&quot;'.freeze, "'" => '&#39;'.freeze}.freeze + if RUBY_VERSION >= '1.9' + def self.h(value) + value.to_s.gsub(/[&<>"']/, ESCAPE_TABLE) + end + else + def self.h(value) + value.to_s.gsub(/[&<>"']/){|s| ESCAPE_TABLE[s]} + end + end + # :nocov: end - # :nocov: end class Engine # The default regular expression used for scanning. DEFAULT_REGEXP = /<%(={1,2}|-|\#|%)?(.*?)([-=])?%>([ \t]*\r?\n)?/m @@ -93,10 +101,10 @@ literal_prefix = properties[:literal_prefix] || '<%' literal_postfix = properties[:literal_postfix] || '%>' preamble = properties[:preamble] || "#{bufvar} = #{bufval};" postamble = properties[:postamble] || "#{bufvar}.to_s\n" @chain_appends = properties[:chain_appends] - @text_end = if properties.fetch(:freeze_template_literals, RUBY_VERSION >= '2.1') + @text_end = if properties.fetch(:freeze_template_literals, FREEZE_TEMPLATE_LITERALS) "'.freeze" else "'" end