Skip to content
Permalink
Browse files

using classList if it is possible

  • Loading branch information
silentroach committed Sep 13, 2011
1 parent 8f44757 commit 54698b8d1a5294fae4c33da41e35485d0d0e9bd8
Showing with 57 additions and 30 deletions.
  1. +54 −29 src/attributes.js
  2. +3 −1 src/support.js
@@ -19,11 +19,11 @@ jQuery.fn.extend({
jQuery.removeAttr( this, name );
});
},

prop: function( name, value ) {
return jQuery.access( this, name, value, true, jQuery.prop );
},

removeProp: function( name ) {
name = jQuery.propFix[ name ] || name;
return this.each(function() {
@@ -52,18 +52,25 @@ jQuery.fn.extend({
elem = this[ i ];

if ( elem.nodeType === 1 ) {
if ( !elem.className && classNames.length === 1 ) {
elem.className = value;
if ( jQuery.support.classList ) {
for ( c = 0, cl = classNames.length; c < cl; c++ ) {
elem.classList.add( classNames[ c ] );
}

elem.className = jQuery.trim( elem.className );
} else {
setClass = " " + elem.className + " ";

for ( c = 0, cl = classNames.length; c < cl; c++ ) {
if ( !~setClass.indexOf( " " + classNames[ c ] + " " ) ) {
setClass += classNames[ c ] + " ";
if ( !elem.className && classNames.length === 1 ) {
elem.className = value;
} else {
setClass = " " + elem.className + " ";

for ( c = 0, cl = classNames.length; c < cl; c++ ) {
if ( !~setClass.indexOf( " " + classNames[ c ] + " " ) ) {
setClass += classNames[ c ] + " ";
}
}
elem.className = jQuery.trim( setClass );
}
elem.className = jQuery.trim( setClass );
}
}
}
@@ -87,16 +94,29 @@ jQuery.fn.extend({
for ( i = 0, l = this.length; i < l; i++ ) {
elem = this[ i ];

if ( elem.nodeType === 1 && elem.className ) {
if ( value ) {
className = (" " + elem.className + " ").replace( rclass, " " );
for ( c = 0, cl = classNames.length; c < cl; c++ ) {
className = className.replace(" " + classNames[ c ] + " ", " ");
}
elem.className = jQuery.trim( className );
if ( elem.nodeType === 1) {
if ( jQuery.support.classList ) {
if ( value ) {
for ( c = 0, cl = classNames.length; c < cl; c++ ) {
elem.classList.remove( classNames[ c ] );
}

elem.className = jQuery.trim( elem.className );
} else {
elem.className = '';
}
} else {
elem.className = "";
if ( elem.className ) {
if ( value ) {
className = (" " + elem.className + " ").replace( rclass, " " );
for ( c = 0, cl = classNames.length; c < cl; c++ ) {
className = className.replace(" " + classNames[ c ] + " ", " ");
}
elem.className = jQuery.trim( className );
} else {
elem.className = "";
}
}
}
}
}
@@ -145,8 +165,13 @@ jQuery.fn.extend({
hasClass: function( selector ) {
var className = " " + selector + " ";
for ( var i = 0, l = this.length; i < l; i++ ) {
if ( this[i].nodeType === 1 && (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
return true;
if ( this[i].nodeType === 1 ) {
if ( jQuery.support.classList ) {
return this[i].classList.contains( selector );
} else
if ((" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
return true;
}
}
}

@@ -156,7 +181,7 @@ jQuery.fn.extend({
val: function( value ) {
var hooks, ret,
elem = this[0];

if ( !arguments.length ) {
if ( elem ) {
hooks = jQuery.valHooks[ elem.nodeName.toLowerCase() ] || jQuery.valHooks[ elem.type ];
@@ -167,9 +192,9 @@ jQuery.fn.extend({

ret = elem.value;

return typeof ret === "string" ?
return typeof ret === "string" ?
// handle most common string cases
ret.replace(rreturn, "") :
ret.replace(rreturn, "") :
// handle cases where value is null/undef or number
ret == null ? "" : ret;
}
@@ -290,15 +315,15 @@ jQuery.extend({
height: true,
offset: true
},

attrFix: {
// Always normalize to ensure hook usage
tabindex: "tabIndex"
},

attr: function( elem, name, value, pass ) {
var nType = elem.nodeType;

// don't get/set attributes on text, comment and attribute nodes
if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
return undefined;
@@ -431,7 +456,7 @@ jQuery.extend({
frameborder: "frameBorder",
contenteditable: "contentEditable"
},

prop: function( elem, name, value ) {
var nType = elem.nodeType;

@@ -466,7 +491,7 @@ jQuery.extend({
}
}
},

propHooks: {
tabIndex: {
get: function( elem ) {
@@ -519,7 +544,7 @@ boolHook = {

// IE6/7 do not support getting/setting some attributes with get/setAttribute
if ( !jQuery.support.getSetAttribute ) {

// Use this for any attribute in IE6/7
// This fixes almost every IE6/7 issue
nodeHook = jQuery.valHooks.button = {
@@ -26,7 +26,6 @@ jQuery.support = (function() {
div.setAttribute("className", "t");
div.innerHTML = " <link/><table></table><a href='/a' style='top:1px;float:left;opacity:.55;'>a</a><input type='checkbox'/>";


all = div.getElementsByTagName( "*" );
a = div.getElementsByTagName( "a" )[ 0 ];

@@ -81,6 +80,9 @@ jQuery.support = (function() {
// Test setAttribute on camelCase class. If it works, we need attrFixes when doing get/setAttribute (ie6/7)
getSetAttribute: div.className !== "t",

// Test if browser supports classList
classList: !!div.classList,

// Will be defined later
submitBubbles: true,
changeBubbles: true,

0 comments on commit 54698b8

Please sign in to comment.
You can’t perform that action at this time.