YUI 3 Quick Tip: Conditionally load dataURI’d CSS or MHTML

If you’re already using the YUI 3 Loader to load some of your CSS like I do for some sites this can come in really handy. You could also do this via conditional comments, but you can’t control when the load happens in that case.

The trick is to define the trigger for the MHTML module to trigger off of the regular module, & also make sure that the MHTML module will supersede the regular one. Then, it’s a simple matter of setting up either the simple UA check or a test function that returns true/false.

Note the when config setting’s value of “instead” that specifies that this module should replace “icons-css”. That only happens if the test function returns true, indicating hte module should be loaded. Since IE 8+ supports dataURIs, we return true only if running IE 7 or lower which causes the “icons-css” module to be replaced by “icons-css-mhtml”.

It’s a neat trick, huge thanks to Adam Moore of the YUI team for teaching me about it on the forums! Also thanks to Nicholas Zakas for creating CSSEmbed (also here), the tool that makes generating DataURI & MHTML versions of a stylesheet ridiculously easy. Stoyan Stefanov gets a mention here too because of his awesome DataURI/MHTML research.

YUI 3 Loader & Aggregated Files

With the launch of the ArenaNet Blog I’ve been working a lot with YUI3 since it is the JS framework that powers all the interactivity on the site. The other great thing it does is provide a Loader that can pull in JS files on-demand as defined by a configuration object you pass to it. Loader has the capability to roll up multiple requests into a single file using a CDN but the functionality to do the same without using a CDN appears to be broken. Adam Moore from the YUI team & I have been discussing the issue on YUI’s bug tracker without a resolution yet.

Since I need to keep moving on this stuff I’ve had to devise my own solution to the issue I’m experiencing. For background, we use Apache Ant to create a “Deploy” version of the WordPress theme on the blog. This process runs through CSS/JS & renames files, compresses them, updates references, does DataURI stuff, etc. It’s pretty nice and allows for easily deploying a very optimized site without making development a nightmare. As part of that I’ve had it roll up two JS files that are always requested together into one file to avoid making an extra HTTP request. Unfortunately when I tried a simple replace of the previous filenames with the new aggregate file’s name YUI 3′s Loader was loading the file twice.

So until I can get a working version of what Adam has suggested here’s how I’ve set up the build system to allow for replacing individual modules in development mode with a single aggregate file for deploying.

//DEV CONFIG
YUI_config = {
    //yes this is a little gross but sadly it's necessary to make loader work how we want w/ ant
    /* dev */ ignore : [ "sidebar-rollup" ], //dev */
    /* live ignore : [ "chiclet", "slider-plugin" ], //live */
    groups : {
        anet : {
            combine : false,
            base : "/blog/wp-content/themes/arenanet/js/lib/",
            modules : {
                "chiclet" : {
                    path : "chiclet.js",
                    skinnable: false,
                    requires : [ "base", "widget", "anim-base", "anim-easing", "sidebar-rollup" ]
                },

                "slider-plugin" : {
                    path : "slider.js",
                    requires : ["plugin", "node", "anim-base", "anim-easing", "sidebar-rollup" ]
                },

                "gallery-lightbox" : {
                    path : "lightbox.js",
                    requires : ["base", "node", "anim-base", "selector-css3"]
                },

                "scroller-plugin" : {
                    path : "scroll.js",
                    requires : ["plugin", "node", "selector-css3", "event-delegate", "anim-base", "anim-scroll", "anim-easing"]
                },

                "social-counts" : {
                    path : "social.js",
                    requires : [ "gallery-yql", "node" ]
                },

                "sidebar-rollup" : {
                    path : "sidebar.js"
                }
            }
        }
    }
};

which is modified by the Ant build script to

//DEPLOY MODE
YUI_config = {
    //yes this is a little gross but sadly it's necessary to make loader work how we want w/ ant
    /* dev  ignore : [ "sidebar-rollup" ], //dev */
    /* live */ ignore : [ "chiclet", "slider-plugin" ], //live */
    groups : {
        anet : {
            //omitted for brevity, see above code block because this doesn't change between them
            }
        }
    }
};

The comments get stripped out during compression so it doesn’t look quite so gnarly when sent to end-users. It’s also pretty easy to do as an Ant task, all it takes is this




to do the switch. Later in my custom loader script those two modules are used with

