<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>The CSS Ninja</title>
	
	<link>http://www.thecssninja.com</link>
	<description>All things CSS, JavaScript &amp; HTML</description>
	<lastBuildDate>Thu, 25 Apr 2013 06:18:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TheCSSNinja" /><feedburner:info uri="thecssninja" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Stealing the users back button with the History API</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/7vD6itl3n0I/stealing-history-api</link>
		<comments>http://www.thecssninja.com/javascript/stealing-history-api#comments</comments>
		<pubDate>Wed, 24 Apr 2013 08:50:08 +0000</pubDate>
		<dc:creator>Ryan Seddon</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=1219</guid>
		<description><![CDATA[Gruber posted a video of a website that does some dodgy history insertion. Go to tgdaily.com let it load (it has horrible perf so give it a bit) and click back and you&#8217;ll notice that you get taken back to exitjunction.com with tgdaily as a query. Insert rage face here. Once past rage face open [...]]]></description>
				<content:encoded><![CDATA[<p>Gruber posted a <a href="http://vimeo.com/64567684 ">video</a> of a website that does some dodgy history insertion. Go to <a href="http://tgdaily.com" rel="nofollow">tgdaily.com</a> let it load (it has horrible perf so give it a bit) and click back and you&#8217;ll notice that you get taken back to exitjunction.com with tgdaily as a query. Insert rage face here. Once past rage face open dev tools and investigate.<span id="more-1219"></span></p>
<h2><abbr title="Too Long; Didn't Read">TL;DR</abbr></h2>
<p>They&#8217;re using the history API to initially replace your state then push the original unaltered state back so it looks like you went nowhere. Clicking back takes you to that special state which then does a <code>location.replace</code> to your new back state, because that&#8217;s how you win readers. See demo below for simplified version.</p>
<div class="resources01"><a href="http://thecssninja.com/demo/history_stealing/" title="History stealing demo" class="demo" target="_blank">View live demo</a> <a href="http://thecssninja.com/demo/history_stealing/history_stealing.zip" title="Download the source of the history stealing demo" class="demo source" target="_blank">Download the source files</a></div>
<div class="syntax code">
<pre><span class="nx">history</span><span class="p">.</span><span class="nx">replaceState</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nb">document</span><span class="p">.</span><span class="nx">title</span><span class="p">,</span> <span class="nx">location</span><span class="p">.</span><span class="nx">pathname</span><span class="o">+</span><span class="s2">"#!/stealingyourhistory"</span><span class="p">);</span>
<span class="nx">history</span><span class="p">.</span><span class="nx">pushState</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nb">document</span><span class="p">.</span><span class="nx">title</span><span class="p">,</span> <span class="nx">location</span><span class="p">.</span><span class="nx">pathname</span><span class="p">);</span>

<span class="nb">window</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="s2">"popstate"</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">if</span><span class="p">(</span><span class="nx">location</span><span class="p">.</span><span class="nx">hash</span> <span class="o">===</span> <span class="s2">"#!/stealingyourhistory"</span><span class="p">)</span> <span class="p">{</span>
	<span class="nx">history</span><span class="p">.</span><span class="nx">replaceState</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nb">document</span><span class="p">.</span><span class="nx">title</span><span class="p">,</span> <span class="nx">location</span><span class="p">.</span><span class="nx">pathname</span><span class="p">);</span>
	<span class="nx">setTimeout</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span>
	  <span class="nx">location</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="s2">"http://www.thecssninja.com/"</span><span class="p">);</span>
	<span class="p">},</span><span class="mi">0</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">},</span> <span class="kc">false</span><span class="p">);</span>
</pre>
</div>
<p>Very simplified version they also cover non history API supporting browsers by doing hashchange and polling failing that. But I only have eyes for history API.</p>
<div class="syntax code">
<pre><span class="nx">history</span><span class="p">.</span><span class="nx">replaceState</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nb">document</span><span class="p">.</span><span class="nx">title</span><span class="p">,</span> <span class="nx">location</span><span class="p">.</span><span class="nx">pathname</span><span class="o">+</span><span class="s2">"#!/stealingyourhistory"</span><span class="p">);</span>
<span class="nx">history</span><span class="p">.</span><span class="nx">pushState</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nb">document</span><span class="p">.</span><span class="nx">title</span><span class="p">,</span> <span class="nx">location</span><span class="p">.</span><span class="nx">pathname</span><span class="p">);</span></pre>
</div>
<p>Upon loading the demo I replace the current URL with a #!/stealingyourhistory then sneakily push a new state on top for the original unaltered domain.</p>
<div class="syntax code">
<pre><span class="nb">window</span><span class="p">.</span><span class="nx">addEventListener</span><span class="p">(</span><span class="s2">"popstate"</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">if</span><span class="p">(</span><span class="nx">location</span><span class="p">.</span><span class="nx">hash</span> <span class="o">===</span> <span class="s2">"#!/stealingyourhistory"</span><span class="p">)</span> <span class="p">{</span>
	<span class="nx">setTimeout</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span>
	  <span class="nx">location</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="s2">"http://www.thecssninja.com/"</span><span class="p">);</span>
	<span class="p">},</span><span class="mi">0</span><span class="p">);</span>
  <span class="p">}</span>
