Skip to content

Commit ded0dde

Browse files
Fix regression in behavior of someElements.each(Element.toggle). [close prototypejs#136]
1 parent 3d37b8e commit ded0dde

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/prototype/dom/dom.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@
274274

275275
/**
276276
* Element.toggle(@element[, bool]) -> Element
277+
* - bool (Boolean): Whether the element should be shown or hidden. If not
278+
a boolean, this argument will be ignored.
277279
*
278280
* Toggles the CSS `display` of `element`. Returns `element`.
279281
*
@@ -284,6 +286,13 @@
284286
* current state, but will use the `bool` argument instead if it's
285287
* provided (`true` to show the element, `false` to hide it).
286288
*
289+
* If the `bool` argument is not a boolean, **it will be ignored**. This
290+
* preserves the ability to toggle elements through comparisons (e.g.,
291+
* `errorElement.toggle(errors > 0)`) while also letting a user do
292+
* `someElements.each(Element.toggle)` without falling victim to
293+
* JavaScript's famous [problems with variadic arguments](http://www.wirfs-brock.com/allen/posts/166).
294+
*
295+
*
287296
* ##### Examples
288297
*
289298
* <div id="welcome-message">Welcome</div>
@@ -335,7 +344,7 @@
335344
**/
336345
function toggle(element, bool) {
337346
element = $(element);
338-
if (Object.isUndefined(bool))
347+
if (typeof bool !== 'boolean')
339348
bool = !Element.visible(element);
340349
Element[bool ? 'show' : 'hide'](element);
341350

test/unit/dom_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ new Test.Unit.Runner({
305305
this.assert($('test-toggle-hidden').visible(), 'test-toggle-hidden 1');
306306
$('test-toggle-hidden').toggle();
307307
this.assert(!$('test-toggle-hidden').visible(), 'test-toggle-hidden 2');
308+
309+
$('test-toggle-hidden', 'test-toggle-visible').each(Element.toggle);
310+
this.assert(!$('test-toggle-visible').visible(), 'test-toggle-visible-3');
311+
this.assert($('test-toggle-hidden').visible(), 'test-toggle-hidden-3');
308312
},
309313

310314
testElementShow: function(){

0 commit comments

Comments
 (0)