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