<span class="p">},</span> <span class="kc">false</span><span class="p">);</span>
</pre>
</div>
<p>Attaching to the &#8220;<code>popstate</code>&#8221; event so we know when the user is going back I check the <code>location.hash</code> for our dodgy one and then replace the location to whatever dodgy website I want you to go back too.</p>
<p>The zero <code>setTimeout</code> is so we&#8217;ll be placed to run at the end of the browser event loop.</p>
<h2>It&#8217;s more noticeable in some browsers</h2>
<p>Thankfully Firefox and Chrome pause for a moment after a popstate so you can see the dodgy inserted hash url before the page redirects to your new history state. Opera and Safari will do it instantaneously :S.</p>
<p>However we can &#8220;improve&#8221; their script by doing another sneaky line of code.</p>
<div class="syntax code">
<pre><span class="nx">history</span><span class="p">.</span><span class="nx">replaceState</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nb">document</span><span class="p">.</span><span class="nx">title</span><span class="p">,</span> <span class="nx">location</span><span class="p">.</span><span class="nx">pathname</span><span class="p">);</span></pre>
</div>
<p>All we&#8217;ve done is once again use a <code>replaceState</code> to restore the url to the clean unaltered state so all that happens is a  slight pause in Chrome and Firefox and no URL change to the user.</p>
<p><strong>Don&#8217;t do this!</strong></p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/047">http://cssn.in/ja/047</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/stealing-history-api/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/stealing-history-api</feedburner:origLink></item>
		<item>
		<title>Testing $location in an Angular service</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/P_15bJQ9ocE/angular-location-testing</link>
		<comments>http://www.thecssninja.com/javascript/angular-location-testing#comments</comments>
		<pubDate>Fri, 01 Feb 2013 07:12:16 +0000</pubDate>
		<dc:creator>Ryan Seddon</dc:creator>
				<category><![CDATA[angularjs]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=1192</guid>
		<description><![CDATA[I&#8217;ve been deep in the Angularjs world and have gone through the many emotions other developers have expressed. One thing that is lacking is best practice on testing, although yearofmoo has a huge article on testing which improves this greatly. I still had some trouble and I thought I&#8217;d post this to help others. How [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been deep in the <a href="http://angularjs.org/ ">Angularjs</a> world and have gone through the many <a href="http://melbjs-preso.angularftw.com/##4.0">emotions</a> other developers have <a href="http://www.bennadel.com/blog/2439-My-Experience-With-AngularJS-The-Super-heroic-JavaScript-MVW-Framework.htm ">expressed</a>. One thing that is lacking is best practice on testing, although <a href="www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-testacular.html">yearofmoo</a> has a huge article on testing which improves this greatly. I still had some trouble and I thought I&#8217;d post this to help others.<span id="more-1192"></span></p>
<h2>How do I test a service that depends on $location?</h2>
<p>This was the very question I had trouble finding an answer. I have a service that injects <code>$location</code> so I can determine the environment my web app is running on.</p>
<div class="syntax code">
<pre><span class="nx">App</span><span class="p">.</span><span class="nx">factory</span><span class="p">(</span><span class="s1">'urlRoot'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'$location'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">$location</span><span class="p">)</span> <span class="p">{</span>
     <span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="s1">'https://api.example.com.au'</span><span class="p">,</span>
          <span class="nx">host</span> <span class="o">=</span> <span class="nx">$location</span><span class="p">.</span><span class="nx">host</span><span class="p">(),</span>
          <span class="nx">suffix</span> <span class="o">=</span> <span class="sr">/\.[\w\-]+$/</span><span class="p">.</span><span class="nx">exec</span><span class="p">(</span><span class="nx">host</span><span class="p">),</span>
          <span class="nx">filter</span> <span class="o">=</span> <span class="sr">/\.(nz|au)/</span><span class="p">;</span>

     <span class="nx">suffix</span> <span class="o">=</span> <span class="p">(</span><span class="o">!</span><span class="nx">suffix</span> <span class="o">||</span> <span class="nx">filter</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">suffix</span><span class="p">))</span> <span class="o">?</span> <span class="s1">''</span> <span class="o">:</span> <span class="nx">suffix</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span>

     <span class="k">return</span> <span class="p">[</span><span class="nx">url</span><span class="p">,</span> <span class="nx">suffix</span><span class="p">,</span> <span class="s1">'/v1/'</span><span class="p">].</span><span class="nx">join</span><span class="p">(</span><span class="s1">''</span><span class="p">);</span>
<span class="p">}]);</span>
</pre>
</div>
<p>Pretty simple service that injects <code>$location</code> inspects the host and extracts the environment e.g. .dev, .staging etc. It then returns the correct API URL to call in that environment.</p>
<p>In the test I needed to be able to mock the Angulars <code>$location</code> object so I could test different scenarios, thankfully <code>$location</code> depends on <code>$browser</code> and in tests <code>$browser</code> is mocked in the angular-mocks.js file allowing us to &#8220;change&#8221; the URL so <code>$location</code> can be tested with different values. This way <code>$location</code> is the real service and <code>$browser</code> is altering what <code>$location</code> sees as the current URL.</p>
<div class="syntax code">
<pre><span class="nx">describe</span><span class="p">(</span><span class="s1">'Service: urlRoot'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>

  <span class="kd">var</span> <span class="nx">envs</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">'http://example.com.au.dev'</span><span class="p">,</span>
    <span class="s1">'http://example.com.au.staging'</span><span class="p">,</span>
    <span class="s1">'http://example.co.nz.staging'</span><span class="p">,</span>
    <span class="s1">'http://example.co.nz'</span>
  <span class="p">],</span>
  <span class="nx">count</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

  <span class="c1">// load the service's module</span>
  <span class="nx">beforeEach</span><span class="p">(</span><span class="nx">module</span><span class="p">(</span><span class="s1">'App'</span><span class="p">));</span>

  <span class="nx">it</span><span class="p">(</span><span class="s1">'urlRoot should exist'</span><span class="p">,</span> <span class="nx">inject</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">urlRoot</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">expect</span><span class="p">(</span><span class="o">!!</span><span class="nx">urlRoot</span><span class="p">).</span><span class="nx">toBe</span><span class="p">(</span><span class="kc">true</span><span class="p">);</span>
  <span class="p">}));</span>
  <span class="nx">it</span><span class="p">(</span><span class="s1">'urlRoot should default to production endpoint'</span><span class="p">,</span> <span class="nx">inject</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">urlRoot</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">expect</span><span class="p">(</span><span class="nx">urlRoot</span><span class="p">).</span><span class="nx">toEqual</span><span class="p">(</span><span class="s1">'https://api.example.com.au/v1/'</span><span class="p">);</span>
  <span class="p">}));</span>

  <span class="nx">describe</span><span class="p">(</span><span class="s1">'Service: urlRoot specific environments'</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="nx">beforeEach</span><span class="p">(</span><span class="nx">inject</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">$browser</span><span class="p">){</span>
      <span class="nx">$browser</span><span class="p">.</span><span class="nx">url</span><span class="p">(</span><span class="nx">envs</span><span class="p">[</span><span class="nx">count</span><span class="o">++</span><span class="p">]);</span>
    <span class="p">}));</span>

    <span class="nx">it</span><span class="p">(</span><span class="s1">'urlRoot should have ".dev" environment suffix'</span><span class="p">,</span> <span class="nx">inject</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">urlRoot</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">expect</span><span class="p">(</span><span class="nx">urlRoot</span><span class="p">).</span><span class="nx">toEqual</span><span class="p">(</span><span class="s1">'https://api.example.com.au.dev/v1/'</span><span class="p">);</span>
    <span class="p">}));</span>

    <span class="nx">it</span><span class="p">(</span><span class="s1">'urlRoot should have ".staging" environment suffix'</span><span class="p">,</span> <span class="nx">inject</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">urlRoot</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">expect</span><span class="p">(</span><span class="nx">urlRoot</span><span class="p">).</span><span class="nx">toEqual</span><span class="p">(</span><span class="s1">'https://api.example.com.au.staging/v1/'</span><span class="p">);</span>
    <span class="p">}));</span>

    <span class="nx">it</span><span class="p">(</span><span class="s1">'urlRoot should be agnostic of .co.nz domains'</span><span class="p">,</span> <span class="nx">inject</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">urlRoot</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">expect</span><span class="p">(</span><span class="nx">urlRoot</span><span class="p">).</span><span class="nx">toEqual</span><span class="p">(</span><span class="s1">'https://api.example.com.au.staging/v1/'</span><span class="p">);</span>
    <span class="p">}));</span>

    <span class="nx">it</span><span class="p">(</span><span class="s1">'urlRoot should ignore the last suffix if it\'s .nz or .au'</span><span class="p">,</span> <span class="nx">inject</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">urlRoot</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">expect</span><span class="p">(</span><span class="nx">urlRoot</span><span class="p">).</span><span class="nx">toEqual</span><span class="p">(</span><span class="s1">'https://api.example.com.au/v1/'</span><span class="p">);</span>
    <span class="p">}));</span>
  <span class="p">});</span>

