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