YUI().use("node", "event-base", "imageloader", "selector-css3", "chiclet", "slider-plugin", function(Y) {
    // Code goes here...
});

which will always make sure that both the “Chiclet” and “Slider-Plugin” modules are available. In dev mode it will request chiclet.js & slider.js versus just asking for sidebar.js when deployed.

Does anybody have a better way of doing this? I think Adam’s version is nicer looking in the config but I’m not sure how it will work if a) the rollup file doesn’t exist or b) you don’t want to use the rollup except in production. I guess you could always just do something similar to what I’m doing now to configure it. That all assumes that the issues with it not actually loading the module get figured out, because if the modules aren’t added to the Y instance what’s the point?

Arena.net

While I don’t work on the team responsible for the Arena.net site I help out all over the company wherever I can. When it was decided that the old site needed to be updated I got the call. After some discussions around what exactly the site should encompass it came down to wanting to have the site be an aggregator of updates by arena.net on various different services.

Here’s the rundown:

I certainly wasn’t looking forward to building parsers for the multiple different feed types that entails, but YQL came along and saved the day again. I only had to do simple things manually like make sure that Twitter links & @username replies were properly wrappped in <a> tags. To keep from bashing on YQL every time the page is loaded it stores the transformed results of the updates for an hour in a local APC cache. This provided the best of both worlds and made caching super-easy.

The site itself was built using YUI grids, YUI 3b1, and DD_belatedPNG to solve transparent PNG issues in IE6. It’s also using my favorite new PHP framework, Nice Dog. It’s a nano framework (about 100 lines) that is more of a VC than MVC. For a site like Arena.net it’s perfect as there’s no need for models. I could’ve plugged in an ORM to make it the full stack but when getting all your data is as simple as

apc_fetch($key);

the need to build a “proper” MVC site kinda isn’t there any longer.

Programmer’s Notepad and JSLint

I’ve been using Programmer’s Notepad for some time now as my go-to text editor for code. It’s simple to use and really robust and over all just a really great tool. As the Javascript I write has gotten more and more complicated, I’ve found myself often hitting up Douglas Crockford’s JS Lint to sanity check what I’m doing. When I found out that Textmate offered JSLint integration via a bundle I have to say I was pretty jealous.

Then I stumbled across a post from Simon Steele, the developer of Programmer’s Notepad.

Tools I Rely On – Those I Use From PN

So now I knew that you could use JSLint from within Programmer’s Notepad, and had a starting point in the link he provides to the Windows Scripting Host compatible version. I began stumbling around the tools menu in PN, trying to figure out how to get the jslint.js file I had downloaded to be usable as a tool. I figured it out with some help from Simon on the PN forums, Issues with using JSLint as a tool.

With that in place, I was able to get JSLint working as a easily-called tool within PN. With a simple keystroke I can sanity check my JS for all sorts of nasty behaviors, it’s totally awesome. Here’s a quick walkthrough I wrote up after mentioning this on a Yahoo! internal mailing list and getting a question about it.

  1. Downloaded the WSH version of JSLint from http://jslint.com/wsh/index.html and copied it into a tools subdirectory in the Programmer’s Notepad directory (just for ease of referencing).
  2. Added a new tool for Javascript files:
    Tools -> Add Tools -> Scheme: Javascript -> Add
  3. Settings are as follows:
    JSLint Settings

Those settings allow for double-clicking the JSLint output line and having PN jump to it, which is really handy. Unfortunately it stops after every error and always complains about a null character at the end of files, but those are minor annoyances. It works really well and is pretty quick to get started, which I appreciate.

I’d love to get it so that it’ll run JSLint every time I save a .js file, but I don’t think PN supports that for tools yet. Something to ask about on their forums, I suppose.

Work Work Work

I am insanely busy with work stuff, but we’re in the home stretch and that’s really exciting. I will share more about what I’ve been doing soon!

Javascript, ActiveX and IFrames

Been working on a project that involves getting some JS to interact with an ActiveX control and then have that work with an IFrame for asynchronous uploading.  It’s been interesting, at least.  Getting the AX object into the page so that my JS could actually reference its methods was the first hurdle.  After a day of banging my head against the wall I stumbled onto a site that recommended using innerHTML to write it out.  Wonder of wonders, it worked great.

