From 7cd7e603b18d0f94b6e3b813f954c4c4b796ab8f Mon Sep 17 00:00:00 2001 From: nex3 Date: Tue, 6 Feb 2007 03:34:49 +0000 Subject: [PATCH] New, nested Sass style. git-svn-id: svn://hamptoncatlin.com/haml/trunk@349 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/sass/engine.rb | 2 +- lib/sass/plugin.rb | 3 +-- lib/sass/tree/attr_node.rb | 7 ++++--- lib/sass/tree/node.rb | 2 +- lib/sass/tree/rule_node.rb | 18 ++++++++++++------ test/haml/results/filters.xhtml | 8 ++++++-- test/sass/engine_test.rb | 13 +++++++++---- test/sass/plugin_test.rb | 13 ++++++++++++- 8 files changed, 46 insertions(+), 20 deletions(-) diff --git a/lib/sass/engine.rb b/lib/sass/engine.rb index 8e979026..82772dfb 100644 --- a/lib/sass/engine.rb +++ b/lib/sass/engine.rb @@ -38,7 +38,7 @@ module Sass # def initialize(template, options={}) @options = { - :style => :compact + :style => :nested }.merge! options @template = template.split("\n") @lines = [] diff --git a/lib/sass/plugin.rb b/lib/sass/plugin.rb index 820d178a..2b3425dc 100644 --- a/lib/sass/plugin.rb +++ b/lib/sass/plugin.rb @@ -15,8 +15,7 @@ module Sass :template_location => RAILS_ROOT + '/public/stylesheets/sass', :css_location => RAILS_ROOT + '/public/stylesheets', :always_update => false, - :always_check => RAILS_ENV != "production", - :style => :nested + :always_check => RAILS_ENV != "production" } # Gets various options for Sass. See README for details. diff --git a/lib/sass/tree/attr_node.rb b/lib/sass/tree/attr_node.rb index d7120c91..90127047 100644 --- a/lib/sass/tree/attr_node.rb +++ b/lib/sass/tree/attr_node.rb @@ -18,12 +18,13 @@ module Sass::Tree if children.size > 0 to_return = String.new children.each do |kid| - if @style == :expanded - to_return << "#{kid.to_s(real_name)}\n " - else + if @style == :compact to_return << "#{kid.to_s(real_name)} " + else + to_return << "#{kid.to_s(real_name)}\n" end end + to_return << "\n" unless @style == :compact to_return[0...-1] else if value.length < 1 diff --git a/lib/sass/tree/node.rb b/lib/sass/tree/node.rb index e555f32f..4655a50f 100644 --- a/lib/sass/tree/node.rb +++ b/lib/sass/tree/node.rb @@ -20,7 +20,7 @@ module Sass raise SyntaxError.new('Attributes aren\'t allowed at the root of a document.', child.line) end - result += "#{child.to_s}\n" + result += "#{child.to_s(1)}\n" end result[0...-1] end diff --git a/lib/sass/tree/rule_node.rb b/lib/sass/tree/rule_node.rb index 3498d31c..ca26d314 100644 --- a/lib/sass/tree/rule_node.rb +++ b/lib/sass/tree/rule_node.rb @@ -6,11 +6,11 @@ module Sass::Tree alias_method :rule, :value alias_method :rule=, :value= - def to_s(super_rules = nil) + def to_s(tabs, super_rules = nil) attributes = [] sub_rules = [] total_rule = if super_rules - super_rules.split(/,\s*/).collect! do |s| + super_rules.split(/,\s*/).collect! do |s| self.rule.split(/,\s*/).collect! {|r| "#{s} #{r}"}.join(", ") end.join(", ") else @@ -27,14 +27,20 @@ module Sass::Tree to_return = '' unless attributes.empty? - if @style == :expanded - to_return << "#{total_rule} {\n #{attributes.join("\n ").rstrip}\n}\n" - else + if @style == :compact to_return << "#{total_rule} { #{attributes.join(' ')} }\n" + else + spaces = (@style == :expanded ? 2 : tabs * 2) + old_spaces = ' ' * (spaces - 2) + spaces = ' ' * spaces + + attributes = attributes.join("\n").gsub("\n", "\n#{spaces}").rstrip + end_attrs = (@style == :expanded ? "\n" : ' ') + to_return << "#{old_spaces}#{total_rule} {\n#{spaces}#{attributes}#{end_attrs}}\n" end end - sub_rules.each { |sub| to_return << sub.to_s(total_rule) } + sub_rules.each { |sub| to_return << sub.to_s(tabs + 1, total_rule) } to_return end end diff --git a/test/haml/results/filters.xhtml b/test/haml/results/filters.xhtml index 884449c8..a336a9c3 100644 --- a/test/haml/results/filters.xhtml +++ b/test/haml/results/filters.xhtml @@ -1,7 +1,11 @@ TESTING HAHAHAHA!

Foo

diff --git a/test/sass/engine_test.rb b/test/sass/engine_test.rb index a16dd324..24fc9978 100644 --- a/test/sass/engine_test.rb +++ b/test/sass/engine_test.rb @@ -36,7 +36,13 @@ class SassEngineTest < Test::Unit::TestCase } def test_basic_render - renders_correctly "basic" + renders_correctly "basic", { :style => :compact } + end + + def test_alternate_styles + renders_correctly "expanded", { :style => :expanded } + renders_correctly "compact", { :style => :compact } + renders_correctly "nested", { :style => :nested } end def test_exceptions @@ -66,11 +72,10 @@ class SassEngineTest < Test::Unit::TestCase private - def renders_correctly(name) + def renders_correctly(name, options={}) sass_file = load_file(name, "sass") css_file = load_file(name, "css") - css_result = Sass::Engine.new(sass_file).render - #puts css_result.collect { |a| a.inspect }.join("\n ") + css_result = Sass::Engine.new(sass_file, options).render assert_equal css_file, css_result end diff --git a/test/sass/plugin_test.rb b/test/sass/plugin_test.rb index 4ca218f1..a29c4c8b 100644 --- a/test/sass/plugin_test.rb +++ b/test/sass/plugin_test.rb @@ -14,6 +14,7 @@ class SassPluginTest < Test::Unit::TestCase Sass::Plugin.options = { :template_location => File.dirname(__FILE__) + '/templates', :css_location => File.dirname(__FILE__) + '/tmp', + :style => :compact } Sass::Plugin.options[:always_update] = true @@ -24,11 +25,21 @@ class SassPluginTest < Test::Unit::TestCase File.delete(*Dir[tempfile_loc('*')]) end - def test_expanded_style + def test_alternate_styles engine = Sass::Engine.new(File.read(File.dirname(__FILE__) + '/templates/expanded.sass'), { :style => :expanded }) File.open(tempfile_loc('expanded'), 'w') { |f| f.write(engine.render) } assert_renders_correctly('expanded') + + engine = Sass::Engine.new(File.read(File.dirname(__FILE__) + '/templates/compact.sass'), + { :style => :compact }) + File.open(tempfile_loc('compact'), 'w') { |f| f.write(engine.render)} + assert_renders_correctly('compact') + + engine = Sass::Engine.new(File.read(File.dirname(__FILE__) + '/templates/nested.sass'), + { :style => :nested }) + File.open(tempfile_loc('nested'), 'w') { |f| f.write(engine.render)} + assert_renders_correctly('nested') end def test_templates_should_render_correctly