What The Interface!!!

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!

  1. 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 []
  • I think this maybe a two pronged issue.

    But Jesse really hit the mark on interfaces. The real issue is giving us - developers more options and flexibility. And as pointed out without interfaces we are stuck the majority of the time using inheritance over composition when developing against the Flex/Flash framework.
  • Completely agree with Jesse on this one. Adobe can do better with interface for both native and AS classes. I can not even count how many times I pull out my hair trying to work around the core AS classes with the numerous inconsistencies in the implementation.

    In Silverlight, coding in C# is way more enjoyable then AS by leaps and bounds.

    "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."
  • You should take a look at Flash Text Engine and Text Layout stuff - it's in Flash Player 10, but was not exposed through any UI in Flash CS4 - it's all script only (with a pre-release component on labs). That's the future, and textfield is going to be obsolete very soon (and probably have a legacy version written in AS using text engine). I would say, you should skip trying to enhance textfield at all at this point, and go directly into Flash Text Engine.

    http://labs.adobe.com/technologies/textlayout/

    They have built their own XML/CSS system, but it should be very possible to actually implement a DOM/CSS system using this technology, and it is probably only a matter of time until someone does just that.
  • CSS: Okay
    Custom CSS: Okay
    But now I hear people wanting to do a full HTML renderer in Flash.

    Now this is going in the wrong direction.

    If you want HTML, use HTML. If you want CSS, use HTML as well.

    Frankly I don't think CSS fits very well with Flash. The *idea* of externalized style data does, however Flash has a different structure from HTML and thus I don't think CSS can cover all the real-world needs of a Flash application.

    For example, CSS can only apply to existing elements. We can't add new decorations from CSS alone, like

    dynamic .Decoration { x:50; y:50; parent:BACKGROUND; animation:#flyFromLeft;}

    CSS doesn't allow nested elements that don't inherit properties. Wouldn't it be nice to have a selector like LeftMenu->Label that's visible only when parsing LeftMenu? Or required values?
  • Okay, maybe I'm an idiot, but why the hell did Adobe create robust HTML parse/display libraries in AIR and NOT IN FLASH!

    That's like telling me you baked a pizza and I CAN'T HAVE ANY! And if you saw me, you'd know I *love* pizza.

    Sorry, but really, why must I rely on hacks to format text properly, or worse, learn a NEW XML language to format using the new TextFormat engine?

    Adobe, please wake up. If you have it, share it. If not, bake it.

    Marcus
  • Hi Jese,
    Great Article! I am with you.

    I am not an super advanced AS 3 developer, but working with AS 3 daily, I have felt the frustrations with AS 3 and CSS!

    I think that the underlining issue is that, flash is limited in CSS support, and only supports CSS1 properties, and a minimal amount for formatting.

    Don't even get me started on using CSS with components, as I personally don't believe it is possible, I might be wrong on this. Okay, again basic CSS works, but not real CSS support!

    I have yet to figure out how to make a form and apply CSS to the input fields.

    I am start starting with Flex, and maybe the CSS is better in Flex.

    I would like to see the day when flash has the same CSS support as html. Back to the form example, it is CSS that makes the form look good!

    Maybe it is because I am not an export developer, and I am not understanding the issues, but I just thought that I would share my thoughts on the subject.
  • i'm on board... i've wanted to extend flash with custom input techniques for years... and its been really difficult to be allowed so much freedom but limited in so many ways :P this "compiled language" is def something that has to be worked out if adobe wishes to grow the player; i'm on board... i've wanted to extend flash with custom input techniques for years... and its been really difficult to be allowed so much freedom but limited in so many ways :P this "compiled language" is def something that has to be worked out if adobe wishes to grow the player;;
  • Well said, Jesse, I too support the cause and would like to see some more loosening of letting the interfaces for the core components let out of the bottle. I think part of this may be Adobe's fear that in doing so, they would open up the language more than they wish to. In the meantime, its a huge burden for any programmer trying to do what you've done with Flash Camo. I'm sure you've smacked your head on the desk a few times in the process as well.
  • i'm on board... i've wanted to extend flash with custom input techniques for years... and its been really difficult to be allowed so much freedom but limited in so many ways :P this "compiled language" is def something that has to be worked out if adobe wishes to grow the player
  • Interfaces, Interfaces, INTERFACES!

    I completely agree with this. There should be an interface for every class that ships with Flash and Flex (both native and AS classes).

    Perhaps we should start a project to write the basic interfaces for Adobe to include. Maybe if it was handed to them for free, they would get it done.
  • raptros-v76
    You know, there's something stupid about people going off about TLF in these comments. Talk about missing the forest for the trees. Jesse is completely right in saying that AS3 needs interfaces. A statically typed object oriented language needs to have and support interfaces, and provide real options for customization. All the major OO languages either use interfaces or dynamic ("duck") typing for object passing. Except for AS3, which has survived only because it has lacked real competition. And now, with competitors on the horizon, it must either provide elegant methods for easy, native-speed customization, or be doomed to become yet another abandoned language, relegated to the trash heap of history. Adobe, beware!
  • I can confirm that the final version doesn't have it pre-installed. It's sitting, waiting for you in "optional updates".

    I did install it though since it's pretty easy to put a checkbox beside it and hit "ok".
  • As others have pointed out Text Layout Framework is what you should be looking at for creating a CSS parser. I think with the new text engine in Flash Player 10, it's just a matter of time before someone creates not just a CSS renderer but a more compatible HTML rendered for Flash.

    Also note that Silverlight is not included with the beta of Windows 7 and I doubt it will included in the final version. Microsoft has gotten into enough trouble with all the anti-trust lawsuits for bundling IE with Windows more Europe (where they hit MS with a $1.3 billion dollar fine) than the US. So they aren't going to be stupid enough to do it again with Silverlight. That's why they spend so much money to get big event websites like the Olympics to use Silverlight. Also Silverlight won't be heavily integrated into the Windows OS, as that's what WPF is for. Plus the only way Microsoft can compete with Adobe's Flash is to make it compatible on the Mac. Silverlight is still going to take a few more years before they reach the penetrations levels of Flash.
  • Dude I'm with you on this one. I wish I would've known about Flash Camo before I developed my own CSS parser.

    Let's not forget that input textfields can't even take CSS. I have NO idea why to be honest, but the work-around that I had to develop sure was a pain.

    I'm guessing there is lack of knowledge/support of the new text rendering engine in FP10 because of the lack of information about it. If it's so easy to build your own CSS parser with the new text stuff... Why hasn't anyone done it yet? Or at least point you in the direction of what to do.
  • Instead of hacking TextField/Stylesheet, you should look into extending the Text Layout Framework (recently opensourced).

    Also, parsing CSS is really not the bottleneck (unless you use RegEx to do that, heh). The bottleneck is resolving the CSS property values.

    +1 on everything else you wrote.
  • I agree it would be nice if have interfaces into the core functionality. I disagree with "There are more dead ends in AS 3 then there ever were in AS 2." I don't think you'll be able to override the built-in stylesheet functionality in AS2, even with prototypes.

    And if you were to follow Ted's suggestion to use TFL (not TextField), you could build it. I do think it was a long time in coming though. TextField has been needing a replacement for years.

    Good luck!
  • Man I totally understand your anger, I hate Flash handicapped CSS (that's not even able to do what it says in the docs, i.e. display: block).

    I love your Camouflage lib, many thanks for doing that! Adobe should just compile it into the FP11.
blog comments powered by Disqus