I started building up the JS scaffolding  around that and promptly ran into another issue.  While the ActiveX object is thinking it calls a predetermined callback method in the JS to give status updates.  Unfortunately, you can’t declare this callback until the object is in the page.  This means dynamically inserting a new JS file in the initialization after dynamically inserting the ActiveX object.  That’s a lot of dynamic inserting and it makes me a bit nervous but it works fine in IE and since we’re using ActiveX that’s all I have to test!

The next step is getting asynchronous uploading using an IFrame working.  This is being complicated by most tutorials that cover this technique being old as dirt.  We’re talking 2002 old as dirt here, before AJAX was anything more than a cleaning product.  “Script Remoting” is not what I want to do (I have XMLHTTPRequest for that, thank god) but that’s what most of these tutorial focus on.  It makes parsing out the useful info needlessly complex and it’s really driving me up the wall.

Having a a good time at JumpCut though. :D

YUI is so awesome even Google uses it

I was linked by a friend to a very interesting article by Zach Leatherman about the CSS used on Google’s customizable homepage, http://google.com/ig/.

Turns out, they’re using the YUI grids component to space everything out on the page. That’d normally be fine, but the terms of the BSD license for the YUI modules requires that you leave in the copyright notice. Google didn’t do this, so technically they are stealing the code. Tsk tsk.

Here is Zach’s much more in-depth article: Google Using YUI Grids CSS

Once more, for old times’ sake

Since I’m not doing any real Restek work any more I decided to resurrect the CVS Pie Graph that I wrote ages ago. It broke during the transition to the new DB class that uses PDO. Fortunately, it’s a pretty simple query so getting it running again wasn’t difficult. We still store CVS checkin stats so the data is reasonably up to date as well.

Somehow I’m still leading Gordon but we all know that won’t last. He’s a monster even with being farmed out to HITS part-time. Kian’s apparently taken himself out of the running by making the script ignore all his checkins. Seems to me he just doesn’t like losing. :D

More than anything this just reminds me of how long I’ve been doing this, it’s definitely time I let somebody else take their hack at it. I’m really curious to see what new apps come out as well as where the old ones are taken. It’s harder nowadays to justify a wide-ranging rewrite to Deb but I bet if someone really clever comes along it could be arranged. Things have settled out and become a lot more stable during my time at Restek but there’s always room for improvement in these kind of things.

I can talk about what I did last summer

Last summer I got an amazing opportunity to intern at Yahoo! for 3 months. It was a really cool experience and I learned a ton while working on a project that at the time I could not talk about. As it turns out, that project has finally gone public beta and I can talk about it (at least so far as it exists and stuff I guess).

All New My Yahoo

All New My Yahoo!

I worked on the team that made that, albeit in a earlier and less polished form. It’s hard to tell what of my code survived since according to my old manager they had to throw out a lot of the old designs. However, the weather widget still looks like the same one I wrote with some moderate refactoring. That was cool to see as I’m pretty proud of that weather widget. The “Set My Yahoo! as your Home Page” link is still there and appears to still work the same way. I can’t say for sure on any of this because untangling jsmin’d javascript is no fun.

The new beta can be accessed by going to http://cm.my.yahoo.com/upgrade and I have to say I like it quite a bit. The inclusion of the big ad on the right is too bad but with AdBlock I never see it so it’s a non-issue for me. The team has done a really amazing job on this in the 6 months I’ve been gone, a lot of the early designs have been polished until they practically shine. It’s definitely still feeling the performance blues but I have no doubt that they’ll get it running smoothly before launch.

How do you prove you’re a human?

This is a pretty serious problem, and currently CAPTCHAs are used on a lot of sites. I know I’m not alone in really hating most CAPTCHAs: they’re hard to understand, it’s an annoying step to have to take, and I never know whether to enter it in upper or lower case. Nothing I run has had any spammer issues, mostly due to a lack of anything interesting for spammers on my sites.

If I had to try and prove signups were from people though, I’d be damn tempted to use Microsoft Research’s recently announced Asirra Project. It’s a lot like KittenAuth only they’ve got a DB of 2 million pet images and add about 10k new ones each day. Definitely an interesting approach. If cute animals aren’t your thing maybe you would prefer Hot Captcha which Asirra cites as inspiration. They went with animals for a couple of good reasons, hotness is relative and pictures could be offensive.

All interesting attempts at solving a very complex problem: how does a machine generate something that a machine cannot recognize, but a human can?