1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Allow flexible indentation for Sass docs.

This commit is contained in:
Nathan Weizenbaum 2008-05-31 21:17:43 -07:00
parent 022d50b2c9
commit 17d5bc40af
2 changed files with 31 additions and 18 deletions

View file

@ -6,6 +6,7 @@ require 'sass/tree/attr_node'
require 'sass/tree/directive_node'
require 'sass/constant'
require 'sass/error'
require 'haml/shared'
module Sass
# This is the class where all the parsing and processing of the Sass
@ -146,7 +147,7 @@ module Sass
if tabs # if line isn't blank
if tabs - old_tabs > 1
raise SyntaxError.new("#{tabs * 2} spaces were used for indentation. Sass must be indented using two spaces.", @line)
raise SyntaxError.new("The line was indented #{tabs - old_tabs} levels deeper than the previous line.", @line)
end
@lines << [line.strip, tabs]
@ -162,19 +163,20 @@ module Sass
# Counts the tabulation of a line.
def count_tabs(line)
return nil if line.strip.empty?
return nil unless spaces = line.index(/[^ ]/)
return 0 unless whitespace = line[/^\s+/]
if spaces % 2 == 1
raise SyntaxError.new(<<END.strip, @line)
#{spaces} space#{spaces == 1 ? ' was' : 's were'} used for indentation. Sass must be indented using two spaces.
END
elsif line[spaces] == ?\t
raise SyntaxError.new(<<END.strip, @line)
A tab character was used for indentation. Sass must be indented using two spaces.
Are you sure you have soft tabs enabled in your editor?
END
if @indentation.nil?
@indentation = whitespace
return 1
end
spaces / 2
tabs = whitespace.length / @indentation.length
return tabs if whitespace == @indentation * tabs
raise SyntaxError.new(<<END.strip.gsub("\n", ' '), @line)
Inconsistent indentation: #{Haml::Shared.human_indentation whitespace, true} used for indentation,
but the rest of the document was indented using #{Haml::Shared.human_indentation @indentation}.
END
end
def build_tree(index)

View file

@ -36,12 +36,6 @@ class SassEngineTest < Test::Unit::TestCase
"!a" => 'Invalid constant: "!a".',
"! a" => 'Invalid constant: "! a".',
"!a b" => 'Invalid constant: "!a b".',
"a\n\t:b c" => <<END.strip,
A tab character was used for indentation. Sass must be indented using two spaces.
Are you sure you have soft tabs enabled in your editor?
END
"a\n :b c" => "1 space was used for indentation. Sass must be indented using two spaces.",
"a\n :b c" => "4 spaces were used for indentation. Sass must be indented using two spaces.",
"a\n :b c\n !d = 3" => "Constants may only be declared at the root of a document.",
"!a = 1b + 2c" => "Incompatible units: b and c.",
"& a\n :b c" => "Base-level rules cannot contain the parent-selector-referencing character '&'.",
@ -58,6 +52,14 @@ END
"=foo\n :color red\n.bar\n +bang" => "Undefined mixin 'bang'.",
".bar\n =foo\n :color red\n" => "Mixins may only be defined at the root of a document.",
"=foo\n :color red\n.bar\n +foo\n :color red" => "Illegal nesting: Nothing may be nested beneath mixin directives.",
"a\n b: c\n b: c" => ["Inconsistent indentation: 1 space was used for indentation, but the rest of the document was indented using 2 spaces.", 3],
"a\n b: c\na\n b: c" => ["Inconsistent indentation: 1 space was used for indentation, but the rest of the document was indented using 2 spaces.", 4],
"a\n\t\tb: c\n\tb: c" => ["Inconsistent indentation: 1 tab was used for indentation, but the rest of the document was indented using 2 tabs.", 3],
"a\n b: c\n b: c" => ["Inconsistent indentation: 3 spaces were used for indentation, but the rest of the document was indented using 2 spaces.", 3],
"a\n \t b: c\n \tb: c" => ['Inconsistent indentation: " \t" was used for indentation, but the rest of the document was indented using " \t ".', 3],
"a\n b: c\n a\n d: e" => ["Inconsistent indentation: 3 spaces were used for indentation, but the rest of the document was indented using 2 spaces.", 4],
"a\n b: c\na\n d: e" => ["The line was indented 2 levels deeper than the previous line.", 4],
"a\n b: c\n a\n d: e" => ["The line was indented 3 levels deeper than the previous line.", 4],
# Regression tests
"a\n b:\n c\n d" => ["Illegal nesting: Only attributes may be nested beneath attributes.", 3]
@ -79,6 +81,15 @@ END
renders_correctly "compressed", { :style => :compressed }
end
def test_flexible_tabulation
assert_equal("p {\n a: b; }\n p q {\n c: d; }\n",
render("p\n a: b\n q\n c: d\n"))
assert_equal("p {\n a: b; }\n p q {\n c: d; }\n",
render("p\n\ta: b\n\tq\n\t\tc: d\n"))
assert_equal("p {\n a: b; }\n p q {\n c: d; }\n",
render("p\n \t \t a: b\n \t \t q\n \t \t \t \t c: d\n"))
end
def test_exceptions
EXCEPTION_MAP.each do |key, value|
begin