9c16687e3c322284f7e10ddc594575f03990bf54
[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                     Optionally, styleUntagged can be set to false, to abort (and return a blank StyleList) if the tag hash is empty.
239                         @see net.systemeD.halcyon.styleparser.StyleList */
240
241                 public function getStyles(obj:Entity, tags:Object, zoom:uint, styleUntagged:Boolean=true):StyleList {
242                         var sl:StyleList=new StyleList();
243                         var tagged:Boolean=styleUntagged;
244                         for (var k:String in tags) { tagged=true; break; }
245                         if (tagged) {
246                                 for each (var sc:StyleChooser in choosers) {
247                                         sc.updateStyles(obj,tags,sl,zoom);
248                                 }
249                         }
250                         return sl;
251                 }
252
253                 // ---------------------------------------------------------------------------------------------------------
254                 // Loading stylesheet
255
256         /** Load and then parse a MapCSS stylesheet. Usually you will supply a filename, but you can also pass 
257                         a complete stylesheet in the string parameter. (Any string containing space characters will be 
258                         assumed to be a stylesheet rather than a filename.) */
259
260                 public function loadFromCSS(str:String):void {
261                         if (str.match(/[\s\n\r\t]/)!=null) { parseCSS(str); loaded=true; redrawCallback(); return; }
262
263                         var cssLoader:NestedCSSLoader=new NestedCSSLoader();
264                         cssLoader.addEventListener(Event.COMPLETE, doParseCSS);
265                         cssLoader.load(str);
266                 }
267
268                 private function doParseCSS(e:Event):void {
269                         parseCSS(e.target.css);
270                 }
271
272                 private function parseCSS(str:String):void {
273                         parse(str);
274                         loaded=true;
275                         if (evals.length==0) { redrawCallback(); }
276                         loadImages();
277                 }
278
279
280                 /// ------------------------------------------------------------------------------------------------
281                 /** Load all images referenced in the RuleSet (for example, icons or bitmap fills). */
282                 
283                 private function loadImages():void {
284                         ImageBank.getInstance().addEventListener(ImageBank.IMAGES_LOADED,doIconCallback);
285                         var filename:String;
286                         for each (var chooser:StyleChooser in choosers) {
287                                 for each (var style:Style in chooser.styles) {
288                                         if      (style is PointStyle  && PointStyle(style).icon_image   ) { filename=PointStyle(style).icon_image; }
289                                         else if (style is ShapeStyle  && ShapeStyle(style).fill_image   ) { filename=ShapeStyle(style).fill_image; }
290                                         else if (style is ShieldStyle && ShieldStyle(style).shield_image) { filename=ShieldStyle(style).shield_image; }
291                                         else { continue; }
292
293                                         if (filename!='square' && filename!='circle')
294                                                 ImageBank.getInstance().loadImage(filename);
295                                 }
296                         }
297                 }
298                 
299                 private function doIconCallback(e:Event):void {
300                         iconCallback();
301                 }
302                 
303                 // ------------------------------------------------------------------------------------------------
304                 // Parse CSS
305
306                 /** Parse a MapCSS stylesheet into a set of StyleChoosers. The parser is regular expression-based 
307                         and runs sequentially through the file from start to end. */
308                 public function parse(css:String):void {
309                         var previous:uint=0;                                    // what was the previous CSS word?
310                         var sc:StyleChooser=new StyleChooser(); // currently being assembled
311                         choosers=new Array();
312                         evals=new Array();
313
314                         var o:Object=new Object();
315                         while (css.length>0) {
316
317                                 // CSS comment
318                                 if ((o=COMMENT.exec(css))) {
319                                         css=css.replace(COMMENT,'');
320
321                                 // Whitespace (probably only at beginning of file)
322                                 } else if ((o=WHITESPACE.exec(css))) {
323                                         css=css.replace(WHITESPACE,'');
324
325                                 // Class - .motorway, .builtup, :hover
326                                 } else if ((o=CLASS.exec(css))) {
327                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
328
329                                         css=css.replace(CLASS,'');
330                                         sc.currentChain.addConditionToLast(new Condition('set',o[1]));
331                                         previous=oCONDITION;
332
333                                 // Not class - !.motorway, !.builtup, !:hover
334                                 } else if ((o=NOT_CLASS.exec(css))) {
335                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
336
337                                         css=css.replace(NOT_CLASS,'');
338                                         sc.currentChain.addConditionToLast(new Condition('unset',o[1]));
339                                         previous=oCONDITION;
340
341                                 // Zoom
342                                 } else if ((o=ZOOM.exec(css))) {
343                                         if (previous!=oOBJECT && previous!=oCONDITION) { sc.currentChain.addRule(); }
344
345                                         css=css.replace(ZOOM,'');
346                                         var z:Array=parseZoom(o[1]);
347                                         sc.currentChain.addZoomToLast(z[0],z[1]);
348                                         sc.zoomSpecific=true;
349                                         previous=oZOOM;
350
351                                 // Grouping - just a comma
352                                 } else if ((o=GROUP.exec(css))) {
353                                         css=css.replace(GROUP,'');
354                                         sc.newRuleChain();
355                                         previous=oGROUP;
356
357                                 // Condition - [highway=primary]
358                                 } else if ((o=CONDITION.exec(css))) {
359                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
360                                         if (previous!=oOBJECT && previous!=oZOOM && previous!=oCONDITION) { sc.currentChain.addRule(); }
361                                         css=css.replace(CONDITION,'');
362                                         sc.currentChain.addConditionToLast(parseCondition(o[1]) as Condition);
363                                         previous=oCONDITION;
364
365                                 // Object - way, node, relation
366                                 } else if ((o=OBJECT.exec(css))) {
367                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
368
369                                         css=css.replace(OBJECT,'');
370                                         sc.currentChain.addRule(o[1]);
371                                         previous=oOBJECT;
372
373                                 // Subpart - ::centreline
374                                 } else if ((o=SUBPART.exec(css))) {
375                                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
376                                         css=css.replace(SUBPART,'');
377                                         sc.currentChain.setSubpart(o[1]);
378                                         previous=oSUBPART;
379
380                                 // Declaration - {...}
381                                 } else if ((o=DECLARATION.exec(css))) {
382                                         css=css.replace(DECLARATION,'');
383                                         sc.addStyles(parseDeclaration(o[1]));
384                                         previous=oDECLARATION;
385                                 
386                                 // Unknown pattern
387                                 } else if ((o=UNKNOWN.exec(css))) {
388                                         css=css.replace(UNKNOWN,'');
389                                         trace("unknown: "+o[1]);
390                                         // ** do some debugging with o[1]
391
392                                 } else {
393                                         trace("choked on "+css);
394                                         return;
395                                 }
396                         }
397                         if (previous==oDECLARATION) { saveChooser(sc); sc=new StyleChooser(); }
398                 }
399                 
400                 private function saveChooser(sc:StyleChooser):void {
401                         choosers.push(sc);
402                 };
403                 
404                 private function saveEval(expr:String):Eval {
405                         evalsToLoad++;
406                         var e:Eval=new Eval(expr);
407                         e.addEventListener("swf_loaded",evalLoaded, false, 0, true);
408                         evals.push(e);
409                         return e;
410                 }
411                 
412                 private function evalLoaded(e:Event):void {
413                         evalsToLoad--;
414                         if (evalsToLoad==0) { redrawCallback(); }
415                 }
416
417                 // Parse declaration string into list of styles
418
419                 private function parseDeclaration(s:String):Array {
420                         var styles:Array=[];
421                         var t:Object=new Object();
422                         var o:Object=new Object();
423                         var a:String, k:String, v:*;
424
425                         // Create styles\10
426                         var ss:ShapeStyle =new ShapeStyle() ;
427                         var ps:PointStyle =new PointStyle() ; 
428                         var ts:TextStyle  =new TextStyle()  ; 
429                         var hs:ShieldStyle=new ShieldStyle(); 
430                         var xs:InstructionStyle=new InstructionStyle(); 
431
432                         for each (a in s.split(';')) {
433                                 if ((o=ASSIGNMENT_EVAL.exec(a)))   { t[o[1].replace(DASH,'_')]=saveEval(o[2]); }
434                                 else if ((o=ASSIGNMENT.exec(a)))   { t[o[1].replace(DASH,'_')]=o[2]; }
435                                 else if ((o=SET_TAG_EVAL.exec(a))) { xs.addSetTag(o[1],saveEval(o[2])); }
436                                 else if ((o=SET_TAG.exec(a)))      { xs.addSetTag(o[1],o[2]); }
437                                 else if ((o=SET_TAG_TRUE.exec(a))) { xs.addSetTag(o[1],true); }
438                                 else if ((o=EXIT.exec(a))) { xs.setPropertyFromString('breaker',true); }
439                         }
440
441                         // Find sublayer
442                         var sub:uint=5;
443                         if (t['z_index']) { sub=Number(t['z_index']); delete t['z_index']; }
444                         ss.sublayer=ps.sublayer=ts.sublayer=hs.sublayer=sub;
445                         xs.sublayer=10;
446                         
447                         // Find "interactive" property - it's true unless explicitly set false.
448                         var inter:Boolean=true;
449                         if (t['interactive']) { inter=t['interactive'].match(FALSE) ? false : true; delete t['interactive']; }
450                         ss.interactive=ps.interactive=ts.interactive=hs.interactive=xs.interactive=inter;
451
452                         // Munge special values
453                         if (t['font_weight']    ) { t['font_bold'  ]    = t['font_weight'    ].match(BOLD  )    ? true : false; delete t['font_weight']; }
454                         if (t['font_style']     ) { t['font_italic']    = t['font_style'     ].match(ITALIC)    ? true : false; delete t['font_style']; }
455                         if (t['text_decoration']) { t['font_underline'] = t['text_decoration'].match(UNDERLINE) ? true : false; delete t['text_decoration']; }
456                         if (t['text_position']  ) { t['text_center']    = t['text_position'  ].match(CENTER)    ? true : false; delete t['text_position']; }
457                         if (t['text_transform']) {
458                                 // ** needs other transformations, e.g. lower-case, sentence-case
459                                 if (t['text_transform'].match(CAPS)) { t['font_caps']=true; } else { t['font_caps']=false; }
460                                 delete t['text_transform'];
461                         }
462
463                         // ** Do compound settings (e.g. line: 5px dotted blue;)
464
465                         // Assign each property to the appropriate style
466                         for (a in t) {
467                                 // Parse properties
468                                 // ** also do units, e.g. px/pt/m
469                                 if (a.match(COLOR)) { v = parseCSSColor(t[a]); }
470                                                else { v = t[a]; }
471                                 
472                                 // Set in styles
473                                 if      (ss.hasOwnProperty(a)) { ss.setPropertyFromString(a,v); }
474                                 else if (ps.hasOwnProperty(a)) { ps.setPropertyFromString(a,v); }
475                                 else if (ts.hasOwnProperty(a)) { ts.setPropertyFromString(a,v); }
476                                 else if (hs.hasOwnProperty(a)) { hs.setPropertyFromString(a,v); }
477                         }
478
479                         // Add each style to list
480                         if (ss.edited) { styles.push(ss); }
481                         if (ps.edited) { styles.push(ps); }
482                         if (ts.edited) { styles.push(ts); }
483                         if (hs.edited) { styles.push(hs); }
484                         if (xs.edited) { styles.push(xs); }
485                         return styles;
486                 }
487                 
488                 private function parseZoom(s:String):Array {
489                         var o:Object=new Object();
490                         if ((o=ZOOM_MINMAX.exec(s))) { return [o[1],o[2]]; }
491                         else if ((o=ZOOM_MIN.exec(s))) { return [o[1],maxscale]; }
492                         else if ((o=ZOOM_MAX.exec(s))) { return [minscale,o[1]]; }
493                         else if ((o=ZOOM_SINGLE.exec(s))) { return [o[1],o[1]]; }
494                         return null;
495                 }
496
497                 private function parseCondition(s:String):Object {
498                         var o:Object=new Object();
499                         if      ((o=CONDITION_TRUE.exec(s)))  { return new Condition('true'     ,o[1]); }
500                         else if ((o=CONDITION_FALSE.exec(s))) { return new Condition('false',o[1]); }
501                         else if ((o=CONDITION_SET.exec(s)))   { return new Condition('set'      ,o[1]); }
502                         else if ((o=CONDITION_UNSET.exec(s))) { return new Condition('unset',o[1]); }
503                         else if ((o=CONDITION_NE.exec(s)))    { return new Condition('ne'       ,o[1],o[2]); }
504                         else if ((o=CONDITION_GT.exec(s)))    { return new Condition('>'        ,o[1],o[2]); }
505                         else if ((o=CONDITION_GE.exec(s)))    { return new Condition('>='       ,o[1],o[2]); }
506                         else if ((o=CONDITION_LT.exec(s)))    { return new Condition('<'        ,o[1],o[2]); }
507                         else if ((o=CONDITION_LE.exec(s)))    { return new Condition('<='       ,o[1],o[2]); }
508                         else if ((o=CONDITION_REGEX.exec(s))) { return new Condition('regex',o[1],o[2]); }
509                         else if ((o=CONDITION_EQ.exec(s)))    { return new Condition('eq'       ,o[1],o[2]); }
510                         return null;
511                 }
512
513         /** Convert a colour string from CSS colour name ("blue"), short hex ("#abc") or long hex ("#a0b0c0"), 
514                         to an integer. 
515                         
516                 @default 0 */
517
518         public static function parseCSSColor(colorStr:String):uint {
519             colorStr = colorStr.toLowerCase();
520             if (CSSCOLORS[colorStr]) {
521                 return CSSCOLORS[colorStr];
522             } else {
523                 var match:Object = HEX.exec(colorStr);
524                 if ( match ) { 
525                   if ( match[1].length == 3) {
526                     // repeat digits. #abc => 0xaabbcc
527                     return Number("0x"+match[1].charAt(0)+match[1].charAt(0)+
528                                        match[1].charAt(1)+match[1].charAt(1)+
529                                        match[1].charAt(2)+match[1].charAt(2));
530                   } else if ( match[1].length == 6) {
531                     return Number("0x"+match[1]);
532                   } else {
533                     return Number("0x000000"); //as good as any
534                   }
535                 }
536             }
537             return 0;
538         }
539         }
540 }