Support @import directives in MapCSS files
authorRichard Fairhurst <richard@systemed.net>
Sun, 23 Jan 2011 17:55:01 +0000 (17:55 +0000)
committerRichard Fairhurst <richard@systemed.net>
Sun, 23 Jan 2011 17:55:01 +0000 (17:55 +0000)
net/systemeD/halcyon/styleparser/NestedCSSLoader.as [new file with mode: 0644]
net/systemeD/halcyon/styleparser/RuleSet.as

diff --git a/net/systemeD/halcyon/styleparser/NestedCSSLoader.as b/net/systemeD/halcyon/styleparser/NestedCSSLoader.as
new file mode 100644 (file)
index 0000000..1709452
--- /dev/null
@@ -0,0 +1,81 @@
+package net.systemeD.halcyon.styleparser {
+
+       /** A class permitting you to load CSS files containing '@import' rules, which will be 
+       *       automatically replaced with the contents of the file.
+       *
+       *   Typical usage:
+       *
+       *               cssLoader=new NestedCSSLoader();
+       *               cssLoader.addEventListener(Event.COMPLETE, doParseCSS);
+       *               cssLoader.load("potlatch.css");
+       *
+       *       doParseCSS can then access the CSS via event.target.css.
+       */
+
+       import flash.events.*;
+       import flash.net.URLLoader;
+       import flash.net.URLLoaderDataFormat;
+       import flash.net.URLRequest;
+
+       public class NestedCSSLoader extends EventDispatcher {
+               private var sourceCSS:String;
+               public var css:String;
+               private var url:String;
+               private var count:int;
+
+               private static const IMPORT:RegExp=/@import [^'"]*['"]([^'"]+)['"][^;]*;/g;             // '
+
+               public function NestedCSSLoader() {
+               }
+               
+               public function load(url:String):void {
+                       this.url=url;
+                       var request:URLRequest=new URLRequest(url+"?d="+Math.random());
+                       var loader:URLLoader = new URLLoader();
+                       loader.dataFormat = URLLoaderDataFormat.TEXT;
+                       loader.addEventListener(Event.COMPLETE, fileLoaded);
+                       loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, fileError);
+                       loader.addEventListener(IOErrorEvent.IO_ERROR, fileError);
+                       loader.load(request);
+               }
+               
+               private function fileLoaded(event:Event):void {
+                       sourceCSS=URLLoader(event.target).data;
+                       css=sourceCSS;
+                       count=0;
+
+                       var result:Object=IMPORT.exec(sourceCSS);
+                       while (result!=null) {
+                               count++;
+                               replaceCSS(result[1],result[0]);
+                               result=IMPORT.exec(sourceCSS);
+                       }
+                       if (count==0) { fireComplete(); }
+               }
+
+               private function replaceCSS(filename:String, toReplace:String):void {
+                       var cssLoader:NestedCSSLoader=new NestedCSSLoader();
+                       var replaceText:String=toReplace;
+                       cssLoader.addEventListener(Event.COMPLETE, function(event:Event):void {
+                               css=css.replace(replaceText,event.target.css);
+                               decreaseCount();
+                       });
+                       cssLoader.load(filename+"?d="+Math.random());
+               }
+
+               private function fileError(event:Event):void {
+                       // just fire a complete event so we don't get an error dialogue
+                       fireComplete();
+               }
+               
+               private function decreaseCount():void {
+                       count--; if (count>0) return;
+                       fireComplete();
+               }
+               
+               private function fireComplete():void {
+                       var event:Event=new Event(Event.COMPLETE);
+                       dispatchEvent(event);
+               }
+       }
+}
index 9ba07cc..f66609b 100644 (file)
@@ -248,20 +248,13 @@ package net.systemeD.halcyon.styleparser {
                public function loadFromCSS(str:String):void {
                        if (str.match(/[\s\n\r\t]/)!=null) { parseCSS(str); loaded=true; redrawCallback(); return; }
 
-                       var request:DebugURLRequest=new DebugURLRequest(str);
-                       var loader:URLLoader=new URLLoader();
-
-//                     request.method=URLRequestMethod.GET;
-                       loader.dataFormat = URLLoaderDataFormat.TEXT;
-                       loader.addEventListener(Event.COMPLETE,                                         doParseCSS);
-                       loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,            httpStatusHandler);
-                       loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,      securityErrorHandler);
-                       loader.addEventListener(IOErrorEvent.IO_ERROR,                          ioErrorHandler);
-                       loader.load(request.request);
+                       var cssLoader:NestedCSSLoader=new NestedCSSLoader();
+                       cssLoader.addEventListener(Event.COMPLETE, doParseCSS);
+                       cssLoader.load(str);
                }
 
                private function doParseCSS(e:Event):void {
-                       parseCSS(e.target.data);
+                       parseCSS(e.target.css);
                }
 
                private function parseCSS(str:String):void {