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