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