1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Revert "Drop escape_html option"

This reverts commit 27b29846a0.
This commit is contained in:
Takashi Kokubun 2015-05-16 12:23:03 +09:00
parent 9b6fad44ef
commit 7f85131cca
11 changed files with 95 additions and 27 deletions

View file

@ -1,6 +1,7 @@
require 'hamlit/compilers/new_attribute'
require 'hamlit/compilers/old_attribute'
require 'hamlit/compilers/runtime_attribute'
require 'hamlit/concerns/escapable'
require 'hamlit/concerns/included'
module Hamlit
@ -12,6 +13,8 @@ module Hamlit
include Compilers::RuntimeAttribute
included do
include Concerns::Escapable
define_options :format, :attr_quote
end
@ -38,7 +41,7 @@ module Hamlit
type, arg = value
next attr unless name && type && type && arg
[:html, :attr, name, [:escape, true, value]]
[:html, :attr, name, escape_html(value, true)]
end
end

View file

@ -1,11 +1,20 @@
require 'hamlit/concerns/escapable'
require 'hamlit/concerns/included'
module Hamlit
module Compilers
module Script
extend Concerns::Included
included do
include Concerns::Escapable
end
def on_haml_script(code, options, *exps)
variable = result_identifier
assign = [:code, "#{variable} = #{code}"]
result = [:escape, true, [:dynamic, variable]]
result = escape_html([:dynamic, variable], options[:force_escape])
result = [:dynamic, variable] if options[:disable_escape]
[:multi, assign, *exps.map { |exp| compile(exp) }, compile(result)]
end

View file

@ -0,0 +1,17 @@
require 'hamlit/concerns/included'
module Hamlit
module Concerns
module Escapable
extend Included
included do
define_options escape_html: false
end
def escape_html(exp, force_escape = false)
[:escape, force_escape || @options[:escape_html], exp]
end
end
end
end

View file

@ -10,6 +10,7 @@ module Hamlit
generator: Temple::Generators::ArrayBuffer,
format: :html,
attr_quote: "'",
escape_html: true,
)
use Parser

View file

@ -86,7 +86,7 @@ module Hamlit
if scanner.scan(/\\/) || scanner.match?(/\#{/)
return parse_text(scanner)
elsif scanner.match?(/&=/)
return parse_script(scanner)
return parse_script(scanner, force_escape: true)
elsif scanner.match?(/!=/)
return parse_script(scanner, disable_escape: true)
elsif scanner.match?(/[.#](\Z|[^a-zA-Z0-9_-])/)

View file

@ -1,14 +1,21 @@
require 'hamlit/concerns/escapable'
require 'hamlit/concerns/error'
require 'hamlit/concerns/included'
require 'hamlit/concerns/indentable'
module Hamlit
module Parsers
module Script
extend Concerns::Included
include Concerns::Error
include Concerns::Indentable
INTERNAL_STATEMENTS = %w[else elsif when].freeze
DEFAULT_SCRIPT_OPTIONS = { disable_escape: false }.freeze
DEFAULT_SCRIPT_OPTIONS = { force_escape: false, disable_escape: false }.freeze
included do
include Concerns::Escapable
end
def parse_script(scanner, options = {})
assert_scan!(scanner, /=|&=|!=|~/)
@ -18,7 +25,7 @@ module Hamlit
return syntax_error("There's no Ruby code for = to evaluate.") if code.empty? && !with_comment
unless has_block?
return [:dynamic, code] if options[:disable_escape]
return [:escape, true, [:dynamic, code]]
return escape_html([:dynamic, code], options[:force_escape])
end
ast = [:haml, :script, code, options]

View file

@ -8,6 +8,7 @@ module Hamlit
Hamlit::Engine,
generator: Temple::Generators::RailsOutputBuffer,
register_as: :haml,
escape_html: true,
streaming: true,
)

View file

@ -10,23 +10,15 @@ def generate_spec(mode)
# This is a spec converted by haml-spec.
# See: https://github.com/haml/haml-spec
describe "haml #{mode} mode with escape_html" do
describe "haml #{mode} mode with ecape_html" do
DEFAULT_OPTIONS = { ugly: true, escape_html: true }.freeze
def assert_haml(haml, locals, options)
engine = Haml::Engine.new(haml, DEFAULT_OPTIONS.merge(options))
hamlit = Hamlit::Template.new(suppress_warnings(options)) { haml }
hamlit = Hamlit::Template.new(options) { haml }
expect(hamlit.render(Object.new, locals)).to eq(engine.render(Object.new, locals))
end
# NOTE: Hamlit only supports escape_html mode.
# Thus the option is not supporeted and prints noisy warnings.
def suppress_warnings(options)
options = options.dup
options.delete(:escape_html)
options
end
SPEC
url = 'https://raw.githubusercontent.com/haml/haml-spec/master/tests.json'

View file

@ -0,0 +1,46 @@
describe Hamlit::Compiler do
describe 'script' do
let(:options) do
{ force_escape: false, disable_escape: false }
end
it 'compiles hamlit script ast into assigning' do
assert_compile(
[:haml,
:script,
'link_to user_path do',
options,
[:static, 'user']],
[:multi,
[:code, "_hamlit_compiler0 = link_to user_path do"],
[:static, "user"],
[:escape, false, [:dynamic, "(_hamlit_compiler0).to_s"]]],
)
end
it 'compiles multiple hamlit scripts' do
assert_compile(
[:multi,
[:haml,
:script,
'link_to user_path do',
options,
[:static, 'user']],
[:haml,
:script,
'link_to repo_path do',
options,
[:static, 'repo']]],
[:multi,
[:multi,
[:code, "_hamlit_compiler0 = link_to user_path do"],
[:static, "user"],
[:escape, false, [:dynamic, "(_hamlit_compiler0).to_s"]]],
[:multi,
[:code, "_hamlit_compiler1 = link_to repo_path do"],
[:static, "repo"],
[:escape, false, [:dynamic, "(_hamlit_compiler1).to_s"]]]],
)
end
end
end

View file

@ -76,7 +76,7 @@ describe Hamlit::Engine do
end
it 'renders !=' do
assert_render(<<-HAML, <<-HTML)
assert_render(<<-HAML, <<-HTML, escape_html: true)
!= '<"&>'
!= '<"&>'.tap do |str|
-# no operation
@ -87,7 +87,7 @@ describe Hamlit::Engine do
end
it 'renders &=' do
assert_render(<<-HAML, <<-HTML)
assert_render(<<-HAML, <<-HTML, escape_html: false)
&= '<"&>'
&= '<"&>'.tap do |str|
-# no operation

View file

@ -2,23 +2,15 @@ require "haml"
# This is a spec converted by haml-spec.
# See: https://github.com/haml/haml-spec
describe "haml ugly mode with escape_html" do
describe "haml ugly mode with ecape_html" do
DEFAULT_OPTIONS = { ugly: true, escape_html: true }.freeze
def assert_haml(haml, locals, options)
engine = Haml::Engine.new(haml, DEFAULT_OPTIONS.merge(options))
hamlit = Hamlit::Template.new(suppress_warnings(options)) { haml }
hamlit = Hamlit::Template.new(options) { haml }
expect(hamlit.render(Object.new, locals)).to eq(engine.render(Object.new, locals))
end
# NOTE: Hamlit only supports escape_html mode.
# Thus the option is not supporeted and prints noisy warnings.
def suppress_warnings(options)
options = options.dup
options.delete(:escape_html)
options
end
context "headers" do
specify "an XHTML XML prolog" do
haml = %q{!!! XML}