<span class="p">});</span>
</pre>
</div>
<p>From the code I have an array of fake environment URLs I want to test against. I use beforeEach to inject <code>$browser</code>, set the URL to one of the environments and using a counter variable increment it for each test. You&#8217;ll notice that I&#8217;m also injecting <code>urlRoot</code> for every test, this is so the <code>beforeEach</code> method can inject <code>$browser</code> and update the URL before the <code>urlRoot</code> service is injected and <code>$location</code> will then be aware of the new URL as it&#8217;s initialised after the URL has been changed.</p>
<p>I would love feedback from fellow Angular devs if this approach is the best way to handle a scenario like this.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/046">http://cssn.in/ja/046</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/angular-location-testing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/angular-location-testing</feedburner:origLink></item>
		<item>
		<title>Multi-level Source maps</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/xU_30JsvpOk/multi-level-sourcemaps</link>
		<comments>http://www.thecssninja.com/javascript/multi-level-sourcemaps#comments</comments>
		<pubDate>Fri, 02 Nov 2012 09:43:27 +0000</pubDate>
		<dc:creator>Ryan Seddon</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=1154</guid>
		<description><![CDATA[Source maps are awesome but one issue is that compiling from x-lang to JavaScript is a single level of mapping, if you want to go from x-lang > JavaScript > minified JavaScript you couldn&#8217;t as closure compiler, currently, only has a single level of mapping, until now. UglifyJS2 allows you to specify an input source [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.thecssninja.com/javascript/source-mapping">Source maps</a> are awesome but one issue is that compiling from x-lang to JavaScript is a single level of mapping, if you want to go from x-lang > JavaScript > minified JavaScript you couldn&#8217;t as closure compiler, currently, only has a single level of mapping, until now. <a href="https://github.com/mishoo/UglifyJS2" target="_blank">UglifyJS2</a> allows you to specify an input source map from the first stage of compilation, enabling multi-level mapping.<span id="more-1154"></span></p>
<p>Check out the following demonstration showing multi-level source maps for TypeScript and Coffeescript. <span id="enable">If you haven&#8217;t enabled source maps you can do so in Chrome and WebKit Nightly by opening the dev tools > clicking the cog in the lower right corner > general > enable source maps</span>.</p>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2012/11/enable-source-maps2.png" alt="" title="enable-source-maps2" width="602" height="230" /></p>
<div class="resources01"><a target="_blank" title="Multi-level Source maps using CoffeeScript and TypeScript" href="http://www.thecssninja.com/demo/source_mapping/multi-level/" target="_blank">Multi-level Source maps demo</a> <a target="_blank" class="demo source" title="Download the source" href="http://www.thecssninja.com/demo/source_mapping/multi-level/demo.zip">Download the source files</a></div>
<h2>Multi-level what?</h2>
<p>Now in code, let&#8217;s say CoffeeScript(CS), you can compile to JavaScript and generate a source map using the new <a href="https://github.com/michaelficarra/CoffeeScriptRedux">redux compiler</a>. You take that generated source map and at the minifying stage, with UglifyJS2, specify <code>--in-source-map</code> option to reference the first level source map, CS > JS, this will map the minified JavaScript directly back to the CS and not the compiled JavaScript output!</p>
<p>This sounds like a lot of work but if you have a build process involved it&#8217;s a matter of kicking of a simple cli command to do the work for you and it gives you the kick-arse ability of having the multi-level mappings.</p>
<p>Here are a few snippets on how you can get this going in CoffeeScript or TypeScript. For more info on source map generation check out the <a href="http://www.aaron-powell.com/web/typescript-source-maps" target="_blank">TypeScript</a> and <a href="http://ryanflorence.com/2012/coffeescript-source-maps/" target="_blank">CoffeeScript</a> blog posts.</p>
<h2>CoffeeScript</h2>
<div class="syntax code">
<pre><span class="nv">$&gt;</span> coffee --js -i test.coffee
<span class="nv">$&gt;</span> coffee --source-map -i test.coffee &gt; test.js.map
</pre>
</div>
<p>The first line will compile your CoffeeScript to JS the second line will then generate the source map file.</p>
<div class="syntax code">
<pre><span class="nv">$&gt;</span> uglifyjs2 test.js <span class="se">\</span>
   -o test.min.js <span class="se">\</span>
   --source-map test.min.js.map <span class="se">\</span>
   --in-source-map test.js.map <span class="se">\</span>
   -m -c
</pre>
</div>
<p>Uglify has a few more options the most important being the <code>--in-source-map</code> which will reference that source map when taking into account the file information and what it&#8217;ll output for the second stage source map.</p>
<h2>TypeScript</h2>
<div class="syntax code">
<pre><span class="nv">$&gt;</span> tsc greeter.ts -sourcemap
</pre>
</div>
<p>The TypeScript compiler outputs greeter.js and greeter.js.map</p>
<div class="syntax code">
<pre><span class="nv">$&gt;</span> uglifyjs2 greeter.js <span class="se">\</span>
   -o greeter.min.js <span class="se">\</span>
   --source-map greeter.min.js.map <span class="se">\</span>
   --in-source-map greeter.js.map <span class="se">\</span>
   -m -c
</pre>
</div>
<p>This command is exactly the same as the CoffeeScript example but with the file names changed.</p>
<p>This will work for any other language that compiles to JavaScript that can generate a source map. Check out the <a href="https://github.com/ryanseddon/source-map/wiki/Source-maps:-languages,-tools-and-other-info" target="_blank">source maps wiki</a> for more info.</p>
<p>Source maps only work in Chrome and WebKit nightly but is coming very soon to <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=771597">Firefox Nightly</a>.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/045">http://cssn.in/ja/045</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/multi-level-sourcemaps/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/multi-level-sourcemaps</feedburner:origLink></item>
		<item>
		<title>Weird behaviour with optgroups in iOS6 Safari</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/L0-DRtK8EVs/optgroup-ios6</link>
		<comments>http://www.thecssninja.com/html/optgroup-ios6#comments</comments>
		<pubDate>Wed, 10 Oct 2012 11:01:34 +0000</pubDate>
		<dc:creator>Ryan Seddon</dc:creator>
				<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=1126</guid>
		<description><![CDATA[So recently the company I work for has been getting quite a few complaints on our location dropdown randomly (un)selecting country locations in iOS6 Safari. Thinking at first it may be something to do with our code I quickly created a reduced test case that stripped everything away except for the select in question and [...]]]></description>
				<content:encoded><![CDATA[<p>So recently the company I work for has been getting quite a few complaints on our location dropdown randomly (un)selecting country locations in iOS6 Safari. Thinking at first it may be something to do with our code I quickly created a reduced <a href="http://codepen.io/ryanseddon/pen/CnEJl">test case</a> that stripped everything away except for the select in question and the behaviour persisted.<span id="more-1126"></span></p>
<p>If you open the test case in iOS6 Safari!, select a few locations and scroll those selections out of view then scroll back it will randomly select/unselect other items you never touched. It will also happen if you unselect a few items then dismiss the select and reopen it, some of the old ones you unselected will be re-selected again. Check out the video below to see the bug in action.</p>
<p><iframe width="640" height="480" src="http://www.youtube.com/embed/PPRUVzeo4TM" frameborder="0" allowfullscreen></iframe></p>
<h2>Optgroups seem to be the culprit</h2>
<p>On our search box we have a few multi selects but the difference between them and the locations one is that location has a series of optgroups. Optgroups are what causes this weird behaviour to happen, if I strip them out this bug goes away. Check out the third select in the test case to see the fix.</p>
<div class="syntax code">
<pre><span class="nt">&lt;select</span> <span class="na">multiple=</span><span class="s">"multiple"</span><span class="nt">&gt;</span>
    <span class="nt">&lt;optgroup</span> <span class="na">label=</span><span class="s">"Options"</span><span class="nt">&gt;</span>
        <span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">"1"</span><span class="nt">&gt;</span>Option 1<span class="nt">&lt;/option&gt;</span>
        <span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">"2"</span><span class="nt">&gt;</span>Option 2<span class="nt">&lt;/option&gt;</span>
        <span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">"3"</span><span class="nt">&gt;</span>Option 3<span class="nt">&lt;/option&gt;</span>
        <span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">"4"</span><span class="nt">&gt;</span>Option 4<span class="nt">&lt;/option&gt;</span>
    <span class="nt">&lt;/optgroup&gt;</span>
<span class="nt">&lt;/select&gt;</span>
</pre>
</div>
<p>The above HTML will cause the issue as long as you can scroll some of the items out of view or (un)select some items dismiss then open.</p>
<h2>Solution</h2>
<p>The only solution I could come up with is to avoid optgroups and use a disabled option to create the section titles. This isn&#8217;t the most ideal solution but works in the meantime.</p>
<div class="syntax code">
<pre><span class="nt">&lt;select</span> <span class="na">multiple=</span><span class="s">"multiple"</span><span class="nt">&gt;</span>
    <span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">"1"</span> <span class="na">disabled</span><span class="nt">&gt;</span>Options<span class="nt">&lt;/option&gt;</span>
    <span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">"1"</span><span class="nt">&gt;</span>Option 1<span class="nt">&lt;/option&gt;</span>
    <span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">"2"</span><span class="nt">&gt;</span>Option 2<span class="nt">&lt;/option&gt;</span>
    <span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">"3"</span><span class="nt">&gt;</span>Option 3<span class="nt">&lt;/option&gt;</span>
    <span class="nt">&lt;option</span> <span class="na">value=</span><span class="s">"4"</span><span class="nt">&gt;</span>Option 4<span class="nt">&lt;/option&gt;</span>
<span class="nt">&lt;/select&gt;</span>
</pre>
</div>
<p>If you know of a better one please let me know. I&#8217;ve filed a bug with Apple and have put it in the <a href="http://openradar.appspot.com/radar?id=2138401">Open Radar tracker</a> so people can see it.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/044">http://cssn.in/ja/044</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/html/optgroup-ios6/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/html/optgroup-ios6</feedburner:origLink></item>
		<item>
		<title>Extreme specificity overriding a CSS ID with 256 chained Classes</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/vSg_ItNDIxs/extreme-specificity</link>
		<comments>http://www.thecssninja.com/css/extreme-specificity#comments</comments>
		<pubDate>Fri, 17 Aug 2012 03:27:58 +0000</pubDate>
		<dc:creator>Ryan Seddon</dc:creator>
				<category><![CDATA[css]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=1096</guid>
		<description><![CDATA[The other day Chris Coyier created a test case demonstrating that chaining together 256 classes will give it greater specificity than an id, in theory it shouldn&#8217;t. But in IE, Mozilla and WebKit browsers it does, Opera on the other hand upholds the specificity. Not familiar with CSS specificity then take a look at Estelle [...]]]></description>
				<content:encoded><![CDATA[<p>The other day Chris Coyier created a <a href="http://codepen.io/chriscoyier/pen/lzjqh">test case</a> demonstrating that chaining together 256 classes will give it greater specificity than an id, in theory it shouldn&#8217;t. But in IE, Mozilla and WebKit browsers it does, Opera on the other hand upholds the specificity. Not familiar with CSS specificity then take a look at Estelle Weyl&#8217;s hilariously informative <a href="http://specifishity.com/">specifishity chart</a>.<span id="more-1096"></span></p>
<div class="resources01"><a href="http://codepen.io/chriscoyier/pen/lzjqh" title="Override an id with 256 chained classes" class="demo" target="_blank">256 chained classes demo</a> <a href="http://jsbin.com/obeliz/1/edit" title="Override a class with 256 tags" class="demo source" target="_blank">256 nested tag selector overrides a class</a></div>
<h2>Why does this work?</h2>
<p>This got me curious as to why this is the case and thankfully a Mozillian, <a href="https://twitter.com/heycam">@heycam</a>, pointed me to the source code of Firefox that shows classes are stored in <a href="http://hg.mozilla.org/mozilla-central/file/tip/layout/style/StyleRule.cpp#l479">8bit strings</a> and if you&#8217;re familiar with binary an 8bit string can only hold a maximum value of 255. You guessed it 256 tips this over the edge and overflows it into the ID count giving the classes higher specificity than the ID itself.</p>
<blockquote class="twitter-tweet" data-in-reply-to="235972134269247488"><p><a href="https://twitter.com/ryanseddon"><s>@</s><b>ryanseddon</b></a> From <a href="http://t.co/uta20DXD" title="http://hg.mozilla.org/mozilla-central/file/tip/layout/style/StyleRule.cpp#l479">hg.mozilla.org/mozilla-centra…</a> Gecko overflows the count of classes into the count of IDs, each of which holds 8 bits.</p>
<p>&mdash; Cameron McCormack (@heycam) <a href="https://twitter.com/heycam/status/235973852935643136" data-datetime="2012-08-16T05:38:45+00:00">August 16, 2012</a></p></blockquote>
<p><script src="//platform.twitter.com/widgets.js" charset="utf-8"></script></p>
<h2>What does Opera do differently?</h2>
<p>According to an Opera employee, <a href="https://twitter.com/patrick_h_lauke">@patrick_h_lauke</a>, they store specificity in 16bit strings, so you would need specify 65536 classes names to override an id and show the same behaviour other vendors do. I would be interested in know if using 16bit over 8bit actually impacts the selector engine.</p>
<blockquote class="twitter-tweet" data-in-reply-to="236025903396814848"><p><a href="https://twitter.com/ryanseddon"><s>@</s><b>ryanseddon</b></a> and to close the loop: yes, opera uses 16 bits instead of 8. bring on 65536 classes&#8230;</p>
<p>&mdash; patrick h. lauke (@patrick_h_lauke) <a href="https://twitter.com/patrick_h_lauke/status/236030224528191488" data-datetime="2012-08-16T09:22:45+00:00">August 16, 2012</a></p></blockquote>
<p><script src="//platform.twitter.com/widgets.js" charset="utf-8"></script></p>
<h2>mark: WONTFIX</h2>
<p>While this is considered an implementation bug it won&#8217;t be fixed because it&#8217;s such a wildly ridiculous edge case. Another reason why is it could potentially slow down a browser selector engine speed and nobody wants that for crazy edges cases.</p>
<p>An interesting exercise that lets you dig into internals of how browsers work.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/043">http://cssn.in/ja/043</a></p>
<div class="clear"> </div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/css/extreme-specificity/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/css/extreme-specificity</feedburner:origLink></item>
		<item>
		<title>Curtain reveal effect using CSS</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/G-D3Snwz_9k/reveal-effect</link>
		<comments>http://www.thecssninja.com/css/reveal-effect#comments</comments>
		<pubDate>Wed, 18 Jul 2012 11:11:49 +0000</pubDate>
		<dc:creator>Ryan Seddon</dc:creator>
				<category><![CDATA[css]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=1079</guid>
		<description><![CDATA[I recently redesigned my site and wanted to add a little fun effect at the bottom of the page (go on scroll down I&#8217;ll wait) that looked liked the whole website was sliding up and revealing a secret section under the footer and I wanted to do it without JavaScript. CSS Reveal effect demo Download [...]]]></description>
				<content:encoded><![CDATA[<p>I recently redesigned my site and wanted to add a little fun effect at the bottom of the page (go on scroll down I&#8217;ll wait) that looked liked the whole website was sliding up and revealing a secret section under the footer and I wanted to do it without JavaScript.<span id="more-1079"></span></p>
<div class="resources01"><a href="http://www.thecssninja.com/demo/css_reveal/" title="Simple curtain reveal effect using CSS" target="_blank">CSS Reveal effect demo</a> <a href="http://www.thecssninja.com/demo/css_reveal/css_reveal.zip" title="Download the source of the CSS reveal demo" class="demo source" target="_blank">Download the source files</a></div>
<h2>Pseudo elements and fixed positioning</h2>
<p>The effect is actually deceptively simple and doesn&#8217;t require a whole lot of CSS, my effect doesn&#8217;t require any extra markup thanks to some use of pseudo-elements (<code>:after/:before</code>). This is the CSS taken from the above demo.</p>
<div class="syntax code">
<pre><span class="nt">body</span> <span class="p">{</span>
     <span class="k">padding-bottom</span><span class="o">:</span> <span class="m">600px</span><span class="p">;</span>
<span class="p">}</span>
<span class="nt">body</span><span class="nd">:after</span> <span class="p">{</span>
    <span class="k">content</span><span class="o">:</span> <span class="s2">""</span><span class="p">;</span>
    <span class="k">height</span><span class="o">:</span> <span class="m">1800px</span><span class="p">;</span>
    <span class="k">position</span><span class="o">:</span> <span class="k">fixed</span><span class="p">;</span>
    <span class="k">left</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span>
    <span class="k">right</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span>
    <span class="k">bottom</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span>
    <span class="k">background</span><span class="o">:</span> <span class="m">#000</span> <span class="sx">url(assets/css-ninja.svg)</span> <span class="m">50%</span> <span class="m">0</span> <span class="k">no-repeat</span><span class="p">;</span>
    <span class="k">background</span><span class="o">-</span><span class="k">size</span><span class="o">:</span> <span class="k">auto</span> <span class="m">280px</span><span class="p">;</span>
    <span class="k">z-index</span><span class="o">:</span> <span class="m">1</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<p>That&#8217;s it to get the section to sit where I want using fixed position at the bottom and setting right and left properties to make the element expand the full width. I re-use my SVG logo that appears in the header and adjust the background-size to make it appear bigger so only the top half of the head appears.</p>
<p>To get the reveal effect All I need to do to my content is make sure it has a higher stacking context than my pseudo-element.</p>
<div class="syntax code">
<pre><span class="nc">.curtain</span> <span class="p">{</span>
    <span class="k">height</span><span class="o">:</span> <span class="m">1200px</span><span class="p">;</span>
    <span class="k">position</span><span class="o">:</span> <span class="k">relative</span><span class="p">;</span>
    <span class="k">z-index</span><span class="o">:</span> <span class="m">2</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<p>Because my demo has no content I have set a height so you can see the effect happen this wouldn&#8217;t be needed if you have content. In the pseudo element I also set a height 600px higher than curtain overlay that&#8217;s so I can reveal a total of 600px of content when the user scroll down.</p>
<h2>A real world case?</h2>
<p>The effect on my site was just for fun but perhaps a more clever way of doing it would be to reveal a get in contact or buy my product upon the user scrolling through why they should use your service/product.</p>
<div class="resources01"><a href="http://www.thecssninja.com/demo/css_reveal/product.html" title="More advanced use of the reveal effect using CSS" target="_blank">Advanced CSS Reveal effect demo</a> <a href="http://www.thecssninja.com/demo/css_reveal/css_reveal.zip" title="Download the source of the CSS reveal demo" class="demo source" target="_blank">Download the source files</a></div>
<p>This demo uses the footer as the reveal element so the section element has a bottom margin that is equivalent to the height of the footer and the stacking context of the section and header elements are higher than the footer so it will only be &#8220;revealed&#8221; when the user scrolls to the bottom of the page.</p>
<div class="syntax code">
<pre><span class="nt">section</span> <span class="p">{</span>
    <span class="k">margin-bottom</span><span class="o">:</span> <span class="m">350px</span><span class="p">;</span>
    <span class="k">position</span><span class="o">:</span> <span class="k">relative</span><span class="p">;</span>
    <span class="k">z-index</span><span class="o">:</span> <span class="m">2</span><span class="p">;</span>
<span class="p">}</span>
<span class="nt">footer</span> <span class="p">{</span>
    <span class="k">position</span><span class="o">:</span> <span class="k">fixed</span><span class="p">;</span>
    <span class="k">bottom</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span>
    <span class="k">left</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span>
    <span class="k">right</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span>
    <span class="k">height</span><span class="o">:</span> <span class="m">350px</span><span class="p">;</span>
    <span class="k">z-index</span><span class="o">:</span> <span class="m">1</span><span class="p">;</span>
<span class="p">}</span>
</pre>
</div>
<h2>Browser support</h2>
<p>The browser support for this is very good the first demo uses pseudo-elements, multiple backgrounds, svg and gradients so it has IE10+ support (With graceful degradation this could easily work IE8+) and the second demo has support IE7+ with just the HTML5 shiv included but wouldn&#8217;t require it if you were using divs.</p>
<h2>iOS has some issues with fixed positioning</h2>
<p>Who am I kidding iOS5 has a crazy amount of <a href="http://remysharp.com/2012/05/24/issues-with-position-fixed-scrolling-on-ios/">weird edge case</a> issues with fixed positioning that will hopefully be addressed in iOS6. In the case of this demo the below screen shot of an iOS5 you&#8217;ll see the stacking context gets confused when the user is scrolling it will also flicker quite a lot and be quite jarring.</p>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2012/07/ios_stacking_issue.png" alt="Showing the weird stacking issue iOS5 has when elements have a fixed position and the user is scrolling" class="article-img main-img"></p>
<p>A simple yet cool looking effect, but avoid on iOS mobile devices for now.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/042">http://cssn.in/ja/042</a></p>
<div class="clear"> </div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/css/reveal-effect/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/css/reveal-effect</feedburner:origLink></item>
		<item>
		<title>Bunyip: client-side unit testing made easy</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/-wtRt2UExjE/bunyip</link>
		<comments>http://www.thecssninja.com/javascript/bunyip#comments</comments>
		<pubDate>Tue, 05 Jun 2012 12:37:11 +0000</pubDate>
		<dc:creator>Ryan Seddon</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=1049</guid>
		<description><![CDATA[Let&#8217;s face it doing thorough client-side unit testing fills me with rage, throw mobile browsers into the mix and I want to start flipping tables. There are tools out there to somewhat tackle this issue but they either require painful setups or want you to rewrite your unit tests using their framework. What if I [...]]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s face it doing thorough client-side unit testing fills me with rage, throw mobile browsers into the mix and I want to start flipping tables. There are tools out there to somewhat tackle this issue but they either require painful setups or want you to rewrite your unit tests using their framework. What if I told you there&#8217;s a tool that is easy to get up and running, doesn&#8217;t require you to rewrite your tests and allows you do it all from the command line in desktop and mobile browsers. Watch a video showing it working.<br />
<span id="more-1049"></span></p>
<div class="constrainer"><iframe width="660" height="400" frameborder="0" allowfullscreen="" src="http://play.codestre.am/embed/b9e2790cc31446a5a183833bc"></iframe></div>
<p><small style="margin-top: -20px; display: block;">The codestream video can take a while to load sometimes, be patient.</small></p>
<p>If you want to get stuck in go check out <a href="http://ryanseddon.github.com/bunyip/">Bunyip</a> on github and see the readme for instructions on getting up and running. It&#8217;s really easy I promise.</p>
<h2 class="subtitle02">Bunyip</h2>
<p>At <a href="http://code12melb.webdirections.org/">Web Directions Code</a> conference in Melbourne this past May I demonstrated this tool on stage in my talk <a href="http://cssn.in/ja/lazydev">&#8220;Debugging secrets of a lazy developer&#8221;</a>. I started thinking about a tool like this when I joined the Modernizr team and how annoying, but important, it was to utilise the powerful test suite we have. Opening all the browsers and visually checking the test suite results is time consuming, thankfully a series of open source tools and a powerful API made this idea become a reality.</p>
<h2 class="subtitle02">On the backs of great projects</h2>
<p>The first tool that got Bunyip into a usable state is <a href="http://yeti.cx/">Yeti</a> an incredibly powerful tool from Yahoo that allows to push out a html file to any connected browsers from the command line. It launches a little server on your machine that browsers on the same network can connect to. The downside initially was that it required you to use the YUI testing framework, but this tool was way too awesome to ignore it on that factor. So I dug in and discovered how it was sending back the results to the cli and wrote some <a href="https://github.com/ryanseddon/yeti-adaptors">adaptors</a> for QUnit and jasmine to work with Yeti. If you use another framework feel free to do a pull request!</p>
<p>In the Yeti docs they suggest enabling mobile support by using a service called <a href="http://progrium.com/localtunnel/">localtunnel</a> which allows you to create an SSH tunnel into your local server so you can access it from outside your local network. I found localtunnel to be slow and sporadic with Yeti especially when the test suite was quite large. I settled on another service called <a href="https://pagekite.net/support/quickstart/">pagekite</a> which gives you 2.5GB of data for free and the ability to register a subdomain after that it&#8217;s pretty cheap. You can also check out <a href="https://showoff.io/">showoff.io</a>.</p>
<p>Next is a great service called <a href="http://www.browserstack.com/">BrowserStack</a>. BrowserStack lets you test web sites in a whole bunch of browsers but more importantly they offer an <a href="http://www.browserstack.com/automated-browser-testing-api">API</a> so you can spin up a browser and pass it a url. Exactly what I needed to make bunyip amazing. When I started building this tool the API only had support for desktop browsers, just before I did my talk they rolled out v2 which included mac and mobile browsers! Since I&#8217;m building this as a node module I&#8217;m using the very awesome node-browserstack module by Scott Gonzales to make using the API very easy. </p>
<h2 class="subtitle02">How it works</h2>
<p>As mentioned previously this is reliant on a lot of great OSS. To get into the deeper level on how this works you can comb through the code on github or read on for a high level look.</p>
<h2 class="subtitle02">Local server</h2>
<p>When you run this command <code>bunyip -f test/index.html -b "firefox:win/12.0|ie:win/8.0|safari:mac/5.1|iPhone 4S:ios/5.0"</code> in bunyip the first thing it does is hand off the <code>-f test/index.html</code> call to Yeti to spin up a local server that will have an intermediate page that will accessible via localhost:9000. Yeti uses socket.io to notify the cli when a slave connects and vice versa to control when a test should begin.</p>
<h2 class="subtitle02">SSH tunnel and BrowserStack</h2>
<p>Once Yeti is up and running bunyip will then grab the tunnel service config values from the file you would of setup. In my case it will run the pagekite.py script passing in my port 9000 and my subdomain I want it accessible via bunyip.pagekite.me. </p>
<p>We now have our tunnel up and running. Next we can parse the <code>-b "firefox:win/12.0|ie:win/8.0|safari:mac/5.1|iPhone 4S:ios/5.0"</code> flag into a usable JSON string we can send off to the BrowserStack API. It looks like this:</p>
<div class="code syntax">
<pre><span class="p">[</span>
    <span class="p">{</span>
        <span class="s2">"browser"</span><span class="o">:</span> <span class="s2">"ie"</span><span class="p">,</span>
        <span class="s2">"os"</span><span class="o">:</span> <span class="s2">"win"</span><span class="p">,</span>
        <span class="s2">"version"</span><span class="o">:</span> <span class="s2">"8.0"</span><span class="p">,</span>
        <span class="s2">"url"</span><span class="o">:</span> <span class="s2">"http://bunyip.pagekite.me/"</span><span class="p">,</span>
        <span class="s2">"timeout"</span><span class="o">:</span> <span class="mi">480</span>
    <span class="p">}</span>
<span class="p">]</span>
</pre>
</div>
<p>We authenticate with the API by passing in our credentials from the <code>config.js</code> file and instructing it to load up the workers we specified, in our case Firefox, IE, Safari and an iPhone 4S. When the Yeti waiting page loads it will emit an event that will trigger the cli to display a message letting us know that the browser/device has connected and is waiting for our test suite.</p>
<h2 class="subtitle02">Pushing out the test suite</h2>
<p>When all devices have connected we push enter to kick off running it through our connected browsers, Yeti will serve the page specified in our <code>-f</code> flag by emitting an event to all connected browser, using <a href="http://socket.io/">socket.io</a>, telling them to redirect to our test suite. On page load Yeti injects a script called inject.js this is for any test suites written in YUI test if you&#8217;re not using YUI test then you would have included one of my yeti adaptors for either <a href="https://github.com/ryanseddon/yeti-adaptors/tree/master/QUnit">QUnit</a> or <a href="https://github.com/ryanseddon/yeti-adaptors/tree/master/jasmine">jasmine</a> this captures the test results as JSON in the format Yeti expects and emits an event back to the global $yetify object which sends off the results to the cli to handle what to display back to the user.</p>
<h2 class="subtitle02">You can help make this awesome</h2>
<p>Find any <a href="https://github.com/ryanseddon/bunyip/issues">issues</a> or want a new feature or think something can be improved. Then please by all means contribute in any way you can. I want this tool to be a community effort.</p>
<h2 class="subtitle02">Other tools </h2>
<p>Of course bunyip isn&#8217;t the first tool out there to try and do something like this so I&#8217;m going to list of some other tools that might be of interest.</p>
<ul>
<li><a href="http://code.google.com/p/js-test-driver/">JSTestDriver</a></li>
<li><a href="http://saucelabs.com/">Sauce Labs</a></li>
<li><a href="http://busterjs.org/">Buster.js</a></li>
<li><a href="http://swarm.jquery.org/">TestSwarm</a></li>
<li><a href="http://www.testling.com/">Testling</a></li>
</ul>
<h2 class="subtitle02">What&#8217;s next?</h2>
<p>Getting this battle tested inside some more real projects, especially people with jasmine test suites (I haven&#8217;t done a lot of testing with the adaptor I wrote for Yeti when using jasmine test suites). </p>
<p>Reid Burke (Yeti dude) is keen to get the Yeti adaptors into Yeti core so it&#8217;ll work with other test frameworks straight out of the box.</p>
<p>I need help making the config.js process less painful see <a href="https://github.com/ryanseddon/bunyip/issues/1">issue #1</a>.</p>
<p>See <a href="https://github.com/ryanseddon/bunyip/wiki/Roadmap---wanted-features">roadmap / wanted features</a> wiki for what I want to add.</p>
<p>Client-side unit testing shouldn&#8217;t be hard and it shouldn&#8217;t get in your way. Hopefully bunyip can alleviate the pain and get people writing unit tests for the first time or more often.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/041">http://cssn.in/ja/041</a></p>
<div class="clear"> </div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/bunyip/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/bunyip</feedburner:origLink></item>
		<item>
		<title>Testing on Mobile devices</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/D01MftNjeoo/testing-devices</link>
		<comments>http://www.thecssninja.com/mobile/testing-devices#comments</comments>
		<pubDate>Wed, 28 Mar 2012 08:58:41 +0000</pubDate>
		<dc:creator>Ryan Seddon</dc:creator>
				<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=1026</guid>
		<description><![CDATA[A while back Brad Frost posted a very useful breakdown on testing mobile devices and how to do it without breaking the bank. This post is kind of an extension to it but pointing to other useful resources that I have come across and used. Frost went down the route of physical devices, what you [...]]]></description>
				<content:encoded><![CDATA[<p>A while back Brad Frost posted a very useful breakdown on <a href="http://bradfrostweb.com/blog/mobile/test-on-real-mobile-devices-without-breaking-the-bank/">testing mobile devices</a> and how to do it without breaking the bank. This post is kind of an extension to it but pointing to other useful resources that I have come across and used.</p>
<p>Frost went down the route of physical devices, what you should test and their breakdown in prices. This post will be a list of services I have come across for testing on a vast array mobile devices without having to purchase said devices.<span id="more-1026"></span></p>
<h2 class="subtitle02">DeviceAnywhere</h2>
<p>DeviceAnywhere <a href="http://www.keynotedeviceanywhere.com/mobile-application-testing.html">Test Center</a> is a device lab chock full of common and not so common devices that you can access remotely though their application. It gives you remote access to real devices, not emulators, and the ability to do almost anything to them. In my case I would only access the browser apps on each phone but you have full access to the phone and its capabilities.</p>
<h2 class="subtitle02">Pros:</h2>
<ul>
<li>Record video and image capture</li>
<li>Invite other users to a watch or participate in testing</li>
<li>Huge amount of <a href="http://www.keynotedeviceanywhere.com/test-center-device-list.html ">devices</a> including tablets.</li>
<li>Good support for emulating touch and gestures.</li>
<li>Relatively <a href="http://www.keynotedeviceanywhere.com/test-center-pricing.html">cheap</a>.</li>
</ul>
<h2 class="subtitle02">Cons:</h2>
<ul>
<li>Laggy and not super responsive (Latency from Australia most likely faster in USA and UK).</li>
<li>Automation and scripting is part of their enterprise product which is very pricey.</li>
</ul>
<p><strong>Price:</strong> ~$16USD/hr for general access to the device lab. Packages and bulk hours equals cheaper prices.</p>
<hr />
<h2 class="subtitle02">Perfecto Mobile</h2>
<p>PerfectoMobile <a href="http://www.perfectomobile.com/portal/cms/services/overview.html">MobileCloud</a> is very similar to DeviceAnywhere both offer a huge array of real devices you can remotely access. PerfectoMobile uses a flash based web app for accessing and testing on devices.</p>
<h2 class="subtitle02">Pros:</h2>
<ul>
<li>Record video and image capture</li>
<li>Invite other users to a watch or participate in testing</li>
<li>Huge amount of <a href="http://www.perfectomobile.com/portal/cms/services/handsets.html">devices</a> including tablets.</li>
<li>Have dedicated handset options: <a href="http://www.perfectomobile.com/portal/cms/services/blackberry.html">Blackberry</a> and <a href="http://www.perfectomobile.com/portal/cms/services/nokia.html">Nokia</a></li>
</ul>
<h2 class="subtitle02">Cons:</h2>
<ul>
<li>Laggy and not super responsive (Latency from Australia most likely faster in USA and UK).</li>
<li>Automation and scripting is part of their enterprise product which is very pricey.</li>
<li>Touch and gestures aren&#8217;t that easy to accomplish.</li>
<li><a href="http://www.perfectomobile.com/portal/cms/pricing/">Pricey</a>, even for entry level access.</li>
</ul>
<p><strong>Price:</strong> ~$170USD/10hrs per month. Blackberry $4USD/hr, Nokia $14USD/hr.</p>
<hr />
<h2 class="subtitle02">BrowserStack</h2>
<p><a href="http://www.browserstack.com/mobile-browser-emulator">BrowserStack</a> recently launched remote access to an array of Mobile and tablet emulators with real device configurations. All in browser access.</p>
<h2 class="subtitle02">Pros:</h2>
<ul>
<li>Good cheap <a href="http://www.browserstack.com/pricing">pricing</a></li>
<li>Can test localhost remotely<sup>1</sup></li>
<li>Automation! BrowserStack has an <a href="http://www.browserstack.com/automated-browser-testing-api">API</a> for spinning up an emulator and scripting interaction. Huge.</li>
</ul>
<h2 class="subtitle02">Cons:</h2>
<ul>
<li>Not real devices, but when it&#8217;s for browser testing that&#8217;s less of an issue.</li>
<li>Smaller, but growing, <a href="http://www.browserstack.com/mobile-browser-emulator#list">list of mobile browsers</a>. But really they cover 99% of the most used ones already.</li>
</ul>
<p><strong>Price:</strong> $19USD/Month for an individual. Gives you access to Mobile and desktop browsers.</p>
<p><sup>1</sup> Note: BrowserStack provides their own implementation of a localtunnel like service. You can of course use localtunnel or similar on your own machine for testing your localhost with other services. </p>
<hr />
<h2 class="subtitle02">Nokia Remote Device Access</h2>
<p><a href="http://apu.ndhub.net/">Nokia RDA</a> is a device lab of, you guessed it, Nokia devices where you can remotely access an array of Nokia phones.</p>
<h2 class="subtitle02">Pros:</h2>
<ul>
<li>It&#8217;s free! You just need to sign up for Nokia dev account.</li>
<li>Huge list of <a href="http://apu.ndhub.net/devices">Nokia phones</a> available.</li>
<li>Record and screen capture.</li>
<li>Pretty responsive.</li>
</ul>
<h2 class="subtitle02">Cons:</h2>
<ul>
<li>Pretty lame con but it&#8217;s free and therefore could disappear.</li>
</ul>
<p><strong>Price:</strong> FREE!</p>
<hr />
<h2 class="subtitle02">AppThwack</h2>
<p><a href="http://apu.ndhub.net/">AppThwack</a> is a device lab of Android devices, iOS soon, that will let you run your app or mobile site through a series of tests and provide feedback and screenshots.</p>
<h2 class="subtitle02">Pros:</h2>
<ul>
<li>Huge list of <a href="https://appthwack.com/devicelab">Android phones</a> available.</li>
<li>Screen capture.</li>
</ul>
<h2 class="subtitle02">Cons:</h2>
<ul>
<li>Free signup only allows testing of apk files, can&#8217;t run url tests.</li>
<li>Android only at the moment.</li>
</ul>
<p><strong>Price:</strong> starts at $29USD/month</p>
<hr />
<h2 class="subtitle02">MITE</h2>
<p><a href="http://mite.keynote.com/">MITE</a> is another product from Keynote that focuses on mobile website testing.</p>
<h2 class="subtitle02">Pros:</h2>
<ul>
<li>Test automation, can record a session and re-run</li>
<li>Waterfall charts</li>
<li>A web inspector</li>
</ul>
<h2 class="subtitle02">Cons:</h2>
<ul>
<li>Windows only.</li>
<li>It&#8217;s a native app and not a web app. Is this 1999?</li>
<li>Don&#8217;t think it really tests on emulators, but has a common WebKit engine and skins the phones.<sup>1</sup></li>
<li>WebKit monoculture.</li>
</ul>
<p><strong>Price:</strong> FREE!</p>
<p><sup>1</sup> Note: From what I could gather on their website.</p>
<p>So that&#8217;s it. If you know of any other services that I may have missed let me know. I think a combination of real devices and remote access is the killer setup for any mobile development shop or individual.</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/040">http://cssn.in/ja/040</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/mobile/testing-devices/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/mobile/testing-devices</feedburner:origLink></item>
		<item>
		<title>Introduction to JavaScript Source Maps</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/e4P1Ztag-6s/source-mapping</link>
		<comments>http://www.thecssninja.com/javascript/source-mapping#comments</comments>
		<pubDate>Wed, 21 Mar 2012 23:55:26 +0000</pubDate>
		<dc:creator>Ryan Seddon</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=982</guid>
		<description><![CDATA[Have you ever found yourself wishing you could keep your client-side code readable and more importantly debuggable even after you&#8217;ve combined and minified it, without impacting performance? Well now you can through the magic of source maps. Basically it&#8217;s a way to map a combined/minified file back to an unbuilt state. When you build for [...]]]></description>
				<content:encoded><![CDATA[<p>Have you ever found yourself wishing you could keep your client-side code readable and more importantly debuggable even after you&#8217;ve combined and minified it, without impacting performance? Well now you can through the magic of <a href="https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit?hl=en_US&amp;pli=1&amp;pli=1">source maps</a>.</p>
<p><span id="more-982"></span></p>
<p>Basically it&#8217;s a way to map a combined/minified file back to an unbuilt state. When you build for production, along with minifying and combining your JavaScript files, you generate a source map which holds information about your original files. When you query a certain line and column number in your generated JavaScript you can do a lookup in the source map which returns the original location. Developer tools (currently WebKit nightly builds, or Google Chrome Canary) can parse the source map automatically and make it appear as though you&#8217;re running unminified and uncombined files. As of writing Firefox has a blocked status for Source Map support. See <a href="https://wiki.mozilla.org/DevTools/Features/SourceMap">MozillaWiki Source Map</a> entry for more details.</p>
<div class="resources01"><a href="http://www.thecssninja.com/demo/source_mapping/" target="_blank" title="Source mapping demo" class="demo">View a live demo</a> <a href="http://www.thecssninja.com/demo/source_mapping/source_mapping.zip" title="Download the source of the source mapping demo" class="demo source">Download the source files</a></div>
<p>The above demo allows you to right click anywhere in the textarea containing the generated source. Select &#8220;Get original location&#8221; will query the source map by passing in the generated line and column number, and return the position in the original code. Make sure your console is open so you can see the output.</p>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2012/03/source-map-demo.png" alt="Source map demo screenshot" title="Example of the Mozilla JavaScript source map library in action" width="609" height="155" class="article-img main-img" /></p>
<h2 class="subtitle02">Real world</h2>
<p>Before you view the following real world implementation of Source Maps make sure you&#8217;ve enabled the source maps feature in either Chrome Canary or WebKit nightly by clicking the settings cog in the dev tools panel and checking the &#8220;Enable source maps&#8221; option. See screenshot below.</p>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2012/03/enable-source-maps.png" alt="" title="How to enable source maps in WebKit dev tools" width="609" height="232" class="article-img main-img" /></p>
<p>So&#8230; That Source Map query demo is cool and all but what about a real world use case? Take a look at the special build of font dragr at <a href="http://dev.fontdragr.com/">dev.fontdragr.com</a> in Chrome Canary or WebKit nightly, with source mapping enabled, and you&#8217;ll notice that the JavaScript isn&#8217;t compiled and you can see all the individual JavaScript files it references. This is using source mapping, but behind the scenes actually running the compiled code. Any errors, logs and breakpoints will map to the dev code for awesome debugging! So in effect it gives you the illusion that you&#8217;re running a dev site in production.</p>
<h2 class="subtitle02">Why should I care about source maps?</h2>
<p>Right now source mapping is only working between uncompressed/combined JavaScript to compressed/uncombined JavaScript, but the future is looking bright with talks of compiled-to-JavaScript languages such as CoffeeScript and even the possibility of adding support for CSS preprocessors like SASS or LESS.</p>
<p>In the future we could easily use almost any language as though it were supported natively in the browser with source maps:</p>
<ul>
<li>CoffeeScript</li>
<li>ECMAScript 6 and beyond</li>
<li>SASS/LESS and others</li>
<li>Pretty much any language that compiles to JavaScript</li>
</ul>
<p>Take a look at this screencast of CoffeeScript being debugged in an experimental build of the Firefox console:</p>
<p><iframe width="609" height="339" src="http://www.youtube.com/embed/2aQw1dSIYko?start=625" frameborder="0" allowfullscreen></iframe></p>
<p>The Google Web Toolkit (GWT) has recently added <a href="http://code.google.com/p/google-web-toolkit/wiki/SourceMaps">support for Source Maps</a> and Ray Cromwell of the GWT team did an awesome screencast showing source map support in action.</p>
<p><iframe width="609" height="339" src="http://www.youtube.com/embed/-xJl22Kvgjg" frameborder="0" allowfullscreen></iframe></p>
<p>Another example I&#8217;ve put together uses Google&#8217;s <a href="http://code.google.com/p/traceur-compiler/">Traceur</a> library which allows you to write ES6 (ECMAScript 6 or Next) and compile it to ES3 compatible code. The Traceur compiler also generates a source map. Take a look at this <a href="http://www.thecssninja.com/demo/source_mapping/ES6/">demo</a> of ES6 traits and classes being used like they&#8217;re supported natively in the browser, thanks to the source map. The textarea in the demo also allows you to write ES6 which will be compiled on the fly and generate a source map plus the equivalent ES3 code.</p>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2012/03/source-map-es6.png" alt="" title="Traceur ES6 debugging using source maps" width="609" height="263" class="article-img main-img" /></p>
<h2 class="subtitle02">How does the source map work?</h2>
<p>The only JavaScript compiler/minifier that has support, at the moment, for source map generation is the Closure compiler. (I&#8217;ll explain how to use it later.) Once you&#8217;ve combined and minified your JavaScript, alongside it will exist a sourcemap file. Currently, the Closure compiler doesn&#8217;t add the special comment at the end that is required to signify to WebKit and Chrome Canary dev tools that a source map is available:</p>
<div class="code syntax">
<pre>//@ sourceMappingURL=/path/to/file.js.map</pre>
</div>
<p>This enables developer tools to map calls back to their location in original source files. If you don&#8217;t like the idea of the weird comment you can alternatively set a special header on your compiled JavaScript file:</p>
<div class="code syntax">
<pre>X-SourceMap: /path/to/file.js.map</pre>
</div>
<p>Like the comment this will tell your source map consumer where to look for the source map associated with a JavaScript file. This header also gets around the issue of referencing source maps in languages that don&#8217;t support single-line comments.</p>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2012/03/sourcemap-on-off.png" alt="WebKit Devtools example of source maps on and source maps off" title="Showing dev tools when source map in off and when it&#039;s on." width="609" height="160" class="article-img main-img" /></p>
<p>The source map file will only be downloaded if you have source maps enabled and your dev tools open. You&#8217;ll also need to upload your original files so the dev tools can reference and display them when necessary.</p>
<h2 class="subtitle02">How do I generate a source map?</h2>
<p>Like I mentioned above you&#8217;ll need to use the <a href="https://developers.google.com/closure/compiler/">Closure compiler</a> to minify, concat and generate a source map for your JavaScript files. The command is as follows:</p>
<div class="code syntax">
<pre>java -jar compiler.jar <span class="se">\ </span>
     --js script.js <span class="se">\</span>
     --create_source_map ./script-min.js.map <span class="se">\</span>
     --source_map_format<span class="o">=</span>V3 <span class="se">\</span>
     --js_output_file script-min.js
</pre>
</div>
<p>The two important command flags are <code>--create_source_map</code> and <code>--source_map_format</code>. This is required as the default version is V2 and we only want to work with V3.</p>
<h2 class="subtitle02">The anatomy of a source map</h2>
<p>In order to better understand a source map we&#8217;ll take a small example of a source map file that would be generated by the Closure compiler and dive into more detail on how the &#8220;mappings&#8221; section works. The following example is a slight variation from the <a href="https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit?hl=en_US&amp;pli=1&amp;pli=1">V3 spec</a> example.</p>
<div class="code syntax">
<pre><span class="p">{</span>
    <span class="nx">version</span> <span class="o">:</span> <span class="mi">3</span><span class="p">,</span>
    <span class="nx">file</span><span class="o">:</span> <span class="s2">"out.js"</span><span class="p">,</span>
    <span class="nx">sourceRoot</span> <span class="o">:</span> <span class="s2">""</span><span class="p">,</span>
    <span class="nx">sources</span><span class="o">:</span> <span class="p">[</span><span class="s2">"foo.js"</span><span class="p">,</span> <span class="s2">"bar.js"</span><span class="p">],</span>
    <span class="nx">names</span><span class="o">:</span> <span class="p">[</span><span class="s2">"src"</span><span class="p">,</span> <span class="s2">"maps"</span><span class="p">,</span> <span class="s2">"are"</span><span class="p">,</span> <span class="s2">"fun"</span><span class="p">],</span>
    <span class="nx">mappings</span><span class="o">:</span> <span class="s2">"AAgBC,SAAQ,CAAEA"</span>
<span class="p">}</span>
</pre>
</div>
<p>Above you can see that a source map is an object literal containing lots of juicy info:</p>
<ul>
<li>Version number that the source map is based off</li>
<li>The file name of the generated code (Your minifed/combined production file)</li>
<li>sourceRoot allows you to prepend the sources with a folder structure &ndash; this is also a space saving technique</li>
<li>sources contains all the file names that were combined</li>
<li>names contains all variable/method names that appear throughout your code.</li>
<li>Lastly the mappings property is where the magic happens using Base64 VLQ values. The real space saving is done here.</li>
</ul>
<h2 class="subtitle02">Base64 VLQ and keeping the source map small</h2>
<p>Originally the source map spec had a very verbose output of all the mappings and resulted in the sourcemap being about 10 times the size of the generated code. Version two reduced that by around 50% and version three reduced it again by another 50%, so for a 133kB file you end up with a ~300kB source map. So how did they reduce the size while still maintaining the complex mappings?</p>
<p><a href="http://en.wikipedia.org/wiki/Variable-length_quantity">VLQ</a> (Variable Length Quantity) is used along with encoding the value into a Base64 value. The mappings property is a super big string. Within this string are semicolons (;) that represent a line number within the generated file. Within each line there are commas (,) that represent each segment within that line. Each of these segments is either 1, 4 or 5 in variable length fields. Some may appear longer but these contain continuation bits. Each segment builds upon the previous, which helps reduce the file size as each bit is relative to its previous segments.</p>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2012/03/source-map-segment.png" alt="" title="Break down of a segment within the source map JSON file" width="609" height="300" class="article-img main-img" /></p>
<p>Like I mentioned above each segment can be 1, 4 or 5 in variable length. This diagram is considered a variable length of four with one continuation bit (g). We&#8217;ll break down this segment and show you how the source map works out the original location. The values shown above are purely the Base64 decoded values, there is some more processing to get their true values. Each segment usually works out five things:</p>
<ul>
<li>Generated column</li>
<li>Original file this appeared in</li>
<li>Original line number</li>
<li>Original column </li>
<li>And if available original name. </li>
</ul>
<p>Not every segment has a name, method name or argument, so segments throughout will switch between four and five variable length. The g value in the segment diagram above is what&#8217;s called a continuation bit this allows for further optimisation in the Base64 VLQ decoding stage. A continuation bit allows you to build on a segment value so you can store big numbers without having to store a big number, a very clever space saving technique that has its roots in the midi format.</p>
<p>The above diagram <code>AAgBC</code> once processed further would return 0, 0, 32, 16, 1 &ndash; the 32 being the continuation bit that helps build the following value of 16. B purely decoded in Base64 is 1. So the important values that are used are 0, 0, 16, 1. This then lets us know that line 1 (lines are kept count by the semi colons) column 0 of the generated file maps to file 0 (array of files 0 is foo.js), line 16 at column 1.</p>
<p>To show how the segments get decoded I will be referencing Mozilla&#8217;s <a href="https://github.com/mozilla/source-map/">Source Map JavaScript library</a>. You can also look at the WebKit dev tools <a href="http://code.google.com/codesearch#OAMlx_jo-ck/src/third_party/WebKit/Source/WebCore/inspector/front-end/CompilerSourceMapping.js">source mapping code</a>, also written in JavaScript.</p>
<p>In order to properly understand how we get the value 16 from B we need to have a basic understanding of bitwise operators and how the spec works for source mapping. The preceding digit, g, gets flagged as a continuation bit by comparing the digit (32) and the <a href="https://github.com/mozilla/source-map/blob/master/lib/source-map/base64-vlq.js#L32">VLQ_CONTINUATION_BIT</a> (binary 100000 or 32) by using the bitwise AND (&amp;) operator.</p>
<div class="code syntax">
<pre>32 &amp; 32 = 32
// or
100000
|
|
V
100000</pre>
</div>
<p>This returns a 1 in each bit position where both have it appear. So a Base64 decoded value of <code>33 &#038; 32</code> would return 32 as they only share the 32 bit location as you can see in the above diagram. This then increases the the bit <a href="https://github.com/mozilla/source-map/blob/master/lib/source-map/base64-vlq.js#L52">shift value</a> by 5 for each preceding continuation bit. In the above case its only shifted by 5 once, so left shifting 1 (B) by 5.</p>
<div class="code syntax">
<pre>1 &lt;&lt; 5 // 32

// Shift the bit by 5 spots
______
|    |
V    V
100001 = 100000 = 32</pre>
</div>
<p>That value is then converted from a VLQ signed value by right shifting the number (32) one spot.</p>
<div class="code syntax">
<pre>32 >> 1 // 16
//or
100000
|
 | 
 V
010000 = 16</pre>
</div>
<p>So there we have it: that is how you turn 1 into 16. This may seem an over complicated process, but once the numbers start getting bigger it makes more sense.</p>
<h2 class="subtitle02">Potential XSSI issues</h2>
<p>The spec mentions cross site script inclusion issues that could arise from the consumption of a source map. To mitigate this it&#8217;s recommended that you prepend the first line of your source map with &#8220;<code>)]}</code>&#8221; to deliberately invalidate JavaScript so a syntax error will be thrown. The WebKit dev tools can handle this already.</p>
<div class="code syntax">
<pre><span class="k">if</span> <span class="p">(</span><span class="nx">response</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="o">===</span> <span class="s2">")]}"</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">response</span> <span class="o">=</span> <span class="nx">response</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="nx">response</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">'\n'</span><span class="p">));</span>
<span class="p">}</span>
</pre>
</div>
<p>As shown above, the first three characters are sliced to check if they match the syntax error in the spec and if so removes all characters leading up to the first new line entity (\n).</p>
<h2 class="subtitle02">@sourceURL and displayName in action: Eval and anonymous functions</h2>
<p>While not part of the source map spec the following two conventions allow you to make development much easier when working with evals and anonymous functions.</p>
<p>The first helper looks very similar to the <code>//@ sourceMappingURL</code> property and is actually mentioned in the source map V3 spec. By including the following special comment in your code, which will be evaled, you can name evals so they appear as more logical names in your dev tools. Check out a <a href="http://www.thecssninja.com/demo/source_mapping/compile.html">simple demo</a> using the CoffeeScript compiler.</p>
<div class="code syntax">
<pre>//@ sourceURL=sqrt.coffee</pre>
</div>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2012/03/source-url.png" alt="" title="What sourceURL special comment looks like in dev tools" width="609" height="195" class="article-img main-img" /></p>
<p>The other helper allows you to name anonymous functions by using the <code>displayName</code> property available on the current context of the anonymous function. Profile the <a href="http://www.thecssninja.com/demo/source_mapping/displayName.html">following demo</a> to see the <code>displayName</code> property in action.</p>
<div class="code syntax">
<pre><span class="nx">btns</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">addEventListener</span><span class="p">(</span><span class="s2">"click"</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">fn</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">"You clicked button number: 1"</span><span class="p">);</span>
    <span class="p">};</span>

    <span class="nx">fn</span><span class="p">.</span><span class="nx">displayName</span> <span class="o">=</span> <span class="s2">"Anonymous function of button 1"</span><span class="p">;</span>

    <span class="k">return</span> <span class="nx">fn</span><span class="p">();</span>
<span class="p">},</span> <span class="kc">false</span><span class="p">);</span>
</pre>
</div>
<p><img src="http://www.thecssninja.com/wp-content/uploads/2012/03/display-name.png" alt="" title="Example showing the displayName property in action" width="611" height="149" class="article-img main-img" /></p>
<p>When profiling your code within the dev tools the <code>displayName</code> property will be shown rather than something like <code>(anonymous)</code>. However displayName is pretty much dead in the water and won&#8217;t be making it into Chrome. But all hope isn&#8217;t lost and a much better proposal has been suggested called <a href="http://code.google.com/p/chromium/issues/detail?id=116220">debugName</a>.</p>
<p>As of writing the eval naming is only available in Firefox and WebKit browsers. The <code>displayName</code> property is only in WebKit nightlies.</p>
<h2 class="subtitle01">Let&#8217;s rally together</h2>
<p>Currently there is very lengthy discussion on <a href="https://github.com/jashkenas/coffee-script/issues/558">source map support</a> being added to CoffeeScript. Go check out the issue and add your support for getting source map generation added to the CoffeeScript compiler. This will be a huge win for CoffeeScript and its devoted followers.</p>
<p>UglifyJS also has a <a href="https://github.com/mishoo/UglifyJS/issues/315">source map issue</a> you should take a look at too.</p>
<p>The more tools available to us that can generate a source maps the better off we&#8217;ll be, so go forth and ask or add source map support to your favourite open source project.</p>
<h2 class="subtitle02">It&#8217;s not perfect</h2>
<p>One thing Source Maps doesn&#8217;t cater for right now is watch expressions. The problem is that trying to inspect an argument or variable name within the current execution context won&#8217;t return anything as it doesn&#8217;t really exist. This would require some sort of reverse mapping to lookup the real name of the argument/variable you wish to inspect compared to the actual argument/variable name in your compiled JavaScript.</p>
<p>This of course is a solvable problem and with more attention on source maps we can start seeing some amazing features and better stability.</p>
<h2 class="subtitle02">Tools and resource</h2>
<p>Here&#8217;s some further resources and tools you should check out:</p>
<ul>
<li>Nick Fitzgerald has a fork of <a href="https://github.com/fitzgen/UglifyJS/tree/source-maps">UglifyJS</a> with source map support</li>
<li>Paul Irish has a handy little <a href="http://dl.dropbox.com/u/39519/sourcemapapp/index.html">demo</a> showing off source maps</li>
<li>Check out the WebKit changeset of when this <a href="http://trac.webkit.org/changeset/103541">dropped</a></li>
<li>The changeset also included a <a href="http://trac.webkit.org/export/105549/trunk/LayoutTests/http/tests/inspector/compiler-source-mapping-debug.html">layout test</a> which got this whole article started</li>
<li>Mozilla has a <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=670002">bug</a> you should follow on the status of source maps in the built-in console</li>
<li>Conrad Irwin has written a super useful <a href="https://github.com/ConradIrwin/ruby-source_map">source map gem</a> for all you Ruby users</li>
<li>Some further reading on <a href="http://blog.getfirebug.com/2009/08/11/give-your-eval-a-name-with-sourceurl/">eval naming</a> and the <a href="http://www.alertdebugging.com/2009/04/29/building-a-better-javascript-profiler-with-webkit/">displayName property</a></li>
<li>You can check out the <a href="http://code.google.com/p/closure-compiler/source/browse/trunk/src/com/google/debugging/sourcemap/SourceMapGeneratorV3.java">Closure Compilers source</a> for creating source maps</li>
<li>There are some screenshots and talk of support for <a href="https://plus.google.com/110412141990454266397/posts/iqXo5AyHkyd">GWT source maps</a></li>
<li>Mozilla has a wiki entry on <a href="https://wiki.mozilla.org/DevTools/Features/CSSSourceMap">CSS Source Maps</a></li>
</ul>
<p>Source maps are a very powerful utility in a developer&#8217;s tool set. It&#8217;s super useful to be able to keep your web app lean but easily debuggable. It&#8217;s also a very powerful learning tool for newer developers to see how experienced devs structure and write their apps without having to wade through unreadable minified code. What are you waiting for? Start generating source maps for all projects now!</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/039">http://cssn.in/ja/039</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/source-mapping/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/source-mapping</feedburner:origLink></item>
		<item>
		<title>A short Modernizr course</title>
		<link>http://feedproxy.google.com/~r/TheCSSNinja/~3/35kGp_2iahY/modernizr-course</link>
		<comments>http://www.thecssninja.com/javascript/modernizr-course#comments</comments>
		<pubDate>Wed, 11 Jan 2012 22:57:34 +0000</pubDate>
		<dc:creator>Ryan Seddon</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.thecssninja.com/?p=955</guid>
		<description><![CDATA[Last year, I did a quick 5 min presentation on some of the features available in Modernizr for Web Directions What Do You Know event. From there, Sitepoint &#038; Learnable&#8217;s Kevin Yank asked me to put together a short course for learnable.com based on the presentation. So after working on the occasional weekend I put [...]]]></description>
				<content:encoded><![CDATA[<p>Last year, I did a quick 5 min presentation on some of the features available in Modernizr for <a href="http://whatdoyouknow.webdirections.org/videos/all-up-in-your-grill-with-modernizr"><em>Web Directions What Do You Know</em></a> event. From there, Sitepoint &#038; Learnable&#8217;s Kevin Yank asked me to put together a short course for <a href="http://learnable.com">learnable.com</a> based on the presentation. So after working on the occasional weekend I put it all together and got it launched just before xmas 2011.<span id="more-955"></span></p>
<div class="clear">&nbsp;</div>
<p><a href="https://learnable.com/courses/modernizr-front-end-development-done-right-2561"><img src="http://www.thecssninja.com/wp-content/uploads/2012/01/mzr_sml.png" alt="Modernizr: front-end development done right" width="609" height="207" class="article-img main-img" /></a></p>
<p><a href="https://learnable.com/courses/modernizr-front-end-development-done-right-2561">Go check it out</a>! <del datetime="2012-04-27T00:42:00+00:00">As a thank you for reading I will give away some free passes to the first 10 people who can answer this question correctly:</del> Promotion has now ended, thanks!</p>
<p>&#8220;Who created the Modernizr project?&#8221;</p>
<p>Mention me on twitter, <a href="http://twitter.com/ryanseddon">@ryanseddon</a>, with your answer to the question.</p>
<p>I will let the winners know via twitter.</p>
<p>If you don&#8217;t answer in time, don&#8217;t fret! It&#8217;s super cheap to get access to <a href="https://learnable.com/courses/modernizr-front-end-development-done-right-2561">my course</a> and many others at <a href="http://learnable.com">learnable.com</a>, 2012 is the year of upskilling so get onto it!</p>
<p class="shorty01">Short URL: <a href="http://cssn.in/ja/038">http://cssn.in/ja/038</a></p>
<div class="clear">&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://www.thecssninja.com/javascript/modernizr-course/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.thecssninja.com/javascript/modernizr-course</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 0.248 seconds. --><!-- Cached page generated by WP-Super-Cache on 2013-05-16 20:55:14 -->
