diff --git a/lib/sass.rb b/lib/sass.rb index bda37a35..531807f8 100644 --- a/lib/sass.rb +++ b/lib/sass.rb @@ -356,6 +356,23 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir) # #main h6 { # font: italic small-caps bold 1.1em sans-serif; } # +# == Comments +# +# It's simple to add non-printing comments to a Sass document. +# Simply use the familiar C-style notation for a one-line comment, "//", +# at the normal indentation level and all text following it won't be output. +# For example: +# +# // A very awesome rule. +# #awesome.rule +# // An equally awesome attribute. +# :awesomeness very +# +# becomes +# +# #awesome.rule { +# awesomeness: very; } +# # == Output Style # # Although the default CSS style that Sass outputs is very nice, diff --git a/lib/sass/engine.rb b/lib/sass/engine.rb index 7d178a48..1e6b6478 100644 --- a/lib/sass/engine.rb +++ b/lib/sass/engine.rb @@ -20,9 +20,17 @@ module Sass # The character that designates that # an attribute should be assigned to the result of constant arithmetic. SCRIPT_CHAR = ?= + + # The character that designates the beginning of a comment, + # either Sass or CSS. + COMMENT_CHAR = ?/ + + # The character that follows the normal COMMENT_CHAR and designates a Sass comment, + # which is not output as a CSS comment. + SASS_COMMENT_CHAR = ?/ # The string that begins one-line comments. - COMMENT_STRING = '//' + COMMENT_STRING = COMMENT_CHAR.chr + '/' # The regex that matches attributes. ATTRIBUTE = /:([^\s=]+)\s*(=?)\s*(.*)/ @@ -57,8 +65,11 @@ module Sass index = 0 while @lines[index] child, index = build_tree(index) - child.line = index if child - root << child if child + + if child.is_a? Tree::Node + child.line = index + root << child + end end @line = nil @@ -80,21 +91,22 @@ module Sass @template.each_with_index do |line, index| @line = index + 1 - # TODO: Allow comments appended to the end of lines, - # find some way to make url(http://www.google.com/) work - unless line[0..1] == COMMENT_STRING # unless line is a comment - tabs = count_tabs(line) + tabs = count_tabs(line) - if tabs # if line isn't blank - if tabs - old_tabs > 1 - raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.", @line) - end - @lines << [line.strip, tabs] + if line[0] == COMMENT_CHAR && line[1] == SASS_COMMENT_CHAR + tabs = old_tabs + end - old_tabs = tabs + if tabs # if line isn't blank + if tabs - old_tabs > 1 + raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.", @line) end + @lines << [line.strip, tabs] + + old_tabs = tabs end end + @line = nil end @@ -118,19 +130,20 @@ module Sass node = parse_line(line) # Node is nil if it's non-outputting, like a constant assignment - return nil, index unless node + return node, index unless node.is_a? Tree::Node has_children = has_children?(index, tabs) while has_children child, index = build_tree(index) - if child.nil? + if child == :constant raise SyntaxError.new("Constants may only be declared at the root of a document.", @line) + elsif child.is_a? Tree::Node + child.line = @line + node << child end - child.line = @line - node << child if child has_children = has_children?(index, tabs) end @@ -144,12 +157,14 @@ module Sass def parse_line(line) case line[0] - when ATTRIBUTE_CHAR - parse_attribute(line) - when Constant::CONSTANT_CHAR - parse_constant(line) - else - Tree::RuleNode.new(line, @options[:style]) + when ATTRIBUTE_CHAR + parse_attribute(line) + when Constant::CONSTANT_CHAR + parse_constant(line) + when COMMENT_CHAR + parse_comment(line) + else + Tree::RuleNode.new(line, @options[:style]) end end @@ -173,7 +188,15 @@ module Sass raise SyntaxError.new("Invalid constant: \"#{line}\"", @line) end @constants[name] = Sass::Constant.parse(value, @constants, @line) - nil + :constant + end + + def parse_comment(line) + if line[1] == SASS_COMMENT_CHAR + :comment + else + Tree::RuleNode.new(line, @options[:style]) + end end end end diff --git a/test/sass/engine_test.rb b/test/sass/engine_test.rb index 0bd544b1..0b62a548 100644 --- a/test/sass/engine_test.rb +++ b/test/sass/engine_test.rb @@ -31,7 +31,7 @@ class SassEngineTest < Test::Unit::TestCase "a\n\t:b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.", "a\n :b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.", "a\n :b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.", - "a\n :b\n !c = 3" => "Constants may only be declared at the root of a document.", + "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 '&'" } diff --git a/test/sass/results/complex.css b/test/sass/results/complex.css index 5c9c231c..94e63afc 100644 --- a/test/sass/results/complex.css +++ b/test/sass/results/complex.css @@ -17,6 +17,7 @@ body { margin: 0; font: 0.85em "Lucida Grande", "Trebuchet MS", Verdana, sans-se #menu { clear: both; text-align: right; height: 20px; border-bottom: 5px solid #006b95; background: #00a4e4; } #menu .contests ul { margin: 0 5px 0 0; padding: 0; } #menu .contests ul li { list-style-type: none; margin: 0 5px; padding: 5px 5px 0 5px; display: inline; font-size: 1.1em; color: #fff; background: #00a4e4; } +#menu .contests ul li / This rule isn't a comment! { red: green; } #menu .contests a:link, #menu .contests a:visited { color: #fff; text-decoration: none; font-weight: bold; } #menu .contests a:hover { text-decoration: underline; } diff --git a/test/sass/templates/complex.sass b/test/sass/templates/complex.sass index 0b806be9..5113b25b 100644 --- a/test/sass/templates/complex.sass +++ b/test/sass/templates/complex.sass @@ -78,9 +78,13 @@ body :margin 0 5px :padding 5px 5px 0 5px :display inline +// This comment is in the middle of this rule :font-size 1.1em + // This comment is properly indented :color #fff :background #00a4e4 + / This rule isn't a comment! + :red green a:link, a:visited :color #fff :text-decoration none