Skip to content

Commit 18b737f

Browse files
Ensure Form.focusFirstElement doesn't raise an exception on forms with no fields. [prototypejs#341 state:resolved] (achernin, Andrew Dupont)
1 parent 6abe2a6 commit 18b737f

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Ensure `Form.focusFirstElement` doesn't raise an exception on forms with no fields. [#341 state:resolved] (achernin, Andrew Dupont)
2+
13
* Define a `relatedTarget` property on extended mouseenter/mouseleave events in IE's legacy event system. [#708 state:resolved] (Walter Smith, Tobie Langel, Andrew Dupont)
24

35
* Fix odd behavior with `new Element('select')` in IE6-7. [#480 state:resolved] (Bruce Harris, kangax, Andrew Dupont)

src/dom/form.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ Form.Methods = {
321321
**/
322322
focusFirstElement: function(form) {
323323
form = $(form);
324-
form.findFirstElement().activate();
324+
var element = form.findFirstElement();
325+
if (element) element.activate();
325326
return form;
326327
},
327328

test/unit/fixtures/form.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
<input type="file" name="file_name" value="foo" />
8585
</form>
8686

87+
<form id="form_empty"></form>
88+
8789
<!-- tabindexed forms -->
8890
<div id="tabindex">
8991
<form id="ffe">

test/unit/form_test.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,26 +180,25 @@ new Test.Unit.Runner({
180180
catch(e){ return null }
181181
}
182182

183-
// Form.focusFirstElement shouldn't focus disabled elements
184183
var element = Form.findFirstElement('bigform');
185-
this.assertEqual('submit', element.id);
184+
this.assertEqual('submit', element.id, "Form.focusFirstElement shouldn't focus disabled elements");
186185

187-
// Test IE doesn't select text on buttons
188186
Form.focusFirstElement('bigform');
189-
if(document.selection) this.assertEqual('', getSelection(element));
187+
if (document.selection) this.assertEqual('', getSelection(element), "IE shouldn't select text on buttons");
190188

191-
// Form.Element.activate shouldn't select text on buttons
192189
element = $('focus_text');
193-
this.assertEqual('', getSelection(element));
190+
this.assertEqual('', getSelection(element), "Form.Element.activate shouldn't select text on buttons");
194191

195-
// Form.Element.activate should select text on text input elements
196192
element.activate();
197-
this.assertEqual('Hello', getSelection(element));
193+
this.assertEqual('Hello', getSelection(element), "Form.Element.activate should select text on text input elements");
198194

199-
// Form.Element.activate shouldn't raise an exception when the form or field is hidden
200195
this.assertNothingRaised(function() {
201196
$('form_focus_hidden').focusFirstElement();
202-
});
197+
}, "Form.Element.activate shouldn't raise an exception when the form or field is hidden");
198+
199+
this.assertNothingRaised(function() {
200+
$('form_empty').focusFirstElement();
201+
}, "Form.focusFirstElement shouldn't raise an exception when the form has no fields");
203202
},
204203

205204
testFormGetElements: function() {

0 commit comments

Comments
 (0)