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

Toggling a radio button to false leaves a checked attribute in the DOM #46

Open
rjgotten opened this issue Jan 23, 2023 · 1 comment
Open

Comments

@rjgotten
Copy link

rjgotten commented Jan 23, 2023

Having a Stache binding such as <input type="radio" checked:from="this.checked" />, when the checked view model property toggles from false to true and then back to false it leaves a rogue valueless checked (Chrome) or empty string valued checked="" (Firefox) DOM attribute on the node.

The culprit is in the special handling for checked properties:

checked: {
get: function(){
return this.checked;
},
set: function(val){
// - `set( truthy )` => TRUE
// - `set( "" )` => TRUE
// - `set()` => TRUE
// - `set(undefined)` => false.
var notFalse = !!val || val === "" || arguments.length === 0;
this.checked = notFalse;
if(notFalse && this.type === "radio") {
this.defaultChecked = true;
}
},
remove: function(){
this.checked = false;
},
test: function(){
return this.nodeName === "INPUT";
}
},

Specifically lines 202-204.
As one can see, these only ever flip defaultChecked (which is wired to the presence of the DOM attribute) to true but never flip it back to false.

Should the relevant lines not simply be:

if ( this.type === "radio" ) {
  this.defaultChecked = notFalse;
}

or is there a specific reason, such as workaround for a browser bug, for the current unidirectional behavior?

This is causing headaches for Cypress automated testing for some of our test engineers. The Cypress .should( "be.checked" ) assertion appears at some level to be using presence of the checked DOM attribute, rather than consider the HtmlInputElement.prototype.checked property or use a :checked query selector; and CanJS setting but never unsetting the attribute is messing with it.

@justinbmeyer
Copy link
Contributor

justinbmeyer commented Jan 25, 2023 via email

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

No branches or pull requests

2 participants