<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Back to Basics</title>
	<atom:link href="http://blog.thejit.org/2009/08/22/back-to-basics/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thejit.org/2009/08/22/back-to-basics/</link>
	<description>Data Visualization, JavaScript and Computer Science related stuff</description>
	<lastBuildDate>Sun, 05 Sep 2010 12:01:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Steven Shaw</title>
		<link>http://blog.thejit.org/2009/08/22/back-to-basics/comment-page-1/#comment-438</link>
		<dc:creator>Steven Shaw</dc:creator>
		<pubDate>Mon, 31 Aug 2009 14:37:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=1033#comment-438</guid>
		<description>Is there a potential problem with your hasClass implementation when the element has no &#039;class&#039; attribute and returns null. You&#039;d have to be testing for a class called &#039;null&#039;.

Interesting exercise. Here&#039;s what I have. I started with addClass and removeClass and then extracted hasClass from addClass. Didn&#039;t know about the .className property so used getAttribute/setAttribute...

&lt;code&gt;
function hasClass(element, className) {
  var v = element.getAttribute(&#039;class&#039;);
  return new RegExp(&quot;\\b&quot; + className + &quot;\\b&quot;).test(v);
}

function addClass(element, className) {
  var v = element.getAttribute(&#039;class&#039;);
  if (v === null) {
    element.setAttribute(&#039;class&#039;, className);
  } else {
    if (!hasClass(element, className)) {
      element.setAttribute(&#039;class&#039;, v + &#039; &#039; + className);
    }
  }
}
  
function removeClass(element, className) {
  var v = element.getAttribute(&#039;class&#039;);
  if (v === &#039;test&#039;) {
    element.removeAttribute(&#039;class&#039;);
  } else {
    newVal = v.replace(new RegExp(&quot;[ ]\\b&quot; + className + &quot;\\b&quot;), &#039;&#039;);
    element.setAttribute(&#039;class&#039;, newVal);
  }
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Is there a potential problem with your hasClass implementation when the element has no &#8216;class&#8217; attribute and returns null. You&#8217;d have to be testing for a class called &#8216;null&#8217;.</p>
<p>Interesting exercise. Here&#8217;s what I have. I started with addClass and removeClass and then extracted hasClass from addClass. Didn&#8217;t know about the .className property so used getAttribute/setAttribute&#8230;</p>
<p><code><br />
function hasClass(element, className) {<br />
  var v = element.getAttribute('class');<br />
  return new RegExp("\\b" + className + "\\b").test(v);<br />
}</p>
<p>function addClass(element, className) {<br />
  var v = element.getAttribute('class');<br />
  if (v === null) {<br />
    element.setAttribute('class', className);<br />
  } else {<br />
    if (!hasClass(element, className)) {<br />
      element.setAttribute('class', v + ' ' + className);<br />
    }<br />
  }<br />
}</p>
<p>function removeClass(element, className) {<br />
  var v = element.getAttribute('class');<br />
  if (v === 'test') {<br />
    element.removeAttribute('class');<br />
  } else {<br />
    newVal = v.replace(new RegExp("[ ]\\b" + className + "\\b"), '');<br />
    element.setAttribute('class', newVal);<br />
  }<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://blog.thejit.org/2009/08/22/back-to-basics/comment-page-1/#comment-428</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Fri, 28 Aug 2009 14:43:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=1033#comment-428</guid>
		<description>I&#039;m not sure I&#039;m understanding this right, but it seems to me that you can put escape sequences in CSS class names (http://www.w3.org/TR/CSS2/grammar.html). Those would interfere with the regexp. I think, to be on the safe side, the class name should be escaped before compiling the regexp:
/**
	Returns a version of the string that is suitable for insertion in a regular expression as a literal value
	@type String

*/
String.prototype.escapeRegexp = function () {
        return(
            this.replace(/\\/g,&#039;\\\\&#039;).
                replace(/([.\$\^\{\[\&#124;\(\)\]\}\*\+\?])/g,&#039;\\$1&#039;)
        );
    };

(Hey, great blog and great work, thanks for sharing!!!)</description>
		<content:encoded><![CDATA[<p>I&#8217;m not sure I&#8217;m understanding this right, but it seems to me that you can put escape sequences in CSS class names (<a href="http://www.w3.org/TR/CSS2/grammar.html" rel="nofollow">http://www.w3.org/TR/CSS2/grammar.html</a>). Those would interfere with the regexp. I think, to be on the safe side, the class name should be escaped before compiling the regexp:<br />
/**<br />
	Returns a version of the string that is suitable for insertion in a regular expression as a literal value<br />
	@type String</p>
<p>*/<br />
String.prototype.escapeRegexp = function () {<br />
        return(<br />
            this.replace(/\\/g,&#8217;\\\\&#8217;).<br />
                replace(/([.\$\^\{\[\|\(\)\]\}\*\+\?])/g,&#8217;\\$1&#8242;)<br />
        );<br />
    };</p>
<p>(Hey, great blog and great work, thanks for sharing!!!)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://blog.thejit.org/2009/08/22/back-to-basics/comment-page-1/#comment-423</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Sun, 23 Aug 2009 14:47:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=1033#comment-423</guid>
		<description>Not always apparently:

&lt;em&gt;The trick is that if you write a document using an XML declaration and an XHTML doctype, then the CSS class names will be case sensitive for some browsers.&lt;/em&gt;

http://webdesign.about.com/od/css/f/blcssfaqcase.htm

Perhaps it would be better to leave the user the freedom make case sensitive searches for classNames.</description>
		<content:encoded><![CDATA[<p>Not always apparently:</p>
<p><em>The trick is that if you write a document using an XML declaration and an XHTML doctype, then the CSS class names will be case sensitive for some browsers.</em></p>
<p><a href="http://webdesign.about.com/od/css/f/blcssfaqcase.htm" rel="nofollow">http://webdesign.about.com/od/css/f/blcssfaqcase.htm</a></p>
<p>Perhaps it would be better to leave the user the freedom make case sensitive searches for classNames.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hozzy</title>
		<link>http://blog.thejit.org/2009/08/22/back-to-basics/comment-page-1/#comment-422</link>
		<dc:creator>Hozzy</dc:creator>
		<pubDate>Sun, 23 Aug 2009 14:36:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=1033#comment-422</guid>
		<description>Hi. What about case insensitivity? You examples don&#039;t seem to care about case insensitivity! In CSS class &quot;kiss&quot; is the same as &quot;KISS&quot;.</description>
		<content:encoded><![CDATA[<p>Hi. What about case insensitivity? You examples don&#8217;t seem to care about case insensitivity! In CSS class &#8220;kiss&#8221; is the same as &#8220;KISS&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://blog.thejit.org/2009/08/22/back-to-basics/comment-page-1/#comment-421</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Sun, 23 Aug 2009 01:13:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=1033#comment-421</guid>
		<description>Hi Matias,

Thanks for sharing your hasClass implementation :) Actually I don&#039;t know which one is faster, I think that in most cases performance will be browser dependent, but since classNames are usually short names the regexp and indexOf methods might be quite similar in performance in &quot;common&quot; cases.</description>
		<content:encoded><![CDATA[<p>Hi Matias,</p>
<p>Thanks for sharing your hasClass implementation <img src='http://blog.thejit.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Actually I don&#8217;t know which one is faster, I think that in most cases performance will be browser dependent, but since classNames are usually short names the regexp and indexOf methods might be quite similar in performance in &#8220;common&#8221; cases.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matías Giovannini</title>
		<link>http://blog.thejit.org/2009/08/22/back-to-basics/comment-page-1/#comment-420</link>
		<dc:creator>Matías Giovannini</dc:creator>
		<pubDate>Sat, 22 Aug 2009 19:06:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.thejit.org/?p=1033#comment-420</guid>
		<description>Nicolás, all good points. The &quot;basic&quot; nature of Javascript is both a blessing and a curse to beginners and experienced practitioners alike. It is perhaps inevitable, certainly expected to see homegrown utility libraries become full-fledged &quot;products&quot;.

With respect to hasClass(), seeing that you&#039;re not loathe to use regular expressions for removeClass(), I&#039;d prefer using a word-boundary matching adverb instead of padding with spaces:

# function hasClass(domElement, className) {  
#   return !!domElement.className.match(&quot;\\b&quot; + className + &quot;\\b&quot;);  
# }

which if not as fast as using indexOf is to me a bit clearer.</description>
		<content:encoded><![CDATA[<p>Nicolás, all good points. The &#8220;basic&#8221; nature of Javascript is both a blessing and a curse to beginners and experienced practitioners alike. It is perhaps inevitable, certainly expected to see homegrown utility libraries become full-fledged &#8220;products&#8221;.</p>
<p>With respect to hasClass(), seeing that you&#8217;re not loathe to use regular expressions for removeClass(), I&#8217;d prefer using a word-boundary matching adverb instead of padding with spaces:</p>
<p># function hasClass(domElement, className) {<br />
#   return !!domElement.className.match(&#8220;\\b&#8221; + className + &#8220;\\b&#8221;);<br />
# }</p>
<p>which if not as fast as using indexOf is to me a bit clearer.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
