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

Make element filtering configurable #40

Merged
merged 1 commit into from
Jan 23, 2024

Conversation

seanpdoyle
Copy link
Contributor

Introduce support for a { storageFilter: (field) => boolean } configuration override for calls to persistResumableFields. By default, preserve the existing behavior of rejecting fields where value is equivalent to defaultValue.

This change is motivated by situations where an element's value is impacted by events or packages that change its [value] attribute directly.

For example, consider the following sample code:

<input id="input" type="hidden" value="the default value">
<output id="output"></output>

<button>Change</button>

<script>
  const button = document.querySelector("button")
  const input = document.getElementById("input")
  const output = document.getElementById("output")

  output.textContent = input.defaultValue

  button.addEventListener("click", () => {
    input.setAttribute("value", "a new default value")
    output.textContent = input.defaultValue
  })
</script>

You can experiment with the code on JSFiddle.

Clicking the button changes the <input> element's [value] directly, which has a side-effect of change its .defaultValue as well.

Within the context of session-resume, this means that any elements impacted by side-effects of other code on the page will always be omitted from being stored.

This is especially incompatible with <trix-editor> elements provided by Trix and Action Text.

Introduce support for a `{ storageFilter: (field) => boolean }`
configuration override for calls to `persistResumableFields`. By
default, preserve the existing behavior of rejecting fields where
[value][] is equivalent to [defaultValue][].

This change is motivated by situations where an element's value is
impacted by events or packages that change its `[value]` attribute
directly.

For example, consider the following sample code:

```html
<input id="input" type="hidden" value="the default value">
<output id="output"></output>

<button>Change</button>

<script>
  const button = document.querySelector("button")
  const input = document.getElementById("input")
  const output = document.getElementById("output")

  output.textContent = input.defaultValue

  button.addEventListener("click", () => {
    input.setAttribute("value", "a new default value")
    output.textContent = input.defaultValue
  })
</script>
```

You can experiment with the code on [JSFiddle][].

Clicking the button changes the `<input>` element's `[value]` directly,
which has a side-effect of change its `.defaultValue` as well.

Within the context of `session-resume`, this means that any elements
impacted by side-effects of other code on the page will always be
omitted from being stored.

This is especially incompatible with `<trix-editor>` elements provided
by [Trix][] and [Action Text][].

[Trix]: https://trix-editor.org
[Action Text]: https://edgeguides.rubyonrails.org/action_text_overview.html

[value]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement#value
[defaultValue]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement#defaultvalue
[JSFiddle]: https://jsfiddle.net/1nwb4o23/
@seanpdoyle seanpdoyle force-pushed the field-storage-filter branch from 5f198c2 to 2709528 Compare January 20, 2024 20:59
@seanpdoyle seanpdoyle requested a review from a team as a code owner January 20, 2024 20:59
@seanpdoyle
Copy link
Contributor Author

@keithamus could you review these changes?

Copy link
Member

@keithamus keithamus left a comment

Choose a reason for hiding this comment

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

Looks like a sensible improvement. @github/web-systems need to review/merge this.

@theinterned
Copy link
Contributor

@seanpdoyle thank you for the contribution!

@theinterned theinterned merged commit e0984cd into github:main Jan 23, 2024
1 check passed
@theinterned
Copy link
Contributor

theinterned commented Jan 23, 2024

@seanpdoyle I just merged this, but realized that, like your other PRs it would be great to see

@seanpdoyle seanpdoyle deleted the field-storage-filter branch January 23, 2024 22:02
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.

3 participants