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

fix: make defaultValue work with spread #14640

Merged
merged 4 commits into from
Dec 12, 2024
Merged

Conversation

paoloricciuti
Copy link
Member

@paoloricciuti paoloricciuti commented Dec 9, 2024

Closes #14634

I was not able to fully port the test without spreads because there's one single test I tried and failed to fix which is the textarea with defaultValue and a child. Do anyone has any idea on how could i fix this?

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Dec 9, 2024

🦋 Changeset detected

Latest commit: 12780ed

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Dec 9, 2024

Playground

pnpm add https://pkg.pr.new/svelte@14640

@trueadm
Copy link
Contributor

trueadm commented Dec 10, 2024

Lovely!

@paoloricciuti
Copy link
Member Author

Lovely!

Do we just ignore that edge case and hope for the best? 😄

Comment on lines 350 to 353
} else if (key === 'defaultValue') {
/** @type {HTMLInputElement} */ (element).defaultValue = value;
} else if (key === 'defaultChecked') {
/** @type {HTMLInputElement} */ (element).defaultChecked = value;
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove those in favor of doing value == null && !is_custom_element && key !== 'defaultValue' && key === 'defaultChecked' below

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we can't because we also want to nullify it if the value is nullish right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh no wait it's because in the else if we are checking for setters.include

assert.htmlEqual(test2_span.innerHTML, 'foo foo foo foo');

after_reset.push(() => {
console.log('-------------');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
console.log('-------------');

Copy link
Member Author

Choose a reason for hiding this comment

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

This is also in your test for default value, should i delete those there too?

Copy link
Member

Choose a reason for hiding this comment

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

oh woops, yeah do that then 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Did it 😄

assert.htmlEqual(test1_span.innerHTML, 'foo foo foo foo');

after_reset.push(() => {
console.log('-------------');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
console.log('-------------');

@@ -368,7 +368,10 @@ export function set_attributes(
} else if (key === 'checked') {
/**@type {HTMLInputElement}*/ (element).defaultChecked = default_checked_reset;
}
} else if (setters.includes(name) && (is_custom_element || typeof value !== 'string')) {
} else if (
is_default_value_or_checked ||
Copy link
Member

Choose a reason for hiding this comment

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

do we need this check? AFAIK the setters should include defaultValue/defaultChecked

Copy link
Member Author

Choose a reason for hiding this comment

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

Before this it was not working at all so I assumed no (tbh I didn't check)

Copy link
Member

Choose a reason for hiding this comment

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

setters does include those values, but defaultValue fails the typeof value !== 'string' part of the check

attributes[key] = null;
// if we remove the value/checked attributes this also for some reasons reset
// the default value so we need to keep track of it and reassign it after the remove
Copy link
Member

Choose a reason for hiding this comment

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

Ah I see, yeah that's why - now I'm wondering if we should model this differently: Check if the dom element is an input or textarea, and if so, if we come across a value or checked attribute, skip to the setters. Not sure which solution ends up being less code.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we still need to remove the attribute, we just need to make sure to restore the default value

dummdidumm added a commit that referenced this pull request Dec 10, 2024
This is a WIP, which I'm not sure if it goes anywhere. It started out to deeper understand the fix in #14640, and to fix some more theoretical loopholes, but this turns out to not fix the issue yet, and I'm not sure if it even will, so I'm punting on it for now but putting it up for others to see.
@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-svelte-14640-svelte.vercel.app/

this is an automated message

@Rich-Harris Rich-Harris merged commit 5e8d6ed into main Dec 12, 2024
11 checks passed
@Rich-Harris Rich-Harris deleted the default-value-spread branch December 12, 2024 19:19
@github-actions github-actions bot mentioned this pull request Dec 12, 2024
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.

Using Spread Operator Breaks defaultValue on Reset
4 participants