complete arbitrary sublayer support; fix roundabout tagging issue; couple of small...
[potlatch2.git] / net / systemeD / halcyon / styleparser / RuleSet.as
1 package net.systemeD.halcyon.styleparser {
2
3         import flash.events.*;
4         import flash.net.*;
5         import net.systemeD.halcyon.ExtendedLoader;
6         import net.systemeD.halcyon.ExtendedURLLoader;
7     import net.systemeD.halcyon.connection.Entity;
8
9     import net.systemeD.halcyon.connection.*;
10         import net.systemeD.halcyon.Globals;
11 //      import bustin.dev.Inspector;
12         
13         public class RuleSet {
14
15                 public var loaded:Boolean=false;                        // has it loaded yet?
16                 public var images:Object=new Object();          // loaded images
17                 public var imageWidths:Object=new Object();     // width of each bitmap image
18                 private var redrawCallback:Function=null;       // function to call when CSS loaded
19                 private var iconCallback:Function=null;         // function to call when all icons loaded
20                 private var iconsToLoad:uint=0;                         // number of icons left to load (fire iconCallback when ==0)
21                 private var evalsToLoad:uint=0;                         // number of evals left to load (fire redrawCallback when ==0)
22
23                 private var minscale:uint;
24                 private var maxscale:uint;
25                 public var choosers:Array;
26                 public var evals:Array;
27
28                 private static const WHITESPACE:RegExp  =/^ \s+ /sx;
29                 private static const COMMENT:RegExp             =/^ \/\* .+? \*\/ \s* /sx;      /* */
30                 private static const CLASS:RegExp               =/^ ([\.:]\w+) \s* /sx;
31                 private static const NOT_CLASS:RegExp   =/^ !([\.:]\w+) \s* /sx;
32                 private static const ZOOM:RegExp                =/^ \| \s* z([\d\-]+) \s* /isx;
33                 private static const GROUP:RegExp               =/^ , \s* /isx;
34                 private static const CONDITION:RegExp   =/^ \[(.+?)\] \s* /sx;
35                 private static const OBJECT:RegExp              =/^ (\w+) \s* /sx;
36                 private static const DECLARATION:RegExp =/^ \{(.+?)\} \s* /sx;
37                 private static const UNKNOWN:RegExp             =/^ (\S+) \s* /sx;
38
39                 private static const ZOOM_MINMAX:RegExp =/^ (\d+)\-(\d+) $/sx;
40                 private static const ZOOM_MIN:RegExp    =/^ (\d+)\-      $/sx;
41                 private static const ZOOM_MAX:RegExp    =/^      \-(\d+) $/sx;
42                 private static const ZOOM_SINGLE:RegExp =/^        (\d+) $/sx;
43
44                 private static const CONDITION_TRUE:RegExp      =/^ \s* ([:\w]+) \s* = \s* yes \s*  $/isx;
45                 private static const CONDITION_FALSE:RegExp     =/^ \s* ([:\w]+) \s* = \s* no  \s*  $/isx;
46                 private static const CONDITION_SET:RegExp       =/^ \s* ([:\w]+) \s* $/sx;
47                 private static const CONDITION_UNSET:RegExp     =/^ \s* !([:\w]+) \s* $/sx;
48                 private static const CONDITION_EQ:RegExp        =/^ \s* ([:\w]+) \s* =  \s* (.+) \s* $/sx;
49                 private static const CONDITION_NE:RegExp        =/^ \s* ([:\w]+) \s* != \s* (.+) \s* $/sx;
50                 private static const CONDITION_GT:RegExp        =/^ \s* ([:\w]+) \s* >  \s* (.+) \s* $/sx;
51                 private static const CONDITION_GE:RegExp        =/^ \s* ([:\w]+) \s* >= \s* (.+) \s* $/sx;
52                 private static const CONDITION_LT:RegExp        =/^ \s* ([:\w]+) \s* <  \s* (.+) \s* $/sx;
53                 private static const CONDITION_LE:RegExp        =/^ \s* ([:\w]+) \s* <= \s* (.+) \s* $/sx;
54                 private static const CONDITION_REGEX:RegExp     =/^ \s* ([:\w]+) \s* =~\/ \s* (.+) \/ \s* $/sx;
55
56                 private static const ASSIGNMENT_EVAL:RegExp     =/^ \s* (\S+) \s* \:      \s* eval \s* \( \s* ' (.+?) ' \s* \) \s* $/isx;
57                 private static const ASSIGNMENT:RegExp          =/^ \s* (\S+) \s* \:      \s*          (.+?) \s*                   $/sx;
58                 private static const SET_TAG_EVAL:RegExp        =/^ \s* set \s+(\S+)\s* = \s* eval \s* \( \s* ' (.+?) ' \s* \) \s* $/isx;
59                 private static const SET_TAG:RegExp                     =/^ \s* set \s+(\S+)\s* = \s*          (.+?) \s*                   $/isx;
60                 private static const SET_TAG_TRUE:RegExp        =/^ \s* set \s+(\S+)\s* $/isx;
61                 private static const EXIT:RegExp                        =/^ \s* exit \s* $/isx;
62
63                 private static const oZOOM:uint=2;
64                 private static const oGROUP:uint=3;
65                 private static const oCONDITION:uint=4;
66                 private static const oOBJECT:uint=5;
67                 private static const oDECLARATION:uint=6;
68
69                 private static const DASH:RegExp=/\-/g;
70                 private static const COLOR:RegExp=/color$/;
71                 private static const BOLD:RegExp=/^bold$/i;
72                 private static const ITALIC:RegExp=/^italic|oblique$/i;
73                 private static const UNDERLINE:RegExp=/^underline$/i;
74                 private static const CAPS:RegExp=/^uppercase$/i;
75                 private static const CENTER:RegExp=/^center$/i;
76
77                 private static const HEX:RegExp=/^#([0-9a-f]+)$/i;
78                 private static const CSSCOLORS:Object = {
79                         aliceblue:0xf0f8ff,
80                         antiquewhite:0xfaebd7,
81                         aqua:0x00ffff,
82                         aquamarine:0x7fffd4,
83                         azure:0xf0ffff,
84                         beige:0xf5f5dc,
85                         bisque:0xffe4c4,
86                         black:0x000000,
87                         blanchedalmond:0xffebcd,
88                         blue:0x0000ff,
89                         blueviolet:0x8a2be2,
90                         brown:0xa52a2a,
91                         burlywood:0xdeb887,
92                         cadetblue:0x5f9ea0,
93                         chartreuse:0x7fff00,
94                         chocolate:0xd2691e,
95                         coral:0xff7f50,
96                         cornflowerblue:0x6495ed,
97                         cornsilk:0xfff8dc,
98                         crimson:0xdc143c,
99                         cyan:0x00ffff,
100                         darkblue:0x00008b,
101                         darkcyan:0x008b8b,
102                         darkgoldenrod:0xb8860b,
103                         darkgray:0xa9a9a9,
104                         darkgreen:0x006400,
105                         darkkhaki:0xbdb76b,
106                         darkmagenta:0x8b008b,
107                         darkolivegreen:0x556b2f,
108                         darkorange:0xff8c00,
109                         darkorchid:0x9932cc,
110                         darkred:0x8b0000,
111                         darksalmon:0xe9967a,
112                         darkseagreen:0x8fbc8f,
113                         darkslateblue:0x483d8b,
114                         darkslategray:0x2f4f4f,
115                         darkturquoise:0x00ced1,
116                         darkviolet:0x9400d3,
117                         deeppink:0xff1493,
118                         deepskyblue:0x00bfff,
119                         dimgray:0x696969,
120                         dodgerblue:0x1e90ff,
121                         firebrick:0xb22222,
122                         floralwhite:0xfffaf0,
123                         forestgreen:0x228b22,
124                         fuchsia:0xff00ff,
125                         gainsboro:0xdcdcdc,
126                         ghostwhite:0xf8f8ff,
127                         gold:0xffd700,
128                         goldenrod:0xdaa520,
129                         gray:0x808080,
130                         green:0x008000,
131                         greenyellow:0xadff2f,
132                         honeydew:0xf0fff0,
133                         hotpink:0xff69b4,
134                         indianred :0xcd5c5c,
135                         indigo :0x4b0082,
136                         ivory:0xfffff0,
137                         khaki:0xf0e68c,
138                         lavender:0xe6e6fa,
139                         lavenderblush:0xfff0f5,
140                         lawngreen:0x7cfc00,
141                         lemonchiffon:0xfffacd,
142                         lightblue:0xadd8e6,
143                         lightcoral:0xf08080,
144                         lightcyan:0xe0ffff,
145                         lightgoldenrodyellow:0xfafad2,
146                         lightgrey:0xd3d3d3,
147                         lightgreen:0x90ee90,
148                         lightpink:0xffb6c1,
149                         lightsalmon:0xffa07a,
150                         lightseagreen:0x20b2aa,
151                         lightskyblue:0x87cefa,
152                         lightslategray:0x778899,
153                         lightsteelblue:0xb0c4de,
154                         lightyellow:0xffffe0,
155                         lime:0x00ff00,
156                         limegreen:0x32cd32,
157                         linen:0xfaf0e6,
158                         magenta:0xff00ff,
159                         maroon:0x800000,
160                         mediumaquamarine:0x66cdaa,
161                         mediumblue:0x0000cd,
162                         mediumorchid:0xba55d3,
163                         mediumpurple:0x9370d8,
164                         mediumseagreen:0x3cb371,
165                         mediumslateblue:0x7b68ee,
166                         mediumspringgreen:0x00fa9a,
167                         mediumturquoise:0x48d1cc,
168                         mediumvioletred:0xc71585,
169                         midnightblue:0x191970,
170                         mintcream:0xf5fffa,
171                         mistyrose:0xffe4e1,
172                         moccasin:0xffe4b5,
173                         navajowhite:0xffdead,
174                         navy:0x000080,
175                         oldlace:0xfdf5e6,
176                         olive:0x808000,
177                         olivedrab:0x6b8e23,
178                         orange:0xffa500,
179                         orangered:0xff4500,
180                         orchid:0xda70d6,
181                         palegoldenrod:0xeee8aa,
182                         palegreen:0x98fb98,
183                         paleturquoise:0xafeeee,
184                         palevioletred:0xd87093,
185                         papayawhip:0xffefd5,
186                         peachpuff:0xffdab9,
187                         peru:0xcd853f,
188                         pink:0xffc0cb,
189                         plum:0xdda0dd,
190                         powderblue:0xb0e0e6,
191                         purple:0x800080,
192                         red:0xff0000,
193                         rosybrown:0xbc8f8f,
194                         royalblue:0x4169e1,
195                         saddlebrown:0x8b4513,
196                         salmon:0xfa8072,
197                         sandybrown:0xf4a460,
198                         seagreen:0x2e8b57,
199                         seashell:0xfff5ee,
200                         sienna:0xa0522d,
201                         silver:0xc0c0c0,
202                         skyblue:0x87ceeb,
203                         slateblue:0x6a5acd,
204                         slategray:0x708090,
205                         snow:0xfffafa,
206                         springgreen:0x00ff7f,
207                         steelblue:0x4682b4,
208                         tan:0xd2b48c,
209                         teal:0x008080,
210                         thistle:0xd8bfd8,
211                         tomato:0xff6347,
212                         turquoise:0x40e0d0,
213                         violet:0xee82ee,
214                         wheat:0xf5deb3,
215                         white:0xffffff,
216                         whitesmoke:0xf5f5f5,
217                         yellow:0xffff00,
218                         yellowgreen:0x9acd32 };
219
220                 public function RuleSet(mins:uint,maxs:uint,redrawCall:Function=null,iconLoadedCallback:Function=null):void {
221                         minscale = mins;
222                         maxscale = maxs;
223                         redrawCallback = redrawCall;
224                         iconCallback = iconLoadedCallback;
225                 }
226
227                 // Get styles for an object
228
229                 public function getStyles(obj:Entity, tags:Object, zoom:uint):StyleList {
230                         var sl:StyleList=new StyleList();
231                         for each (var sc:StyleChooser in choosers) {
232                                 sc.updateStyles(obj,tags,sl,imageWidths,zoom);
233                         }
234                         return sl;
235                 }
236
237                 // ---------------------------------------------------------------------------------------------------------
238                 // Loading stylesheet
239
240                 public function loadFromCSS(str:String):void {
241                         if (str.match(/[\s\n\r\t]/)!=null) { parseCSS(str); loaded=true; redrawCallback(); return; }
242
243                         var request:URLRequest=new URLRequest(str);
244                         var loader:URLLoader=new URLLoader();
245
246                         request.method=URLRequestMethod.GET;
247                         loader.dataFormat = URLLoaderDataFormat.TEXT;
248                         loader.addEventListener(Event.COMPLETE,                                         doParseCSS);
249                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler);
250                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler);
251                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler);
252                         loader.load(request);
253                 }
254
255                 private function doParseCSS(e:Event):void {
256                         parseCSS(e.target.data);
257                 }
258
259                 private function parseCSS(str:String):void {
260                         parse(str);
261                         loaded=true;
262                         if (evals.length==0) { redrawCallback(); }
263                         loadImages();
264                 }
265
266
267                 // ------------------------------------------------------------------------------------------------
268                 // Load all referenced images
269                 // ** will duplicate if referenced twice, shouldn't
270                 
271                 public function loadImages():void {
272                         var filename:String;
273                         for each (var chooser:StyleChooser in choosers) {
274                                 for each (var style:Style in chooser.styles) {
275                                         if      (style is PointStyle  && PointStyle(style).icon_image   ) { filename=PointStyle(style).icon_image; }
276                                         else if (style is ShapeStyle  && ShapeStyle(style).fill_image   ) { filename=ShapeStyle(style).fill_image; }
277                                         else if (style is ShieldStyle && ShieldStyle(style).shield_image) { filename=ShieldStyle(style).shield_image; }
278                                         else { continue; }
279                                         if (filename=='square' || filename=='circle') { continue; }
280                                 
281                                         iconsToLoad++;
282                                         var request:URLRequest=new URLRequest(filename);
283                                         var loader:ExtendedURLLoader=new ExtendedURLLoader();
284                                         loader.dataFormat=URLLoaderDataFormat.BINARY;
285                                         loader.info['filename']=filename;
286                                         loader.addEventListener(Event.COMPLETE,                                         loadedImage,                    false, 0, true);
287                                         loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler,              false, 0, true);
288                                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler,   false, 0, true);
289                                         loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler,                 false, 0, true);
290                                         loader.load(request);
291                                 }
292                         }
293                 }
294
295                 // data handler
296
297                 private function loadedImage(event:Event):void {
298                         var fn:String=event.target.info['filename'];
299                         images[fn]=event.target.data;
300
301                         var loader:ExtendedLoader = new ExtendedLoader();
302                         loader.info['filename']=fn;
303                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, measureWidth);
304                         loader.loadBytes(images[fn]);
305                 }
306                 
307                 private function measureWidth(event:Event):void {
308                         var fn:String=event.target.loader.info['filename'];
309                         imageWidths[fn]=event.target.width;
310                         // ** do we need to explicitly remove the loader object now?
311
312                         iconsToLoad--;
313                         if (iconsToLoad==0 && iconCallback!=null) { iconCallback(); }
314                 }
315
316                 private function httpStatusHandler( event:HTTPStatusEvent ):void { }
317                 private function securityErrorHandler( event:SecurityErrorEvent ):void { Globals.vars.root.addDebug("securityerrorevent"); }
318                 private function ioErrorHandler( event:IOErrorEvent ):void { Globals.vars.root.addDebug("ioerrorevent"); }
319
320                 // ------------------------------------------------------------------------------------------------
321                 // Parse CSS
322
323                 public function parse(css:String):void {
324                         var previous:uint=0;                                    // what was the previous CSS word?
325                         var sc:StyleChooser=new StyleChooser(); // currently being assembled
326                         choosers=new Array();
327                         evals=new Array();
328
329                         var o:Object=new Object();
330                         while (css.length>0) {
331
332                                 // CSS comment
333                                 if ((o=COMMENT.exec(css))) {
334                                         css=css.replace(COMMENT,'');
335
336                                 // Whitespace (probably only at beginning of file)
337                                 } else if ((o=WHITESPACE.exec(css))) {
338                                         css=css.replace(WHITESPACE,'');
339
340                                 // Class - .motorway, .builtup, :hover
341                                 } else if ((o=CLASS.exec(css))) {
342                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
343
344                                         css=css.replace(CLASS,'');
345                                         sc.addCondition(new Condition('set',o[1]));
346                                         previous=oCONDITION;
347
348                                 // Not class - !.motorway, !.builtup, !:hover
349                                 } else if ((o=NOT_CLASS.exec(css))) {
350                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
351
352                                         css=css.replace(NOT_CLASS,'');
353                                         sc.addCondition(new Condition('unset',o[1]));
354                                         previous=oCONDITION;
355
356                                 // Zoom
357                                 } else if ((o=ZOOM.exec(css))) {
358                                         if (previous!=oOBJECT && previous!=oCONDITION) { sc.newObject(); }
359
360                                         css=css.replace(ZOOM,'');
361                                         var z:Array=parseZoom(o[1]);
362                                         sc.addZoom(z[0],z[1]);
363                                         previous=oZOOM;
364
365                                 // Grouping - just a comma
366                                 } else if ((o=GROUP.exec(css))) {
367                                         css=css.replace(GROUP,'');
368                                         sc.newGroup();
369                                         previous=oGROUP;
370
371                                 // Condition - [highway=primary]
372                                 } else if ((o=CONDITION.exec(css))) {
373                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
374                                         if (previous!=oOBJECT && previous!=oZOOM && previous!=oCONDITION) { sc.newObject(); }
375                                         css=css.replace(CONDITION,'');
376                                         sc.addCondition(parseCondition(o[1]) as Condition);
377                                         previous=oCONDITION;
378
379                                 // Object - way, node, relation
380                                 } else if ((o=OBJECT.exec(css))) {
381                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
382
383                                         css=css.replace(OBJECT,'');
384                                         sc.newObject(o[1]);
385                                         previous=oOBJECT;
386
387                                 // Declaration - {...}
388                                 } else if ((o=DECLARATION.exec(css))) {
389                                         css=css.replace(DECLARATION,'');
390                                         sc.addStyles(parseDeclaration(o[1]));
391                                         previous=oDECLARATION;
392                                 
393                                 // Unknown pattern
394                                 } else if ((o=UNKNOWN.exec(css))) {
395                                         css=css.replace(UNKNOWN,'');
396                                         Globals.vars.root.addDebug("unknown: "+o[1]);
397                                         // ** do some debugging with o[1]
398
399                                 } else {
400                                         Globals.vars.root.addDebug("choked on "+css);
401                                         return;
402                                 }
403                         }
404                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
405                 }
406                 
407                 private function saveChooser(sc:StyleChooser):void {
408                         choosers.push(sc);
409                 };
410                 
411                 private function saveEval(expr:String):Eval {
412                         evalsToLoad++;
413                         var e:Eval=new Eval(expr);
414                         e.addEventListener("swf_loaded",evalLoaded);
415                         evals.push(e);
416                         return e;
417                 }
418                 
419                 private function evalLoaded(e:Event):void {
420                         evalsToLoad--;
421                         if (evalsToLoad==0) { redrawCallback(); }
422                 }
423
424                 // Parse declaration string into list of styles
425
426                 private function parseDeclaration(s:String):Array {
427                         var styles:Array=[];
428                         var t:Object=new Object();
429                         var o:Object=new Object();
430                         var a:String, k:String, v:*;
431
432                         // Create styles\10
433                         var ss:ShapeStyle =new ShapeStyle() ;
434                         var ps:PointStyle =new PointStyle() ; 
435                         var ts:TextStyle  =new TextStyle()  ; 
436                         var hs:ShieldStyle=new ShieldStyle(); 
437                         var xs:InstructionStyle=new InstructionStyle(); 
438
439                         for each (a in s.split(';')) {
440                                 if ((o=ASSIGNMENT_EVAL.exec(a)))   { t[o[1].replace(DASH,'_')]=saveEval(o[2]); }
441                                 else if ((o=ASSIGNMENT.exec(a)))   { t[o[1].replace(DASH,'_')]=o[2]; }
442                                 else if ((o=SET_TAG_EVAL.exec(a))) { xs.addSetTag(o[1],saveEval(o[2])); }
443                                 else if ((o=SET_TAG.exec(a)))      { xs.addSetTag(o[1],o[2]); }
444                                 else if ((o=SET_TAG_TRUE.exec(a))) { xs.addSetTag(o[1],true); }
445                                 else if ((o=EXIT.exec(a))) { xs.setPropertyFromString('breaker',true); }
446                         }
447
448                         // Find sublayer
449                         var sub:uint=5;
450                         if (t['z_index']) { sub=Number(t['z_index']); delete t['z_index']; }
451                         ss.sublayer=ps.sublayer=ts.sublayer=hs.sublayer=sub;
452                         xs.sublayer=10;
453
454                         // Munge special values
455                         if (t['font_weight']    ) { t['font_bold'  ]    = t['font_weight'    ].match(BOLD  )    ? true : false; delete t['font_weight']; }
456                         if (t['font_style']     ) { t['font_italic']    = t['font_style'     ].match(ITALIC)    ? true : false; delete t['font_style']; }
457                         if (t['text_decoration']) { t['font_underline'] = t['text_decoration'].match(UNDERLINE) ? true : false; delete t['text_decoration']; }
458                         if (t['text_position']  ) { t['text_center']    = t['text_position'  ].match(CENTER)    ? true : false; delete t['text_position']; }
459                         if (t['text_transform']) {
460                                 // ** needs other transformations, e.g. lower-case, sentence-case
461                                 if (t['text_transform'].match(CAPS)) { t['font_caps']=true; } else { t['font_caps']=false; }
462                                 delete t['text_transform'];
463                         }
464
465                         // ** Do compound settings (e.g. line: 5px dotted blue;)
466
467                         // Assign each property to the appropriate style
468                         for (a in t) {
469                                 // Parse properties
470                                 // ** also do units, e.g. px/pt
471                                 if (a.match(COLOR)) { v = parseCSSColor(t[a]); }
472                                                else { v = t[a]; }
473                                 
474                                 // Set in styles
475                                 if      (ss.hasOwnProperty(a)) { ss.setPropertyFromString(a,v); }
476                                 else if (ps.hasOwnProperty(a)) { ps.setPropertyFromString(a,v); }
477                                 else if (ts.hasOwnProperty(a)) { ts.setPropertyFromString(a,v); }
478                                 else if (hs.hasOwnProperty(a)) { hs.setPropertyFromString(a,v); }
479                         }
480
481                         // Add each style to list
482                         if (ss.edited) { styles.push(ss); }
483                         if (ps.edited) { styles.push(ps); }
484                         if (ts.edited) { styles.push(ts); }
485                         if (hs.edited) { styles.push(hs); }
486                         if (xs.edited) { styles.push(xs); }
487                         return styles;
488                 }
489                 
490                 private function parseZoom(s:String):Array {
491                         var o:Object=new Object();
492                         if ((o=ZOOM_MINMAX.exec(s))) { return [o[1],o[2]]; }
493                         else if ((o=ZOOM_MIN.exec(s))) { return [o[1],maxscale]; }
494                         else if ((o=ZOOM_MAX.exec(s))) { return [minscale,o[1]]; }
495                         else if ((o=ZOOM_SINGLE.exec(s))) { return [o[1],o[1]]; }
496                         return null;
497                 }
498
499                 private function parseCondition(s:String):Object {
500                         var o:Object=new Object();
501                         if      ((o=CONDITION_TRUE.exec(s)))  { return new Condition('true'     ,o[1]); }
502                         else if ((o=CONDITION_FALSE.exec(s))) { return new Condition('false',o[1]); }
503                         else if ((o=CONDITION_SET.exec(s)))   { return new Condition('set'      ,o[1]); }
504                         else if ((o=CONDITION_UNSET.exec(s))) { return new Condition('unset',o[1]); }
505                         else if ((o=CONDITION_NE.exec(s)))    { return new Condition('ne'       ,o[1],o[2]); }
506                         else if ((o=CONDITION_GT.exec(s)))    { return new Condition('>'        ,o[1],o[2]); }
507                         else if ((o=CONDITION_GE.exec(s)))    { return new Condition('>='       ,o[1],o[2]); }
508                         else if ((o=CONDITION_LT.exec(s)))    { return new Condition('<'        ,o[1],o[2]); }
509                         else if ((o=CONDITION_LE.exec(s)))    { return new Condition('<='       ,o[1],o[2]); }
510                         else if ((o=CONDITION_REGEX.exec(s))) { return new Condition('regex',o[1],o[2]); }
511                         else if ((o=CONDITION_EQ.exec(s)))    { return new Condition('eq'       ,o[1],o[2]); }
512                         return null;
513                 }
514
515         public static function parseCSSColor(colorStr:String):uint {
516             colorStr = colorStr.toLowerCase();
517             if (CSSCOLORS[colorStr]) {
518                 return CSSCOLORS[colorStr];
519             } else {
520                 var match:Object = HEX.exec(colorStr);
521                 if ( match ) { 
522                   if ( match[1].length == 3) {
523                     // repeat digits. #abc => 0xaabbcc
524                     return Number("0x"+match[1].charAt(0)+match[1].charAt(0)+
525                                        match[1].charAt(1)+match[1].charAt(1)+
526                                        match[1].charAt(2)+match[1].charAt(2));
527                   } else if ( match[1].length == 6) {
528                     return Number("0x"+match[1]);
529                   } else {
530                     return Number("0x000000"); //as good as any
531                   }
532                 }
533             }
534             return 0;
535         }
536         }
537 }