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