From 0fde51795d69f35be3b8d72215a5d82701c69f9b Mon Sep 17 00:00:00 2001 From: hcatlin Date: Fri, 29 Sep 2006 19:57:35 +0000 Subject: [PATCH] Fixed some of the way that the engines were setup. git-svn-id: svn://hamptoncatlin.com/haml/trunk@52 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/haml/engine.rb | 38 +++++++++++++++++++++++--------------- lib/haml/template.rb | 2 +- test/engine_test.rb | 10 ++++------ test/template_test.rb | 2 +- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb index 2d6916da..7a2ce81e 100644 --- a/lib/haml/engine.rb +++ b/lib/haml/engine.rb @@ -12,8 +12,10 @@ module Haml #:nodoc: MULTILINE_CHAR_VALUE = '|'[0] MULTILINE_STARTERS = SPECIAL_CHARACTERS - ["/"[0]] - def initialize(template, action_view=nil) - @view = action_view + def initialize(template, options = {}) + #turn each of the options into instance variables for the object + options.each { |k,v| eval("@#{k} = v") } + @template = template #String @result = String.new @to_close_queue = [] @@ -53,9 +55,9 @@ module Haml #:nodoc: when '/' render_comment(line) when '=' - add template_eval(line[1, line.length]).to_s if @view + add template_eval(line[1, line.length]).to_s when '~' - add find_and_flatten(template_eval(line[1, line.length])).to_s if @view + add find_and_flatten(template_eval(line[1, line.length])).to_s else add line.strip end @@ -92,7 +94,15 @@ module Haml #:nodoc: end def build_attributes(attributes = {}) - attributes.empty? ? String.new : String.new(' ') << (attributes.collect {|a,v| "#{a.to_s}='#{v.to_s}'" unless v.nil? }).compact.join(' ') + result = attributes.collect { |a,v| + unless v.nil? + first_quote_type = v.to_s.scan(/['"]/).first + quote_type = (first_quote_type == "'") ? '"' : "'" + "#{a.to_s}=#{quote_type}#{v.to_s}#{quote_type}" + end + } + result = result.compact.join(' ') + (attributes.empty? ? String.new : String.new(' ')) + result end def open_tag(name, attributes = {}) @@ -150,16 +160,14 @@ module Haml #:nodoc: attributes = parse_class_and_id(attributes.to_s) #SimplyHelpful style logic with the [@model] helper - if @view - if object_ref && (object_ref = template_eval(object_ref).first) - class_name = object_ref.class.to_s.underscore - attributes.merge!(:id => "#{class_name}_#{object_ref.id}", :class => class_name) - end + if object_ref && (object_ref = template_eval(object_ref).first) + class_name = object_ref.class.to_s.underscore + attributes.merge!(:id => "#{class_name}_#{object_ref.id}", :class => class_name) end unless (attributes_hash.nil? || attributes_hash.empty?) # Determine whether to eval the attributes hash in the context of a template - add_attributes = @view ? template_eval(attributes_hash) : eval(attributes_hash) + add_attributes = template_eval(attributes_hash) attributes.merge!(add_attributes) end @@ -167,9 +175,9 @@ module Haml #:nodoc: when '/' atomic_tag(tag_name, attributes) when '=', '~' - value = template_eval(value) if @view - value = find_and_flatten(value) if action == '~' and @view - print_tag(tag_name, value.to_s, attributes) if value + value = template_eval(value) + value = find_and_flatten(value) if action == '~' + print_tag(tag_name, value.to_s, attributes) else print_tag(tag_name, value.to_s.strip, attributes) end @@ -185,7 +193,7 @@ module Haml #:nodoc: end def template_eval(args) - @view.instance_eval(args) + !@supress_eval ? @scope_object.instance_eval(args) : "" end end end diff --git a/lib/haml/template.rb b/lib/haml/template.rb index 7e9bfe0c..07f8e4f7 100644 --- a/lib/haml/template.rb +++ b/lib/haml/template.rb @@ -26,7 +26,7 @@ module Haml end end - Haml::Engine.new(template, @view).to_html + Haml::Engine.new(template, :scope_object => @view).to_html end end end \ No newline at end of file diff --git a/test/engine_test.rb b/test/engine_test.rb index ddb46275..662b3fca 100644 --- a/test/engine_test.rb +++ b/test/engine_test.rb @@ -5,18 +5,16 @@ class EngineTest < Test::Unit::TestCase def setup end - def render(text) - Haml::Engine.new(text).to_html + def render(text, options = {}) + Haml::Engine.new(text, options).to_html end def test_empty_render_should_remain_empty assert_equal('', render('')) end - def test_normal_renders_should_not_eval - assert_equal("", render("= 1+1")) - assert_equal("", render("= @content_for_layout")) - assert_equal("", render("~ @foobar")) + def test_stop_eval + assert_equal("", render("= 'Hello'", :supress_eval => true)) end # This is ugly because Hashes are unordered; we don't always know the order diff --git a/test/template_test.rb b/test/template_test.rb index bcf20886..90e39c58 100644 --- a/test/template_test.rb +++ b/test/template_test.rb @@ -13,7 +13,7 @@ class TemplateTest < Test::Unit::TestCase end def render(text) - Haml::Engine.new(text, @base).to_html + Haml::Engine.new(text, :scope_object => @base).to_html end def load_result(name)