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

Exclude document.body when morphing with ignoreActiveValue: true #34

Merged

Conversation

seanpdoyle
Copy link
Contributor

When morphing with ignoreActiveValue: true, the <body> element and its children (that is, the entire document) can be preserved if no other element has focus.

Its common in some browsers (for example, Safari) to return document.body from document.activeElement when no other element has focus.

This commit incorporates that condition into the
ignoreValueOfActiveElement() predicate function.

When morphing with `ignoreActiveValue: true`, the `<body>` element and
its children (that is, the entire document) can be preserved if no other
element has focus.

Its common in some browsers (for example, Safari) to return
`document.body` from `document.activeElement` when no other element has
focus.

This commit incorporates that condition into the
`ignoreValueOfActiveElement()` predicate function.
seanpdoyle added a commit to seanpdoyle/turbo that referenced this pull request Jan 25, 2024
Morph with the [ignoreActiveValue: true][] option to morph the currently
focused element's attributes, but preserve its value.

This behavior can be extremely helpful when paired with an
auto-submitting `<form>` element, like a typeahead `[role="combobox"]`,
or an auto-submitting [`<input type="search">`][search].

This commit depends on a fork of `idiomorph` that [fixes a bug related
to `ignoreActiveValue: true`][bigskysoftware/idiomorph#34].

[ignoreActiveValue: true]: https://github.com/bigskysoftware/idiomorph#options
[search]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/search
[bigskysoftware/idiomorph#34]: bigskysoftware/idiomorph#34
Comment on lines +244 to +265
it('does not ignore body when ignoreActiveValue is true and no element has focus', function()
{
let parent = make("<div><input value='foo'></div>");
document.body.append(parent);

let initial = parent.querySelector("input");

// morph
let finalSrc = '<input value="bar">';
Idiomorph.morph(initial, finalSrc, {morphStyle:'outerHTML'});
initial.outerHTML.should.equal('<input value="bar">');

document.activeElement.should.equal(initial);

let finalSrc2 = '<input class="foo" value="doh">';
Idiomorph.morph(initial, finalSrc2, {morphStyle:'outerHTML', ignoreActiveValue: true});
initial.value.should.equal('doh');
initial.classList.value.should.equal('foo');

document.body.removeChild(parent);
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@1cg I wasn't able to execute the test suite locally. I've copied this test body from the one that precedes it, and have removed the call to .focus(). Does that feel sufficient?

I noticed that this project doesn't have CI. Are you able to execute this test in your working test harness?

seanpdoyle added a commit to seanpdoyle/turbo that referenced this pull request Jan 29, 2024
Morph with the [ignoreActiveValue: true][] option to morph the currently
focused element's attributes, but preserve its value.

This behavior can be extremely helpful when paired with an
auto-submitting `<form>` element, like a typeahead `[role="combobox"]`,
or an auto-submitting [`<input type="search">`][search].

This commit depends on a fork of `idiomorph` that [fixes a bug related
to `ignoreActiveValue: true`][bigskysoftware/idiomorph#34].

[ignoreActiveValue: true]: https://github.com/bigskysoftware/idiomorph#options
[search]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/search
[bigskysoftware/idiomorph#34]: bigskysoftware/idiomorph#34
@1cg 1cg merged commit d031215 into bigskysoftware:main Jan 29, 2024
@seanpdoyle seanpdoyle deleted the ignore-active-value-excludes-body-element branch January 30, 2024 01:37
afcapel pushed a commit to hotwired/turbo that referenced this pull request Jan 30, 2024
* Morph with `ignoreActiveValue: true`

Morph with the [ignoreActiveValue: true][] option to morph the currently
focused element's attributes, but preserve its value.

This behavior can be extremely helpful when paired with an
auto-submitting `<form>` element, like a typeahead `[role="combobox"]`,
or an auto-submitting [`<input type="search">`][search].

This commit depends on a fork of `idiomorph` that [fixes a bug related
to `ignoreActiveValue: true`][bigskysoftware/idiomorph#34].

[ignoreActiveValue: true]: https://github.com/bigskysoftware/idiomorph#options
[search]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/search
[bigskysoftware/idiomorph#34]: bigskysoftware/idiomorph#34

* Depend on latest `idiomorph`
seanpdoyle added a commit to seanpdoyle/idiomorph that referenced this pull request Apr 12, 2024
Follow-up to [bigskysoftware#34][]

As mentioned in a [comment on bigskysoftware#34][], the test suite wasn't executed
when the code was initially contributed. When opening `test/index.html`
locally in a browser, there is a single test failure:

```
does not ignore body when ignoreActiveValue is true and no element has focus ‣
  AssertionError: expected <body …(1)>…(28)</body> to equal <input value="bar"></input>@[native code]
```

This commit resolves that failure by changing the test to more
accurately exercise the desired behavior.

[bigskysoftware#34]: bigskysoftware#34
[comment on bigskysoftware#34]: bigskysoftware#34 (comment)
seanpdoyle added a commit to seanpdoyle/idiomorph that referenced this pull request Apr 12, 2024
Follow-up to [bigskysoftware#34][]

As mentioned in a [comment on bigskysoftware#34][], the test suite wasn't executed
when the code was initially contributed. When opening `test/index.html`
locally in a browser, there is a single test failure:

```
does not ignore body when ignoreActiveValue is true and no element has focus ‣
  AssertionError: expected <body …(1)>…(28)</body> to equal <input value="bar"></input>@[native code]
```

This commit resolves that failure by changing the test to more
accurately exercise the desired behavior.

[bigskysoftware#34]: bigskysoftware#34
[comment on bigskysoftware#34]: bigskysoftware#34 (comment)
@seanpdoyle seanpdoyle mentioned this pull request Apr 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.

2 participants