1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/lib/haml/escapable.rb
2017-04-19 03:10:22 +09:00

49 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Haml
# Like Temple::Filters::Escapable, but with support for escaping by
# Haml::Herlpers.html_escape and Haml::Herlpers.escape_once.
class Escapable < Temple::Filter
def initialize(*)
super
@escape_code = "::Haml::Helpers.html_escape((%s))"
@escaper = eval("proc {|v| #{@escape_code % 'v'} }")
@once_escape_code = "::Haml::Helpers.escape_once((%s))"
@once_escaper = eval("proc {|v| #{@once_escape_code % 'v'} }")
@escape = false
end
def on_escape(flag, exp)
old = @escape
@escape = flag
compile(exp)
ensure
@escape = old
end
# The same as Haml::AttributeBuilder.build_attributes
def on_static(value)
[:static,
if @escape == :once
@once_escaper[value]
elsif @escape
@escaper[value]
else
value
end
]
end
# The same as Haml::AttributeBuilder.build_attributes
def on_dynamic(value)
[:dynamic,
if @escape == :once
@once_escape_code % value
elsif @escape
@escape_code % value
else
"(#{value}).to_s"
end
]
end
end
end