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

Broaden the alternate attribute syntax for Sass.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@541 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-06-28 08:43:09 +00:00
parent 4bf132877f
commit dc2cda98c3
5 changed files with 28 additions and 18 deletions

View file

@ -39,14 +39,16 @@ module Sass
# The character used to denote a compiler directive. # The character used to denote a compiler directive.
DIRECTIVE_CHAR = ?@ DIRECTIVE_CHAR = ?@
# The regex that matches attributes of the form <tt>:name attr</tt>. # The regex that matches and extracts data from
ATTRIBUTE = /:([^\s=:]+)\s*(=?)(?:\s+|$)(.*)/ # attributes of the form <tt>:name attr</tt>.
ATTRIBUTE = /^:([^\s=:]+)\s*(=?)(?:\s+|$)(.*)/
# The regex that matches attributes of the form <tt>name: attr</tt>. # The regex that matches attributes of the form <tt>name: attr</tt>.
ALTERNATE_ATTRIBUTE_SELECTOR = /^[^\s:]+:(\s+|$)/ ATTRIBUTE_ALTERNATE_MATCHER = /^[^\s:]+\s*[=:](\s|$)/
# The regex that extracts data from attributes of the form <tt>name: attr</tt>. # The regex that matches and extracts data from
ATTRIBUTE_ALTERNATE = /([^\s=]+):\s*(=?)\s*(.*)/ # attributes of the form <tt>name: attr</tt>.
ATTRIBUTE_ALTERNATE = /^([^\s=:]+)(\s*=|:)(?:\s+|$)(.*)/
# Creates a new instace of Sass::Engine that will compile the given # Creates a new instace of Sass::Engine that will compile the given
# template string when <tt>render</tt> is called. # template string when <tt>render</tt> is called.
@ -217,18 +219,18 @@ module Sass
end end
def parse_line(line) def parse_line(line)
if line[0] == ATTRIBUTE_CHAR
parse_attribute(line, ATTRIBUTE)
elsif line.match(ALTERNATE_ATTRIBUTE_SELECTOR)
parse_attribute(line, ATTRIBUTE_ALTERNATE)
else
case line[0] case line[0]
when ATTRIBUTE_CHAR
parse_attribute(line, ATTRIBUTE)
when Constant::CONSTANT_CHAR when Constant::CONSTANT_CHAR
parse_constant(line) parse_constant(line)
when COMMENT_CHAR when COMMENT_CHAR
parse_comment(line) parse_comment(line)
when DIRECTIVE_CHAR when DIRECTIVE_CHAR
parse_directive(line) parse_directive(line)
else
if line =~ ATTRIBUTE_ALTERNATE_MATCHER
parse_attribute(line, ATTRIBUTE_ALTERNATE)
else else
Tree::RuleNode.new(line, @options[:style]) Tree::RuleNode.new(line, @options[:style])
end end
@ -242,7 +244,7 @@ module Sass
raise SyntaxError.new("Invalid attribute: \"#{line}\"", @line) raise SyntaxError.new("Invalid attribute: \"#{line}\"", @line)
end end
if eq[0] == SCRIPT_CHAR if eq.strip[0] == SCRIPT_CHAR
value = Sass::Constant.parse(value, @constants, @line).to_s value = Sass::Constant.parse(value, @constants, @line).to_s
end end

View file

@ -22,7 +22,11 @@ class SassEngineTest < Test::Unit::TestCase
":= a" => 'Invalid attribute: ":= a"', ":= a" => 'Invalid attribute: ":= a"',
"a\n :b" => 'Invalid attribute: ":b "', "a\n :b" => 'Invalid attribute: ":b "',
"a\n :b: c" => 'Invalid attribute: ":b: c"', "a\n :b: c" => 'Invalid attribute: ":b: c"',
"a\n :b:c d" => 'Invalid attribute: ":b:c d"',
"a\n :b=c d" => 'Invalid attribute: ":b=c d"',
"a\n :b c;" => 'Invalid attribute: ":b c;" (This isn\'t CSS!)', "a\n :b c;" => 'Invalid attribute: ":b c;" (This isn\'t CSS!)',
"a\n b : c" => 'Invalid attribute: "b : c"',
"a\n b=c: d" => 'Invalid attribute: "b=c: d"',
":a" => 'Attributes aren\'t allowed at the root of a document.', ":a" => 'Attributes aren\'t allowed at the root of a document.',
"!" => 'Invalid constant: "!"', "!" => 'Invalid constant: "!"',
"!a" => 'Invalid constant: "!a"', "!a" => 'Invalid constant: "!a"',
@ -61,7 +65,7 @@ class SassEngineTest < Test::Unit::TestCase
assert(err.sass_line, "Line: #{key}") assert(err.sass_line, "Line: #{key}")
assert_match(/\(sass\):[0-9]+/, err.backtrace[0], "Line: #{key}") assert_match(/\(sass\):[0-9]+/, err.backtrace[0], "Line: #{key}")
else else
assert(false, "Exception not raised for '#{key}'!") assert(false, "Exception not raised for\n#{key}")
end end
end end
end end

View file

@ -1,3 +1,4 @@
h1 { float: left; width: 274px; height: 75px; margin: 0; background-repeat: no-repeat; background-image: none; } h1 { float: left; width: 274px; height: 75px; margin: 0; background-repeat: no-repeat; background-image: none; }
h1 a:hover, h1 a:visited { color: green; } h1 a:hover, h1 a:visited { color: green; }
h1 b:hover { color: red; background-color: green; } h1 b:hover { color: red; background-color: green; }
h1 const { nosp: 3; sp: 3; }

View file

@ -11,3 +11,6 @@ h1
b:hover b:hover
color: red color: red
:background-color green :background-color green
const
nosp= 1 + 2
sp = 1 + 2