From 62f4fc9a5acd5cc22f47e9d5131ad146e089b567 Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Tue, 27 Jul 2021 23:09:13 -0700 Subject: [PATCH] Merge Haml::Utils and Haml::HamlUtil as Haml::Util --- ext/haml/haml.c | 8 +++--- lib/haml/attribute_builder.rb | 2 +- lib/haml/compiler/script_compiler.rb | 2 +- lib/haml/compiler/tag_compiler.rb | 2 +- lib/haml/escapable.rb | 4 +-- lib/haml/filters/base.rb | 2 +- lib/haml/filters/escaped.rb | 4 +-- lib/haml/filters/plain.rb | 4 +-- lib/haml/filters/preserve.rb | 4 +-- lib/haml/filters/text_base.rb | 4 +-- lib/haml/filters/tilt_base.rb | 4 +-- lib/haml/force_escapable.rb | 2 +- lib/haml/parser.rb | 34 +++++++++++------------ lib/haml/rails_template.rb | 7 +---- lib/haml/{parser/haml_util.rb => util.rb} | 34 ++++++++++++----------- lib/haml/utils.rb | 20 ------------- test/test_helper.rb | 2 +- 17 files changed, 58 insertions(+), 81 deletions(-) rename lib/haml/{parser/haml_util.rb => util.rb} (93%) delete mode 100644 lib/haml/utils.rb diff --git a/ext/haml/haml.c b/ext/haml/haml.c index f8ab9cef..fe9d1987 100644 --- a/ext/haml/haml.c +++ b/ext/haml/haml.c @@ -504,14 +504,14 @@ rb_haml_build(int argc, VALUE *argv, RB_UNUSED_VAR(VALUE self)) void Init_haml(void) { - VALUE mHaml, mUtils; + VALUE mHaml, mUtil; - mHaml = rb_define_module("Haml"); + mHaml = rb_define_module("Haml"); mObjectRef = rb_define_module_under(mHaml, "ObjectRef"); - mUtils = rb_define_module_under(mHaml, "Utils"); + mUtil = rb_define_module_under(mHaml, "Util"); mAttributeBuilder = rb_define_module_under(mHaml, "AttributeBuilder"); - rb_define_singleton_method(mUtils, "escape_html", rb_escape_html, 1); + rb_define_singleton_method(mUtil, "escape_html", rb_escape_html, 1); rb_define_singleton_method(mAttributeBuilder, "build", rb_haml_build, -1); rb_define_singleton_method(mAttributeBuilder, "build_id", rb_haml_build_id, -1); rb_define_singleton_method(mAttributeBuilder, "build_class", rb_haml_build_class, -1); diff --git a/lib/haml/attribute_builder.rb b/lib/haml/attribute_builder.rb index 04210737..f478f0b6 100644 --- a/lib/haml/attribute_builder.rb +++ b/lib/haml/attribute_builder.rb @@ -158,7 +158,7 @@ module Haml::AttributeBuilder def escape_html(escape_attrs, str) if escape_attrs - Haml::Utils.escape_html(str) + Haml::Util.escape_html(str) else str end diff --git a/lib/haml/compiler/script_compiler.rb b/lib/haml/compiler/script_compiler.rb index fbe2cdcf..70fb3318 100644 --- a/lib/haml/compiler/script_compiler.rb +++ b/lib/haml/compiler/script_compiler.rb @@ -65,7 +65,7 @@ module Haml def static_compile(node) str = eval(node.value[:text]).to_s if node.value[:escape_html] - str = Haml::Utils.escape_html(str) + str = Haml::Util.escape_html(str) elsif node.value[:preserve] str = ScriptCompiler.find_and_preserve(str, %w(textarea pre code)) end diff --git a/lib/haml/compiler/tag_compiler.rb b/lib/haml/compiler/tag_compiler.rb index 9104d279..89dc3b4a 100644 --- a/lib/haml/compiler/tag_compiler.rb +++ b/lib/haml/compiler/tag_compiler.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -require 'haml/parser/haml_util' +require 'haml/util' require 'haml/attribute_compiler' require 'haml/string_splitter' diff --git a/lib/haml/escapable.rb b/lib/haml/escapable.rb index 6a39ae11..bb068fad 100644 --- a/lib/haml/escapable.rb +++ b/lib/haml/escapable.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true -require 'haml/utils' +require 'haml/util' module Haml class Escapable < Temple::Filters::Escapable def initialize(opts = {}) super @escape_code = options[:escape_code] || - "::Haml::Utils.escape_html#{options[:use_html_safe] ? '_safe' : ''}((%s))" + "::Haml::Util.escape_html#{options[:use_html_safe] ? '_safe' : ''}((%s))" @escaper = eval("proc {|v| #{@escape_code % 'v'} }") end end diff --git a/lib/haml/filters/base.rb b/lib/haml/filters/base.rb index 97ec0142..4d344ef0 100644 --- a/lib/haml/filters/base.rb +++ b/lib/haml/filters/base.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -require 'haml/parser/haml_util' +require 'haml/util' module Haml class Filters diff --git a/lib/haml/filters/escaped.rb b/lib/haml/filters/escaped.rb index f5375e95..eb7abdf4 100644 --- a/lib/haml/filters/escaped.rb +++ b/lib/haml/filters/escaped.rb @@ -11,8 +11,8 @@ module Haml private def compile_text(text) - if ::Haml::HamlUtil.contains_interpolation?(text) - [:dynamic, ::Haml::HamlUtil.unescape_interpolation(text)] + if ::Haml::Util.contains_interpolation?(text) + [:dynamic, ::Haml::Util.unescape_interpolation(text)] else [:static, text] end diff --git a/lib/haml/filters/plain.rb b/lib/haml/filters/plain.rb index a910b7bd..16e8abbb 100644 --- a/lib/haml/filters/plain.rb +++ b/lib/haml/filters/plain.rb @@ -6,14 +6,14 @@ module Haml class Plain < Base def compile(node) text = node.value[:text] - text = text.rstrip unless ::Haml::HamlUtil.contains_interpolation?(text) # for compatibility + text = text.rstrip unless ::Haml::Util.contains_interpolation?(text) # for compatibility [:multi, *compile_plain(text)] end private def compile_plain(text) - string_literal = ::Haml::HamlUtil.unescape_interpolation(text) + string_literal = ::Haml::Util.unescape_interpolation(text) StringSplitter.compile(string_literal).map do |temple| type, str = temple case type diff --git a/lib/haml/filters/preserve.rb b/lib/haml/filters/preserve.rb index f26492c2..0f42a0ab 100644 --- a/lib/haml/filters/preserve.rb +++ b/lib/haml/filters/preserve.rb @@ -11,8 +11,8 @@ module Haml private def compile_text(text) - if ::Haml::HamlUtil.contains_interpolation?(text) - [:dynamic, ::Haml::HamlUtil.unescape_interpolation(text)] + if ::Haml::Util.contains_interpolation?(text) + [:dynamic, ::Haml::Util.unescape_interpolation(text)] else [:static, text] end diff --git a/lib/haml/filters/text_base.rb b/lib/haml/filters/text_base.rb index 6da50718..f79ed748 100644 --- a/lib/haml/filters/text_base.rb +++ b/lib/haml/filters/text_base.rb @@ -4,9 +4,9 @@ module Haml class TextBase < Base def compile_text!(temple, node, prefix) text = node.value[:text].rstrip.gsub(/^/, prefix) - if ::Haml::HamlUtil.contains_interpolation?(node.value[:text]) + if ::Haml::Util.contains_interpolation?(node.value[:text]) # original: Haml::Filters#compile - text = ::Haml::HamlUtil.unescape_interpolation(text).gsub(/(\\+)n/) do |s| + text = ::Haml::Util.unescape_interpolation(text).gsub(/(\\+)n/) do |s| escapes = $1.size next s if escapes % 2 == 0 "#{'\\' * (escapes - 1)}\n" diff --git a/lib/haml/filters/tilt_base.rb b/lib/haml/filters/tilt_base.rb index 07aeb11d..e7c2a638 100644 --- a/lib/haml/filters/tilt_base.rb +++ b/lib/haml/filters/tilt_base.rb @@ -18,7 +18,7 @@ module Haml private def compile_with_tilt(node, name, indent_width: 0) - if ::Haml::HamlUtil.contains_interpolation?(node.value[:text]) + if ::Haml::Util.contains_interpolation?(node.value[:text]) dynamic_compile(node, name, indent_width: indent_width) else static_compile(node, name, indent_width: indent_width) @@ -35,7 +35,7 @@ module Haml def dynamic_compile(node, name, indent_width: 0) # original: Haml::Filters#compile - text = ::Haml::HamlUtil.unescape_interpolation(node.value[:text]).gsub(/(\\+)n/) do |s| + text = ::Haml::Util.unescape_interpolation(node.value[:text]).gsub(/(\\+)n/) do |s| escapes = $1.size next s if escapes % 2 == 0 "#{'\\' * (escapes - 1)}\n" diff --git a/lib/haml/force_escapable.rb b/lib/haml/force_escapable.rb index 17de3893..e7be6d6f 100644 --- a/lib/haml/force_escapable.rb +++ b/lib/haml/force_escapable.rb @@ -14,7 +14,7 @@ module Haml class ForceEscapable < Escapable def initialize(opts = {}) super - @escape_code = options[:escape_code] || "::Haml::Utils.escape_html((%s))" + @escape_code = options[:escape_code] || "::Haml::Util.escape_html((%s))" @escaper = eval("proc {|v| #{@escape_code % 'v'} }") end diff --git a/lib/haml/parser.rb b/lib/haml/parser.rb index 06736ad8..6912f8d7 100644 --- a/lib/haml/parser.rb +++ b/lib/haml/parser.rb @@ -3,14 +3,14 @@ require 'ripper' require 'strscan' require 'haml/haml_error' +require 'haml/util' # haml/parser/haml_* are a copy of Haml 5. Most of them should be removed. -require 'haml/parser/haml_util' require 'haml/parser/haml_options' module Haml class Parser - include Haml::HamlUtil + include Haml::Util attr_reader :root @@ -125,7 +125,7 @@ module Haml end def call(template) - template = Haml::HamlUtil.check_haml_encoding(template) do |msg, line| + template = Haml::Util.check_haml_encoding(template) do |msg, line| raise Haml::Error.new(msg, line) end @@ -342,12 +342,12 @@ module Haml raise HamlSyntaxError.new(HamlError.message(:illegal_nesting_plain), @next_line.index) end - unless contains_interpolation?(line.text) + unless Util.contains_interpolation?(line.text) return ParseNode.new(:plain, line.index + 1, :text => line.text) end escape_html = @options.escape_html && @options.mime_type != 'text/plain' if escape_html.nil? - line.text = unescape_interpolation(line.text) + line.text = Util.unescape_interpolation(line.text) script(line, false).tap { |n| n.value[:escape_interpolation] = true if escape_html } end @@ -435,7 +435,7 @@ module Haml when '=' parse = true if value[0] == ?= - value = unescape_interpolation(value[1..-1].strip) + value = Util.unescape_interpolation(value[1..-1].strip) escape_interpolation = true if escape_html escape_html = false end @@ -444,21 +444,21 @@ module Haml parse = true preserve_script = (value[0] == ?~) if value[1] == ?= - value = unescape_interpolation(value[2..-1].strip) + value = Util.unescape_interpolation(value[2..-1].strip) escape_interpolation = true if escape_html escape_html = false else value = value[1..-1].strip end - elsif contains_interpolation?(value) - value = unescape_interpolation(value) + elsif Util.contains_interpolation?(value) + value = Util.unescape_interpolation(value) escape_interpolation = true if escape_html parse = true escape_html = false end else - if contains_interpolation?(value) - value = unescape_interpolation(value, escape_html) + if Util.contains_interpolation?(value) + value = Util.unescape_interpolation(value, escape_html) parse = true escape_html = false end @@ -520,9 +520,9 @@ module Haml conditional, text = balance(text, ?[, ?]) if text[0] == ?[ text.strip! - if contains_interpolation?(text) + if Util.contains_interpolation?(text) parse = true - text = unescape_interpolation(text) + text = Util.unescape_interpolation(text) else parse = false end @@ -732,7 +732,7 @@ module Haml break if name.nil? if name == false - scanned = Haml::HamlUtil.balance(text, ?(, ?)) + scanned = Haml::Util.balance(text, ?(, ?)) text = scanned ? scanned.first : text raise Haml::HamlSyntaxError.new(HamlError.message(:invalid_attribute_list, text.inspect), last_line - 1) end @@ -753,7 +753,7 @@ module Haml if type == :static static_attributes[name] = val else - dynamic_attributes << "#{inspect_obj(name)} => #{val}," + dynamic_attributes << "#{Util.inspect_obj(name)} => #{val}," end end dynamic_attributes << "}" @@ -788,7 +788,7 @@ module Haml return name, [:static, content.first[1]] if content.size == 1 return name, [:dynamic, - %!"#{content.each_with_object(''.dup) {|(t, v), s| s << (t == :str ? inspect_obj(v)[1...-1] : "\#{#{v}}")}}"!] + %!"#{content.each_with_object(''.dup) {|(t, v), s| s << (t == :str ? Util.inspect_obj(v)[1...-1] : "\#{#{v}}")}}"!] end def next_line @@ -856,7 +856,7 @@ module Haml end def balance(*args) - Haml::HamlUtil.balance(*args) or raise(HamlSyntaxError.new(HamlError.message(:unbalanced_brackets))) + Haml::Util.balance(*args) or raise(HamlSyntaxError.new(HamlError.message(:unbalanced_brackets))) end # Unlike #balance, this balances Ripper tokens to balance something like `{ a: "}" }` correctly. diff --git a/lib/haml/rails_template.rb b/lib/haml/rails_template.rb index 428c1e84..e66038d6 100644 --- a/lib/haml/rails_template.rb +++ b/lib/haml/rails_template.rb @@ -2,7 +2,7 @@ require 'temple' require 'haml/engine' require 'haml/rails_helpers' -require 'haml/parser/haml_util' +require 'haml/util' module Haml class RailsTemplate @@ -45,11 +45,6 @@ module Haml end end ActionView::Template.register_template_handler(:haml, RailsTemplate.new) - - module HamlUtil - undef :rails_xss_safe? if defined? rails_xss_safe? - def rails_xss_safe?; true; end - end end # Haml extends Haml::Helpers in ActionView each time. diff --git a/lib/haml/parser/haml_util.rb b/lib/haml/util.rb similarity index 93% rename from lib/haml/parser/haml_util.rb rename to lib/haml/util.rb index 40f37249..f4d6f011 100644 --- a/lib/haml/parser/haml_util.rb +++ b/lib/haml/util.rb @@ -11,9 +11,25 @@ require 'strscan' module Haml # A module containing various useful functions. - module HamlUtil + module Util extend self + # Java extension is not implemented for JRuby yet. + # TruffleRuby does not implement `rb_ary_sort_bang`, etc. + if /java/ === RUBY_PLATFORM || RUBY_ENGINE == 'truffleruby' + require 'cgi/escape' + + def self.escape_html(html) + CGI.escapeHTML(html.to_s) + end + else + require 'haml/haml' # Haml::Util.escape_html + end + + def self.escape_html_safe(html) + html.html_safe? ? html : escape_html(html) + end + # Silence all output to STDERR within a block. # # @yield A block in which no output will be printed to STDERR @@ -35,20 +51,6 @@ module Haml false end - # Returns the given text, marked as being HTML-safe. - # With older versions of the Rails XSS-safety mechanism, - # this destructively modifies the HTML-safety of `text`. - # - # It only works if you are using ActiveSupport or the parameter `text` - # implements the #html_safe method. - # - # @param text [String, nil] - # @return [String, nil] `text`, marked as HTML-safe - def html_safe(text) - return unless text - text.html_safe - end - # Checks that the encoding of a string is valid # and cleans up potential encoding gotchas like the UTF-8 BOM. # If it's not, yields an error string describing the invalid character @@ -200,7 +202,7 @@ MSG def unescape_interpolation(str, escape_html = nil) res = ''.dup - rest = Haml::HamlUtil.handle_interpolation str.dump do |scan| + rest = Haml::Util.handle_interpolation str.dump do |scan| escapes = (scan[2].size - 1) / 2 char = scan[3] # '{', '@' or '$' res << scan.matched[0...-3 - escapes] diff --git a/lib/haml/utils.rb b/lib/haml/utils.rb deleted file mode 100644 index c62823c9..00000000 --- a/lib/haml/utils.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true -module Haml - module Utils - # Java extension is not implemented for JRuby yet. - # TruffleRuby does not implement `rb_ary_sort_bang`, etc. - if /java/ === RUBY_PLATFORM || RUBY_ENGINE == 'truffleruby' - require 'cgi/escape' - - def self.escape_html(html) - CGI.escapeHTML(html.to_s) - end - else - require 'haml/haml' # Haml::Utils.escape_html - end - - def self.escape_html_safe(html) - html.html_safe? ? html : escape_html(html) - end - end -end diff --git a/test/test_helper.rb b/test/test_helper.rb index dfdaea8c..57aebf58 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -79,7 +79,7 @@ class Haml::TestCase < BASE_TEST_CLASS end def silence_warnings(&block) - Haml::HamlUtil.silence_warnings(&block) + Haml::Util.silence_warnings(&block) end def assert_raises_message(klass, message)