What The Interface!!!
Posted by Jesse Freeman | Filed under Advanced Topics, Featured
For anyone who was not following Ted or me yesterday on Twitter you may have missed me ranting about my deep frustration and hatred for flash’s CSS parser. I wound up trying to explain myself in small spurts of 144 characters, hardly a way to have a real conversation on a serous subject. I felt that my blog would be a better outlet to finish my thoughts.
I was actually ready to retract my argument that Flash needs a real CSS parser which follows standards until Ted said something that really doesn’t sit right we me: Build it! Nothing is in your way”. You may be asking “what’s wrong with that”? Everyday developers write libraries and frameworks that enhance the core functionality of the language. Considering I have spent more then a year writing a powerfully CSS parser for Flash Camouflage Framework I feel this is finally something I can publicly have a strong opinion about! Let’s look at why I, or anyone else for that matter, will never be able to fix the AS 3’s CSS parser or properly enhance it.
The biggest underlying problem with adding functionality onto core parts of AS 3 starts with the numerous inconsistencies in the implementation of the language. There are more dead ends in AS 3 then there ever were in AS 2. There I said it, and this is coming from a person who would quit his job if I had to touch AS 2 again. At least in AS 2 you could prototype in the functionality you wanted, AS 3 locks everything down and the core classes are intrinsic so there is no source code to modify, reuse or enhance without doing huge rewrites. Here is the problem:
So I want to extend the functionality of the StyleSheet class in flash, how do I go about doing that with the tools I have been given?
Let’s start with extending StyleSheet. This sounds like the right place to start. I want TextFields to be able to use my custom StyleSheet so let’s implement StyleSheet’s interface. Wow we didn’t get far before our first road block. Now why would you need an interface?
When I extend StyleSheet my custom StyleSheet will technically pass the compilers type check when I try to use it with a TextField. Looks like we are good so far, lets take a look at the parsing algorithm so we can see where to modify it. Damn hit our second wall. The logic that runs behind the StyleSheet class is built into the player and added at runtime. I have no idea what happens behind the scenes nor is there a way to take advantage of it so it so we are left with no choice but to create a custom parser. We can start by overrideing parseCSS.
So I have to create my own custom parser using the only text manipulation tool I have, RegEX. RegEX is incredibly powerfully but what we need to do is really intensive. Let’s take a look:
- Validate the CSS structure
- Compress the white space
- Remove everything we can’t use like comments px etc
- Pull out all the selectors
- Parse the selectors with string manipulation
Here is what the basic RegEx from Flash Camo looks like:
/\s*([@{}:;,]|\)\s|\s\()\s*|\/\*([^*\\\\]|\*(?!\/))+\*\/|[\n\r\t]|(px)/g;
That is kind of crazy, there is a lot going on but wait there is still more parsing that needs to be done. We still have to pull out the CSS blocks:
/[^{]*\{([^}]*)*}/g
Cool so now we have all of the CSS blocks so now we can begin indexing then. If you look at the ASDocs for StyleSheet you will see all of the public methods. I have to use the ASDocs because Flash CS 4 and Flex Builder can’t explore SWCs. Looks like we hit our next roadblock. Adobe’s own tools just got in my way of cutting down another step in creating my custom StyleSheet. I have to this all by hand now.
If you look at the API we will need to override and create custom methods for:
- getStyle()
- parseCSS()
- setStyle()
- toString()
- transform()
We are now overriding every method of StyleSheet. Not only are we overriding it but since we don’t have access to or any way of finding out what private variables the StyleSheet uses to store each CSS Block including the index of CSS selector names we will have to create our own variables to store these. At this point we have now overridden the entire StyleSheet class with our own logic. Why do we have to do this? If we had an interface for all of these public methods I could have just created a new class that could extend any number of my own custom classes to act as the parser. If the TextField was typed to IStyleSheet we could swap out any class (not just ones that extended IStyleSheet) as long as it correctly implemented the interface.
This brings be to my number 1 beef with AS 3, where are the interfaces? Adobe totes AS 3 as an object oriented language. Well the most important part of OOP programing is to code to an interface rather than an implementation. Right here I am locked into always extending core classes of AS 3 instead of implementing an interface and using composition. One of the biggest failures of AS 3’s implementation is the lack of interfaces for the DisplayObjects. I can’t do anything custom without forcing myself to directly type to a specific display class or create my own interface for IDisplayObject which I do but fails in cases when you want to use addChild.
The final straw that breaks the developers back is that even with my custom solution it will never run at native speed. I am always locked into using RegEX and string manipulation to handle my custom CSS. I have no way, outside of writing a C++ parser and using Alchemy to do the heavy lifting for me. How many developers are really going to do this? I don’t want to mix multiple languages, especially a cutting edge feature, in a production product. So how can we as users/developer ever fix the problem?
This all is in Adobe’s hands! How can I implement interfaces into a compiled language? This gets us to last weeks Adobe bashing on Twitter. Ted really opened a can of worms by asking people to comment on his blog about what they want to see in future versions of the language. As you would expect, everything degraded from there. The part that really got to me was a tweet from Grant Skinner toting how he made the list of people to influence Adobe twice http://twitter.com/gskinner/status/3152707469. Thats great if you are Grant Skinner but what about regular flash developers? You know the people who use Flash day in and out and don’t do crazy experiments or have an “in” with Adobe. This is not an attack on Grant but for “the rest of us” it’s a tough pill to swallow when we have no real say in how the platform grows. On one side I am heavily involved in the Flash Community but I walk a thin line that makes it harder and harder for me to really express my opinion. I don’t want to insult/offend people or Adobe but at the same time I have a voice and my style is not to sit back and keep quiet.
Where do we go from here? To another language I guess! I say that half as a joke and as the reality. I am watching everyday as Unity, HTML 5 and Silverlight creep up on Flash. The writing is on the walls for Adobe, they can no longer get by like they did with minor feature upgrades like in Flash Player 9 -> 10. If you think about the reality of it, next year Silverlight will have reached Flash’s penetration levels. How will this happen? Microsoft will have a copy of Silverlight 3 with every Windows 7 Install. Not to mention whenever there is a system upgrade they will throw in a few updates for Silverlight that is transparent to the end user. Silverlight will be heavily integrated into the OS. When you can’t question penetration numbers, what is really keeping you back from building sites for it?1
I want to keep this positive and productive in the end. I know it is hard to believe after ranting for this entire article but I am sick of having to explain to Creative Directors that Flash can’t do the typography treatment they want but I can use Augmented Reality, stream HD video, and build interactive 3d worlds. I am tired of creating complex systems for handling text in cms driven sites. I don’t want to have to teach another client how to wrap HTML text with one set of styles then the other half of with textformat tags. However, I am able to fix what Macromedia/Adobe has neglected for years with my own CSS parser but I just can’t integrate it into the core language.
With all this being said I would love to open up the discussion with Ted and Adobe to figure out how to fix this issue? While I wait I’m start a new campaign called What the Interface: Tell Adobe to Fix AS 3. I ask everyone who has pulled out their hair out over the native CSS parser or if you can’t build the classes you want because there is no interface to leave a comment, post a link to this on your site, and retweet it with #WhatTheInterface hash. Until then I will keep supporting Flash Camo’s CSS parser for anyone who wants real CSS support. The only problem is that you have to spend lots of time abstracting core AS 3 classes to have it fit your needs!
- This has been corrected by Matthew Fabb who explained that Micro$oft is not allowed to pre-install Silverlight. That makes no sense to me but I somehow believe it. Lucky for adobe, so as long as M$ keeps throwing money at companies/developers to build it Adobe will have some competition [↩]
-
Josh Iverson
-
Peter C
-
Kevin Newman
-
archont
-
Marcus Blankenship
-
Johnny
-
Bulova
-
Alex Baker
-
cmoore
-
Steve Mathews
-
raptros-v76
-
Scott
-
Matthew Fabb
-
Scott (@Endoplasmic)
-
Claus Wahlers
-
Jacob Wright
-
Og2t












