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