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

Remove checked attr for radio-button by default #1558

Merged
merged 7 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/dom/src/serialize-inputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ export function serializeInputElements({ dom, clone, warnings }) {
switch (elem.type) {
case 'checkbox':
case 'radio':
/*
here we are removing the checked attr if present by default,
so that only the current selected radio-button will have the checked attr present in the dom

this happens because in html, when the checked attribute is present in the multiple radio-buttons by default,
the browser will only render the last checked radio-button as when the user is selecting any particular button,
the checked attribute on other buttons is not removed,
hence sometimes it shows inconsistent state even though the `element.checked` attribute returns correct state
*/
cloneEl.removeAttribute('checked');
if (elem.checked) {
cloneEl.setAttribute('checked', '');
}
Expand Down
22 changes: 20 additions & 2 deletions packages/dom/test/serialize-inputs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ describe('serializeInputs', () => {
<input id="mailing" type="checkbox" />
<label for="mailing">Subscribe?</label>

<input id="radio" type="radio" />
<input id="radio" type="radio" checked=""/>
<label for="radio">Radio</label>

<input id="nevercheckedradio" type="radio" />
<label for="nevercheckedradio">Never checked</label>

<form>
<input type="radio" id="option1" name="option" value="option1" checked>
<label for="option1">Option 1</label><br>
<input type="radio" id="option2" name="option" value="option2" checked>
<label for="option2">Option 2</label><br>
</form>

<label for="singleSelect">Does this work?</label>
<select id="singleSelect">
<option value="yes">Yes</option>
Expand Down Expand Up @@ -84,9 +91,20 @@ describe('serializeInputs', () => {
});

it(`${platform}: serializes checked radio buttons`, () => {
expect($('#radio')[0].outerHTML).toContain('checked=""');
expect($('#radio')[0].checked).toBe(true);
});

it(`${platform}: removes checked attr from radio-buttons if present in dom by default but radio is not checked`, () => {
// validates `checked=""` is removed from the dom for option1 as it is not checked
expect($('#option1')[0].outerHTML).not.toContain('checked=""');
expect($('#option1')[0].checked).toBe(false);

// validates `checked=""` is not removed from the dom for option2 as it is in checked state
expect($('#option2')[0].outerHTML).toContain('checked=""');
shantanuk-browserstack marked this conversation as resolved.
Show resolved Hide resolved
expect($('#option2')[0].checked).toBe(true);
});

it(`${platform}: serializes textareas`, () => {
expect($('#feedback')[0].innerText).toBe('This is my feedback... And it is not very helpful');
});
Expand Down Expand Up @@ -124,7 +142,7 @@ describe('serializeInputs', () => {

it(`${platform}: adds a guid data-attribute to the original DOM`, () => {
// plain platform has extra element #test-shadow
expect(dom.querySelectorAll('[data-percy-element-id]')).toHaveSize(platform === 'plain' ? 10 : 9);
expect(dom.querySelectorAll('[data-percy-element-id]')).toHaveSize(platform === 'plain' ? 12 : 11);
});

it(`${platform}: adds matching guids to the orignal DOM and cloned DOM`, () => {
Expand Down
Loading