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

Add an ||= optional-assignment operator for Sass.

This commit is contained in:
Nathan Weizenbaum 2008-02-29 13:18:52 -08:00
parent 73afdbf11b
commit ec4c08716e
4 changed files with 48 additions and 3 deletions

View file

@ -363,6 +363,39 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
# #main {
# content: string(Hello, "Hubert" Bean.) }
#
# === Optional Assignment
#
# You can assign Sass constants if they aren't already assigned
# using the ||= assignment operator.
# This means that if the constant has already been assigned to,
# it won't be re-assigned,
# but if it doesn't have a value yet,
# it will be given one.
# For example:
#
# !content = "First content"
# !content ||= "Second content?"
#
# #main
# content = content
#
# is compiled to:
#
# #main {
# content: First content; }
#
# However,
#
# !content ||= "Second content?"
#
# #main
# content = content
#
# is compiled to:
#
# #main {
# content: Second content?; }
#
# === Default Concatenation
#
# All those plusses and quotes for concatenating strings

View file

@ -31,7 +31,7 @@ module Sass
}
# The regular expression used to parse constants
MATCH = /^#{Regexp.escape(CONSTANT_CHAR.chr)}([^\s#{(SYMBOLS.keys + [ ?= ]).map {|c| Regexp.escape("#{c.chr}") }.join}]+)\s*=\s*(.+)/
MATCH = /^#{Regexp.escape(CONSTANT_CHAR.chr)}([^\s#{(SYMBOLS.keys + [ ?= ]).map {|c| Regexp.escape("#{c.chr}") }.join}]+)\s*((?:\|\|)?=)\s*(.+)/
# First-order operations
FIRST_ORDER = [:times, :div, :mod]

View file

@ -270,11 +270,18 @@ module Sass
end
def parse_constant(line)
name, value = line.scan(Sass::Constant::MATCH)[0]
name, op, value = line.scan(Sass::Constant::MATCH)[0]
unless name && value
raise SyntaxError.new("Invalid constant: \"#{line}\"", @line)
end
@constants[name] = Sass::Constant.parse(value, @constants, @line)
constant = Sass::Constant.parse(value, @constants, @line)
if op == '||='
@constants[name] ||= constant
else
@constants[name] = constant
end
:constant
end

View file

@ -208,6 +208,11 @@ END
def test_cr_newline
assert_equal("foo {\n a: b;\n c: d;\n e: f; }\n", render("foo\r a: b\r\n c: d\n\r e: f"))
end
def test_or_eq
assert_equal("foo {\n a: b; }\n", render("!foo = b\n!foo ||= c\nfoo\n a = !foo"))
assert_equal("foo {\n a: b; }\n", render("!foo ||= b\nfoo\n a = !foo"))
end
private