mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Optimize string nterpolation in attributes
This commit is contained in:
parent
c967f3c531
commit
3a6870ee0f
3 changed files with 25 additions and 8 deletions
2
benchmark/string_interpolation.haml
Normal file
2
benchmark/string_interpolation.haml
Normal file
|
@ -0,0 +1,2 @@
|
|||
- id = 12347
|
||||
%a{ href: "https://example.com/users/#{id}" }= "id: #{id}"
|
|
@ -1,6 +1,8 @@
|
|||
require 'hamlit/attribute_builder'
|
||||
require 'hamlit/hash_parser'
|
||||
require 'hamlit/ruby_expression'
|
||||
require 'hamlit/static_analyzer'
|
||||
require 'hamlit/string_interpolation'
|
||||
|
||||
module Hamlit
|
||||
class Compiler
|
||||
|
@ -109,14 +111,27 @@ module Hamlit
|
|||
def compile_common!(temple, key, values)
|
||||
type, exp = values.last
|
||||
|
||||
if type == :dynamic && StaticAnalyzer.static?(exp)
|
||||
type, exp = :static, eval("(#{exp}).to_s")
|
||||
case
|
||||
when type == :dynamic && StaticAnalyzer.static?(exp)
|
||||
temple << [:html, :attr, key, [:escape, @escape_attrs, [:static, eval(exp).to_s]]]
|
||||
when type == :dynamic && RubyExpression.string_literal?(exp)
|
||||
value_temple = [:multi]
|
||||
StringInterpolation.compile(exp).each do |type, v|
|
||||
case type
|
||||
when :static
|
||||
value_temple << [:escape, @escape_attrs, [:static, v]]
|
||||
when :dynamic
|
||||
if Hamlit::StaticAnalyzer.static?(v)
|
||||
value_temple << [:escape, @escape_attrs, [:static, eval(v).to_s]]
|
||||
else
|
||||
value_temple << [:escape, @escape_attrs, [:dynamic, v]]
|
||||
end
|
||||
end
|
||||
end
|
||||
temple << [:html, :attr, key, value_temple]
|
||||
else
|
||||
temple << [:html, :attr, key, [:escape, @escape_attrs, [type, exp]]]
|
||||
end
|
||||
temple << build_attr(key, type, exp)
|
||||
end
|
||||
|
||||
def build_attr(key, type, exp)
|
||||
[:html, :attr, key, [:escape, @escape_attrs, [type, exp]]]
|
||||
end
|
||||
|
||||
def attribute_builder(type, values)
|
||||
|
|
|
@ -1469,7 +1469,7 @@ HAML
|
|||
assert_equal("<a></a>\n", render('%a{:b => "a #{1 + 1} b", :c => "d"}', :suppress_eval => true))
|
||||
end
|
||||
|
||||
def test_interpolates_instance_vars_in_attribute_values
|
||||
def test_interpolates_instance_vars_in_attribute_values; skip # special interpolation
|
||||
scope = Object.new
|
||||
scope.instance_variable_set :@foo, 'bar'
|
||||
assert_haml_ugly('%a{:b => "a #@foo b"}', :scope => scope)
|
||||
|
|
Loading…
Add table
Reference in a new issue