mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
parent
9b6fad44ef
commit
7f85131cca
11 changed files with 95 additions and 27 deletions
|
@ -1,6 +1,7 @@
|
||||||
require 'hamlit/compilers/new_attribute'
|
require 'hamlit/compilers/new_attribute'
|
||||||
require 'hamlit/compilers/old_attribute'
|
require 'hamlit/compilers/old_attribute'
|
||||||
require 'hamlit/compilers/runtime_attribute'
|
require 'hamlit/compilers/runtime_attribute'
|
||||||
|
require 'hamlit/concerns/escapable'
|
||||||
require 'hamlit/concerns/included'
|
require 'hamlit/concerns/included'
|
||||||
|
|
||||||
module Hamlit
|
module Hamlit
|
||||||
|
@ -12,6 +13,8 @@ module Hamlit
|
||||||
include Compilers::RuntimeAttribute
|
include Compilers::RuntimeAttribute
|
||||||
|
|
||||||
included do
|
included do
|
||||||
|
include Concerns::Escapable
|
||||||
|
|
||||||
define_options :format, :attr_quote
|
define_options :format, :attr_quote
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -38,7 +41,7 @@ module Hamlit
|
||||||
type, arg = value
|
type, arg = value
|
||||||
next attr unless name && type && type && arg
|
next attr unless name && type && type && arg
|
||||||
|
|
||||||
[:html, :attr, name, [:escape, true, value]]
|
[:html, :attr, name, escape_html(value, true)]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,20 @@
|
||||||
|
require 'hamlit/concerns/escapable'
|
||||||
|
require 'hamlit/concerns/included'
|
||||||
|
|
||||||
module Hamlit
|
module Hamlit
|
||||||
module Compilers
|
module Compilers
|
||||||
module Script
|
module Script
|
||||||
|
extend Concerns::Included
|
||||||
|
|
||||||
|
included do
|
||||||
|
include Concerns::Escapable
|
||||||
|
end
|
||||||
|
|
||||||
def on_haml_script(code, options, *exps)
|
def on_haml_script(code, options, *exps)
|
||||||
variable = result_identifier
|
variable = result_identifier
|
||||||
|
|
||||||
assign = [:code, "#{variable} = #{code}"]
|
assign = [:code, "#{variable} = #{code}"]
|
||||||
result = [:escape, true, [:dynamic, variable]]
|
result = escape_html([:dynamic, variable], options[:force_escape])
|
||||||
result = [:dynamic, variable] if options[:disable_escape]
|
result = [:dynamic, variable] if options[:disable_escape]
|
||||||
[:multi, assign, *exps.map { |exp| compile(exp) }, compile(result)]
|
[:multi, assign, *exps.map { |exp| compile(exp) }, compile(result)]
|
||||||
end
|
end
|
||||||
|
|
17
lib/hamlit/concerns/escapable.rb
Normal file
17
lib/hamlit/concerns/escapable.rb
Normal 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
|
|
@ -10,6 +10,7 @@ module Hamlit
|
||||||
generator: Temple::Generators::ArrayBuffer,
|
generator: Temple::Generators::ArrayBuffer,
|
||||||
format: :html,
|
format: :html,
|
||||||
attr_quote: "'",
|
attr_quote: "'",
|
||||||
|
escape_html: true,
|
||||||
)
|
)
|
||||||
|
|
||||||
use Parser
|
use Parser
|
||||||
|
|
|
@ -86,7 +86,7 @@ module Hamlit
|
||||||
if scanner.scan(/\\/) || scanner.match?(/\#{/)
|
if scanner.scan(/\\/) || scanner.match?(/\#{/)
|
||||||
return parse_text(scanner)
|
return parse_text(scanner)
|
||||||
elsif scanner.match?(/&=/)
|
elsif scanner.match?(/&=/)
|
||||||
return parse_script(scanner)
|
return parse_script(scanner, force_escape: true)
|
||||||
elsif scanner.match?(/!=/)
|
elsif scanner.match?(/!=/)
|
||||||
return parse_script(scanner, disable_escape: true)
|
return parse_script(scanner, disable_escape: true)
|
||||||
elsif scanner.match?(/[.#](\Z|[^a-zA-Z0-9_-])/)
|
elsif scanner.match?(/[.#](\Z|[^a-zA-Z0-9_-])/)
|
||||||
|
|
|
@ -1,14 +1,21 @@
|
||||||
|
require 'hamlit/concerns/escapable'
|
||||||
require 'hamlit/concerns/error'
|
require 'hamlit/concerns/error'
|
||||||
|
require 'hamlit/concerns/included'
|
||||||
require 'hamlit/concerns/indentable'
|
require 'hamlit/concerns/indentable'
|
||||||
|
|
||||||
module Hamlit
|
module Hamlit
|
||||||
module Parsers
|
module Parsers
|
||||||
module Script
|
module Script
|
||||||
|
extend Concerns::Included
|
||||||
include Concerns::Error
|
include Concerns::Error
|
||||||
include Concerns::Indentable
|
include Concerns::Indentable
|
||||||
|
|
||||||
INTERNAL_STATEMENTS = %w[else elsif when].freeze
|
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 = {})
|
def parse_script(scanner, options = {})
|
||||||
assert_scan!(scanner, /=|&=|!=|~/)
|
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
|
return syntax_error("There's no Ruby code for = to evaluate.") if code.empty? && !with_comment
|
||||||
unless has_block?
|
unless has_block?
|
||||||
return [:dynamic, code] if options[:disable_escape]
|
return [:dynamic, code] if options[:disable_escape]
|
||||||
return [:escape, true, [:dynamic, code]]
|
return escape_html([:dynamic, code], options[:force_escape])
|
||||||
end
|
end
|
||||||
|
|
||||||
ast = [:haml, :script, code, options]
|
ast = [:haml, :script, code, options]
|
||||||
|
|
|
@ -8,6 +8,7 @@ module Hamlit
|
||||||
Hamlit::Engine,
|
Hamlit::Engine,
|
||||||
generator: Temple::Generators::RailsOutputBuffer,
|
generator: Temple::Generators::RailsOutputBuffer,
|
||||||
register_as: :haml,
|
register_as: :haml,
|
||||||
|
escape_html: true,
|
||||||
streaming: true,
|
streaming: true,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -10,23 +10,15 @@ def generate_spec(mode)
|
||||||
|
|
||||||
# This is a spec converted by haml-spec.
|
# This is a spec converted by haml-spec.
|
||||||
# See: https://github.com/haml/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
|
DEFAULT_OPTIONS = { ugly: true, escape_html: true }.freeze
|
||||||
|
|
||||||
def assert_haml(haml, locals, options)
|
def assert_haml(haml, locals, options)
|
||||||
engine = Haml::Engine.new(haml, DEFAULT_OPTIONS.merge(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))
|
expect(hamlit.render(Object.new, locals)).to eq(engine.render(Object.new, locals))
|
||||||
end
|
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
|
SPEC
|
||||||
|
|
||||||
url = 'https://raw.githubusercontent.com/haml/haml-spec/master/tests.json'
|
url = 'https://raw.githubusercontent.com/haml/haml-spec/master/tests.json'
|
||||||
|
|
46
spec/hamlit/compilers/script_spec.rb
Normal file
46
spec/hamlit/compilers/script_spec.rb
Normal 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
|
|
@ -76,7 +76,7 @@ describe Hamlit::Engine do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'renders !=' do
|
it 'renders !=' do
|
||||||
assert_render(<<-HAML, <<-HTML)
|
assert_render(<<-HAML, <<-HTML, escape_html: true)
|
||||||
!= '<"&>'
|
!= '<"&>'
|
||||||
!= '<"&>'.tap do |str|
|
!= '<"&>'.tap do |str|
|
||||||
-# no operation
|
-# no operation
|
||||||
|
@ -87,7 +87,7 @@ describe Hamlit::Engine do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'renders &=' do
|
it 'renders &=' do
|
||||||
assert_render(<<-HAML, <<-HTML)
|
assert_render(<<-HAML, <<-HTML, escape_html: false)
|
||||||
&= '<"&>'
|
&= '<"&>'
|
||||||
&= '<"&>'.tap do |str|
|
&= '<"&>'.tap do |str|
|
||||||
-# no operation
|
-# no operation
|
||||||
|
|
|
@ -2,23 +2,15 @@ require "haml"
|
||||||
|
|
||||||
# This is a spec converted by haml-spec.
|
# This is a spec converted by haml-spec.
|
||||||
# See: https://github.com/haml/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
|
DEFAULT_OPTIONS = { ugly: true, escape_html: true }.freeze
|
||||||
|
|
||||||
def assert_haml(haml, locals, options)
|
def assert_haml(haml, locals, options)
|
||||||
engine = Haml::Engine.new(haml, DEFAULT_OPTIONS.merge(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))
|
expect(hamlit.render(Object.new, locals)).to eq(engine.render(Object.new, locals))
|
||||||
end
|
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
|
context "headers" do
|
||||||
specify "an XHTML XML prolog" do
|
specify "an XHTML XML prolog" do
|
||||||
haml = %q{!!! XML}
|
haml = %q{!!! XML}
|
||||||
|
|
Loading…
Reference in a new issue