mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Compile static script statically
This commit is contained in:
parent
6b3c46267b
commit
2c23f95fbe
3 changed files with 33 additions and 2 deletions
|
@ -1,2 +1,4 @@
|
|||
= ['&', '"', "'", '<', '>']
|
||||
= "hello#{ ' ' }world"
|
||||
- dynamic = 'dynamic'
|
||||
= "hello #{ dynamic } world"
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'hamlit/static_analyzer'
|
||||
|
||||
module Hamlit
|
||||
class Compiler
|
||||
class ScriptCompiler
|
||||
|
@ -6,13 +8,31 @@ module Hamlit
|
|||
end
|
||||
|
||||
def compile(node, &block)
|
||||
if node.children.empty? && StaticAnalyzer.static?(node.value[:text])
|
||||
static_compile(node)
|
||||
else
|
||||
dynamic_compile(node, &block)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def static_compile(node)
|
||||
str = eval("(#{node.value[:text]}).to_s")
|
||||
if node.value[:escape_html]
|
||||
str = Temple::Utils.escape_html(str)
|
||||
elsif node.value[:preserve]
|
||||
str = Haml::Helpers.find_and_preserve(str, %w(textarea pre code))
|
||||
end
|
||||
[:static, str]
|
||||
end
|
||||
|
||||
def dynamic_compile(node, &block)
|
||||
var = unique_identifier
|
||||
temple = compile_script_assign(var, node, &block)
|
||||
temple << compile_script_result(var, node)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def compile_script_assign(var, node, &block)
|
||||
if node.children.empty?
|
||||
[:multi,
|
||||
|
|
|
@ -12,6 +12,15 @@ describe Hamlit::Engine do
|
|||
HTML
|
||||
end
|
||||
|
||||
it 'renders dynamic interpolated string' do
|
||||
assert_render(<<-'HAML', <<-HTML)
|
||||
- nya = 'nya'
|
||||
= "hello #{nya} world"
|
||||
HAML
|
||||
hello nya world
|
||||
HTML
|
||||
end
|
||||
|
||||
it 'renders array with escape_html: false' do
|
||||
assert_render(<<-HAML, <<-HTML, escape_html: false)
|
||||
= ['<', '>']
|
||||
|
|
Loading…
Add table
Reference in a new issue