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

How to handle multiple roots? #65

Closed
finnp opened this issue Mar 14, 2018 · 1 comment
Closed

How to handle multiple roots? #65

finnp opened this issue Mar 14, 2018 · 1 comment

Comments

@finnp
Copy link
Contributor

finnp commented Mar 14, 2018

Right now, this will throw the error multiple root elements must be wrapped in an enclosing tag if more than one root element are given.

For nanohtml it would be pretty great to support multiple root elements, probably using DocumentFragment.

However it seems to me like all hyperscript-style modules also don't support multiple roots. There is a proposal, but it was never implemented: hyperhype/hyperscript#37

I suggest that maybe for now, we can allow an option on hyperx like createFragment. If it's spacified hyperx will call that function with the array of nodes instead of throwing an error.

The change would simply be:

      if (opts.createFragment) return opts.createFragment(tree[2])
      throw new Error(
        'multiple root elements must be wrapped in an enclosing tag'
      )

Then whoever uses hyperx (like nanohtml) could do something like for example:

function createFragment (nodes) {
  var fragment = document.createDocumentFragment()
  for (var i = 0; i < nodes.length; i++) {
    if (typeof nodes[i] === 'string') nodes[i] = document.createTextNode(nodes[i])
    fragment.appendChild(nodes[i])
  }
  return fragment  
}

hyperx(createElement, {
  comments: true
  createFragment: createFragment
})

I just tried this and it seemed to work, but I want to get some feedback first :)

@yoshuawuyts
Copy link
Member

yoshuawuyts commented Mar 14, 2018

@finnp I think that's a pretty reasonable approach. Defaults to safety, but allows opting-in to new behavior for those of us that can support it. Neat!

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

No branches or pull requests

2 participants