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

[Sass] Make identifiers treat underscores as hyphens (and vice-versa).

This commit is contained in:
Nathan Weizenbaum 2010-02-20 23:36:44 -08:00
parent cf4b8bdc14
commit a9d9ed4519
4 changed files with 75 additions and 8 deletions

View file

@ -102,7 +102,7 @@ and all of them will be watched:
File and directory watching is accessible from Ruby,
using the {Sass::Plugin#watch} function.
### Bulk Updating
#### Bulk Updating
Another new flag for the `sass` command-line utility is `--update`.
It checks a group of Sass files to see if their CSS needs to be updated,
@ -118,14 +118,30 @@ In fact, `--update` work exactly the same as `--watch`,
except that it doesn't continue watching the files
after the first check.
### Variable Names
### Syntax
SassScript variable names may now contain hyphens.
#### Variable Names
SassScript variable and mixin names may now contain hyphens.
For example:
!prettiest-color = #542FA9
=pretty-text
color = !prettiest-color
### Single-Quoted Strings
In order to allow frameworks like [Compass](http://compass-style.org)
to use hyphens in variable names
while maintaining backwards-compatibility,
variables and mixins using hyphens may be referred to
with underscores, and vice versa.
For example:
!prettiest-color = #542FA9
.pretty
// Using an underscore instead of a hyphen works
color = !prettiest_color
#### Single-Quoted Strings
SassScript now supports single-quoted strings.
They behave identically to double-quoted strings,

View file

@ -43,10 +43,16 @@ module Sass
def inherited_hash(name)
class_eval <<RUBY, __FILE__, __LINE__ + 1
def #{name}(name)
@#{name}s[name] || @parent && @parent.#{name}(name)
_#{name}(name.gsub('_', '-'))
end
def _#{name}(name)
@#{name}s[name] || @parent && @parent._#{name}(name)
end
protected :_#{name}
def set_#{name}(name, value)
name = name.gsub('_', '-')
@#{name}s[name] = value unless try_set_#{name}(name, value)
end
@ -63,7 +69,7 @@ module Sass
protected :try_set_#{name}
def set_local_#{name}(name, value)
@#{name}s[name] = value
@#{name}s[name.gsub('_', '-')] = value
end
RUBY
end

View file

@ -18,11 +18,11 @@ module Sass
# The regular expression used to parse variables.
# @private
MATCH = /^!([a-zA-Z_]\w*)\s*((?:\|\|)?=)\s*(.+)/
MATCH = /^!([a-zA-Z_-][\w-]*)\s*((?:\|\|)?=)\s*(.+)/
# The regular expression used to validate variables without matching.
# @private
VALIDATE = /^![a-zA-Z_]\w*$/
VALIDATE = /^![a-zA-Z_-][\w-]*$/
# Parses a string of SassScript
#

View file

@ -63,6 +63,8 @@ MSG
"foo\n @import #{File.dirname(__FILE__)}/templates/basic" => "Import directives may only be used at the root of a document.",
%Q{!foo = "bar" "baz" !} => %Q{Syntax error in '"bar" "baz" !' at character 20.},
"=foo\n :color red\n.bar\n +bang" => "Undefined mixin 'bang'.",
"=foo\n :color red\n.bar\n +bang_bop" => "Undefined mixin 'bang_bop'.",
"=foo\n :color red\n.bar\n +bang-bop" => "Undefined mixin 'bang-bop'.",
".bar\n =foo\n :color red\n" => ["Mixins may only be defined at the root of a document.", 2],
"=foo\n :color red\n.bar\n +foo\n :color red" => "Illegal nesting: Nothing may be nested beneath mixin directives.",
" a\n b: c" => ["Indenting at the beginning of the document is illegal.", 1],
@ -90,6 +92,8 @@ MSG
"@if false\n@else if " => "Invalid else directive '@else if': expected 'if <expr>'.",
"a\n !b = 12\nc\n d = !b" => 'Undefined variable: "!b".',
"=foo\n !b = 12\nc\n +foo\n d = !b" => 'Undefined variable: "!b".',
"c\n d = !b-foo" => 'Undefined variable: "!b-foo".',
"c\n d = !b_foo" => 'Undefined variable: "!b_foo".',
'@for !a from "foo" to 1' => '"foo" is not an integer.',
'@for !a from 1 to "2"' => '"2" is not an integer.',
'@for !a from 1 to "foo"' => '"foo" is not an integer.',
@ -738,6 +742,24 @@ three
SASS
end
def test_hyphen_underscore_insensitive_mixins
assert_equal(<<CSS, render(<<SASS))
a {
b: 12;
c: foo; }
CSS
=mixin-hyphen
b: 12
=mixin_under
c: foo
a
+mixin_hyphen
+mixin-under
SASS
end
def test_interpolation
assert_equal("a-1 {\n b-2-3: c-3; }\n", render(<<SASS))
!a = 1
@ -900,6 +922,29 @@ b
SASS
end
def test_hyphen_underscore_insensitive_variables
assert_equal(<<CSS, render(<<SASS))
a {
b: c; }
d {
e: 13;
f: foobar; }
CSS
!var-hyphen = 12
!var_under = "foo"
a
!var_hyphen = 1 + !var_hyphen
!var-under = !var-under + "bar"
b: c
d
e = !var-hyphen
f = !var_under
SASS
end
def test_argument_error
assert_raise(Sass::SyntaxError) { render("a\n b = hsl(1)") }
end