GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Rubygem
Fork of nex3/haml
Description: HTML Abstraction Markup Language - A Markup Haiku
Homepage: http://haml.hamptoncatlin.com
Clone URL: git://github.com/chriseppstein/haml.git
Add an ||= optional-assignment operator for Sass.
nex3 (author)
Fri Feb 29 13:18:52 -0800 2008
commit  ec4c08716e918601b67a327633ba7fdbcfeeefb3
tree    4d72efdd2d8841ad74060cfbfab6d2bea6b2c361
parent  73afdbf11bf4b8ee6d0a3ae728b518f6ba704c83
...
363
364
365
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
366
367
368
...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
0
@@ -363,6 +363,39 @@ $LOAD_PATH << dir unless $LOAD_PATH.include?(dir)
0
 # #main {
0
 # content: string(Hello, "Hubert" Bean.) }
0
 #
0
+# === Optional Assignment
0
+#
0
+# You can assign Sass constants if they aren't already assigned
0
+# using the ||= assignment operator.
0
+# This means that if the constant has already been assigned to,
0
+# it won't be re-assigned,
0
+# but if it doesn't have a value yet,
0
+# it will be given one.
0
+# For example:
0
+#
0
+# !content = "First content"
0
+# !content ||= "Second content?"
0
+#
0
+# #main
0
+# content = content
0
+#
0
+# is compiled to:
0
+#
0
+# #main {
0
+# content: First content; }
0
+#
0
+# However,
0
+#
0
+# !content ||= "Second content?"
0
+#
0
+# #main
0
+# content = content
0
+#
0
+# is compiled to:
0
+#
0
+# #main {
0
+# content: Second content?; }
0
+#
0
 # === Default Concatenation
0
 #
0
 # All those plusses and quotes for concatenating strings
...
31
32
33
34
 
35
36
37
...
31
32
33
 
34
35
36
37
0
@@ -31,7 +31,7 @@ module Sass
0
     }
0
 
0
     # The regular expression used to parse constants
0
- MATCH = /^#{Regexp.escape(CONSTANT_CHAR.chr)}([^\s#{(SYMBOLS.keys + [ ?= ]).map {|c| Regexp.escape("#{c.chr}") }.join}]+)\s*=\s*(.+)/
0
+ MATCH = /^#{Regexp.escape(CONSTANT_CHAR.chr)}([^\s#{(SYMBOLS.keys + [ ?= ]).map {|c| Regexp.escape("#{c.chr}") }.join}]+)\s*((?:\|\|)?=)\s*(.+)/
0
     
0
     # First-order operations
0
     FIRST_ORDER = [:times, :div, :mod]
...
270
271
272
273
 
274
275
276
277
 
 
 
 
 
 
 
 
278
279
280
...
270
271
272
 
273
274
275
276
 
277
278
279
280
281
282
283
284
285
286
287
0
@@ -270,11 +270,18 @@ module Sass
0
     end
0
 
0
     def parse_constant(line)
0
- name, value = line.scan(Sass::Constant::MATCH)[0]
0
+ name, op, value = line.scan(Sass::Constant::MATCH)[0]
0
       unless name && value
0
         raise SyntaxError.new("Invalid constant: \"#{line}\"", @line)
0
       end
0
- @constants[name] = Sass::Constant.parse(value, @constants, @line)
0
+
0
+ constant = Sass::Constant.parse(value, @constants, @line)
0
+ if op == '||='
0
+ @constants[name] ||= constant
0
+ else
0
+ @constants[name] = constant
0
+ end
0
+
0
       :constant
0
     end
0
 
...
208
209
210
 
 
 
 
 
211
212
213
...
208
209
210
211
212
213
214
215
216
217
218
0
@@ -208,6 +208,11 @@ END
0
   def test_cr_newline
0
     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"))
0
   end
0
+
0
+ def test_or_eq
0
+ assert_equal("foo {\n a: b; }\n", render("!foo = b\n!foo ||= c\nfoo\n a = !foo"))
0
+ assert_equal("foo {\n a: b; }\n", render("!foo ||= b\nfoo\n a = !foo"))
0
+ end
0
   
0
   private
0
 

Comments

    No one has commented yet.