diff --git a/CHANGELOG b/CHANGELOG index f10bdda..3f8b16f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Remove the dependency on Element.ClassNames from Element#addClassName/removeClassName/toggleClassName, and deprecate Element.ClassNames. Closes #9073. [Tobie Langel] + * Make Element#wrap accept a second argument for setting attributes on the wrapper. Allow wrapping elements which are not part of the document. Closes #9071. [Tobie Langel] * Improvements for Element#replace, Element#update and Element#insert. Closes #7429, #9060. [Tobie Langel] diff --git a/src/deprecated.js b/src/deprecated.js index b320049..e284025 100644 --- a/src/deprecated.js +++ b/src/deprecated.js @@ -20,7 +20,7 @@ var Insertion = { After: function(element, content) { return Element.insert(element, {after:content}); } -} +}; var $continue = new Error('"throw $continue" is deprecated, use "return" instead'); @@ -109,5 +109,41 @@ var Position = { options = options || {}; return Element.clonePosition(target, source, options); } -} +}; + +/*--------------------------------------------------------------------------*/ + +Element.ClassNames = Class.create(); +Element.ClassNames.prototype = { + initialize: function(element) { + this.element = $(element); + }, + + _each: function(iterator) { + this.element.className.split(/\s+/).select(function(name) { + return name.length > 0; + })._each(iterator); + }, + + set: function(className) { + this.element.className = className; + }, + + add: function(classNameToAdd) { + if (this.include(classNameToAdd)) return; + this.set($A(this).concat(classNameToAdd).join(' ')); + }, + + remove: function(classNameToRemove) { + if (!this.include(classNameToRemove)) return; + this.set($A(this).without(classNameToRemove).join(' ')); + }, + + toString: function() { + return $A(this).join(' '); + } +}; + +Object.extend(Element.ClassNames.prototype, Enumerable); + /*--------------------------------------------------------------------------*/ diff --git a/src/dom.js b/src/dom.js index f485a7f..27da475 100644 --- a/src/dom.js +++ b/src/dom.js @@ -285,7 +285,7 @@ Element.Methods = { }, classNames: function(element) { - return new Element.ClassNames(element); + return $w(element.className); }, hasClassName: function(element, className) { @@ -297,20 +297,22 @@ Element.Methods = { addClassName: function(element, className) { if (!(element = $(element))) return; - Element.classNames(element).add(className); + if (!element.hasClassName(className)) + element.className += (element.className ? ' ' : '') + className; return element; }, removeClassName: function(element, className) { if (!(element = $(element))) return; - Element.classNames(element).remove(className); + element.className = element.className.replace( + new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip(); return element; }, toggleClassName: function(element, className) { if (!(element = $(element))) return; - Element.classNames(element)[element.hasClassName(className) ? 'remove' : 'add'](className); - return element; + return element[element.hasClassName(className) ? + 'removeClassName' : 'addClassName'](className); }, observe: function() { @@ -1158,38 +1160,3 @@ Element.addMethods = function(methods) { if (Element.extend.refresh) Element.extend.refresh(); Element.cache = {}; }; - -/*--------------------------------------------------------------------------*/ - -Element.ClassNames = Class.create(); -Element.ClassNames.prototype = { - initialize: function(element) { - this.element = $(element); - }, - - _each: function(iterator) { - this.element.className.split(/\s+/).select(function(name) { - return name.length > 0; - })._each(iterator); - }, - - set: function(className) { - this.element.className = className; - }, - - add: function(classNameToAdd) { - if (this.include(classNameToAdd)) return; - this.set($A(this).concat(classNameToAdd).join(' ')); - }, - - remove: function(classNameToRemove) { - if (!this.include(classNameToRemove)) return; - this.set($A(this).without(classNameToRemove).join(' ')); - }, - - toString: function() { - return $A(this).join(' '); - } -}; - -Object.extend(Element.ClassNames.prototype, Enumerable);