Merge Haml::Utils and Haml::HamlUtil as Haml::Util

This commit is contained in:
Takashi Kokubun 2021-07-27 23:09:13 -07:00
parent 02cfbab623
commit 62f4fc9a5a
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD
17 changed files with 58 additions and 81 deletions

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/parser/haml_util'
require 'haml/util'
require 'haml/attribute_compiler'
require 'haml/string_splitter'

View File

@ -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

View File

@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'haml/parser/haml_util'
require 'haml/util'
module Haml
class Filters

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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"

View File

@ -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

View File

@ -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.

View File

@ -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.

View File

@ -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]

View File

@ -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

View File

@ -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)