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

Drop escape_html option

This commit is contained in:
Takashi Kokubun 2015-04-12 19:12:14 +09:00
parent 768617f2f0
commit 27b29846a0
11 changed files with 27 additions and 95 deletions

View file

@ -1,7 +1,6 @@
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
@ -13,8 +12,6 @@ module Hamlit
include Compilers::RuntimeAttribute
included do
include Concerns::Escapable
define_options :format, :attr_quote
end
@ -41,7 +38,7 @@ module Hamlit
type, arg = value
next attr unless name && type && type && arg
[:html, :attr, name, escape_html(value, true)]
[:html, :attr, name, [:escape, true, value]]
end
end

View file

@ -1,20 +1,11 @@
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_html([:dynamic, variable], options[:force_escape])
result = [:escape, true, [:dynamic, variable]]
result = [:dynamic, variable] if options[:disable_escape]
[:multi, assign, *exps.map { |exp| compile(exp) }, compile(result)]
end

View file

@ -1,17 +0,0 @@
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

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

View file

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

View file

@ -1,21 +1,14 @@
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 = { force_escape: false, disable_escape: false }.freeze
included do
include Concerns::Escapable
end
DEFAULT_SCRIPT_OPTIONS = { disable_escape: false }.freeze
def parse_script(scanner, options = {})
assert_scan!(scanner, /=|&=|!=|~/)
@ -25,7 +18,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_html([:dynamic, code], options[:force_escape])
return [:escape, true, [:dynamic, code]]
end
ast = [:haml, :script, code, options]

View file

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

View file

@ -10,15 +10,23 @@ def generate_spec(mode)
# This is a spec converted by haml-spec.
# See: https://github.com/haml/haml-spec
describe "haml #{mode} mode with ecape_html" do
describe "haml #{mode} mode with escape_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(options) { haml }
hamlit = Hamlit::Template.new(suppress_warnings(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

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

View file

@ -2,15 +2,23 @@ require "haml"
# This is a spec converted by haml-spec.
# See: https://github.com/haml/haml-spec
describe "haml ugly mode with ecape_html" do
describe "haml ugly mode with escape_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(options) { haml }
hamlit = Hamlit::Template.new(suppress_warnings(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}