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

Return DocumentFragment from Handlebars plugin #49

Merged
merged 1 commit into from
Dec 16, 2023

Conversation

mbland
Copy link
Owner

@mbland mbland commented Dec 16, 2023

More accurately, return a DocumentFragment from each template function generated by the Handlebars plugin:

Individual elements within the DocumentFragment can be accessed via its .children property. Passing the DocumentFragment to appendChild() will append all of its elements at once, detaching them from the fragment.

  import Template from './foobar.hbs'

  const t = Template({ some: 'data' })
  const [firstChild, secondChild] = t.children
  document.body.appendChild(t)

I'm hoping returning the DocumentFragment will eventually help me solve a problem with testing <form> elements attached to DocumentFragments. Some of this weirdness is currently documented in a comment inside the "Request > postForm > succeeds" test in:

  • strcalc/src/main/frontend/components/request.test.js

We have to be careful creating the <form>, because form.action
resolves differently depending on how we created it.

Originally I tried creating it using fragment() from
'../test/helpers', which creates elements using a new <template>
containing a DocumentFragment. However, the elements in that
DocumentFragment are in a separate DOM. This caused the
<form action="/fetch"> attribute to be:

  • '/fetch' in jsdom
  • '' in Chrome
  • #{document.location.origin}/fetch in Firefox

Creating a <form> element via document.createElement() as below
causes form.action to become #{document.location.origin}/fetch in
every environment.

On top of that, in the branch in which I'm developing the new Calculator component, I'm submitting the form via HTMLFormElement.requestSubmit(). This works in jsdom, but not in Chrome or Firefox. I'm wondering if the problem is related, and if having the <form> attached to a DocumentFragment is to blame.

More accurately, return a DocumentFragment from each template function
generated by the Handlebars plugin:

Individual elements within the DocumentFragment can be accessed via its
`.children` property. Passing the DocumentFragment to appendChild() will
append all of its elements at once, detaching them from the fragment.

```js
  import Template from './foobar.hbs'

  const t = Template({ some: 'data' })
  const [firstChild, secondChild] = t.children
  document.body.appendChild(t)
```

---

I'm hoping returning the DocumentFragment will eventually help me solve
a problem with testing <form> elements attached to DocumentFragments.
Some of this weirdness is currently documented in a comment inside the
"Request > postForm > succeeds" test in:

- strcalc/src/main/frontend/components/request.test.js

> We have to be careful creating the <form>, because form.action
> resolves differently depending on how we created it.
>
> Originally I tried creating it using fragment() from
> '../test/helpers', which creates elements using a new <template>
> containing a DocumentFragment. However, the elements in that
> DocumentFragment are in > a separate DOM. This caused the
> <form action="/fetch"> attribute to be:
>
> - '/fetch' in jsdom
> - '' in Chrome
> - `#{document.location.origin}/fetch` in Firefox
>
> Creating a <form> element via document.createElement() as below
> causes form.action to become `#{document.location.origin}/fetch` in
> every environment.

On top of that, in the branch in which I'm developing the new Calculator
component, I'm submitting the form via HTMLFormElement.requestSubmit().
This works in jsdom, but not in Chrome or Firefox. I'm wondering if the
problem is related, and if having the <form> attached to a
DocumentFragment is to blame.
@mbland mbland self-assigned this Dec 16, 2023
@mbland mbland merged commit 2b86a65 into main Dec 16, 2023
3 checks passed
@mbland mbland deleted the handlebars-plugin-return-doc-frag branch December 16, 2023 21:42
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.

None yet

1 participant