<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Anand Nalya &#187; css</title>
	<atom:link href="http://anandnalya.com/tag/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://anandnalya.com</link>
	<description>blog</description>
	<lastBuildDate>Wed, 04 Jan 2012 06:55:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Role based views using css and javascript</title>
		<link>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=role-based-views-using-css-and-javascript</link>
		<comments>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 07:40:24 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=157</guid>
		<description><![CDATA[In web applications, role based views are normally done using server side if..else tags etc. When developing pure javascript clients, one way of doing role based views is again using javascript conditionals. This approach suffers from the fact that role-related &#8230; <a href="http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In web applications, role based views are normally done using server side <code>if..else</code> <a href="http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html#d0e6296">tags</a> etc. When developing pure javascript clients, one way of doing role based views is again using javascript conditionals. This approach suffers from the fact that role-related code is scattered throughout the document. Another simple way to achieve the same is using css classes.</p>
<p>Let us assume that we have three roles, <code>ADMIN</code> and <code>USER</code> and <code>GUEST</code>. Then we will define three css classes as follows:</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nc">.role_admin</span><span class="o">,</span> <span class="nc">.role_user</span><span class="o">,</span> <span class="nc">.role_guest</span><span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">display</span><span class="o">:</span><span class="k">none</span><span class="p">;</span></div><div class='line' id='LC3'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/f8e3f87ba993b3f17cb7cac9404c8b2566263ed3/role.css" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role.css" style="float:right;margin-right:10px;color:#666">role.css</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now consider the following html doc, that has css classes attached to elements according to the current user&#8217;s role.</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;html&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;head&gt;</span></div><div class='line' id='LC3'><span class="nt">&lt;link</span> <span class="na">href=</span><span class="s">&quot;./role.css&quot;</span> <span class="na">rel=</span><span class="s">&quot;stylesheet&quot;</span> <span class="na">type=</span><span class="s">&quot;text/css&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC4'><span class="nt">&lt;/head&gt;</span></div><div class='line' id='LC5'><span class="nt">&lt;body&gt;</span></div><div class='line' id='LC6'><span class="nt">&lt;h1</span> <span class="na">class=</span><span class="s">&quot;role_admin&quot;</span><span class="nt">&gt;</span>This should be visible to admin only.<span class="nt">&lt;h1&gt;</span></div><div class='line' id='LC7'><span class="nt">&lt;h2</span> <span class="na">class=</span><span class="s">&quot;role_user&quot;</span><span class="nt">&gt;</span>This is for the normal logged in user.<span class="nt">&lt;/h1&gt;</span></div><div class='line' id='LC8'><span class="nt">&lt;h3</span> <span class="na">class=</span><span class="s">&quot;role_guest&quot;</span><span class="nt">&gt;</span>And this is for the guest.<span class="nt">&lt;/h1&gt;</span></div><div class='line' id='LC9'><span class="nt">&lt;/body&gt;</span></div><div class='line' id='LC10'><span class="nt">&lt;/html&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/a30ef0aa9a9852da64669667cfd8b3fd922dc6eb/roleSample.html" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role_sample.html" style="float:right;margin-right:10px;color:#666">roleSample.html</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>At this stage if you load the document in a browser, you will not see anything as nothing all the roles are invisible. So lets spruce this up with a bit of javascript to set proper css class properties.</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We are assuming that jQuery and jQuery Cookie plugin are included in the doc</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">role</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">cookie</span><span class="p">(</span><span class="s1">&#39;user_role&#39;</span><span class="p">);</span> <span class="c1">// Should return one of &#39;ADMIN&#39;, &#39;USER&#39; OR &#39;GUEST&#39;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// set property for relevant css class</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">cssClass</span> <span class="o">=</span> <span class="s1">&#39;.role_&#39;</span><span class="o">+</span> <span class="nx">role</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">();</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt; &quot;</span><span class="o">+</span> <span class="nx">cssClass</span> <span class="o">+</span> <span class="s2">&quot;{ display:block } &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC9'><span class="p">});</span></div><div class='line' id='LC10'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/d27ccc00e3b0607785bb11cec4d118a9ca62695b/role.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role.js" style="float:right;margin-right:10px;color:#666">role.js</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>If a cookie <code>role</code> is set that represents the current user&#8217;s role then adding the above snippet of code will enable all the elements tagged with css class <code>.role_{roleCookie}</code> to be visible.</p>
<p>Even though this is a very simple implementation, we can modify it easily to take into account more complex scenarios. For example, if you have completely ordered role based visibility (access level), i.e. given role R1 and R2, we can always tell which role has more visibility, then we can extend as follows:
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We are assuming that jQuery and jQuery Cookie plugin are included in the doc</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">role</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">cookie</span><span class="p">(</span><span class="s1">&#39;user_role&#39;</span><span class="p">);</span> <span class="c1">// Should return one of &#39;ADMIN&#39;, &#39;USER&#39; OR &#39;GUEST&#39;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// set property for relevant css class</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="p">(</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;ADMIN&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// make everything visible</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_admin, .role_user, .role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span> <span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">app</span><span class="p">.</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;USER&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// make user and guest part visible, admin part will remain invisible</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_user, .role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span> <span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">app</span><span class="p">.</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;GUEST&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// only show the guest part</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC19'><span class="p">});</span></div><div class='line' id='LC20'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/ca1c750910a67bc77f119a2551e42561337a85f6/roleOrdered.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role_ordered.js" style="float:right;margin-right:10px;color:#666">roleOrdered.js</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p><em>Caution:</em> One thing to note here is that all the parts of the document are sent to all the users, only what is visible on the browser is role based. So, server side authorisation checks will always be there as nothing stops a suspecting user from looking into the source and firing that dreaded request.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Minifying Javascript/css without changing file references in your source</title>
		<link>http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=minifying-javascriptcss-without-changing-file-references-in-your-source</link>
		<comments>http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 13:21:02 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[compressor]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=79</guid>
		<description><![CDATA[Rule 10 of Steve Souders High Performance Web Sites: Minify Javascript The most common problem faced while implemnting this is how you handle the full and minified version and how to change there reference in referencing documents. One of the &#8230; <a href="http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Rule 10 of Steve Souders High Performance Web Sites: <a href="http://developer.yahoo.net/blog/archives/2007/07/high_performanc_8.html">Minify Javascript</a></p>
<p>The most common problem faced while implemnting this is how you handle the full and minified version and how to change there reference in referencing documents. One of the easier ways to do this is to make it part of the deployment process. </p>
<p>Here are the relevent steps involved.</p>
<p>I&#8217;m using <a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a>.<br />
<div id="gist-1124427" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">#!/bin/bash</span></div><div class='line' id='LC2'>&nbsp;</div><div class='line' id='LC3'><span class="c">#Execute this script after checking out the latest source from repository.</span></div><div class='line' id='LC4'>&nbsp;</div><div class='line' id='LC5'><span class="c">#Minify all javascript files</span></div><div class='line' id='LC6'><span class="nb">cd</span> /path/to/javascript</div><div class='line' id='LC7'><span class="k">for </span>x in <span class="sb">`</span>ls *.js<span class="sb">`</span></div><div class='line' id='LC8'><span class="k">do</span></div><div class='line' id='LC9'><span class="k">        </span>java -jar /path/to/compressor/yuicompressor-2.4.2.jar -o <span class="k">${</span><span class="nv">x</span><span class="p">%%.*</span><span class="k">}</span>-min.js --preserve-semi  <span class="nv">$x</span></div><div class='line' id='LC10'><span class="k">done</span></div><div class='line' id='LC11'>&nbsp;</div><div class='line' id='LC12'><span class="c">#Minfiy all css files</span></div><div class='line' id='LC13'><span class="nb">cd</span> /path/to/css</div><div class='line' id='LC14'><span class="k">for </span>x in <span class="sb">`</span>ls *.css<span class="sb">`</span></div><div class='line' id='LC15'><span class="k">do</span></div><div class='line' id='LC16'><span class="k">        </span>java -jar /path/to/compressor/yuicompressor-2.4.2.jar -o <span class="k">${</span><span class="nv">x</span><span class="p">%%.*</span><span class="k">}</span>-min.css  <span class="nv">$x</span></div><div class='line' id='LC17'><span class="k">done</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1124427/ad846053c02470535698614eed39c9401f5caa88/minify.sh" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1124427#file_minify.sh" style="float:right;margin-right:10px;color:#666">minify.sh</a>
            <a href="https://gist.github.com/1124427">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
<br />
Now you don&#8217;t want to replace all references to <em>x.css</em> or <em>x.js</em> in your development code with references to <em>x-min.css</em> and <em>x-min.js</em> respectively. So what you can do is rewrite all those filenames at the web server level.</p>
<p>For apache the following rewrite rules work fine:<br />
<div id="gist-1124427" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>#enable rewriting</div><div class='line' id='LC2'>RewriteEngine on</div><div class='line' id='LC3'>RewriteRule /(.*)\.js /$1-min.js</div><div class='line' id='LC4'>RewriteRule /(.*)\.css /$1-min.css</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1124427/0a7c12d3f31f779d173b821962361c0d2f176f65/httpd.conf" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1124427#file_httpd.conf" style="float:right;margin-right:10px;color:#666">httpd.conf</a>
            <a href="https://gist.github.com/1124427">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
<br />
<em>Caution:</em> Remember to delete existing minified css/js file before running the minifying script or you will end up with file names like x-min-min-min.js and so on. One way to do this is to clear the js/css folder before checking out files from your source repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

