Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for button elements with descendants #1901

Merged
merged 13 commits into from Jan 28, 2017

Conversation

phistuck
Copy link
Contributor

@phistuck phistuck commented Dec 3, 2016

JSFiddle -
https://jsfiddle.net/0b3we0gm/9/ (with the fix)
https://jsfiddle.net/0c8nvnob/7/ (without the fix)

Non-Firefox browsers dispatch click on descendants of <button>, which breaks
the submitHandler logic that adds a hidden field with the name and value of the
submit button (since it is not included in the submission if it did not
initiate the submission, which might be the case when using form.submit in
submitHandler and returning false). submitButton is instead a descendant of
<button> and the hidden field does not compensate for the lack of an actual
submit button.

This would fix https://crbug.com/668524 and make submitButton an actual button.

The issue is reproducible in Chrome.
Firefox does not dispatch events to the descendants of <button> (but the specification requires that it will, see https://bugzilla.mozilla.org/show_bug.cgi?id=1089326 for details). The rest of the gang dispatch events to the descendants, so I imagine the problem exists in all of them and not just in Chrome.

Non-Firefox browsers dispatch "click" on descendants of <button>, which breaks
the submitHandler logic that adds a hidden field with the name and value of the
submit button (since it is not included in the submission if it did not
initiate the submission, which might be the case when using form.submit in
submitHandler and returning false). submitButton is instead a descendant of
<button> and the hidden field does not compensate for the lack of an actual
submit button.

This would fix crbug.com/668524 and make submitButton an actual button.
For testing that clicking on descendants of a button element still keeps the button element as the submit button.
For testing that clicking on descendants of a button element still
keeps the button element as the submit button.
Forgot to wrap the parenthesized value with spaces.
Forgot to wrap a parenthesized value with spaces.
Forgot to add a blank line above a comment.
Replaced a complex event initiation with a basic click().
Removed extraneous trailing spaces.
Removed two unused variables.
Instead of returning a DOM element, a jQuery element was returned.
Changed to return the original element in the target is already a button case.
@phistuck
Copy link
Contributor Author

phistuck commented Dec 3, 2016

An alternative approach is to use currentTarget instead of target. Thoughts?

Instead of looking for the parent or ancestor of the target,
just used the currentTarget property.
@phistuck
Copy link
Contributor Author

phistuck commented Dec 3, 2016

The currentTarget approach seems more reasonable and simpler, so I went ahead with it.

Copy link

@b1zzu b1zzu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't simple use event.currentTarget istant of event.target

@phistuck
Copy link
Contributor Author

@davbizz - I did already. See c257fc0.

Copy link
Member

@Arkni Arkni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also compare the property submitButton with the actual button.

button = $( "#testForm27 :submit" )[ 0 ];
$( button ).find( "span" ).click();
} );

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can extend your tests by testing the property submitButton of the validator. So, your tests will look like this:

var button = $( "#testForm27 :submit" )[ 0 ];
var v = $( "#testForm27" ).validate( {
	debug: true,
	submitHandler: function( form ) {

		// Compare the button with the `submitButton` property
		assert.deepEqual(
			v.submitButton, button, "The submitButton property should be the same as button"
		);

		var hidden = $( form ).find( "input:hidden" )[ 0 ];
		assert.deepEqual( hidden.value, button.value );
		assert.deepEqual( hidden.name, button.name );

		return false;
	}
} );

$( "#testForm27 [name=\"year\"]" ).val( "2016" );

$( button ).find( "span" ).click();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added your test as well.

@staabm
Copy link
Member

staabm commented Jan 28, 2017

Thx for your fix.

I will merge this now but have mixed feelings about it. Be warned that we could revert this again in case it breaks something...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants